2019-10-19 11:36:13 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
|
|
# Redmine - project management software
|
2024-02-26 22:55:54 +00:00
|
|
|
# Copyright (C) 2006- Jean-Philippe Lang
|
2019-10-19 11:36:13 +00:00
|
|
|
#
|
|
|
|
|
# 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
|
|
|
|
|
self.queried_class = Project
|
2019-10-19 11:37:30 +00:00
|
|
|
self.view_permission = :search_project
|
2019-10-19 11:36:13 +00:00
|
|
|
|
2019-11-20 15:53:04 +00:00
|
|
|
validate do |query|
|
|
|
|
|
# project must be blank for ProjectQuery
|
|
|
|
|
errors.add(:project_id, :exclusion) if query.project_id.present?
|
|
|
|
|
end
|
|
|
|
|
|
2025-03-09 23:25:12 +00:00
|
|
|
# Inheriting ProjectAdminQuery from ProjectQuery introduces the problem that
|
|
|
|
|
# ProjectQuery.visible also yields ProjectAdminQueries, as
|
|
|
|
|
# well. We fix that by adding a condition on the actual class name.
|
|
|
|
|
def self.visible(*)
|
|
|
|
|
super.where type: name
|
|
|
|
|
end
|
|
|
|
|
|
2019-10-19 11:42:20 +00:00
|
|
|
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),
|
2024-05-03 13:14:18 +00:00
|
|
|
QueryColumn.new(:created_on, :sortable => "#{Project.table_name}.created_on", :default_order => 'desc'),
|
2024-06-27 04:46:27 +00:00
|
|
|
QueryColumn.new(:updated_on, :sortable => "#{Project.table_name}.updated_on", :default_order => 'desc'),
|
2024-05-03 13:14:18 +00:00
|
|
|
QueryColumn.new(:last_activity_date)
|
2019-10-19 11:42:20 +00:00
|
|
|
]
|
2019-10-19 11:36:13 +00:00
|
|
|
|
2021-11-15 21:58:39 +00:00
|
|
|
def self.default(project: nil, user: User.current)
|
2022-05-28 09:54:34 +00:00
|
|
|
if user&.logged? && (query_id = user.pref.default_project_query).present?
|
|
|
|
|
query = find_by(id: query_id)
|
2022-07-31 13:33:27 +00:00
|
|
|
return query if query&.visible?
|
2021-11-15 21:58:39 +00:00
|
|
|
end
|
2022-07-31 13:33:27 +00:00
|
|
|
if (query_id = Setting.default_project_query).present?
|
2022-05-28 09:54:34 +00:00
|
|
|
query = find_by(id: query_id)
|
2022-07-31 13:33:27 +00:00
|
|
|
return query if query&.visibility == VISIBILITY_PUBLIC
|
2022-05-28 09:54:34 +00:00
|
|
|
end
|
2022-07-31 13:33:27 +00:00
|
|
|
nil
|
2021-11-15 21:58:39 +00:00
|
|
|
end
|
|
|
|
|
|
2019-10-19 11:36:13 +00:00
|
|
|
def initialize(attributes=nil, *args)
|
2023-12-20 07:15:11 +00:00
|
|
|
super(attributes)
|
2020-11-17 12:41:11 +00:00
|
|
|
self.filters ||= {'status' => {:operator => "=", :values => ['1']}}
|
2019-10-19 11:36:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def initialize_available_filters
|
2019-11-09 09:18:17 +00:00
|
|
|
add_available_filter(
|
|
|
|
|
"status",
|
2020-11-17 12:41:11 +00:00
|
|
|
:type => :list, :values => lambda {project_statuses_values}
|
2019-11-09 09:18:17 +00:00
|
|
|
)
|
|
|
|
|
add_available_filter(
|
|
|
|
|
"id",
|
2020-11-17 12:41:11 +00:00
|
|
|
:type => :list, :values => lambda {project_values}, :label => :field_project
|
2019-10-19 11:37:30 +00:00
|
|
|
)
|
2019-10-19 11:36:13 +00:00
|
|
|
add_available_filter "name", :type => :text
|
|
|
|
|
add_available_filter "description", :type => :text
|
2019-11-09 09:18:17 +00:00
|
|
|
add_available_filter(
|
|
|
|
|
"parent_id",
|
2020-11-17 12:41:11 +00:00
|
|
|
:type => :list_subprojects, :values => lambda {project_values}, :label => :field_parent
|
2019-10-19 11:38:15 +00:00
|
|
|
)
|
2019-11-09 09:18:17 +00:00
|
|
|
add_available_filter(
|
|
|
|
|
"is_public",
|
2019-10-19 11:36:13 +00:00
|
|
|
:type => :list,
|
|
|
|
|
:values => [[l(:general_text_yes), "1"], [l(:general_text_no), "0"]]
|
2019-11-09 09:18:17 +00:00
|
|
|
)
|
2019-10-19 11:36:13 +00:00
|
|
|
add_available_filter "created_on", :type => :date_past
|
2024-06-27 04:46:27 +00:00
|
|
|
add_available_filter "updated_on", :type => :date_past
|
2019-10-19 11:39:02 +00:00
|
|
|
add_custom_fields_filters(project_custom_fields)
|
2019-10-19 11:36:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def available_columns
|
2019-10-19 11:42:20 +00:00
|
|
|
return @available_columns if @available_columns
|
2020-11-26 14:46:29 +00:00
|
|
|
|
2019-10-19 11:42:20 +00:00
|
|
|
@available_columns = self.class.available_columns.dup
|
2020-05-08 04:30:24 +00:00
|
|
|
@available_columns += project_custom_fields.visible.
|
2020-11-17 12:41:11 +00:00
|
|
|
map {|cf| QueryCustomFieldColumn.new(cf)}
|
2019-10-19 11:42:20 +00:00
|
|
|
@available_columns
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def available_display_types
|
2025-03-09 23:25:12 +00:00
|
|
|
['board', 'list']
|
2019-10-19 11:42:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def default_columns_names
|
2019-10-19 11:43:23 +00:00
|
|
|
@default_columns_names = Setting.project_list_defaults.symbolize_keys[:column_names].map(&:to_sym)
|
2019-10-19 11:42:20 +00:00
|
|
|
end
|
|
|
|
|
|
2020-02-11 01:10:48 +00:00
|
|
|
def default_display_type
|
|
|
|
|
Setting.project_list_display_type
|
|
|
|
|
end
|
|
|
|
|
|
2019-10-19 11:42:20 +00:00
|
|
|
def default_sort_criteria
|
|
|
|
|
[[]]
|
2019-10-19 11:36:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def base_scope
|
2025-03-09 23:25:12 +00:00
|
|
|
Project.visible.where(statement)
|
2019-10-19 11:36:13 +00:00
|
|
|
end
|
|
|
|
|
|
2024-05-03 13:14:18 +00:00
|
|
|
# Returns the project count
|
|
|
|
|
def result_count
|
|
|
|
|
base_scope.count
|
|
|
|
|
rescue ::ActiveRecord::StatementInvalid => e
|
|
|
|
|
raise StatementInvalid, e.message
|
|
|
|
|
end
|
|
|
|
|
|
2019-10-19 11:36:13 +00:00
|
|
|
def results_scope(options={})
|
|
|
|
|
order_option = [group_by_sort_order, (options[:order] || sort_clause)].flatten.reject(&:blank?)
|
|
|
|
|
|
2019-10-26 07:07:17 +00:00
|
|
|
order_option << "#{Project.table_name}.lft ASC"
|
2019-10-19 11:36:13 +00:00
|
|
|
scope = base_scope.
|
|
|
|
|
order(order_option).
|
2024-11-23 11:17:54 +00:00
|
|
|
joins(joins_for_order_statement(order_option.join(','))).
|
|
|
|
|
limit(options[:limit]).
|
|
|
|
|
offset(options[:offset])
|
2019-10-19 11:36:13 +00:00
|
|
|
|
|
|
|
|
if has_custom_field_column?
|
|
|
|
|
scope = scope.preload(:custom_values)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if has_column?(:parent_id)
|
|
|
|
|
scope = scope.preload(:parent)
|
|
|
|
|
end
|
|
|
|
|
|
2024-05-03 13:14:18 +00:00
|
|
|
if has_column?(:last_activity_date)
|
|
|
|
|
Project.load_last_activity_date(scope)
|
|
|
|
|
end
|
|
|
|
|
|
2024-11-03 11:56:23 +00:00
|
|
|
scope
|
2019-10-19 11:36:13 +00:00
|
|
|
end
|
|
|
|
|
end
|