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