You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

project_query.rb 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2021 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. class ProjectQuery < Query
  19. self.queried_class = Project
  20. self.view_permission = :search_project
  21. validate do |query|
  22. # project must be blank for ProjectQuery
  23. errors.add(:project_id, :exclusion) if query.project_id.present?
  24. end
  25. self.available_columns = [
  26. QueryColumn.new(:name, :sortable => "#{Project.table_name}.name"),
  27. QueryColumn.new(:status, :sortable => "#{Project.table_name}.status"),
  28. QueryColumn.new(:short_description, :sortable => "#{Project.table_name}.description", :caption => :field_description),
  29. QueryColumn.new(:homepage, :sortable => "#{Project.table_name}.homepage"),
  30. QueryColumn.new(:identifier, :sortable => "#{Project.table_name}.identifier"),
  31. QueryColumn.new(:parent_id, :sortable => "#{Project.table_name}.lft ASC", :default_order => 'desc', :caption => :field_parent),
  32. QueryColumn.new(:is_public, :sortable => "#{Project.table_name}.is_public", :groupable => true),
  33. QueryColumn.new(:created_on, :sortable => "#{Project.table_name}.created_on", :default_order => 'desc')
  34. ]
  35. def self.default(project: nil, user: User.current)
  36. query = nil
  37. if user&.logged?
  38. query = find_by_id user.pref.default_project_query
  39. end
  40. query || find_by_id(Setting.default_project_query)
  41. end
  42. def initialize(attributes=nil, *args)
  43. super attributes
  44. self.filters ||= {'status' => {:operator => "=", :values => ['1']}}
  45. end
  46. def initialize_available_filters
  47. add_available_filter(
  48. "status",
  49. :type => :list, :values => lambda {project_statuses_values}
  50. )
  51. add_available_filter(
  52. "id",
  53. :type => :list, :values => lambda {project_values}, :label => :field_project
  54. )
  55. add_available_filter "name", :type => :text
  56. add_available_filter "description", :type => :text
  57. add_available_filter(
  58. "parent_id",
  59. :type => :list_subprojects, :values => lambda {project_values}, :label => :field_parent
  60. )
  61. add_available_filter(
  62. "is_public",
  63. :type => :list,
  64. :values => [[l(:general_text_yes), "1"], [l(:general_text_no), "0"]]
  65. )
  66. add_available_filter "created_on", :type => :date_past
  67. add_custom_fields_filters(project_custom_fields)
  68. end
  69. def available_columns
  70. return @available_columns if @available_columns
  71. @available_columns = self.class.available_columns.dup
  72. @available_columns += project_custom_fields.visible.
  73. map {|cf| QueryCustomFieldColumn.new(cf)}
  74. @available_columns
  75. end
  76. def available_display_types
  77. ['board', 'list']
  78. end
  79. def default_columns_names
  80. @default_columns_names = Setting.project_list_defaults.symbolize_keys[:column_names].map(&:to_sym)
  81. end
  82. def default_display_type
  83. Setting.project_list_display_type
  84. end
  85. def default_sort_criteria
  86. [[]]
  87. end
  88. def base_scope
  89. Project.visible.where(statement)
  90. end
  91. def results_scope(options={})
  92. order_option = [group_by_sort_order, (options[:order] || sort_clause)].flatten.reject(&:blank?)
  93. order_option << "#{Project.table_name}.lft ASC"
  94. scope = base_scope.
  95. order(order_option).
  96. joins(joins_for_order_statement(order_option.join(',')))
  97. if has_custom_field_column?
  98. scope = scope.preload(:custom_values)
  99. end
  100. if has_column?(:parent_id)
  101. scope = scope.preload(:parent)
  102. end
  103. scope
  104. end
  105. end