summaryrefslogtreecommitdiffstats
path: root/app/models/project_query.rb
blob: 69d10aeb4871753157cbaa4109e2a6bd611cf2a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# frozen_string_literal: true

# Redmine - project management software
# Copyright (C) 2006-2022  Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

class ProjectQuery < Query
  attr_accessor :admin_projects

  self.queried_class = Project
  self.view_permission = :search_project

  validate do |query|
    # project must be blank for ProjectQuery
    errors.add(:project_id, :exclusion) if query.project_id.present?
  end

  self.available_columns = [
    QueryColumn.new(:name, :sortable => "#{Project.table_name}.name"),
    QueryColumn.new(:status, :sortable => "#{Project.table_name}.status"),
    QueryColumn.new(:short_description, :sortable => "#{Project.table_name}.description", :caption => :field_description),
    QueryColumn.new(:homepage, :sortable => "#{Project.table_name}.homepage"),
    QueryColumn.new(:identifier, :sortable => "#{Project.table_name}.identifier"),
    QueryColumn.new(:parent_id, :sortable => "#{Project.table_name}.lft ASC", :default_order => 'desc', :caption => :field_parent),
    QueryColumn.new(:is_public, :sortable => "#{Project.table_name}.is_public", :groupable => true),
    QueryColumn.new(:created_on, :sortable => "#{Project.table_name}.created_on", :default_order => 'desc')
  ]

  def self.default(project: nil, user: User.current)
    if user&.logged? && (query_id = user.pref.default_project_query).present?
      query = find_by(id: query_id)
      return query if query&.visible?
    end
    if (query_id = Setting.default_project_query).present?
      query = find_by(id: query_id)
      return query if query&.visibility == VISIBILITY_PUBLIC
    end
    nil
  end

  def initialize(attributes=nil, *args)
    super attributes
    self.filters ||= {'status' => {:operator => "=", :values => ['1']}}
  end

  def initialize_available_filters
    add_available_filter(
      "status",
      :type => :list, :values => lambda {project_statuses_values}
    )
    add_available_filter(
      "id",
      :type => :list, :values => lambda {project_values}, :label => :field_project
    )
    add_available_filter "name", :type => :text
    add_available_filter "description", :type => :text
    add_available_filter(
      "parent_id",
      :type => :list_subprojects, :values => lambda {project_values}, :label => :field_parent
    )
    add_available_filter(
      "is_public",
      :type => :list,
      :values => [[l(:general_text_yes), "1"], [l(:general_text_no), "0"]]
    )
    add_available_filter "created_on", :type => :date_past
    add_custom_fields_filters(project_custom_fields)
  end

  def build_from_params(params, defaults={})
    query = super
    query.admin_projects = params[:admin_projects]
    query
  end

  def available_columns
    return @available_columns if @available_columns

    @available_columns = self.class.available_columns.dup
    @available_columns += project_custom_fields.visible.
                            map {|cf| QueryCustomFieldColumn.new(cf)}
    @available_columns
  end

  def available_display_types
    if self.admin_projects
      ['list']
    else
      ['board', 'list']
    end
  end

  def display_type
    if self.admin_projects
      'list'
    else
      super
    end
  end

  def project_statuses_values
    values = super
    if self.admin_projects
      values << [l(:project_status_archived), Project::STATUS_ARCHIVED.to_s]
      values << [l(:project_status_scheduled_for_deletion), Project::STATUS_SCHEDULED_FOR_DELETION.to_s]
    end
    values
  end

  def default_columns_names
    @default_columns_names = Setting.project_list_defaults.symbolize_keys[:column_names].map(&:to_sym)
  end

  def default_display_type
    Setting.project_list_display_type
  end

  def default_sort_criteria
    [[]]
  end

  def base_scope
    if self.admin_projects
      Project.where(statement)
    else
      Project.visible.where(statement)
    end
  end

  def results_scope(options={})
    order_option = [group_by_sort_order, (options[:order] || sort_clause)].flatten.reject(&:blank?)

    order_option << "#{Project.table_name}.lft ASC"
    scope = base_scope.
      order(order_option).
      joins(joins_for_order_statement(order_option.join(',')))

    if has_custom_field_column?
      scope = scope.preload(:custom_values)
    end

    if has_column?(:parent_id)
      scope = scope.preload(:parent)
    end

    scope
  end
end