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

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