annotate app/models/time_entry_query.rb @ 1600:ed9c467ef922 dockerise

Add hggit extension
author Chris Cannam
date Wed, 23 Aug 2017 11:32:50 +0100
parents dffacf8a6908
children
rev   line source
Chris@1464 1 # Redmine - project management software
Chris@1494 2 # Copyright (C) 2006-2014 Jean-Philippe Lang
Chris@1464 3 #
Chris@1464 4 # This program is free software; you can redistribute it and/or
Chris@1464 5 # modify it under the terms of the GNU General Public License
Chris@1464 6 # as published by the Free Software Foundation; either version 2
Chris@1464 7 # of the License, or (at your option) any later version.
Chris@1464 8 #
Chris@1464 9 # This program is distributed in the hope that it will be useful,
Chris@1464 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1464 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1464 12 # GNU General Public License for more details.
Chris@1464 13 #
Chris@1464 14 # You should have received a copy of the GNU General Public License
Chris@1464 15 # along with this program; if not, write to the Free Software
Chris@1464 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1464 17
Chris@1464 18 class TimeEntryQuery < Query
Chris@1464 19
Chris@1464 20 self.queried_class = TimeEntry
Chris@1464 21
Chris@1464 22 self.available_columns = [
Chris@1464 23 QueryColumn.new(:project, :sortable => "#{Project.table_name}.name", :groupable => true),
Chris@1464 24 QueryColumn.new(:spent_on, :sortable => ["#{TimeEntry.table_name}.spent_on", "#{TimeEntry.table_name}.created_on"], :default_order => 'desc', :groupable => true),
Chris@1464 25 QueryColumn.new(:user, :sortable => lambda {User.fields_for_order_statement}, :groupable => true),
Chris@1464 26 QueryColumn.new(:activity, :sortable => "#{TimeEntryActivity.table_name}.position", :groupable => true),
Chris@1464 27 QueryColumn.new(:issue, :sortable => "#{Issue.table_name}.id"),
Chris@1464 28 QueryColumn.new(:comments),
Chris@1464 29 QueryColumn.new(:hours, :sortable => "#{TimeEntry.table_name}.hours"),
Chris@1464 30 ]
Chris@1464 31
Chris@1464 32 def initialize(attributes=nil, *args)
Chris@1464 33 super attributes
Chris@1464 34 self.filters ||= {}
Chris@1464 35 add_filter('spent_on', '*') unless filters.present?
Chris@1464 36 end
Chris@1464 37
Chris@1464 38 def initialize_available_filters
Chris@1464 39 add_available_filter "spent_on", :type => :date_past
Chris@1464 40
Chris@1464 41 principals = []
Chris@1464 42 if project
Chris@1464 43 principals += project.principals.sort
Chris@1464 44 unless project.leaf?
Chris@1464 45 subprojects = project.descendants.visible.all
Chris@1464 46 if subprojects.any?
Chris@1464 47 add_available_filter "subproject_id",
Chris@1464 48 :type => :list_subprojects,
Chris@1464 49 :values => subprojects.collect{|s| [s.name, s.id.to_s] }
Chris@1464 50 principals += Principal.member_of(subprojects)
Chris@1464 51 end
Chris@1464 52 end
Chris@1464 53 else
Chris@1464 54 if all_projects.any?
Chris@1464 55 # members of visible projects
Chris@1464 56 principals += Principal.member_of(all_projects)
Chris@1464 57 # project filter
Chris@1464 58 project_values = []
Chris@1464 59 if User.current.logged? && User.current.memberships.any?
Chris@1464 60 project_values << ["<< #{l(:label_my_projects).downcase} >>", "mine"]
Chris@1464 61 end
Chris@1464 62 project_values += all_projects_values
Chris@1464 63 add_available_filter("project_id",
Chris@1464 64 :type => :list, :values => project_values
Chris@1464 65 ) unless project_values.empty?
Chris@1464 66 end
Chris@1464 67 end
Chris@1464 68 principals.uniq!
Chris@1464 69 principals.sort!
Chris@1464 70 users = principals.select {|p| p.is_a?(User)}
Chris@1464 71
Chris@1464 72 users_values = []
Chris@1464 73 users_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged?
Chris@1464 74 users_values += users.collect{|s| [s.name, s.id.to_s] }
Chris@1464 75 add_available_filter("user_id",
Chris@1464 76 :type => :list_optional, :values => users_values
Chris@1464 77 ) unless users_values.empty?
Chris@1464 78
Chris@1464 79 activities = (project ? project.activities : TimeEntryActivity.shared.active)
Chris@1464 80 add_available_filter("activity_id",
Chris@1464 81 :type => :list, :values => activities.map {|a| [a.name, a.id.to_s]}
Chris@1464 82 ) unless activities.empty?
Chris@1464 83
Chris@1464 84 add_available_filter "comments", :type => :text
Chris@1464 85 add_available_filter "hours", :type => :float
Chris@1464 86
Chris@1464 87 add_custom_fields_filters(TimeEntryCustomField)
Chris@1464 88 add_associations_custom_fields_filters :project, :issue, :user
Chris@1464 89 end
Chris@1464 90
Chris@1464 91 def available_columns
Chris@1464 92 return @available_columns if @available_columns
Chris@1464 93 @available_columns = self.class.available_columns.dup
Chris@1517 94 @available_columns += TimeEntryCustomField.visible.
Chris@1517 95 map {|cf| QueryCustomFieldColumn.new(cf) }
Chris@1517 96 @available_columns += IssueCustomField.visible.
Chris@1517 97 map {|cf| QueryAssociationCustomFieldColumn.new(:issue, cf) }
Chris@1464 98 @available_columns
Chris@1464 99 end
Chris@1464 100
Chris@1464 101 def default_columns_names
Chris@1464 102 @default_columns_names ||= [:project, :spent_on, :user, :activity, :issue, :comments, :hours]
Chris@1464 103 end
Chris@1464 104
Chris@1464 105 def results_scope(options={})
Chris@1464 106 order_option = [group_by_sort_order, options[:order]].flatten.reject(&:blank?)
Chris@1464 107
Chris@1464 108 TimeEntry.visible.
Chris@1464 109 where(statement).
Chris@1464 110 order(order_option).
Chris@1464 111 joins(joins_for_order_statement(order_option.join(','))).
Chris@1464 112 includes(:activity)
Chris@1464 113 end
Chris@1464 114
Chris@1464 115 def sql_for_activity_id_field(field, operator, value)
Chris@1464 116 condition_on_id = sql_for_field(field, operator, value, Enumeration.table_name, 'id')
Chris@1464 117 condition_on_parent_id = sql_for_field(field, operator, value, Enumeration.table_name, 'parent_id')
Chris@1464 118 ids = value.map(&:to_i).join(',')
Chris@1464 119 table_name = Enumeration.table_name
Chris@1464 120 if operator == '='
Chris@1464 121 "(#{table_name}.id IN (#{ids}) OR #{table_name}.parent_id IN (#{ids}))"
Chris@1464 122 else
Chris@1464 123 "(#{table_name}.id NOT IN (#{ids}) AND (#{table_name}.parent_id IS NULL OR #{table_name}.parent_id NOT IN (#{ids})))"
Chris@1464 124 end
Chris@1464 125 end
Chris@1464 126
Chris@1464 127 # Accepts :from/:to params as shortcut filters
Chris@1464 128 def build_from_params(params)
Chris@1464 129 super
Chris@1464 130 if params[:from].present? && params[:to].present?
Chris@1464 131 add_filter('spent_on', '><', [params[:from], params[:to]])
Chris@1464 132 elsif params[:from].present?
Chris@1464 133 add_filter('spent_on', '>=', [params[:from]])
Chris@1464 134 elsif params[:to].present?
Chris@1464 135 add_filter('spent_on', '<=', [params[:to]])
Chris@1464 136 end
Chris@1464 137 self
Chris@1464 138 end
Chris@1464 139 end