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