Mercurial > hg > soundsoftware-site
comparison app/models/issue_query.rb @ 1295:622f24f53b42 redmine-2.3
Update to Redmine SVN revision 11972 on 2.3-stable branch
author | Chris Cannam |
---|---|
date | Fri, 14 Jun 2013 09:02:21 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1294:3e4c3460b6ca | 1295:622f24f53b42 |
---|---|
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 IssueQuery < Query | |
19 | |
20 self.queried_class = Issue | |
21 | |
22 self.available_columns = [ | |
23 QueryColumn.new(:id, :sortable => "#{Issue.table_name}.id", :default_order => 'desc', :caption => '#', :frozen => true), | |
24 QueryColumn.new(:project, :sortable => "#{Project.table_name}.name", :groupable => true), | |
25 QueryColumn.new(:tracker, :sortable => "#{Tracker.table_name}.position", :groupable => true), | |
26 QueryColumn.new(:parent, :sortable => ["#{Issue.table_name}.root_id", "#{Issue.table_name}.lft ASC"], :default_order => 'desc', :caption => :field_parent_issue), | |
27 QueryColumn.new(:status, :sortable => "#{IssueStatus.table_name}.position", :groupable => true), | |
28 QueryColumn.new(:priority, :sortable => "#{IssuePriority.table_name}.position", :default_order => 'desc', :groupable => true), | |
29 QueryColumn.new(:subject, :sortable => "#{Issue.table_name}.subject"), | |
30 QueryColumn.new(:author, :sortable => lambda {User.fields_for_order_statement("authors")}, :groupable => true), | |
31 QueryColumn.new(:assigned_to, :sortable => lambda {User.fields_for_order_statement}, :groupable => true), | |
32 QueryColumn.new(:updated_on, :sortable => "#{Issue.table_name}.updated_on", :default_order => 'desc'), | |
33 QueryColumn.new(:category, :sortable => "#{IssueCategory.table_name}.name", :groupable => true), | |
34 QueryColumn.new(:fixed_version, :sortable => lambda {Version.fields_for_order_statement}, :groupable => true), | |
35 QueryColumn.new(:start_date, :sortable => "#{Issue.table_name}.start_date"), | |
36 QueryColumn.new(:due_date, :sortable => "#{Issue.table_name}.due_date"), | |
37 QueryColumn.new(:estimated_hours, :sortable => "#{Issue.table_name}.estimated_hours"), | |
38 QueryColumn.new(:done_ratio, :sortable => "#{Issue.table_name}.done_ratio", :groupable => true), | |
39 QueryColumn.new(:created_on, :sortable => "#{Issue.table_name}.created_on", :default_order => 'desc'), | |
40 QueryColumn.new(:closed_on, :sortable => "#{Issue.table_name}.closed_on", :default_order => 'desc'), | |
41 QueryColumn.new(:relations, :caption => :label_related_issues), | |
42 QueryColumn.new(:description, :inline => false) | |
43 ] | |
44 | |
45 scope :visible, lambda {|*args| | |
46 user = args.shift || User.current | |
47 base = Project.allowed_to_condition(user, :view_issues, *args) | |
48 user_id = user.logged? ? user.id : 0 | |
49 | |
50 includes(:project).where("(#{table_name}.project_id IS NULL OR (#{base})) AND (#{table_name}.is_public = ? OR #{table_name}.user_id = ?)", true, user_id) | |
51 } | |
52 | |
53 def initialize(attributes=nil, *args) | |
54 super attributes | |
55 self.filters ||= { 'status_id' => {:operator => "o", :values => [""]} } | |
56 end | |
57 | |
58 # Returns true if the query is visible to +user+ or the current user. | |
59 def visible?(user=User.current) | |
60 (project.nil? || user.allowed_to?(:view_issues, project)) && (self.is_public? || self.user_id == user.id) | |
61 end | |
62 | |
63 def initialize_available_filters | |
64 principals = [] | |
65 subprojects = [] | |
66 versions = [] | |
67 categories = [] | |
68 issue_custom_fields = [] | |
69 | |
70 if project | |
71 principals += project.principals.sort | |
72 unless project.leaf? | |
73 subprojects = project.descendants.visible.all | |
74 principals += Principal.member_of(subprojects) | |
75 end | |
76 versions = project.shared_versions.all | |
77 categories = project.issue_categories.all | |
78 issue_custom_fields = project.all_issue_custom_fields | |
79 else | |
80 if all_projects.any? | |
81 principals += Principal.member_of(all_projects) | |
82 end | |
83 versions = Version.visible.find_all_by_sharing('system') | |
84 issue_custom_fields = IssueCustomField.where(:is_filter => true, :is_for_all => true).all | |
85 end | |
86 principals.uniq! | |
87 principals.sort! | |
88 users = principals.select {|p| p.is_a?(User)} | |
89 | |
90 | |
91 add_available_filter "status_id", | |
92 :type => :list_status, :values => IssueStatus.sorted.all.collect{|s| [s.name, s.id.to_s] } | |
93 | |
94 if project.nil? | |
95 project_values = [] | |
96 if User.current.logged? && User.current.memberships.any? | |
97 project_values << ["<< #{l(:label_my_projects).downcase} >>", "mine"] | |
98 end | |
99 project_values += all_projects_values | |
100 add_available_filter("project_id", | |
101 :type => :list, :values => project_values | |
102 ) unless project_values.empty? | |
103 end | |
104 | |
105 add_available_filter "tracker_id", | |
106 :type => :list, :values => trackers.collect{|s| [s.name, s.id.to_s] } | |
107 add_available_filter "priority_id", | |
108 :type => :list, :values => IssuePriority.all.collect{|s| [s.name, s.id.to_s] } | |
109 | |
110 author_values = [] | |
111 author_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged? | |
112 author_values += users.collect{|s| [s.name, s.id.to_s] } | |
113 add_available_filter("author_id", | |
114 :type => :list, :values => author_values | |
115 ) unless author_values.empty? | |
116 | |
117 assigned_to_values = [] | |
118 assigned_to_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged? | |
119 assigned_to_values += (Setting.issue_group_assignment? ? | |
120 principals : users).collect{|s| [s.name, s.id.to_s] } | |
121 add_available_filter("assigned_to_id", | |
122 :type => :list_optional, :values => assigned_to_values | |
123 ) unless assigned_to_values.empty? | |
124 | |
125 group_values = Group.all.collect {|g| [g.name, g.id.to_s] } | |
126 add_available_filter("member_of_group", | |
127 :type => :list_optional, :values => group_values | |
128 ) unless group_values.empty? | |
129 | |
130 role_values = Role.givable.collect {|r| [r.name, r.id.to_s] } | |
131 add_available_filter("assigned_to_role", | |
132 :type => :list_optional, :values => role_values | |
133 ) unless role_values.empty? | |
134 | |
135 if versions.any? | |
136 add_available_filter "fixed_version_id", | |
137 :type => :list_optional, | |
138 :values => versions.sort.collect{|s| ["#{s.project.name} - #{s.name}", s.id.to_s] } | |
139 end | |
140 | |
141 if categories.any? | |
142 add_available_filter "category_id", | |
143 :type => :list_optional, | |
144 :values => categories.collect{|s| [s.name, s.id.to_s] } | |
145 end | |
146 | |
147 add_available_filter "subject", :type => :text | |
148 add_available_filter "created_on", :type => :date_past | |
149 add_available_filter "updated_on", :type => :date_past | |
150 add_available_filter "closed_on", :type => :date_past | |
151 add_available_filter "start_date", :type => :date | |
152 add_available_filter "due_date", :type => :date | |
153 add_available_filter "estimated_hours", :type => :float | |
154 add_available_filter "done_ratio", :type => :integer | |
155 | |
156 if User.current.allowed_to?(:set_issues_private, nil, :global => true) || | |
157 User.current.allowed_to?(:set_own_issues_private, nil, :global => true) | |
158 add_available_filter "is_private", | |
159 :type => :list, | |
160 :values => [[l(:general_text_yes), "1"], [l(:general_text_no), "0"]] | |
161 end | |
162 | |
163 if User.current.logged? | |
164 add_available_filter "watcher_id", | |
165 :type => :list, :values => [["<< #{l(:label_me)} >>", "me"]] | |
166 end | |
167 | |
168 if subprojects.any? | |
169 add_available_filter "subproject_id", | |
170 :type => :list_subprojects, | |
171 :values => subprojects.collect{|s| [s.name, s.id.to_s] } | |
172 end | |
173 | |
174 add_custom_fields_filters(issue_custom_fields) | |
175 | |
176 add_associations_custom_fields_filters :project, :author, :assigned_to, :fixed_version | |
177 | |
178 IssueRelation::TYPES.each do |relation_type, options| | |
179 add_available_filter relation_type, :type => :relation, :label => options[:name] | |
180 end | |
181 | |
182 Tracker.disabled_core_fields(trackers).each {|field| | |
183 delete_available_filter field | |
184 } | |
185 end | |
186 | |
187 def available_columns | |
188 return @available_columns if @available_columns | |
189 @available_columns = self.class.available_columns.dup | |
190 @available_columns += (project ? | |
191 project.all_issue_custom_fields : | |
192 IssueCustomField.all | |
193 ).collect {|cf| QueryCustomFieldColumn.new(cf) } | |
194 | |
195 if User.current.allowed_to?(:view_time_entries, project, :global => true) | |
196 index = nil | |
197 @available_columns.each_with_index {|column, i| index = i if column.name == :estimated_hours} | |
198 index = (index ? index + 1 : -1) | |
199 # insert the column after estimated_hours or at the end | |
200 @available_columns.insert index, QueryColumn.new(:spent_hours, | |
201 :sortable => "COALESCE((SELECT SUM(hours) FROM #{TimeEntry.table_name} WHERE #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id), 0)", | |
202 :default_order => 'desc', | |
203 :caption => :label_spent_time | |
204 ) | |
205 end | |
206 | |
207 if User.current.allowed_to?(:set_issues_private, nil, :global => true) || | |
208 User.current.allowed_to?(:set_own_issues_private, nil, :global => true) | |
209 @available_columns << QueryColumn.new(:is_private, :sortable => "#{Issue.table_name}.is_private") | |
210 end | |
211 | |
212 disabled_fields = Tracker.disabled_core_fields(trackers).map {|field| field.sub(/_id$/, '')} | |
213 @available_columns.reject! {|column| | |
214 disabled_fields.include?(column.name.to_s) | |
215 } | |
216 | |
217 @available_columns | |
218 end | |
219 | |
220 def default_columns_names | |
221 @default_columns_names ||= begin | |
222 default_columns = Setting.issue_list_default_columns.map(&:to_sym) | |
223 | |
224 project.present? ? default_columns : [:project] | default_columns | |
225 end | |
226 end | |
227 | |
228 # Returns the issue count | |
229 def issue_count | |
230 Issue.visible.count(:include => [:status, :project], :conditions => statement) | |
231 rescue ::ActiveRecord::StatementInvalid => e | |
232 raise StatementInvalid.new(e.message) | |
233 end | |
234 | |
235 # Returns the issue count by group or nil if query is not grouped | |
236 def issue_count_by_group | |
237 r = nil | |
238 if grouped? | |
239 begin | |
240 # Rails3 will raise an (unexpected) RecordNotFound if there's only a nil group value | |
241 r = Issue.visible.count(:joins => joins_for_order_statement(group_by_statement), :group => group_by_statement, :include => [:status, :project], :conditions => statement) | |
242 rescue ActiveRecord::RecordNotFound | |
243 r = {nil => issue_count} | |
244 end | |
245 c = group_by_column | |
246 if c.is_a?(QueryCustomFieldColumn) | |
247 r = r.keys.inject({}) {|h, k| h[c.custom_field.cast_value(k)] = r[k]; h} | |
248 end | |
249 end | |
250 r | |
251 rescue ::ActiveRecord::StatementInvalid => e | |
252 raise StatementInvalid.new(e.message) | |
253 end | |
254 | |
255 # Returns the issues | |
256 # Valid options are :order, :offset, :limit, :include, :conditions | |
257 def issues(options={}) | |
258 order_option = [group_by_sort_order, options[:order]].flatten.reject(&:blank?) | |
259 | |
260 issues = Issue.visible.where(options[:conditions]).all( | |
261 :include => ([:status, :project] + (options[:include] || [])).uniq, | |
262 :conditions => statement, | |
263 :order => order_option, | |
264 :joins => joins_for_order_statement(order_option.join(',')), | |
265 :limit => options[:limit], | |
266 :offset => options[:offset] | |
267 ) | |
268 | |
269 if has_column?(:spent_hours) | |
270 Issue.load_visible_spent_hours(issues) | |
271 end | |
272 if has_column?(:relations) | |
273 Issue.load_visible_relations(issues) | |
274 end | |
275 issues | |
276 rescue ::ActiveRecord::StatementInvalid => e | |
277 raise StatementInvalid.new(e.message) | |
278 end | |
279 | |
280 # Returns the issues ids | |
281 def issue_ids(options={}) | |
282 order_option = [group_by_sort_order, options[:order]].flatten.reject(&:blank?) | |
283 | |
284 Issue.visible.scoped(:conditions => options[:conditions]).scoped(:include => ([:status, :project] + (options[:include] || [])).uniq, | |
285 :conditions => statement, | |
286 :order => order_option, | |
287 :joins => joins_for_order_statement(order_option.join(',')), | |
288 :limit => options[:limit], | |
289 :offset => options[:offset]).find_ids | |
290 rescue ::ActiveRecord::StatementInvalid => e | |
291 raise StatementInvalid.new(e.message) | |
292 end | |
293 | |
294 # Returns the journals | |
295 # Valid options are :order, :offset, :limit | |
296 def journals(options={}) | |
297 Journal.visible.all( | |
298 :include => [:details, :user, {:issue => [:project, :author, :tracker, :status]}], | |
299 :conditions => statement, | |
300 :order => options[:order], | |
301 :limit => options[:limit], | |
302 :offset => options[:offset] | |
303 ) | |
304 rescue ::ActiveRecord::StatementInvalid => e | |
305 raise StatementInvalid.new(e.message) | |
306 end | |
307 | |
308 # Returns the versions | |
309 # Valid options are :conditions | |
310 def versions(options={}) | |
311 Version.visible.where(options[:conditions]).all( | |
312 :include => :project, | |
313 :conditions => project_statement | |
314 ) | |
315 rescue ::ActiveRecord::StatementInvalid => e | |
316 raise StatementInvalid.new(e.message) | |
317 end | |
318 | |
319 def sql_for_watcher_id_field(field, operator, value) | |
320 db_table = Watcher.table_name | |
321 "#{Issue.table_name}.id #{ operator == '=' ? 'IN' : 'NOT IN' } (SELECT #{db_table}.watchable_id FROM #{db_table} WHERE #{db_table}.watchable_type='Issue' AND " + | |
322 sql_for_field(field, '=', value, db_table, 'user_id') + ')' | |
323 end | |
324 | |
325 def sql_for_member_of_group_field(field, operator, value) | |
326 if operator == '*' # Any group | |
327 groups = Group.all | |
328 operator = '=' # Override the operator since we want to find by assigned_to | |
329 elsif operator == "!*" | |
330 groups = Group.all | |
331 operator = '!' # Override the operator since we want to find by assigned_to | |
332 else | |
333 groups = Group.find_all_by_id(value) | |
334 end | |
335 groups ||= [] | |
336 | |
337 members_of_groups = groups.inject([]) {|user_ids, group| | |
338 user_ids + group.user_ids + [group.id] | |
339 }.uniq.compact.sort.collect(&:to_s) | |
340 | |
341 '(' + sql_for_field("assigned_to_id", operator, members_of_groups, Issue.table_name, "assigned_to_id", false) + ')' | |
342 end | |
343 | |
344 def sql_for_assigned_to_role_field(field, operator, value) | |
345 case operator | |
346 when "*", "!*" # Member / Not member | |
347 sw = operator == "!*" ? 'NOT' : '' | |
348 nl = operator == "!*" ? "#{Issue.table_name}.assigned_to_id IS NULL OR" : '' | |
349 "(#{nl} #{Issue.table_name}.assigned_to_id #{sw} IN (SELECT DISTINCT #{Member.table_name}.user_id FROM #{Member.table_name}" + | |
350 " WHERE #{Member.table_name}.project_id = #{Issue.table_name}.project_id))" | |
351 when "=", "!" | |
352 role_cond = value.any? ? | |
353 "#{MemberRole.table_name}.role_id IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")" : | |
354 "1=0" | |
355 | |
356 sw = operator == "!" ? 'NOT' : '' | |
357 nl = operator == "!" ? "#{Issue.table_name}.assigned_to_id IS NULL OR" : '' | |
358 "(#{nl} #{Issue.table_name}.assigned_to_id #{sw} IN (SELECT DISTINCT #{Member.table_name}.user_id FROM #{Member.table_name}, #{MemberRole.table_name}" + | |
359 " WHERE #{Member.table_name}.project_id = #{Issue.table_name}.project_id AND #{Member.table_name}.id = #{MemberRole.table_name}.member_id AND #{role_cond}))" | |
360 end | |
361 end | |
362 | |
363 def sql_for_is_private_field(field, operator, value) | |
364 op = (operator == "=" ? 'IN' : 'NOT IN') | |
365 va = value.map {|v| v == '0' ? connection.quoted_false : connection.quoted_true}.uniq.join(',') | |
366 | |
367 "#{Issue.table_name}.is_private #{op} (#{va})" | |
368 end | |
369 | |
370 def sql_for_relations(field, operator, value, options={}) | |
371 relation_options = IssueRelation::TYPES[field] | |
372 return relation_options unless relation_options | |
373 | |
374 relation_type = field | |
375 join_column, target_join_column = "issue_from_id", "issue_to_id" | |
376 if relation_options[:reverse] || options[:reverse] | |
377 relation_type = relation_options[:reverse] || relation_type | |
378 join_column, target_join_column = target_join_column, join_column | |
379 end | |
380 | |
381 sql = case operator | |
382 when "*", "!*" | |
383 op = (operator == "*" ? 'IN' : 'NOT IN') | |
384 "#{Issue.table_name}.id #{op} (SELECT DISTINCT #{IssueRelation.table_name}.#{join_column} FROM #{IssueRelation.table_name} WHERE #{IssueRelation.table_name}.relation_type = '#{connection.quote_string(relation_type)}')" | |
385 when "=", "!" | |
386 op = (operator == "=" ? 'IN' : 'NOT IN') | |
387 "#{Issue.table_name}.id #{op} (SELECT DISTINCT #{IssueRelation.table_name}.#{join_column} FROM #{IssueRelation.table_name} WHERE #{IssueRelation.table_name}.relation_type = '#{connection.quote_string(relation_type)}' AND #{IssueRelation.table_name}.#{target_join_column} = #{value.first.to_i})" | |
388 when "=p", "=!p", "!p" | |
389 op = (operator == "!p" ? 'NOT IN' : 'IN') | |
390 comp = (operator == "=!p" ? '<>' : '=') | |
391 "#{Issue.table_name}.id #{op} (SELECT DISTINCT #{IssueRelation.table_name}.#{join_column} FROM #{IssueRelation.table_name}, #{Issue.table_name} relissues WHERE #{IssueRelation.table_name}.relation_type = '#{connection.quote_string(relation_type)}' AND #{IssueRelation.table_name}.#{target_join_column} = relissues.id AND relissues.project_id #{comp} #{value.first.to_i})" | |
392 end | |
393 | |
394 if relation_options[:sym] == field && !options[:reverse] | |
395 sqls = [sql, sql_for_relations(field, operator, value, :reverse => true)] | |
396 sqls.join(["!", "!*", "!p"].include?(operator) ? " AND " : " OR ") | |
397 else | |
398 sql | |
399 end | |
400 end | |
401 | |
402 IssueRelation::TYPES.keys.each do |relation_type| | |
403 alias_method "sql_for_#{relation_type}_field".to_sym, :sql_for_relations | |
404 end | |
405 end |