@@ -1,914 +1,914 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2012 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2012 Jean-Philippe Lang | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or |
|
4 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License |
|
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; either version 2 |
|
6 | # as published by the Free Software Foundation; either version 2 | |
7 | # of the License, or (at your option) any later version. |
|
7 | # of the License, or (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
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 |
|
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. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | class QueryColumn |
|
18 | class QueryColumn | |
19 | attr_accessor :name, :sortable, :groupable, :default_order |
|
19 | attr_accessor :name, :sortable, :groupable, :default_order | |
20 | include Redmine::I18n |
|
20 | include Redmine::I18n | |
21 |
|
21 | |||
22 | def initialize(name, options={}) |
|
22 | def initialize(name, options={}) | |
23 | self.name = name |
|
23 | self.name = name | |
24 | self.sortable = options[:sortable] |
|
24 | self.sortable = options[:sortable] | |
25 | self.groupable = options[:groupable] || false |
|
25 | self.groupable = options[:groupable] || false | |
26 | if groupable == true |
|
26 | if groupable == true | |
27 | self.groupable = name.to_s |
|
27 | self.groupable = name.to_s | |
28 | end |
|
28 | end | |
29 | self.default_order = options[:default_order] |
|
29 | self.default_order = options[:default_order] | |
30 | @caption_key = options[:caption] || "field_#{name}" |
|
30 | @caption_key = options[:caption] || "field_#{name}" | |
31 | end |
|
31 | end | |
32 |
|
32 | |||
33 | def caption |
|
33 | def caption | |
34 | l(@caption_key) |
|
34 | l(@caption_key) | |
35 | end |
|
35 | end | |
36 |
|
36 | |||
37 | # Returns true if the column is sortable, otherwise false |
|
37 | # Returns true if the column is sortable, otherwise false | |
38 | def sortable? |
|
38 | def sortable? | |
39 | !@sortable.nil? |
|
39 | !@sortable.nil? | |
40 | end |
|
40 | end | |
41 |
|
41 | |||
42 | def sortable |
|
42 | def sortable | |
43 | @sortable.is_a?(Proc) ? @sortable.call : @sortable |
|
43 | @sortable.is_a?(Proc) ? @sortable.call : @sortable | |
44 | end |
|
44 | end | |
45 |
|
45 | |||
46 | def value(issue) |
|
46 | def value(issue) | |
47 | issue.send name |
|
47 | issue.send name | |
48 | end |
|
48 | end | |
49 |
|
49 | |||
50 | def css_classes |
|
50 | def css_classes | |
51 | name |
|
51 | name | |
52 | end |
|
52 | end | |
53 | end |
|
53 | end | |
54 |
|
54 | |||
55 | class QueryCustomFieldColumn < QueryColumn |
|
55 | class QueryCustomFieldColumn < QueryColumn | |
56 |
|
56 | |||
57 | def initialize(custom_field) |
|
57 | def initialize(custom_field) | |
58 | self.name = "cf_#{custom_field.id}".to_sym |
|
58 | self.name = "cf_#{custom_field.id}".to_sym | |
59 | self.sortable = custom_field.order_statement || false |
|
59 | self.sortable = custom_field.order_statement || false | |
60 | self.groupable = custom_field.group_statement || false |
|
60 | self.groupable = custom_field.group_statement || false | |
61 | @cf = custom_field |
|
61 | @cf = custom_field | |
62 | end |
|
62 | end | |
63 |
|
63 | |||
64 | def caption |
|
64 | def caption | |
65 | @cf.name |
|
65 | @cf.name | |
66 | end |
|
66 | end | |
67 |
|
67 | |||
68 | def custom_field |
|
68 | def custom_field | |
69 | @cf |
|
69 | @cf | |
70 | end |
|
70 | end | |
71 |
|
71 | |||
72 | def value(issue) |
|
72 | def value(issue) | |
73 | cv = issue.custom_values.select {|v| v.custom_field_id == @cf.id}.collect {|v| @cf.cast_value(v.value)} |
|
73 | cv = issue.custom_values.select {|v| v.custom_field_id == @cf.id}.collect {|v| @cf.cast_value(v.value)} | |
74 | cv.size > 1 ? cv : cv.first |
|
74 | cv.size > 1 ? cv : cv.first | |
75 | end |
|
75 | end | |
76 |
|
76 | |||
77 | def css_classes |
|
77 | def css_classes | |
78 | @css_classes ||= "#{name} #{@cf.field_format}" |
|
78 | @css_classes ||= "#{name} #{@cf.field_format}" | |
79 | end |
|
79 | end | |
80 | end |
|
80 | end | |
81 |
|
81 | |||
82 | class Query < ActiveRecord::Base |
|
82 | class Query < ActiveRecord::Base | |
83 | class StatementInvalid < ::ActiveRecord::StatementInvalid |
|
83 | class StatementInvalid < ::ActiveRecord::StatementInvalid | |
84 | end |
|
84 | end | |
85 |
|
85 | |||
86 | belongs_to :project |
|
86 | belongs_to :project | |
87 | belongs_to :user |
|
87 | belongs_to :user | |
88 | serialize :filters |
|
88 | serialize :filters | |
89 | serialize :column_names |
|
89 | serialize :column_names | |
90 | serialize :sort_criteria, Array |
|
90 | serialize :sort_criteria, Array | |
91 |
|
91 | |||
92 | attr_protected :project_id, :user_id |
|
92 | attr_protected :project_id, :user_id | |
93 |
|
93 | |||
94 | validates_presence_of :name |
|
94 | validates_presence_of :name | |
95 | validates_length_of :name, :maximum => 255 |
|
95 | validates_length_of :name, :maximum => 255 | |
96 | validate :validate_query_filters |
|
96 | validate :validate_query_filters | |
97 |
|
97 | |||
98 | @@operators = { "=" => :label_equals, |
|
98 | @@operators = { "=" => :label_equals, | |
99 | "!" => :label_not_equals, |
|
99 | "!" => :label_not_equals, | |
100 | "o" => :label_open_issues, |
|
100 | "o" => :label_open_issues, | |
101 | "c" => :label_closed_issues, |
|
101 | "c" => :label_closed_issues, | |
102 | "!*" => :label_none, |
|
102 | "!*" => :label_none, | |
103 | "*" => :label_all, |
|
103 | "*" => :label_all, | |
104 | ">=" => :label_greater_or_equal, |
|
104 | ">=" => :label_greater_or_equal, | |
105 | "<=" => :label_less_or_equal, |
|
105 | "<=" => :label_less_or_equal, | |
106 | "><" => :label_between, |
|
106 | "><" => :label_between, | |
107 | "<t+" => :label_in_less_than, |
|
107 | "<t+" => :label_in_less_than, | |
108 | ">t+" => :label_in_more_than, |
|
108 | ">t+" => :label_in_more_than, | |
109 | "t+" => :label_in, |
|
109 | "t+" => :label_in, | |
110 | "t" => :label_today, |
|
110 | "t" => :label_today, | |
111 | "w" => :label_this_week, |
|
111 | "w" => :label_this_week, | |
112 | ">t-" => :label_less_than_ago, |
|
112 | ">t-" => :label_less_than_ago, | |
113 | "<t-" => :label_more_than_ago, |
|
113 | "<t-" => :label_more_than_ago, | |
114 | "t-" => :label_ago, |
|
114 | "t-" => :label_ago, | |
115 | "~" => :label_contains, |
|
115 | "~" => :label_contains, | |
116 | "!~" => :label_not_contains } |
|
116 | "!~" => :label_not_contains } | |
117 |
|
117 | |||
118 | cattr_reader :operators |
|
118 | cattr_reader :operators | |
119 |
|
119 | |||
120 | @@operators_by_filter_type = { :list => [ "=", "!" ], |
|
120 | @@operators_by_filter_type = { :list => [ "=", "!" ], | |
121 | :list_status => [ "o", "=", "!", "c", "*" ], |
|
121 | :list_status => [ "o", "=", "!", "c", "*" ], | |
122 | :list_optional => [ "=", "!", "!*", "*" ], |
|
122 | :list_optional => [ "=", "!", "!*", "*" ], | |
123 | :list_subprojects => [ "*", "!*", "=" ], |
|
123 | :list_subprojects => [ "*", "!*", "=" ], | |
124 | :date => [ "=", ">=", "<=", "><", "<t+", ">t+", "t+", "t", "w", ">t-", "<t-", "t-", "!*", "*" ], |
|
124 | :date => [ "=", ">=", "<=", "><", "<t+", ">t+", "t+", "t", "w", ">t-", "<t-", "t-", "!*", "*" ], | |
125 | :date_past => [ "=", ">=", "<=", "><", ">t-", "<t-", "t-", "t", "w", "!*", "*" ], |
|
125 | :date_past => [ "=", ">=", "<=", "><", ">t-", "<t-", "t-", "t", "w", "!*", "*" ], | |
126 | :string => [ "=", "~", "!", "!~", "!*", "*" ], |
|
126 | :string => [ "=", "~", "!", "!~", "!*", "*" ], | |
127 | :text => [ "~", "!~", "!*", "*" ], |
|
127 | :text => [ "~", "!~", "!*", "*" ], | |
128 | :integer => [ "=", ">=", "<=", "><", "!*", "*" ], |
|
128 | :integer => [ "=", ">=", "<=", "><", "!*", "*" ], | |
129 | :float => [ "=", ">=", "<=", "><", "!*", "*" ] } |
|
129 | :float => [ "=", ">=", "<=", "><", "!*", "*" ] } | |
130 |
|
130 | |||
131 | cattr_reader :operators_by_filter_type |
|
131 | cattr_reader :operators_by_filter_type | |
132 |
|
132 | |||
133 | @@available_columns = [ |
|
133 | @@available_columns = [ | |
134 | QueryColumn.new(:project, :sortable => "#{Project.table_name}.name", :groupable => true), |
|
134 | QueryColumn.new(:project, :sortable => "#{Project.table_name}.name", :groupable => true), | |
135 | QueryColumn.new(:tracker, :sortable => "#{Tracker.table_name}.position", :groupable => true), |
|
135 | QueryColumn.new(:tracker, :sortable => "#{Tracker.table_name}.position", :groupable => true), | |
136 | QueryColumn.new(:parent, :sortable => ["#{Issue.table_name}.root_id", "#{Issue.table_name}.lft ASC"], :default_order => 'desc', :caption => :field_parent_issue), |
|
136 | QueryColumn.new(:parent, :sortable => ["#{Issue.table_name}.root_id", "#{Issue.table_name}.lft ASC"], :default_order => 'desc', :caption => :field_parent_issue), | |
137 | QueryColumn.new(:status, :sortable => "#{IssueStatus.table_name}.position", :groupable => true), |
|
137 | QueryColumn.new(:status, :sortable => "#{IssueStatus.table_name}.position", :groupable => true), | |
138 | QueryColumn.new(:priority, :sortable => "#{IssuePriority.table_name}.position", :default_order => 'desc', :groupable => true), |
|
138 | QueryColumn.new(:priority, :sortable => "#{IssuePriority.table_name}.position", :default_order => 'desc', :groupable => true), | |
139 | QueryColumn.new(:subject, :sortable => "#{Issue.table_name}.subject"), |
|
139 | QueryColumn.new(:subject, :sortable => "#{Issue.table_name}.subject"), | |
140 | QueryColumn.new(:author, :sortable => lambda {User.fields_for_order_statement("authors")}, :groupable => true), |
|
140 | QueryColumn.new(:author, :sortable => lambda {User.fields_for_order_statement("authors")}, :groupable => true), | |
141 | QueryColumn.new(:assigned_to, :sortable => lambda {User.fields_for_order_statement}, :groupable => true), |
|
141 | QueryColumn.new(:assigned_to, :sortable => lambda {User.fields_for_order_statement}, :groupable => true), | |
142 | QueryColumn.new(:updated_on, :sortable => "#{Issue.table_name}.updated_on", :default_order => 'desc'), |
|
142 | QueryColumn.new(:updated_on, :sortable => "#{Issue.table_name}.updated_on", :default_order => 'desc'), | |
143 | QueryColumn.new(:category, :sortable => "#{IssueCategory.table_name}.name", :groupable => true), |
|
143 | QueryColumn.new(:category, :sortable => "#{IssueCategory.table_name}.name", :groupable => true), | |
144 |
QueryColumn.new(:fixed_version, :sortable => lambda {Version.fields_for_order_statement}, |
|
144 | QueryColumn.new(:fixed_version, :sortable => lambda {Version.fields_for_order_statement}, :groupable => true), | |
145 | QueryColumn.new(:start_date, :sortable => "#{Issue.table_name}.start_date"), |
|
145 | QueryColumn.new(:start_date, :sortable => "#{Issue.table_name}.start_date"), | |
146 | QueryColumn.new(:due_date, :sortable => "#{Issue.table_name}.due_date"), |
|
146 | QueryColumn.new(:due_date, :sortable => "#{Issue.table_name}.due_date"), | |
147 | QueryColumn.new(:estimated_hours, :sortable => "#{Issue.table_name}.estimated_hours"), |
|
147 | QueryColumn.new(:estimated_hours, :sortable => "#{Issue.table_name}.estimated_hours"), | |
148 | QueryColumn.new(:done_ratio, :sortable => "#{Issue.table_name}.done_ratio", :groupable => true), |
|
148 | QueryColumn.new(:done_ratio, :sortable => "#{Issue.table_name}.done_ratio", :groupable => true), | |
149 | QueryColumn.new(:created_on, :sortable => "#{Issue.table_name}.created_on", :default_order => 'desc'), |
|
149 | QueryColumn.new(:created_on, :sortable => "#{Issue.table_name}.created_on", :default_order => 'desc'), | |
150 | ] |
|
150 | ] | |
151 | cattr_reader :available_columns |
|
151 | cattr_reader :available_columns | |
152 |
|
152 | |||
153 | scope :visible, lambda {|*args| |
|
153 | scope :visible, lambda {|*args| | |
154 | user = args.shift || User.current |
|
154 | user = args.shift || User.current | |
155 | base = Project.allowed_to_condition(user, :view_issues, *args) |
|
155 | base = Project.allowed_to_condition(user, :view_issues, *args) | |
156 | user_id = user.logged? ? user.id : 0 |
|
156 | user_id = user.logged? ? user.id : 0 | |
157 | { |
|
157 | { | |
158 | :conditions => ["(#{table_name}.project_id IS NULL OR (#{base})) AND (#{table_name}.is_public = ? OR #{table_name}.user_id = ?)", true, user_id], |
|
158 | :conditions => ["(#{table_name}.project_id IS NULL OR (#{base})) AND (#{table_name}.is_public = ? OR #{table_name}.user_id = ?)", true, user_id], | |
159 | :include => :project |
|
159 | :include => :project | |
160 | } |
|
160 | } | |
161 | } |
|
161 | } | |
162 |
|
162 | |||
163 | def initialize(attributes=nil, *args) |
|
163 | def initialize(attributes=nil, *args) | |
164 | super attributes |
|
164 | super attributes | |
165 | self.filters ||= { 'status_id' => {:operator => "o", :values => [""]} } |
|
165 | self.filters ||= { 'status_id' => {:operator => "o", :values => [""]} } | |
166 | @is_for_all = project.nil? |
|
166 | @is_for_all = project.nil? | |
167 | end |
|
167 | end | |
168 |
|
168 | |||
169 | def validate_query_filters |
|
169 | def validate_query_filters | |
170 | filters.each_key do |field| |
|
170 | filters.each_key do |field| | |
171 | if values_for(field) |
|
171 | if values_for(field) | |
172 | case type_for(field) |
|
172 | case type_for(field) | |
173 | when :integer |
|
173 | when :integer | |
174 | add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !v.match(/^[+-]?\d+$/) } |
|
174 | add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !v.match(/^[+-]?\d+$/) } | |
175 | when :float |
|
175 | when :float | |
176 | add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !v.match(/^[+-]?\d+(\.\d*)?$/) } |
|
176 | add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !v.match(/^[+-]?\d+(\.\d*)?$/) } | |
177 | when :date, :date_past |
|
177 | when :date, :date_past | |
178 | case operator_for(field) |
|
178 | case operator_for(field) | |
179 | when "=", ">=", "<=", "><" |
|
179 | when "=", ">=", "<=", "><" | |
180 | add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && (!v.match(/^\d{4}-\d{2}-\d{2}$/) || (Date.parse(v) rescue nil).nil?) } |
|
180 | add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && (!v.match(/^\d{4}-\d{2}-\d{2}$/) || (Date.parse(v) rescue nil).nil?) } | |
181 | when ">t-", "<t-", "t-" |
|
181 | when ">t-", "<t-", "t-" | |
182 | add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !v.match(/^\d+$/) } |
|
182 | add_filter_error(field, :invalid) if values_for(field).detect {|v| v.present? && !v.match(/^\d+$/) } | |
183 | end |
|
183 | end | |
184 | end |
|
184 | end | |
185 | end |
|
185 | end | |
186 |
|
186 | |||
187 | add_filter_error(field, :blank) unless |
|
187 | add_filter_error(field, :blank) unless | |
188 | # filter requires one or more values |
|
188 | # filter requires one or more values | |
189 | (values_for(field) and !values_for(field).first.blank?) or |
|
189 | (values_for(field) and !values_for(field).first.blank?) or | |
190 | # filter doesn't require any value |
|
190 | # filter doesn't require any value | |
191 | ["o", "c", "!*", "*", "t", "w"].include? operator_for(field) |
|
191 | ["o", "c", "!*", "*", "t", "w"].include? operator_for(field) | |
192 | end if filters |
|
192 | end if filters | |
193 | end |
|
193 | end | |
194 |
|
194 | |||
195 | def add_filter_error(field, message) |
|
195 | def add_filter_error(field, message) | |
196 | m = label_for(field) + " " + l(message, :scope => 'activerecord.errors.messages') |
|
196 | m = label_for(field) + " " + l(message, :scope => 'activerecord.errors.messages') | |
197 | errors.add(:base, m) |
|
197 | errors.add(:base, m) | |
198 | end |
|
198 | end | |
199 |
|
199 | |||
200 | # Returns true if the query is visible to +user+ or the current user. |
|
200 | # Returns true if the query is visible to +user+ or the current user. | |
201 | def visible?(user=User.current) |
|
201 | def visible?(user=User.current) | |
202 | (project.nil? || user.allowed_to?(:view_issues, project)) && (self.is_public? || self.user_id == user.id) |
|
202 | (project.nil? || user.allowed_to?(:view_issues, project)) && (self.is_public? || self.user_id == user.id) | |
203 | end |
|
203 | end | |
204 |
|
204 | |||
205 | def editable_by?(user) |
|
205 | def editable_by?(user) | |
206 | return false unless user |
|
206 | return false unless user | |
207 | # Admin can edit them all and regular users can edit their private queries |
|
207 | # Admin can edit them all and regular users can edit their private queries | |
208 | return true if user.admin? || (!is_public && self.user_id == user.id) |
|
208 | return true if user.admin? || (!is_public && self.user_id == user.id) | |
209 | # Members can not edit public queries that are for all project (only admin is allowed to) |
|
209 | # Members can not edit public queries that are for all project (only admin is allowed to) | |
210 | is_public && !@is_for_all && user.allowed_to?(:manage_public_queries, project) |
|
210 | is_public && !@is_for_all && user.allowed_to?(:manage_public_queries, project) | |
211 | end |
|
211 | end | |
212 |
|
212 | |||
213 | def trackers |
|
213 | def trackers | |
214 | @trackers ||= project.nil? ? Tracker.find(:all, :order => 'position') : project.rolled_up_trackers |
|
214 | @trackers ||= project.nil? ? Tracker.find(:all, :order => 'position') : project.rolled_up_trackers | |
215 | end |
|
215 | end | |
216 |
|
216 | |||
217 | def available_filters |
|
217 | def available_filters | |
218 | return @available_filters if @available_filters |
|
218 | return @available_filters if @available_filters | |
219 |
|
219 | |||
220 | @available_filters = { "status_id" => { :type => :list_status, :order => 1, :values => IssueStatus.find(:all, :order => 'position').collect{|s| [s.name, s.id.to_s] } }, |
|
220 | @available_filters = { "status_id" => { :type => :list_status, :order => 1, :values => IssueStatus.find(:all, :order => 'position').collect{|s| [s.name, s.id.to_s] } }, | |
221 | "tracker_id" => { :type => :list, :order => 2, :values => trackers.collect{|s| [s.name, s.id.to_s] } }, |
|
221 | "tracker_id" => { :type => :list, :order => 2, :values => trackers.collect{|s| [s.name, s.id.to_s] } }, | |
222 | "priority_id" => { :type => :list, :order => 3, :values => IssuePriority.all.collect{|s| [s.name, s.id.to_s] } }, |
|
222 | "priority_id" => { :type => :list, :order => 3, :values => IssuePriority.all.collect{|s| [s.name, s.id.to_s] } }, | |
223 | "subject" => { :type => :text, :order => 8 }, |
|
223 | "subject" => { :type => :text, :order => 8 }, | |
224 | "created_on" => { :type => :date_past, :order => 9 }, |
|
224 | "created_on" => { :type => :date_past, :order => 9 }, | |
225 | "updated_on" => { :type => :date_past, :order => 10 }, |
|
225 | "updated_on" => { :type => :date_past, :order => 10 }, | |
226 | "start_date" => { :type => :date, :order => 11 }, |
|
226 | "start_date" => { :type => :date, :order => 11 }, | |
227 | "due_date" => { :type => :date, :order => 12 }, |
|
227 | "due_date" => { :type => :date, :order => 12 }, | |
228 | "estimated_hours" => { :type => :float, :order => 13 }, |
|
228 | "estimated_hours" => { :type => :float, :order => 13 }, | |
229 | "done_ratio" => { :type => :integer, :order => 14 }} |
|
229 | "done_ratio" => { :type => :integer, :order => 14 }} | |
230 |
|
230 | |||
231 | principals = [] |
|
231 | principals = [] | |
232 | if project |
|
232 | if project | |
233 | principals += project.principals.sort |
|
233 | principals += project.principals.sort | |
234 | unless project.leaf? |
|
234 | unless project.leaf? | |
235 | subprojects = project.descendants.visible.all |
|
235 | subprojects = project.descendants.visible.all | |
236 | if subprojects.any? |
|
236 | if subprojects.any? | |
237 | @available_filters["subproject_id"] = { :type => :list_subprojects, :order => 13, :values => subprojects.collect{|s| [s.name, s.id.to_s] } } |
|
237 | @available_filters["subproject_id"] = { :type => :list_subprojects, :order => 13, :values => subprojects.collect{|s| [s.name, s.id.to_s] } } | |
238 | principals += Principal.member_of(subprojects) |
|
238 | principals += Principal.member_of(subprojects) | |
239 | end |
|
239 | end | |
240 | end |
|
240 | end | |
241 | else |
|
241 | else | |
242 | all_projects = Project.visible.all |
|
242 | all_projects = Project.visible.all | |
243 | if all_projects.any? |
|
243 | if all_projects.any? | |
244 | # members of visible projects |
|
244 | # members of visible projects | |
245 | principals += Principal.member_of(all_projects) |
|
245 | principals += Principal.member_of(all_projects) | |
246 |
|
246 | |||
247 | # project filter |
|
247 | # project filter | |
248 | project_values = [] |
|
248 | project_values = [] | |
249 | if User.current.logged? && User.current.memberships.any? |
|
249 | if User.current.logged? && User.current.memberships.any? | |
250 | project_values << ["<< #{l(:label_my_projects).downcase} >>", "mine"] |
|
250 | project_values << ["<< #{l(:label_my_projects).downcase} >>", "mine"] | |
251 | end |
|
251 | end | |
252 | Project.project_tree(all_projects) do |p, level| |
|
252 | Project.project_tree(all_projects) do |p, level| | |
253 | prefix = (level > 0 ? ('--' * level + ' ') : '') |
|
253 | prefix = (level > 0 ? ('--' * level + ' ') : '') | |
254 | project_values << ["#{prefix}#{p.name}", p.id.to_s] |
|
254 | project_values << ["#{prefix}#{p.name}", p.id.to_s] | |
255 | end |
|
255 | end | |
256 | @available_filters["project_id"] = { :type => :list, :order => 1, :values => project_values} unless project_values.empty? |
|
256 | @available_filters["project_id"] = { :type => :list, :order => 1, :values => project_values} unless project_values.empty? | |
257 | end |
|
257 | end | |
258 | end |
|
258 | end | |
259 | principals.uniq! |
|
259 | principals.uniq! | |
260 | principals.sort! |
|
260 | principals.sort! | |
261 | users = principals.select {|p| p.is_a?(User)} |
|
261 | users = principals.select {|p| p.is_a?(User)} | |
262 |
|
262 | |||
263 | assigned_to_values = [] |
|
263 | assigned_to_values = [] | |
264 | assigned_to_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged? |
|
264 | assigned_to_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged? | |
265 | assigned_to_values += (Setting.issue_group_assignment? ? principals : users).collect{|s| [s.name, s.id.to_s] } |
|
265 | assigned_to_values += (Setting.issue_group_assignment? ? principals : users).collect{|s| [s.name, s.id.to_s] } | |
266 | @available_filters["assigned_to_id"] = { :type => :list_optional, :order => 4, :values => assigned_to_values } unless assigned_to_values.empty? |
|
266 | @available_filters["assigned_to_id"] = { :type => :list_optional, :order => 4, :values => assigned_to_values } unless assigned_to_values.empty? | |
267 |
|
267 | |||
268 | author_values = [] |
|
268 | author_values = [] | |
269 | author_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged? |
|
269 | author_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged? | |
270 | author_values += users.collect{|s| [s.name, s.id.to_s] } |
|
270 | author_values += users.collect{|s| [s.name, s.id.to_s] } | |
271 | @available_filters["author_id"] = { :type => :list, :order => 5, :values => author_values } unless author_values.empty? |
|
271 | @available_filters["author_id"] = { :type => :list, :order => 5, :values => author_values } unless author_values.empty? | |
272 |
|
272 | |||
273 | group_values = Group.all.collect {|g| [g.name, g.id.to_s] } |
|
273 | group_values = Group.all.collect {|g| [g.name, g.id.to_s] } | |
274 | @available_filters["member_of_group"] = { :type => :list_optional, :order => 6, :values => group_values } unless group_values.empty? |
|
274 | @available_filters["member_of_group"] = { :type => :list_optional, :order => 6, :values => group_values } unless group_values.empty? | |
275 |
|
275 | |||
276 | role_values = Role.givable.collect {|r| [r.name, r.id.to_s] } |
|
276 | role_values = Role.givable.collect {|r| [r.name, r.id.to_s] } | |
277 | @available_filters["assigned_to_role"] = { :type => :list_optional, :order => 7, :values => role_values } unless role_values.empty? |
|
277 | @available_filters["assigned_to_role"] = { :type => :list_optional, :order => 7, :values => role_values } unless role_values.empty? | |
278 |
|
278 | |||
279 | if User.current.logged? |
|
279 | if User.current.logged? | |
280 | @available_filters["watcher_id"] = { :type => :list, :order => 15, :values => [["<< #{l(:label_me)} >>", "me"]] } |
|
280 | @available_filters["watcher_id"] = { :type => :list, :order => 15, :values => [["<< #{l(:label_me)} >>", "me"]] } | |
281 | end |
|
281 | end | |
282 |
|
282 | |||
283 | if project |
|
283 | if project | |
284 | # project specific filters |
|
284 | # project specific filters | |
285 | categories = project.issue_categories.all |
|
285 | categories = project.issue_categories.all | |
286 | unless categories.empty? |
|
286 | unless categories.empty? | |
287 | @available_filters["category_id"] = { :type => :list_optional, :order => 6, :values => categories.collect{|s| [s.name, s.id.to_s] } } |
|
287 | @available_filters["category_id"] = { :type => :list_optional, :order => 6, :values => categories.collect{|s| [s.name, s.id.to_s] } } | |
288 | end |
|
288 | end | |
289 | versions = project.shared_versions.all |
|
289 | versions = project.shared_versions.all | |
290 | unless versions.empty? |
|
290 | unless versions.empty? | |
291 | @available_filters["fixed_version_id"] = { :type => :list_optional, :order => 7, :values => versions.sort.collect{|s| ["#{s.project.name} - #{s.name}", s.id.to_s] } } |
|
291 | @available_filters["fixed_version_id"] = { :type => :list_optional, :order => 7, :values => versions.sort.collect{|s| ["#{s.project.name} - #{s.name}", s.id.to_s] } } | |
292 | end |
|
292 | end | |
293 | add_custom_fields_filters(project.all_issue_custom_fields) |
|
293 | add_custom_fields_filters(project.all_issue_custom_fields) | |
294 | else |
|
294 | else | |
295 | # global filters for cross project issue list |
|
295 | # global filters for cross project issue list | |
296 | system_shared_versions = Version.visible.find_all_by_sharing('system') |
|
296 | system_shared_versions = Version.visible.find_all_by_sharing('system') | |
297 | unless system_shared_versions.empty? |
|
297 | unless system_shared_versions.empty? | |
298 | @available_filters["fixed_version_id"] = { :type => :list_optional, :order => 7, :values => system_shared_versions.sort.collect{|s| ["#{s.project.name} - #{s.name}", s.id.to_s] } } |
|
298 | @available_filters["fixed_version_id"] = { :type => :list_optional, :order => 7, :values => system_shared_versions.sort.collect{|s| ["#{s.project.name} - #{s.name}", s.id.to_s] } } | |
299 | end |
|
299 | end | |
300 | add_custom_fields_filters(IssueCustomField.find(:all, :conditions => {:is_filter => true, :is_for_all => true})) |
|
300 | add_custom_fields_filters(IssueCustomField.find(:all, :conditions => {:is_filter => true, :is_for_all => true})) | |
301 | end |
|
301 | end | |
302 |
|
302 | |||
303 | if User.current.allowed_to?(:set_issues_private, nil, :global => true) || |
|
303 | if User.current.allowed_to?(:set_issues_private, nil, :global => true) || | |
304 | User.current.allowed_to?(:set_own_issues_private, nil, :global => true) |
|
304 | User.current.allowed_to?(:set_own_issues_private, nil, :global => true) | |
305 | @available_filters["is_private"] = { :type => :list, :order => 15, :values => [[l(:general_text_yes), "1"], [l(:general_text_no), "0"]] } |
|
305 | @available_filters["is_private"] = { :type => :list, :order => 15, :values => [[l(:general_text_yes), "1"], [l(:general_text_no), "0"]] } | |
306 | end |
|
306 | end | |
307 |
|
307 | |||
308 | Tracker.disabled_core_fields(trackers).each {|field| |
|
308 | Tracker.disabled_core_fields(trackers).each {|field| | |
309 | @available_filters.delete field |
|
309 | @available_filters.delete field | |
310 | } |
|
310 | } | |
311 |
|
311 | |||
312 | @available_filters |
|
312 | @available_filters | |
313 | end |
|
313 | end | |
314 |
|
314 | |||
315 | def add_filter(field, operator, values) |
|
315 | def add_filter(field, operator, values) | |
316 | # values must be an array |
|
316 | # values must be an array | |
317 | return unless values.nil? || values.is_a?(Array) |
|
317 | return unless values.nil? || values.is_a?(Array) | |
318 | # check if field is defined as an available filter |
|
318 | # check if field is defined as an available filter | |
319 | if available_filters.has_key? field |
|
319 | if available_filters.has_key? field | |
320 | filter_options = available_filters[field] |
|
320 | filter_options = available_filters[field] | |
321 | # check if operator is allowed for that filter |
|
321 | # check if operator is allowed for that filter | |
322 | #if @@operators_by_filter_type[filter_options[:type]].include? operator |
|
322 | #if @@operators_by_filter_type[filter_options[:type]].include? operator | |
323 | # allowed_values = values & ([""] + (filter_options[:values] || []).collect {|val| val[1]}) |
|
323 | # allowed_values = values & ([""] + (filter_options[:values] || []).collect {|val| val[1]}) | |
324 | # filters[field] = {:operator => operator, :values => allowed_values } if (allowed_values.first and !allowed_values.first.empty?) or ["o", "c", "!*", "*", "t"].include? operator |
|
324 | # filters[field] = {:operator => operator, :values => allowed_values } if (allowed_values.first and !allowed_values.first.empty?) or ["o", "c", "!*", "*", "t"].include? operator | |
325 | #end |
|
325 | #end | |
326 | filters[field] = {:operator => operator, :values => (values || [''])} |
|
326 | filters[field] = {:operator => operator, :values => (values || [''])} | |
327 | end |
|
327 | end | |
328 | end |
|
328 | end | |
329 |
|
329 | |||
330 | def add_short_filter(field, expression) |
|
330 | def add_short_filter(field, expression) | |
331 | return unless expression && available_filters.has_key?(field) |
|
331 | return unless expression && available_filters.has_key?(field) | |
332 | field_type = available_filters[field][:type] |
|
332 | field_type = available_filters[field][:type] | |
333 | @@operators_by_filter_type[field_type].sort.reverse.detect do |operator| |
|
333 | @@operators_by_filter_type[field_type].sort.reverse.detect do |operator| | |
334 | next unless expression =~ /^#{Regexp.escape(operator)}(.*)$/ |
|
334 | next unless expression =~ /^#{Regexp.escape(operator)}(.*)$/ | |
335 | add_filter field, operator, $1.present? ? $1.split('|') : [''] |
|
335 | add_filter field, operator, $1.present? ? $1.split('|') : [''] | |
336 | end || add_filter(field, '=', expression.split('|')) |
|
336 | end || add_filter(field, '=', expression.split('|')) | |
337 | end |
|
337 | end | |
338 |
|
338 | |||
339 | # Add multiple filters using +add_filter+ |
|
339 | # Add multiple filters using +add_filter+ | |
340 | def add_filters(fields, operators, values) |
|
340 | def add_filters(fields, operators, values) | |
341 | if fields.is_a?(Array) && operators.is_a?(Hash) && (values.nil? || values.is_a?(Hash)) |
|
341 | if fields.is_a?(Array) && operators.is_a?(Hash) && (values.nil? || values.is_a?(Hash)) | |
342 | fields.each do |field| |
|
342 | fields.each do |field| | |
343 | add_filter(field, operators[field], values && values[field]) |
|
343 | add_filter(field, operators[field], values && values[field]) | |
344 | end |
|
344 | end | |
345 | end |
|
345 | end | |
346 | end |
|
346 | end | |
347 |
|
347 | |||
348 | def has_filter?(field) |
|
348 | def has_filter?(field) | |
349 | filters and filters[field] |
|
349 | filters and filters[field] | |
350 | end |
|
350 | end | |
351 |
|
351 | |||
352 | def type_for(field) |
|
352 | def type_for(field) | |
353 | available_filters[field][:type] if available_filters.has_key?(field) |
|
353 | available_filters[field][:type] if available_filters.has_key?(field) | |
354 | end |
|
354 | end | |
355 |
|
355 | |||
356 | def operator_for(field) |
|
356 | def operator_for(field) | |
357 | has_filter?(field) ? filters[field][:operator] : nil |
|
357 | has_filter?(field) ? filters[field][:operator] : nil | |
358 | end |
|
358 | end | |
359 |
|
359 | |||
360 | def values_for(field) |
|
360 | def values_for(field) | |
361 | has_filter?(field) ? filters[field][:values] : nil |
|
361 | has_filter?(field) ? filters[field][:values] : nil | |
362 | end |
|
362 | end | |
363 |
|
363 | |||
364 | def value_for(field, index=0) |
|
364 | def value_for(field, index=0) | |
365 | (values_for(field) || [])[index] |
|
365 | (values_for(field) || [])[index] | |
366 | end |
|
366 | end | |
367 |
|
367 | |||
368 | def label_for(field) |
|
368 | def label_for(field) | |
369 | label = available_filters[field][:name] if available_filters.has_key?(field) |
|
369 | label = available_filters[field][:name] if available_filters.has_key?(field) | |
370 | label ||= l("field_#{field.to_s.gsub(/_id$/, '')}", :default => field) |
|
370 | label ||= l("field_#{field.to_s.gsub(/_id$/, '')}", :default => field) | |
371 | end |
|
371 | end | |
372 |
|
372 | |||
373 | def available_columns |
|
373 | def available_columns | |
374 | return @available_columns if @available_columns |
|
374 | return @available_columns if @available_columns | |
375 | @available_columns = ::Query.available_columns.dup |
|
375 | @available_columns = ::Query.available_columns.dup | |
376 | @available_columns += (project ? |
|
376 | @available_columns += (project ? | |
377 | project.all_issue_custom_fields : |
|
377 | project.all_issue_custom_fields : | |
378 | IssueCustomField.find(:all) |
|
378 | IssueCustomField.find(:all) | |
379 | ).collect {|cf| QueryCustomFieldColumn.new(cf) } |
|
379 | ).collect {|cf| QueryCustomFieldColumn.new(cf) } | |
380 |
|
380 | |||
381 | if User.current.allowed_to?(:view_time_entries, project, :global => true) |
|
381 | if User.current.allowed_to?(:view_time_entries, project, :global => true) | |
382 | index = nil |
|
382 | index = nil | |
383 | @available_columns.each_with_index {|column, i| index = i if column.name == :estimated_hours} |
|
383 | @available_columns.each_with_index {|column, i| index = i if column.name == :estimated_hours} | |
384 | index = (index ? index + 1 : -1) |
|
384 | index = (index ? index + 1 : -1) | |
385 | # insert the column after estimated_hours or at the end |
|
385 | # insert the column after estimated_hours or at the end | |
386 | @available_columns.insert index, QueryColumn.new(:spent_hours, |
|
386 | @available_columns.insert index, QueryColumn.new(:spent_hours, | |
387 | :sortable => "(SELECT COALESCE(SUM(hours), 0) FROM #{TimeEntry.table_name} WHERE #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id)", |
|
387 | :sortable => "(SELECT COALESCE(SUM(hours), 0) FROM #{TimeEntry.table_name} WHERE #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id)", | |
388 | :default_order => 'desc', |
|
388 | :default_order => 'desc', | |
389 | :caption => :label_spent_time |
|
389 | :caption => :label_spent_time | |
390 | ) |
|
390 | ) | |
391 | end |
|
391 | end | |
392 |
|
392 | |||
393 | if User.current.allowed_to?(:set_issues_private, nil, :global => true) || |
|
393 | if User.current.allowed_to?(:set_issues_private, nil, :global => true) || | |
394 | User.current.allowed_to?(:set_own_issues_private, nil, :global => true) |
|
394 | User.current.allowed_to?(:set_own_issues_private, nil, :global => true) | |
395 | @available_columns << QueryColumn.new(:is_private, :sortable => "#{Issue.table_name}.is_private") |
|
395 | @available_columns << QueryColumn.new(:is_private, :sortable => "#{Issue.table_name}.is_private") | |
396 | end |
|
396 | end | |
397 |
|
397 | |||
398 | disabled_fields = Tracker.disabled_core_fields(trackers).map {|field| field.sub(/_id$/, '')} |
|
398 | disabled_fields = Tracker.disabled_core_fields(trackers).map {|field| field.sub(/_id$/, '')} | |
399 | @available_columns.reject! {|column| |
|
399 | @available_columns.reject! {|column| | |
400 | disabled_fields.include?(column.name.to_s) |
|
400 | disabled_fields.include?(column.name.to_s) | |
401 | } |
|
401 | } | |
402 |
|
402 | |||
403 | @available_columns |
|
403 | @available_columns | |
404 | end |
|
404 | end | |
405 |
|
405 | |||
406 | def self.available_columns=(v) |
|
406 | def self.available_columns=(v) | |
407 | self.available_columns = (v) |
|
407 | self.available_columns = (v) | |
408 | end |
|
408 | end | |
409 |
|
409 | |||
410 | def self.add_available_column(column) |
|
410 | def self.add_available_column(column) | |
411 | self.available_columns << (column) if column.is_a?(QueryColumn) |
|
411 | self.available_columns << (column) if column.is_a?(QueryColumn) | |
412 | end |
|
412 | end | |
413 |
|
413 | |||
414 | # Returns an array of columns that can be used to group the results |
|
414 | # Returns an array of columns that can be used to group the results | |
415 | def groupable_columns |
|
415 | def groupable_columns | |
416 | available_columns.select {|c| c.groupable} |
|
416 | available_columns.select {|c| c.groupable} | |
417 | end |
|
417 | end | |
418 |
|
418 | |||
419 | # Returns a Hash of columns and the key for sorting |
|
419 | # Returns a Hash of columns and the key for sorting | |
420 | def sortable_columns |
|
420 | def sortable_columns | |
421 | {'id' => "#{Issue.table_name}.id"}.merge(available_columns.inject({}) {|h, column| |
|
421 | {'id' => "#{Issue.table_name}.id"}.merge(available_columns.inject({}) {|h, column| | |
422 | h[column.name.to_s] = column.sortable |
|
422 | h[column.name.to_s] = column.sortable | |
423 | h |
|
423 | h | |
424 | }) |
|
424 | }) | |
425 | end |
|
425 | end | |
426 |
|
426 | |||
427 | def columns |
|
427 | def columns | |
428 | # preserve the column_names order |
|
428 | # preserve the column_names order | |
429 | (has_default_columns? ? default_columns_names : column_names).collect do |name| |
|
429 | (has_default_columns? ? default_columns_names : column_names).collect do |name| | |
430 | available_columns.find { |col| col.name == name } |
|
430 | available_columns.find { |col| col.name == name } | |
431 | end.compact |
|
431 | end.compact | |
432 | end |
|
432 | end | |
433 |
|
433 | |||
434 | def default_columns_names |
|
434 | def default_columns_names | |
435 | @default_columns_names ||= begin |
|
435 | @default_columns_names ||= begin | |
436 | default_columns = Setting.issue_list_default_columns.map(&:to_sym) |
|
436 | default_columns = Setting.issue_list_default_columns.map(&:to_sym) | |
437 |
|
437 | |||
438 | project.present? ? default_columns : [:project] | default_columns |
|
438 | project.present? ? default_columns : [:project] | default_columns | |
439 | end |
|
439 | end | |
440 | end |
|
440 | end | |
441 |
|
441 | |||
442 | def column_names=(names) |
|
442 | def column_names=(names) | |
443 | if names |
|
443 | if names | |
444 | names = names.select {|n| n.is_a?(Symbol) || !n.blank? } |
|
444 | names = names.select {|n| n.is_a?(Symbol) || !n.blank? } | |
445 | names = names.collect {|n| n.is_a?(Symbol) ? n : n.to_sym } |
|
445 | names = names.collect {|n| n.is_a?(Symbol) ? n : n.to_sym } | |
446 | # Set column_names to nil if default columns |
|
446 | # Set column_names to nil if default columns | |
447 | if names == default_columns_names |
|
447 | if names == default_columns_names | |
448 | names = nil |
|
448 | names = nil | |
449 | end |
|
449 | end | |
450 | end |
|
450 | end | |
451 | write_attribute(:column_names, names) |
|
451 | write_attribute(:column_names, names) | |
452 | end |
|
452 | end | |
453 |
|
453 | |||
454 | def has_column?(column) |
|
454 | def has_column?(column) | |
455 | column_names && column_names.include?(column.is_a?(QueryColumn) ? column.name : column) |
|
455 | column_names && column_names.include?(column.is_a?(QueryColumn) ? column.name : column) | |
456 | end |
|
456 | end | |
457 |
|
457 | |||
458 | def has_default_columns? |
|
458 | def has_default_columns? | |
459 | column_names.nil? || column_names.empty? |
|
459 | column_names.nil? || column_names.empty? | |
460 | end |
|
460 | end | |
461 |
|
461 | |||
462 | def sort_criteria=(arg) |
|
462 | def sort_criteria=(arg) | |
463 | c = [] |
|
463 | c = [] | |
464 | if arg.is_a?(Hash) |
|
464 | if arg.is_a?(Hash) | |
465 | arg = arg.keys.sort.collect {|k| arg[k]} |
|
465 | arg = arg.keys.sort.collect {|k| arg[k]} | |
466 | end |
|
466 | end | |
467 | c = arg.select {|k,o| !k.to_s.blank?}.slice(0,3).collect {|k,o| [k.to_s, o == 'desc' ? o : 'asc']} |
|
467 | c = arg.select {|k,o| !k.to_s.blank?}.slice(0,3).collect {|k,o| [k.to_s, o == 'desc' ? o : 'asc']} | |
468 | write_attribute(:sort_criteria, c) |
|
468 | write_attribute(:sort_criteria, c) | |
469 | end |
|
469 | end | |
470 |
|
470 | |||
471 | def sort_criteria |
|
471 | def sort_criteria | |
472 | read_attribute(:sort_criteria) || [] |
|
472 | read_attribute(:sort_criteria) || [] | |
473 | end |
|
473 | end | |
474 |
|
474 | |||
475 | def sort_criteria_key(arg) |
|
475 | def sort_criteria_key(arg) | |
476 | sort_criteria && sort_criteria[arg] && sort_criteria[arg].first |
|
476 | sort_criteria && sort_criteria[arg] && sort_criteria[arg].first | |
477 | end |
|
477 | end | |
478 |
|
478 | |||
479 | def sort_criteria_order(arg) |
|
479 | def sort_criteria_order(arg) | |
480 | sort_criteria && sort_criteria[arg] && sort_criteria[arg].last |
|
480 | sort_criteria && sort_criteria[arg] && sort_criteria[arg].last | |
481 | end |
|
481 | end | |
482 |
|
482 | |||
483 | # Returns the SQL sort order that should be prepended for grouping |
|
483 | # Returns the SQL sort order that should be prepended for grouping | |
484 | def group_by_sort_order |
|
484 | def group_by_sort_order | |
485 | if grouped? && (column = group_by_column) |
|
485 | if grouped? && (column = group_by_column) | |
486 | column.sortable.is_a?(Array) ? |
|
486 | column.sortable.is_a?(Array) ? | |
487 | column.sortable.collect {|s| "#{s} #{column.default_order}"}.join(',') : |
|
487 | column.sortable.collect {|s| "#{s} #{column.default_order}"}.join(',') : | |
488 | "#{column.sortable} #{column.default_order}" |
|
488 | "#{column.sortable} #{column.default_order}" | |
489 | end |
|
489 | end | |
490 | end |
|
490 | end | |
491 |
|
491 | |||
492 | # Returns true if the query is a grouped query |
|
492 | # Returns true if the query is a grouped query | |
493 | def grouped? |
|
493 | def grouped? | |
494 | !group_by_column.nil? |
|
494 | !group_by_column.nil? | |
495 | end |
|
495 | end | |
496 |
|
496 | |||
497 | def group_by_column |
|
497 | def group_by_column | |
498 | groupable_columns.detect {|c| c.groupable && c.name.to_s == group_by} |
|
498 | groupable_columns.detect {|c| c.groupable && c.name.to_s == group_by} | |
499 | end |
|
499 | end | |
500 |
|
500 | |||
501 | def group_by_statement |
|
501 | def group_by_statement | |
502 | group_by_column.try(:groupable) |
|
502 | group_by_column.try(:groupable) | |
503 | end |
|
503 | end | |
504 |
|
504 | |||
505 | def project_statement |
|
505 | def project_statement | |
506 | project_clauses = [] |
|
506 | project_clauses = [] | |
507 | if project && !project.descendants.active.empty? |
|
507 | if project && !project.descendants.active.empty? | |
508 | ids = [project.id] |
|
508 | ids = [project.id] | |
509 | if has_filter?("subproject_id") |
|
509 | if has_filter?("subproject_id") | |
510 | case operator_for("subproject_id") |
|
510 | case operator_for("subproject_id") | |
511 | when '=' |
|
511 | when '=' | |
512 | # include the selected subprojects |
|
512 | # include the selected subprojects | |
513 | ids += values_for("subproject_id").each(&:to_i) |
|
513 | ids += values_for("subproject_id").each(&:to_i) | |
514 | when '!*' |
|
514 | when '!*' | |
515 | # main project only |
|
515 | # main project only | |
516 | else |
|
516 | else | |
517 | # all subprojects |
|
517 | # all subprojects | |
518 | ids += project.descendants.collect(&:id) |
|
518 | ids += project.descendants.collect(&:id) | |
519 | end |
|
519 | end | |
520 | elsif Setting.display_subprojects_issues? |
|
520 | elsif Setting.display_subprojects_issues? | |
521 | ids += project.descendants.collect(&:id) |
|
521 | ids += project.descendants.collect(&:id) | |
522 | end |
|
522 | end | |
523 | project_clauses << "#{Project.table_name}.id IN (%s)" % ids.join(',') |
|
523 | project_clauses << "#{Project.table_name}.id IN (%s)" % ids.join(',') | |
524 | elsif project |
|
524 | elsif project | |
525 | project_clauses << "#{Project.table_name}.id = %d" % project.id |
|
525 | project_clauses << "#{Project.table_name}.id = %d" % project.id | |
526 | end |
|
526 | end | |
527 | project_clauses.any? ? project_clauses.join(' AND ') : nil |
|
527 | project_clauses.any? ? project_clauses.join(' AND ') : nil | |
528 | end |
|
528 | end | |
529 |
|
529 | |||
530 | def statement |
|
530 | def statement | |
531 | # filters clauses |
|
531 | # filters clauses | |
532 | filters_clauses = [] |
|
532 | filters_clauses = [] | |
533 | filters.each_key do |field| |
|
533 | filters.each_key do |field| | |
534 | next if field == "subproject_id" |
|
534 | next if field == "subproject_id" | |
535 | v = values_for(field).clone |
|
535 | v = values_for(field).clone | |
536 | next unless v and !v.empty? |
|
536 | next unless v and !v.empty? | |
537 | operator = operator_for(field) |
|
537 | operator = operator_for(field) | |
538 |
|
538 | |||
539 | # "me" value subsitution |
|
539 | # "me" value subsitution | |
540 | if %w(assigned_to_id author_id watcher_id).include?(field) |
|
540 | if %w(assigned_to_id author_id watcher_id).include?(field) | |
541 | if v.delete("me") |
|
541 | if v.delete("me") | |
542 | if User.current.logged? |
|
542 | if User.current.logged? | |
543 | v.push(User.current.id.to_s) |
|
543 | v.push(User.current.id.to_s) | |
544 | v += User.current.group_ids.map(&:to_s) if field == 'assigned_to_id' |
|
544 | v += User.current.group_ids.map(&:to_s) if field == 'assigned_to_id' | |
545 | else |
|
545 | else | |
546 | v.push("0") |
|
546 | v.push("0") | |
547 | end |
|
547 | end | |
548 | end |
|
548 | end | |
549 | end |
|
549 | end | |
550 |
|
550 | |||
551 | if field == 'project_id' |
|
551 | if field == 'project_id' | |
552 | if v.delete('mine') |
|
552 | if v.delete('mine') | |
553 | v += User.current.memberships.map(&:project_id).map(&:to_s) |
|
553 | v += User.current.memberships.map(&:project_id).map(&:to_s) | |
554 | end |
|
554 | end | |
555 | end |
|
555 | end | |
556 |
|
556 | |||
557 | if field =~ /^cf_(\d+)$/ |
|
557 | if field =~ /^cf_(\d+)$/ | |
558 | # custom field |
|
558 | # custom field | |
559 | filters_clauses << sql_for_custom_field(field, operator, v, $1) |
|
559 | filters_clauses << sql_for_custom_field(field, operator, v, $1) | |
560 | elsif respond_to?("sql_for_#{field}_field") |
|
560 | elsif respond_to?("sql_for_#{field}_field") | |
561 | # specific statement |
|
561 | # specific statement | |
562 | filters_clauses << send("sql_for_#{field}_field", field, operator, v) |
|
562 | filters_clauses << send("sql_for_#{field}_field", field, operator, v) | |
563 | else |
|
563 | else | |
564 | # regular field |
|
564 | # regular field | |
565 | filters_clauses << '(' + sql_for_field(field, operator, v, Issue.table_name, field) + ')' |
|
565 | filters_clauses << '(' + sql_for_field(field, operator, v, Issue.table_name, field) + ')' | |
566 | end |
|
566 | end | |
567 | end if filters and valid? |
|
567 | end if filters and valid? | |
568 |
|
568 | |||
569 | filters_clauses << project_statement |
|
569 | filters_clauses << project_statement | |
570 | filters_clauses.reject!(&:blank?) |
|
570 | filters_clauses.reject!(&:blank?) | |
571 |
|
571 | |||
572 | filters_clauses.any? ? filters_clauses.join(' AND ') : nil |
|
572 | filters_clauses.any? ? filters_clauses.join(' AND ') : nil | |
573 | end |
|
573 | end | |
574 |
|
574 | |||
575 | # Returns the issue count |
|
575 | # Returns the issue count | |
576 | def issue_count |
|
576 | def issue_count | |
577 | Issue.visible.count(:include => [:status, :project], :conditions => statement) |
|
577 | Issue.visible.count(:include => [:status, :project], :conditions => statement) | |
578 | rescue ::ActiveRecord::StatementInvalid => e |
|
578 | rescue ::ActiveRecord::StatementInvalid => e | |
579 | raise StatementInvalid.new(e.message) |
|
579 | raise StatementInvalid.new(e.message) | |
580 | end |
|
580 | end | |
581 |
|
581 | |||
582 | # Returns the issue count by group or nil if query is not grouped |
|
582 | # Returns the issue count by group or nil if query is not grouped | |
583 | def issue_count_by_group |
|
583 | def issue_count_by_group | |
584 | r = nil |
|
584 | r = nil | |
585 | if grouped? |
|
585 | if grouped? | |
586 | begin |
|
586 | begin | |
587 | # Rails3 will raise an (unexpected) RecordNotFound if there's only a nil group value |
|
587 | # Rails3 will raise an (unexpected) RecordNotFound if there's only a nil group value | |
588 | r = Issue.visible.count(:group => group_by_statement, :include => [:status, :project], :conditions => statement) |
|
588 | r = Issue.visible.count(:group => group_by_statement, :include => [:status, :project], :conditions => statement) | |
589 | rescue ActiveRecord::RecordNotFound |
|
589 | rescue ActiveRecord::RecordNotFound | |
590 | r = {nil => issue_count} |
|
590 | r = {nil => issue_count} | |
591 | end |
|
591 | end | |
592 | c = group_by_column |
|
592 | c = group_by_column | |
593 | if c.is_a?(QueryCustomFieldColumn) |
|
593 | if c.is_a?(QueryCustomFieldColumn) | |
594 | r = r.keys.inject({}) {|h, k| h[c.custom_field.cast_value(k)] = r[k]; h} |
|
594 | r = r.keys.inject({}) {|h, k| h[c.custom_field.cast_value(k)] = r[k]; h} | |
595 | end |
|
595 | end | |
596 | end |
|
596 | end | |
597 | r |
|
597 | r | |
598 | rescue ::ActiveRecord::StatementInvalid => e |
|
598 | rescue ::ActiveRecord::StatementInvalid => e | |
599 | raise StatementInvalid.new(e.message) |
|
599 | raise StatementInvalid.new(e.message) | |
600 | end |
|
600 | end | |
601 |
|
601 | |||
602 | # Returns the issues |
|
602 | # Returns the issues | |
603 | # Valid options are :order, :offset, :limit, :include, :conditions |
|
603 | # Valid options are :order, :offset, :limit, :include, :conditions | |
604 | def issues(options={}) |
|
604 | def issues(options={}) | |
605 | order_option = [group_by_sort_order, options[:order]].reject {|s| s.blank?}.join(',') |
|
605 | order_option = [group_by_sort_order, options[:order]].reject {|s| s.blank?}.join(',') | |
606 | order_option = nil if order_option.blank? |
|
606 | order_option = nil if order_option.blank? | |
607 |
|
607 | |||
608 | issues = Issue.visible.scoped(:conditions => options[:conditions]).find :all, :include => ([:status, :project] + (options[:include] || [])).uniq, |
|
608 | issues = Issue.visible.scoped(:conditions => options[:conditions]).find :all, :include => ([:status, :project] + (options[:include] || [])).uniq, | |
609 | :conditions => statement, |
|
609 | :conditions => statement, | |
610 | :order => order_option, |
|
610 | :order => order_option, | |
611 | :joins => joins_for_order_statement(order_option), |
|
611 | :joins => joins_for_order_statement(order_option), | |
612 | :limit => options[:limit], |
|
612 | :limit => options[:limit], | |
613 | :offset => options[:offset] |
|
613 | :offset => options[:offset] | |
614 |
|
614 | |||
615 | if has_column?(:spent_hours) |
|
615 | if has_column?(:spent_hours) | |
616 | Issue.load_visible_spent_hours(issues) |
|
616 | Issue.load_visible_spent_hours(issues) | |
617 | end |
|
617 | end | |
618 | issues |
|
618 | issues | |
619 | rescue ::ActiveRecord::StatementInvalid => e |
|
619 | rescue ::ActiveRecord::StatementInvalid => e | |
620 | raise StatementInvalid.new(e.message) |
|
620 | raise StatementInvalid.new(e.message) | |
621 | end |
|
621 | end | |
622 |
|
622 | |||
623 | # Returns the issues ids |
|
623 | # Returns the issues ids | |
624 | def issue_ids(options={}) |
|
624 | def issue_ids(options={}) | |
625 | order_option = [group_by_sort_order, options[:order]].reject {|s| s.blank?}.join(',') |
|
625 | order_option = [group_by_sort_order, options[:order]].reject {|s| s.blank?}.join(',') | |
626 | order_option = nil if order_option.blank? |
|
626 | order_option = nil if order_option.blank? | |
627 |
|
627 | |||
628 | Issue.visible.scoped(:conditions => options[:conditions]).scoped(:include => ([:status, :project] + (options[:include] || [])).uniq, |
|
628 | Issue.visible.scoped(:conditions => options[:conditions]).scoped(:include => ([:status, :project] + (options[:include] || [])).uniq, | |
629 | :conditions => statement, |
|
629 | :conditions => statement, | |
630 | :order => order_option, |
|
630 | :order => order_option, | |
631 | :joins => joins_for_order_statement(order_option), |
|
631 | :joins => joins_for_order_statement(order_option), | |
632 | :limit => options[:limit], |
|
632 | :limit => options[:limit], | |
633 | :offset => options[:offset]).find_ids |
|
633 | :offset => options[:offset]).find_ids | |
634 | rescue ::ActiveRecord::StatementInvalid => e |
|
634 | rescue ::ActiveRecord::StatementInvalid => e | |
635 | raise StatementInvalid.new(e.message) |
|
635 | raise StatementInvalid.new(e.message) | |
636 | end |
|
636 | end | |
637 |
|
637 | |||
638 | # Returns the journals |
|
638 | # Returns the journals | |
639 | # Valid options are :order, :offset, :limit |
|
639 | # Valid options are :order, :offset, :limit | |
640 | def journals(options={}) |
|
640 | def journals(options={}) | |
641 | Journal.visible.find :all, :include => [:details, :user, {:issue => [:project, :author, :tracker, :status]}], |
|
641 | Journal.visible.find :all, :include => [:details, :user, {:issue => [:project, :author, :tracker, :status]}], | |
642 | :conditions => statement, |
|
642 | :conditions => statement, | |
643 | :order => options[:order], |
|
643 | :order => options[:order], | |
644 | :limit => options[:limit], |
|
644 | :limit => options[:limit], | |
645 | :offset => options[:offset] |
|
645 | :offset => options[:offset] | |
646 | rescue ::ActiveRecord::StatementInvalid => e |
|
646 | rescue ::ActiveRecord::StatementInvalid => e | |
647 | raise StatementInvalid.new(e.message) |
|
647 | raise StatementInvalid.new(e.message) | |
648 | end |
|
648 | end | |
649 |
|
649 | |||
650 | # Returns the versions |
|
650 | # Returns the versions | |
651 | # Valid options are :conditions |
|
651 | # Valid options are :conditions | |
652 | def versions(options={}) |
|
652 | def versions(options={}) | |
653 | Version.visible.scoped(:conditions => options[:conditions]).find :all, :include => :project, :conditions => project_statement |
|
653 | Version.visible.scoped(:conditions => options[:conditions]).find :all, :include => :project, :conditions => project_statement | |
654 | rescue ::ActiveRecord::StatementInvalid => e |
|
654 | rescue ::ActiveRecord::StatementInvalid => e | |
655 | raise StatementInvalid.new(e.message) |
|
655 | raise StatementInvalid.new(e.message) | |
656 | end |
|
656 | end | |
657 |
|
657 | |||
658 | def sql_for_watcher_id_field(field, operator, value) |
|
658 | def sql_for_watcher_id_field(field, operator, value) | |
659 | db_table = Watcher.table_name |
|
659 | db_table = Watcher.table_name | |
660 | "#{Issue.table_name}.id #{ operator == '=' ? 'IN' : 'NOT IN' } (SELECT #{db_table}.watchable_id FROM #{db_table} WHERE #{db_table}.watchable_type='Issue' AND " + |
|
660 | "#{Issue.table_name}.id #{ operator == '=' ? 'IN' : 'NOT IN' } (SELECT #{db_table}.watchable_id FROM #{db_table} WHERE #{db_table}.watchable_type='Issue' AND " + | |
661 | sql_for_field(field, '=', value, db_table, 'user_id') + ')' |
|
661 | sql_for_field(field, '=', value, db_table, 'user_id') + ')' | |
662 | end |
|
662 | end | |
663 |
|
663 | |||
664 | def sql_for_member_of_group_field(field, operator, value) |
|
664 | def sql_for_member_of_group_field(field, operator, value) | |
665 | if operator == '*' # Any group |
|
665 | if operator == '*' # Any group | |
666 | groups = Group.all |
|
666 | groups = Group.all | |
667 | operator = '=' # Override the operator since we want to find by assigned_to |
|
667 | operator = '=' # Override the operator since we want to find by assigned_to | |
668 | elsif operator == "!*" |
|
668 | elsif operator == "!*" | |
669 | groups = Group.all |
|
669 | groups = Group.all | |
670 | operator = '!' # Override the operator since we want to find by assigned_to |
|
670 | operator = '!' # Override the operator since we want to find by assigned_to | |
671 | else |
|
671 | else | |
672 | groups = Group.find_all_by_id(value) |
|
672 | groups = Group.find_all_by_id(value) | |
673 | end |
|
673 | end | |
674 | groups ||= [] |
|
674 | groups ||= [] | |
675 |
|
675 | |||
676 | members_of_groups = groups.inject([]) {|user_ids, group| |
|
676 | members_of_groups = groups.inject([]) {|user_ids, group| | |
677 | if group && group.user_ids.present? |
|
677 | if group && group.user_ids.present? | |
678 | user_ids << group.user_ids |
|
678 | user_ids << group.user_ids | |
679 | end |
|
679 | end | |
680 | user_ids.flatten.uniq.compact |
|
680 | user_ids.flatten.uniq.compact | |
681 | }.sort.collect(&:to_s) |
|
681 | }.sort.collect(&:to_s) | |
682 |
|
682 | |||
683 | '(' + sql_for_field("assigned_to_id", operator, members_of_groups, Issue.table_name, "assigned_to_id", false) + ')' |
|
683 | '(' + sql_for_field("assigned_to_id", operator, members_of_groups, Issue.table_name, "assigned_to_id", false) + ')' | |
684 | end |
|
684 | end | |
685 |
|
685 | |||
686 | def sql_for_assigned_to_role_field(field, operator, value) |
|
686 | def sql_for_assigned_to_role_field(field, operator, value) | |
687 | case operator |
|
687 | case operator | |
688 | when "*", "!*" # Member / Not member |
|
688 | when "*", "!*" # Member / Not member | |
689 | sw = operator == "!*" ? 'NOT' : '' |
|
689 | sw = operator == "!*" ? 'NOT' : '' | |
690 | nl = operator == "!*" ? "#{Issue.table_name}.assigned_to_id IS NULL OR" : '' |
|
690 | nl = operator == "!*" ? "#{Issue.table_name}.assigned_to_id IS NULL OR" : '' | |
691 | "(#{nl} #{Issue.table_name}.assigned_to_id #{sw} IN (SELECT DISTINCT #{Member.table_name}.user_id FROM #{Member.table_name}" + |
|
691 | "(#{nl} #{Issue.table_name}.assigned_to_id #{sw} IN (SELECT DISTINCT #{Member.table_name}.user_id FROM #{Member.table_name}" + | |
692 | " WHERE #{Member.table_name}.project_id = #{Issue.table_name}.project_id))" |
|
692 | " WHERE #{Member.table_name}.project_id = #{Issue.table_name}.project_id))" | |
693 | when "=", "!" |
|
693 | when "=", "!" | |
694 | role_cond = value.any? ? |
|
694 | role_cond = value.any? ? | |
695 | "#{MemberRole.table_name}.role_id IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")" : |
|
695 | "#{MemberRole.table_name}.role_id IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")" : | |
696 | "1=0" |
|
696 | "1=0" | |
697 |
|
697 | |||
698 | sw = operator == "!" ? 'NOT' : '' |
|
698 | sw = operator == "!" ? 'NOT' : '' | |
699 | nl = operator == "!" ? "#{Issue.table_name}.assigned_to_id IS NULL OR" : '' |
|
699 | nl = operator == "!" ? "#{Issue.table_name}.assigned_to_id IS NULL OR" : '' | |
700 | "(#{nl} #{Issue.table_name}.assigned_to_id #{sw} IN (SELECT DISTINCT #{Member.table_name}.user_id FROM #{Member.table_name}, #{MemberRole.table_name}" + |
|
700 | "(#{nl} #{Issue.table_name}.assigned_to_id #{sw} IN (SELECT DISTINCT #{Member.table_name}.user_id FROM #{Member.table_name}, #{MemberRole.table_name}" + | |
701 | " WHERE #{Member.table_name}.project_id = #{Issue.table_name}.project_id AND #{Member.table_name}.id = #{MemberRole.table_name}.member_id AND #{role_cond}))" |
|
701 | " WHERE #{Member.table_name}.project_id = #{Issue.table_name}.project_id AND #{Member.table_name}.id = #{MemberRole.table_name}.member_id AND #{role_cond}))" | |
702 | end |
|
702 | end | |
703 | end |
|
703 | end | |
704 |
|
704 | |||
705 | def sql_for_is_private_field(field, operator, value) |
|
705 | def sql_for_is_private_field(field, operator, value) | |
706 | op = (operator == "=" ? 'IN' : 'NOT IN') |
|
706 | op = (operator == "=" ? 'IN' : 'NOT IN') | |
707 | va = value.map {|v| v == '0' ? connection.quoted_false : connection.quoted_true}.uniq.join(',') |
|
707 | va = value.map {|v| v == '0' ? connection.quoted_false : connection.quoted_true}.uniq.join(',') | |
708 |
|
708 | |||
709 | "#{Issue.table_name}.is_private #{op} (#{va})" |
|
709 | "#{Issue.table_name}.is_private #{op} (#{va})" | |
710 | end |
|
710 | end | |
711 |
|
711 | |||
712 | private |
|
712 | private | |
713 |
|
713 | |||
714 | def sql_for_custom_field(field, operator, value, custom_field_id) |
|
714 | def sql_for_custom_field(field, operator, value, custom_field_id) | |
715 | db_table = CustomValue.table_name |
|
715 | db_table = CustomValue.table_name | |
716 | db_field = 'value' |
|
716 | db_field = 'value' | |
717 | filter = @available_filters[field] |
|
717 | filter = @available_filters[field] | |
718 | if filter && filter[:format] == 'user' |
|
718 | if filter && filter[:format] == 'user' | |
719 | if value.delete('me') |
|
719 | if value.delete('me') | |
720 | value.push User.current.id.to_s |
|
720 | value.push User.current.id.to_s | |
721 | end |
|
721 | end | |
722 | end |
|
722 | end | |
723 | not_in = nil |
|
723 | not_in = nil | |
724 | if operator == '!' |
|
724 | if operator == '!' | |
725 | # Makes ! operator work for custom fields with multiple values |
|
725 | # Makes ! operator work for custom fields with multiple values | |
726 | operator = '=' |
|
726 | operator = '=' | |
727 | not_in = 'NOT' |
|
727 | not_in = 'NOT' | |
728 | end |
|
728 | end | |
729 | "#{Issue.table_name}.id #{not_in} IN (SELECT #{Issue.table_name}.id FROM #{Issue.table_name} LEFT OUTER JOIN #{db_table} ON #{db_table}.customized_type='Issue' AND #{db_table}.customized_id=#{Issue.table_name}.id AND #{db_table}.custom_field_id=#{custom_field_id} WHERE " + |
|
729 | "#{Issue.table_name}.id #{not_in} IN (SELECT #{Issue.table_name}.id FROM #{Issue.table_name} LEFT OUTER JOIN #{db_table} ON #{db_table}.customized_type='Issue' AND #{db_table}.customized_id=#{Issue.table_name}.id AND #{db_table}.custom_field_id=#{custom_field_id} WHERE " + | |
730 | sql_for_field(field, operator, value, db_table, db_field, true) + ')' |
|
730 | sql_for_field(field, operator, value, db_table, db_field, true) + ')' | |
731 | end |
|
731 | end | |
732 |
|
732 | |||
733 | # Helper method to generate the WHERE sql for a +field+, +operator+ and a +value+ |
|
733 | # Helper method to generate the WHERE sql for a +field+, +operator+ and a +value+ | |
734 | def sql_for_field(field, operator, value, db_table, db_field, is_custom_filter=false) |
|
734 | def sql_for_field(field, operator, value, db_table, db_field, is_custom_filter=false) | |
735 | sql = '' |
|
735 | sql = '' | |
736 | case operator |
|
736 | case operator | |
737 | when "=" |
|
737 | when "=" | |
738 | if value.any? |
|
738 | if value.any? | |
739 | case type_for(field) |
|
739 | case type_for(field) | |
740 | when :date, :date_past |
|
740 | when :date, :date_past | |
741 | sql = date_clause(db_table, db_field, (Date.parse(value.first) rescue nil), (Date.parse(value.first) rescue nil)) |
|
741 | sql = date_clause(db_table, db_field, (Date.parse(value.first) rescue nil), (Date.parse(value.first) rescue nil)) | |
742 | when :integer |
|
742 | when :integer | |
743 | if is_custom_filter |
|
743 | if is_custom_filter | |
744 | sql = "(#{db_table}.#{db_field} <> '' AND CAST(#{db_table}.#{db_field} AS decimal(60,3)) = #{value.first.to_i})" |
|
744 | sql = "(#{db_table}.#{db_field} <> '' AND CAST(#{db_table}.#{db_field} AS decimal(60,3)) = #{value.first.to_i})" | |
745 | else |
|
745 | else | |
746 | sql = "#{db_table}.#{db_field} = #{value.first.to_i}" |
|
746 | sql = "#{db_table}.#{db_field} = #{value.first.to_i}" | |
747 | end |
|
747 | end | |
748 | when :float |
|
748 | when :float | |
749 | if is_custom_filter |
|
749 | if is_custom_filter | |
750 | sql = "(#{db_table}.#{db_field} <> '' AND CAST(#{db_table}.#{db_field} AS decimal(60,3)) BETWEEN #{value.first.to_f - 1e-5} AND #{value.first.to_f + 1e-5})" |
|
750 | sql = "(#{db_table}.#{db_field} <> '' AND CAST(#{db_table}.#{db_field} AS decimal(60,3)) BETWEEN #{value.first.to_f - 1e-5} AND #{value.first.to_f + 1e-5})" | |
751 | else |
|
751 | else | |
752 | sql = "#{db_table}.#{db_field} BETWEEN #{value.first.to_f - 1e-5} AND #{value.first.to_f + 1e-5}" |
|
752 | sql = "#{db_table}.#{db_field} BETWEEN #{value.first.to_f - 1e-5} AND #{value.first.to_f + 1e-5}" | |
753 | end |
|
753 | end | |
754 | else |
|
754 | else | |
755 | sql = "#{db_table}.#{db_field} IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")" |
|
755 | sql = "#{db_table}.#{db_field} IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + ")" | |
756 | end |
|
756 | end | |
757 | else |
|
757 | else | |
758 | # IN an empty set |
|
758 | # IN an empty set | |
759 | sql = "1=0" |
|
759 | sql = "1=0" | |
760 | end |
|
760 | end | |
761 | when "!" |
|
761 | when "!" | |
762 | if value.any? |
|
762 | if value.any? | |
763 | sql = "(#{db_table}.#{db_field} IS NULL OR #{db_table}.#{db_field} NOT IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + "))" |
|
763 | sql = "(#{db_table}.#{db_field} IS NULL OR #{db_table}.#{db_field} NOT IN (" + value.collect{|val| "'#{connection.quote_string(val)}'"}.join(",") + "))" | |
764 | else |
|
764 | else | |
765 | # NOT IN an empty set |
|
765 | # NOT IN an empty set | |
766 | sql = "1=1" |
|
766 | sql = "1=1" | |
767 | end |
|
767 | end | |
768 | when "!*" |
|
768 | when "!*" | |
769 | sql = "#{db_table}.#{db_field} IS NULL" |
|
769 | sql = "#{db_table}.#{db_field} IS NULL" | |
770 | sql << " OR #{db_table}.#{db_field} = ''" if is_custom_filter |
|
770 | sql << " OR #{db_table}.#{db_field} = ''" if is_custom_filter | |
771 | when "*" |
|
771 | when "*" | |
772 | sql = "#{db_table}.#{db_field} IS NOT NULL" |
|
772 | sql = "#{db_table}.#{db_field} IS NOT NULL" | |
773 | sql << " AND #{db_table}.#{db_field} <> ''" if is_custom_filter |
|
773 | sql << " AND #{db_table}.#{db_field} <> ''" if is_custom_filter | |
774 | when ">=" |
|
774 | when ">=" | |
775 | if [:date, :date_past].include?(type_for(field)) |
|
775 | if [:date, :date_past].include?(type_for(field)) | |
776 | sql = date_clause(db_table, db_field, (Date.parse(value.first) rescue nil), nil) |
|
776 | sql = date_clause(db_table, db_field, (Date.parse(value.first) rescue nil), nil) | |
777 | else |
|
777 | else | |
778 | if is_custom_filter |
|
778 | if is_custom_filter | |
779 | sql = "(#{db_table}.#{db_field} <> '' AND CAST(#{db_table}.#{db_field} AS decimal(60,3)) >= #{value.first.to_f})" |
|
779 | sql = "(#{db_table}.#{db_field} <> '' AND CAST(#{db_table}.#{db_field} AS decimal(60,3)) >= #{value.first.to_f})" | |
780 | else |
|
780 | else | |
781 | sql = "#{db_table}.#{db_field} >= #{value.first.to_f}" |
|
781 | sql = "#{db_table}.#{db_field} >= #{value.first.to_f}" | |
782 | end |
|
782 | end | |
783 | end |
|
783 | end | |
784 | when "<=" |
|
784 | when "<=" | |
785 | if [:date, :date_past].include?(type_for(field)) |
|
785 | if [:date, :date_past].include?(type_for(field)) | |
786 | sql = date_clause(db_table, db_field, nil, (Date.parse(value.first) rescue nil)) |
|
786 | sql = date_clause(db_table, db_field, nil, (Date.parse(value.first) rescue nil)) | |
787 | else |
|
787 | else | |
788 | if is_custom_filter |
|
788 | if is_custom_filter | |
789 | sql = "(#{db_table}.#{db_field} <> '' AND CAST(#{db_table}.#{db_field} AS decimal(60,3)) <= #{value.first.to_f})" |
|
789 | sql = "(#{db_table}.#{db_field} <> '' AND CAST(#{db_table}.#{db_field} AS decimal(60,3)) <= #{value.first.to_f})" | |
790 | else |
|
790 | else | |
791 | sql = "#{db_table}.#{db_field} <= #{value.first.to_f}" |
|
791 | sql = "#{db_table}.#{db_field} <= #{value.first.to_f}" | |
792 | end |
|
792 | end | |
793 | end |
|
793 | end | |
794 | when "><" |
|
794 | when "><" | |
795 | if [:date, :date_past].include?(type_for(field)) |
|
795 | if [:date, :date_past].include?(type_for(field)) | |
796 | sql = date_clause(db_table, db_field, (Date.parse(value[0]) rescue nil), (Date.parse(value[1]) rescue nil)) |
|
796 | sql = date_clause(db_table, db_field, (Date.parse(value[0]) rescue nil), (Date.parse(value[1]) rescue nil)) | |
797 | else |
|
797 | else | |
798 | if is_custom_filter |
|
798 | if is_custom_filter | |
799 | sql = "(#{db_table}.#{db_field} <> '' AND CAST(#{db_table}.#{db_field} AS decimal(60,3)) BETWEEN #{value[0].to_f} AND #{value[1].to_f})" |
|
799 | sql = "(#{db_table}.#{db_field} <> '' AND CAST(#{db_table}.#{db_field} AS decimal(60,3)) BETWEEN #{value[0].to_f} AND #{value[1].to_f})" | |
800 | else |
|
800 | else | |
801 | sql = "#{db_table}.#{db_field} BETWEEN #{value[0].to_f} AND #{value[1].to_f}" |
|
801 | sql = "#{db_table}.#{db_field} BETWEEN #{value[0].to_f} AND #{value[1].to_f}" | |
802 | end |
|
802 | end | |
803 | end |
|
803 | end | |
804 | when "o" |
|
804 | when "o" | |
805 | sql = "#{Issue.table_name}.status_id IN (SELECT id FROM #{IssueStatus.table_name} WHERE is_closed=#{connection.quoted_false})" if field == "status_id" |
|
805 | sql = "#{Issue.table_name}.status_id IN (SELECT id FROM #{IssueStatus.table_name} WHERE is_closed=#{connection.quoted_false})" if field == "status_id" | |
806 | when "c" |
|
806 | when "c" | |
807 | sql = "#{Issue.table_name}.status_id IN (SELECT id FROM #{IssueStatus.table_name} WHERE is_closed=#{connection.quoted_true})" if field == "status_id" |
|
807 | sql = "#{Issue.table_name}.status_id IN (SELECT id FROM #{IssueStatus.table_name} WHERE is_closed=#{connection.quoted_true})" if field == "status_id" | |
808 | when ">t-" |
|
808 | when ">t-" | |
809 | sql = relative_date_clause(db_table, db_field, - value.first.to_i, 0) |
|
809 | sql = relative_date_clause(db_table, db_field, - value.first.to_i, 0) | |
810 | when "<t-" |
|
810 | when "<t-" | |
811 | sql = relative_date_clause(db_table, db_field, nil, - value.first.to_i) |
|
811 | sql = relative_date_clause(db_table, db_field, nil, - value.first.to_i) | |
812 | when "t-" |
|
812 | when "t-" | |
813 | sql = relative_date_clause(db_table, db_field, - value.first.to_i, - value.first.to_i) |
|
813 | sql = relative_date_clause(db_table, db_field, - value.first.to_i, - value.first.to_i) | |
814 | when ">t+" |
|
814 | when ">t+" | |
815 | sql = relative_date_clause(db_table, db_field, value.first.to_i, nil) |
|
815 | sql = relative_date_clause(db_table, db_field, value.first.to_i, nil) | |
816 | when "<t+" |
|
816 | when "<t+" | |
817 | sql = relative_date_clause(db_table, db_field, 0, value.first.to_i) |
|
817 | sql = relative_date_clause(db_table, db_field, 0, value.first.to_i) | |
818 | when "t+" |
|
818 | when "t+" | |
819 | sql = relative_date_clause(db_table, db_field, value.first.to_i, value.first.to_i) |
|
819 | sql = relative_date_clause(db_table, db_field, value.first.to_i, value.first.to_i) | |
820 | when "t" |
|
820 | when "t" | |
821 | sql = relative_date_clause(db_table, db_field, 0, 0) |
|
821 | sql = relative_date_clause(db_table, db_field, 0, 0) | |
822 | when "w" |
|
822 | when "w" | |
823 | first_day_of_week = l(:general_first_day_of_week).to_i |
|
823 | first_day_of_week = l(:general_first_day_of_week).to_i | |
824 | day_of_week = Date.today.cwday |
|
824 | day_of_week = Date.today.cwday | |
825 | days_ago = (day_of_week >= first_day_of_week ? day_of_week - first_day_of_week : day_of_week + 7 - first_day_of_week) |
|
825 | days_ago = (day_of_week >= first_day_of_week ? day_of_week - first_day_of_week : day_of_week + 7 - first_day_of_week) | |
826 | sql = relative_date_clause(db_table, db_field, - days_ago, - days_ago + 6) |
|
826 | sql = relative_date_clause(db_table, db_field, - days_ago, - days_ago + 6) | |
827 | when "~" |
|
827 | when "~" | |
828 | sql = "LOWER(#{db_table}.#{db_field}) LIKE '%#{connection.quote_string(value.first.to_s.downcase)}%'" |
|
828 | sql = "LOWER(#{db_table}.#{db_field}) LIKE '%#{connection.quote_string(value.first.to_s.downcase)}%'" | |
829 | when "!~" |
|
829 | when "!~" | |
830 | sql = "LOWER(#{db_table}.#{db_field}) NOT LIKE '%#{connection.quote_string(value.first.to_s.downcase)}%'" |
|
830 | sql = "LOWER(#{db_table}.#{db_field}) NOT LIKE '%#{connection.quote_string(value.first.to_s.downcase)}%'" | |
831 | else |
|
831 | else | |
832 | raise "Unknown query operator #{operator}" |
|
832 | raise "Unknown query operator #{operator}" | |
833 | end |
|
833 | end | |
834 |
|
834 | |||
835 | return sql |
|
835 | return sql | |
836 | end |
|
836 | end | |
837 |
|
837 | |||
838 | def add_custom_fields_filters(custom_fields) |
|
838 | def add_custom_fields_filters(custom_fields) | |
839 | @available_filters ||= {} |
|
839 | @available_filters ||= {} | |
840 |
|
840 | |||
841 | custom_fields.select(&:is_filter?).each do |field| |
|
841 | custom_fields.select(&:is_filter?).each do |field| | |
842 | case field.field_format |
|
842 | case field.field_format | |
843 | when "text" |
|
843 | when "text" | |
844 | options = { :type => :text, :order => 20 } |
|
844 | options = { :type => :text, :order => 20 } | |
845 | when "list" |
|
845 | when "list" | |
846 | options = { :type => :list_optional, :values => field.possible_values, :order => 20} |
|
846 | options = { :type => :list_optional, :values => field.possible_values, :order => 20} | |
847 | when "date" |
|
847 | when "date" | |
848 | options = { :type => :date, :order => 20 } |
|
848 | options = { :type => :date, :order => 20 } | |
849 | when "bool" |
|
849 | when "bool" | |
850 | options = { :type => :list, :values => [[l(:general_text_yes), "1"], [l(:general_text_no), "0"]], :order => 20 } |
|
850 | options = { :type => :list, :values => [[l(:general_text_yes), "1"], [l(:general_text_no), "0"]], :order => 20 } | |
851 | when "int" |
|
851 | when "int" | |
852 | options = { :type => :integer, :order => 20 } |
|
852 | options = { :type => :integer, :order => 20 } | |
853 | when "float" |
|
853 | when "float" | |
854 | options = { :type => :float, :order => 20 } |
|
854 | options = { :type => :float, :order => 20 } | |
855 | when "user", "version" |
|
855 | when "user", "version" | |
856 | next unless project |
|
856 | next unless project | |
857 | values = field.possible_values_options(project) |
|
857 | values = field.possible_values_options(project) | |
858 | if User.current.logged? && field.field_format == 'user' |
|
858 | if User.current.logged? && field.field_format == 'user' | |
859 | values.unshift ["<< #{l(:label_me)} >>", "me"] |
|
859 | values.unshift ["<< #{l(:label_me)} >>", "me"] | |
860 | end |
|
860 | end | |
861 | options = { :type => :list_optional, :values => values, :order => 20} |
|
861 | options = { :type => :list_optional, :values => values, :order => 20} | |
862 | else |
|
862 | else | |
863 | options = { :type => :string, :order => 20 } |
|
863 | options = { :type => :string, :order => 20 } | |
864 | end |
|
864 | end | |
865 | @available_filters["cf_#{field.id}"] = options.merge({ :name => field.name, :format => field.field_format }) |
|
865 | @available_filters["cf_#{field.id}"] = options.merge({ :name => field.name, :format => field.field_format }) | |
866 | end |
|
866 | end | |
867 | end |
|
867 | end | |
868 |
|
868 | |||
869 | # Returns a SQL clause for a date or datetime field. |
|
869 | # Returns a SQL clause for a date or datetime field. | |
870 | def date_clause(table, field, from, to) |
|
870 | def date_clause(table, field, from, to) | |
871 | s = [] |
|
871 | s = [] | |
872 | if from |
|
872 | if from | |
873 | from_yesterday = from - 1 |
|
873 | from_yesterday = from - 1 | |
874 | from_yesterday_time = Time.local(from_yesterday.year, from_yesterday.month, from_yesterday.day) |
|
874 | from_yesterday_time = Time.local(from_yesterday.year, from_yesterday.month, from_yesterday.day) | |
875 | if self.class.default_timezone == :utc |
|
875 | if self.class.default_timezone == :utc | |
876 | from_yesterday_time = from_yesterday_time.utc |
|
876 | from_yesterday_time = from_yesterday_time.utc | |
877 | end |
|
877 | end | |
878 | s << ("#{table}.#{field} > '%s'" % [connection.quoted_date(from_yesterday_time.end_of_day)]) |
|
878 | s << ("#{table}.#{field} > '%s'" % [connection.quoted_date(from_yesterday_time.end_of_day)]) | |
879 | end |
|
879 | end | |
880 | if to |
|
880 | if to | |
881 | to_time = Time.local(to.year, to.month, to.day) |
|
881 | to_time = Time.local(to.year, to.month, to.day) | |
882 | if self.class.default_timezone == :utc |
|
882 | if self.class.default_timezone == :utc | |
883 | to_time = to_time.utc |
|
883 | to_time = to_time.utc | |
884 | end |
|
884 | end | |
885 | s << ("#{table}.#{field} <= '%s'" % [connection.quoted_date(to_time.end_of_day)]) |
|
885 | s << ("#{table}.#{field} <= '%s'" % [connection.quoted_date(to_time.end_of_day)]) | |
886 | end |
|
886 | end | |
887 | s.join(' AND ') |
|
887 | s.join(' AND ') | |
888 | end |
|
888 | end | |
889 |
|
889 | |||
890 | # Returns a SQL clause for a date or datetime field using relative dates. |
|
890 | # Returns a SQL clause for a date or datetime field using relative dates. | |
891 | def relative_date_clause(table, field, days_from, days_to) |
|
891 | def relative_date_clause(table, field, days_from, days_to) | |
892 | date_clause(table, field, (days_from ? Date.today + days_from : nil), (days_to ? Date.today + days_to : nil)) |
|
892 | date_clause(table, field, (days_from ? Date.today + days_from : nil), (days_to ? Date.today + days_to : nil)) | |
893 | end |
|
893 | end | |
894 |
|
894 | |||
895 | # Additional joins required for the given sort options |
|
895 | # Additional joins required for the given sort options | |
896 | def joins_for_order_statement(order_options) |
|
896 | def joins_for_order_statement(order_options) | |
897 | joins = [] |
|
897 | joins = [] | |
898 |
|
898 | |||
899 | if order_options |
|
899 | if order_options | |
900 | if order_options.include?('authors') |
|
900 | if order_options.include?('authors') | |
901 | joins << "LEFT OUTER JOIN #{User.table_name} authors ON authors.id = #{Issue.table_name}.author_id" |
|
901 | joins << "LEFT OUTER JOIN #{User.table_name} authors ON authors.id = #{Issue.table_name}.author_id" | |
902 | end |
|
902 | end | |
903 | order_options.scan(/cf_\d+/).uniq.each do |name| |
|
903 | order_options.scan(/cf_\d+/).uniq.each do |name| | |
904 | column = available_columns.detect {|c| c.name.to_s == name} |
|
904 | column = available_columns.detect {|c| c.name.to_s == name} | |
905 | join = column && column.custom_field.join_for_order_statement |
|
905 | join = column && column.custom_field.join_for_order_statement | |
906 | if join |
|
906 | if join | |
907 | joins << join |
|
907 | joins << join | |
908 | end |
|
908 | end | |
909 | end |
|
909 | end | |
910 | end |
|
910 | end | |
911 |
|
911 | |||
912 | joins.any? ? joins.join(' ') : nil |
|
912 | joins.any? ? joins.join(' ') : nil | |
913 | end |
|
913 | end | |
914 | end |
|
914 | end |
General Comments 0
You need to be logged in to leave comments.
Login now