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