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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2023 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. attr_accessor :admin_projects
  20. self.queried_class = Project
  21. self.view_permission = :search_project
  22. validate do |query|
  23. # project must be blank for ProjectQuery
  24. errors.add(:project_id, :exclusion) if query.project_id.present?
  25. end
  26. self.available_columns = [
  27. QueryColumn.new(:name, :sortable => "#{Project.table_name}.name"),
  28. QueryColumn.new(:status, :sortable => "#{Project.table_name}.status"),
  29. QueryColumn.new(:short_description, :sortable => "#{Project.table_name}.description", :caption => :field_description),
  30. QueryColumn.new(:homepage, :sortable => "#{Project.table_name}.homepage"),
  31. QueryColumn.new(:identifier, :sortable => "#{Project.table_name}.identifier"),
  32. QueryColumn.new(:parent_id, :sortable => "#{Project.table_name}.lft ASC", :default_order => 'desc', :caption => :field_parent),
  33. QueryColumn.new(:is_public, :sortable => "#{Project.table_name}.is_public", :groupable => true),
  34. QueryColumn.new(:created_on, :sortable => "#{Project.table_name}.created_on", :default_order => 'desc')
  35. ]
  36. def self.default(project: nil, user: User.current)
  37. if user&.logged? && (query_id = user.pref.default_project_query).present?
  38. query = find_by(id: query_id)
  39. return query if query&.visible?
  40. end
  41. if (query_id = Setting.default_project_query).present?
  42. query = find_by(id: query_id)
  43. return query if query&.visibility == VISIBILITY_PUBLIC
  44. end
  45. nil
  46. end
  47. def initialize(attributes=nil, *args)
  48. super(attributes)
  49. self.filters ||= {'status' => {:operator => "=", :values => ['1']}}
  50. end
  51. def initialize_available_filters
  52. add_available_filter(
  53. "status",
  54. :type => :list, :values => lambda {project_statuses_values}
  55. )
  56. add_available_filter(
  57. "id",
  58. :type => :list, :values => lambda {project_values}, :label => :field_project
  59. )
  60. add_available_filter "name", :type => :text
  61. add_available_filter "description", :type => :text
  62. add_available_filter(
  63. "parent_id",
  64. :type => :list_subprojects, :values => lambda {project_values}, :label => :field_parent
  65. )
  66. add_available_filter(
  67. "is_public",
  68. :type => :list,
  69. :values => [[l(:general_text_yes), "1"], [l(:general_text_no), "0"]]
  70. )
  71. add_available_filter "created_on", :type => :date_past
  72. add_custom_fields_filters(project_custom_fields)
  73. end
  74. def build_from_params(params, defaults={})
  75. query = super
  76. query.admin_projects = params[:admin_projects]
  77. query
  78. end
  79. def available_columns
  80. return @available_columns if @available_columns
  81. @available_columns = self.class.available_columns.dup
  82. @available_columns += project_custom_fields.visible.
  83. map {|cf| QueryCustomFieldColumn.new(cf)}
  84. @available_columns
  85. end
  86. def available_display_types
  87. if self.admin_projects
  88. ['list']
  89. else
  90. ['board', 'list']
  91. end
  92. end
  93. def display_type
  94. if self.admin_projects
  95. 'list'
  96. else
  97. super
  98. end
  99. end
  100. def project_statuses_values
  101. values = super
  102. if self.admin_projects
  103. values << [l(:project_status_archived), Project::STATUS_ARCHIVED.to_s]
  104. values << [l(:project_status_scheduled_for_deletion), Project::STATUS_SCHEDULED_FOR_DELETION.to_s]
  105. end
  106. values
  107. end
  108. def default_columns_names
  109. @default_columns_names = Setting.project_list_defaults.symbolize_keys[:column_names].map(&:to_sym)
  110. end
  111. def default_display_type
  112. Setting.project_list_display_type
  113. end
  114. def default_sort_criteria
  115. [[]]
  116. end
  117. def base_scope
  118. if self.admin_projects
  119. Project.where(statement)
  120. else
  121. Project.visible.where(statement)
  122. end
  123. end
  124. def results_scope(options={})
  125. order_option = [group_by_sort_order, (options[:order] || sort_clause)].flatten.reject(&:blank?)
  126. order_option << "#{Project.table_name}.lft ASC"
  127. scope = base_scope.
  128. order(order_option).
  129. joins(joins_for_order_statement(order_option.join(',')))
  130. if has_custom_field_column?
  131. scope = scope.preload(:custom_values)
  132. end
  133. if has_column?(:parent_id)
  134. scope = scope.preload(:parent)
  135. end
  136. scope
  137. end
  138. end