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