comparison app/models/time_entry_query.rb @ 1298:4f746d8966dd redmine_2.3_integration

Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author Chris Cannam
date Fri, 14 Jun 2013 09:28:30 +0100
parents 622f24f53b42
children
comparison
equal deleted inserted replaced
1297:0a574315af3e 1298:4f746d8966dd
1 # Redmine - project management software
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18 class TimeEntryQuery < Query
19
20 self.queried_class = TimeEntry
21
22 self.available_columns = [
23 QueryColumn.new(:project, :sortable => "#{Project.table_name}.name", :groupable => true),
24 QueryColumn.new(:spent_on, :sortable => ["#{TimeEntry.table_name}.spent_on", "#{TimeEntry.table_name}.created_on"], :default_order => 'desc', :groupable => true),
25 QueryColumn.new(:user, :sortable => lambda {User.fields_for_order_statement}, :groupable => true),
26 QueryColumn.new(:activity, :sortable => "#{TimeEntryActivity.table_name}.position", :groupable => true),
27 QueryColumn.new(:issue, :sortable => "#{Issue.table_name}.id"),
28 QueryColumn.new(:comments),
29 QueryColumn.new(:hours, :sortable => "#{TimeEntry.table_name}.hours"),
30 ]
31
32 def initialize(attributes=nil, *args)
33 super attributes
34 self.filters ||= {}
35 add_filter('spent_on', '*') unless filters.present?
36 end
37
38 def initialize_available_filters
39 add_available_filter "spent_on", :type => :date_past
40
41 principals = []
42 if project
43 principals += project.principals.sort
44 unless project.leaf?
45 subprojects = project.descendants.visible.all
46 if subprojects.any?
47 add_available_filter "subproject_id",
48 :type => :list_subprojects,
49 :values => subprojects.collect{|s| [s.name, s.id.to_s] }
50 principals += Principal.member_of(subprojects)
51 end
52 end
53 else
54 if all_projects.any?
55 # members of visible projects
56 principals += Principal.member_of(all_projects)
57 # project filter
58 project_values = []
59 if User.current.logged? && User.current.memberships.any?
60 project_values << ["<< #{l(:label_my_projects).downcase} >>", "mine"]
61 end
62 project_values += all_projects_values
63 add_available_filter("project_id",
64 :type => :list, :values => project_values
65 ) unless project_values.empty?
66 end
67 end
68 principals.uniq!
69 principals.sort!
70 users = principals.select {|p| p.is_a?(User)}
71
72 users_values = []
73 users_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged?
74 users_values += users.collect{|s| [s.name, s.id.to_s] }
75 add_available_filter("user_id",
76 :type => :list_optional, :values => users_values
77 ) unless users_values.empty?
78
79 activities = (project ? project.activities : TimeEntryActivity.shared.active)
80 add_available_filter("activity_id",
81 :type => :list, :values => activities.map {|a| [a.name, a.id.to_s]}
82 ) unless activities.empty?
83
84 add_available_filter "comments", :type => :text
85 add_available_filter "hours", :type => :float
86
87 add_custom_fields_filters(TimeEntryCustomField.where(:is_filter => true).all)
88 add_associations_custom_fields_filters :project, :issue, :user
89 end
90
91 def available_columns
92 return @available_columns if @available_columns
93 @available_columns = self.class.available_columns.dup
94 @available_columns += TimeEntryCustomField.all.map {|cf| QueryCustomFieldColumn.new(cf) }
95 @available_columns += IssueCustomField.all.map {|cf| QueryAssociationCustomFieldColumn.new(:issue, cf) }
96 @available_columns
97 end
98
99 def default_columns_names
100 @default_columns_names ||= [:project, :spent_on, :user, :activity, :issue, :comments, :hours]
101 end
102
103 # Accepts :from/:to params as shortcut filters
104 def build_from_params(params)
105 super
106 if params[:from].present? && params[:to].present?
107 add_filter('spent_on', '><', [params[:from], params[:to]])
108 elsif params[:from].present?
109 add_filter('spent_on', '>=', [params[:from]])
110 elsif params[:to].present?
111 add_filter('spent_on', '<=', [params[:to]])
112 end
113 self
114 end
115 end