@@ -1,535 +1,535 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2016 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2016 Jean-Philippe Lang | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or |
|
4 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License |
|
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; either version 2 |
|
6 | # as published by the Free Software Foundation; either version 2 | |
7 | # of the License, or (at your option) any later version. |
|
7 | # of the License, or (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
14 | # You should have received a copy of the GNU General Public License | |
15 | # along with this program; if not, write to the Free Software |
|
15 | # along with this program; if not, write to the Free Software | |
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | class IssueQuery < Query |
|
18 | class IssueQuery < Query | |
19 |
|
19 | |||
20 | self.queried_class = Issue |
|
20 | self.queried_class = Issue | |
21 | self.view_permission = :view_issues |
|
21 | self.view_permission = :view_issues | |
22 |
|
22 | |||
23 | self.available_columns = [ |
|
23 | self.available_columns = [ | |
24 | QueryColumn.new(:id, :sortable => "#{Issue.table_name}.id", :default_order => 'desc', :caption => '#', :frozen => true), |
|
24 | QueryColumn.new(:id, :sortable => "#{Issue.table_name}.id", :default_order => 'desc', :caption => '#', :frozen => true), | |
25 | QueryColumn.new(:project, :sortable => "#{Project.table_name}.name", :groupable => true), |
|
25 | QueryColumn.new(:project, :sortable => "#{Project.table_name}.name", :groupable => true), | |
26 | QueryColumn.new(:tracker, :sortable => "#{Tracker.table_name}.position", :groupable => true), |
|
26 | QueryColumn.new(:tracker, :sortable => "#{Tracker.table_name}.position", :groupable => true), | |
27 | QueryColumn.new(:parent, :sortable => ["#{Issue.table_name}.root_id", "#{Issue.table_name}.lft ASC"], :default_order => 'desc', :caption => :field_parent_issue), |
|
27 | QueryColumn.new(:parent, :sortable => ["#{Issue.table_name}.root_id", "#{Issue.table_name}.lft ASC"], :default_order => 'desc', :caption => :field_parent_issue), | |
28 | QueryColumn.new(:status, :sortable => "#{IssueStatus.table_name}.position", :groupable => true), |
|
28 | QueryColumn.new(:status, :sortable => "#{IssueStatus.table_name}.position", :groupable => true), | |
29 | QueryColumn.new(:priority, :sortable => "#{IssuePriority.table_name}.position", :default_order => 'desc', :groupable => true), |
|
29 | QueryColumn.new(:priority, :sortable => "#{IssuePriority.table_name}.position", :default_order => 'desc', :groupable => true), | |
30 | QueryColumn.new(:subject, :sortable => "#{Issue.table_name}.subject"), |
|
30 | QueryColumn.new(:subject, :sortable => "#{Issue.table_name}.subject"), | |
31 | QueryColumn.new(:author, :sortable => lambda {User.fields_for_order_statement("authors")}, :groupable => true), |
|
31 | QueryColumn.new(:author, :sortable => lambda {User.fields_for_order_statement("authors")}, :groupable => true), | |
32 | QueryColumn.new(:assigned_to, :sortable => lambda {User.fields_for_order_statement}, :groupable => true), |
|
32 | QueryColumn.new(:assigned_to, :sortable => lambda {User.fields_for_order_statement}, :groupable => true), | |
33 | QueryColumn.new(:updated_on, :sortable => "#{Issue.table_name}.updated_on", :default_order => 'desc'), |
|
33 | QueryColumn.new(:updated_on, :sortable => "#{Issue.table_name}.updated_on", :default_order => 'desc'), | |
34 | QueryColumn.new(:category, :sortable => "#{IssueCategory.table_name}.name", :groupable => true), |
|
34 | QueryColumn.new(:category, :sortable => "#{IssueCategory.table_name}.name", :groupable => true), | |
35 | QueryColumn.new(:fixed_version, :sortable => lambda {Version.fields_for_order_statement}, :groupable => true), |
|
35 | QueryColumn.new(:fixed_version, :sortable => lambda {Version.fields_for_order_statement}, :groupable => true), | |
36 | QueryColumn.new(:start_date, :sortable => "#{Issue.table_name}.start_date"), |
|
36 | QueryColumn.new(:start_date, :sortable => "#{Issue.table_name}.start_date"), | |
37 | QueryColumn.new(:due_date, :sortable => "#{Issue.table_name}.due_date"), |
|
37 | QueryColumn.new(:due_date, :sortable => "#{Issue.table_name}.due_date"), | |
38 | QueryColumn.new(:estimated_hours, :sortable => "#{Issue.table_name}.estimated_hours", :totalable => true), |
|
38 | QueryColumn.new(:estimated_hours, :sortable => "#{Issue.table_name}.estimated_hours", :totalable => true), | |
39 | QueryColumn.new(:total_estimated_hours, |
|
39 | QueryColumn.new(:total_estimated_hours, | |
40 | :sortable => "COALESCE((SELECT SUM(estimated_hours) FROM #{Issue.table_name} subtasks" + |
|
40 | :sortable => "COALESCE((SELECT SUM(estimated_hours) FROM #{Issue.table_name} subtasks" + | |
41 | " WHERE subtasks.root_id = #{Issue.table_name}.root_id AND subtasks.lft >= #{Issue.table_name}.lft AND subtasks.rgt <= #{Issue.table_name}.rgt), 0)", |
|
41 | " WHERE subtasks.root_id = #{Issue.table_name}.root_id AND subtasks.lft >= #{Issue.table_name}.lft AND subtasks.rgt <= #{Issue.table_name}.rgt), 0)", | |
42 | :default_order => 'desc'), |
|
42 | :default_order => 'desc'), | |
43 | QueryColumn.new(:done_ratio, :sortable => "#{Issue.table_name}.done_ratio", :groupable => true), |
|
43 | QueryColumn.new(:done_ratio, :sortable => "#{Issue.table_name}.done_ratio", :groupable => true), | |
44 | QueryColumn.new(:created_on, :sortable => "#{Issue.table_name}.created_on", :default_order => 'desc'), |
|
44 | QueryColumn.new(:created_on, :sortable => "#{Issue.table_name}.created_on", :default_order => 'desc'), | |
45 | QueryColumn.new(:closed_on, :sortable => "#{Issue.table_name}.closed_on", :default_order => 'desc'), |
|
45 | QueryColumn.new(:closed_on, :sortable => "#{Issue.table_name}.closed_on", :default_order => 'desc'), | |
46 | QueryColumn.new(:relations, :caption => :label_related_issues), |
|
46 | QueryColumn.new(:relations, :caption => :label_related_issues), | |
47 | QueryColumn.new(:description, :inline => false) |
|
47 | QueryColumn.new(:description, :inline => false) | |
48 | ] |
|
48 | ] | |
49 |
|
49 | |||
50 | def initialize(attributes=nil, *args) |
|
50 | def initialize(attributes=nil, *args) | |
51 | super attributes |
|
51 | super attributes | |
52 | self.filters ||= { 'status_id' => {:operator => "o", :values => [""]} } |
|
52 | self.filters ||= { 'status_id' => {:operator => "o", :values => [""]} } | |
53 | end |
|
53 | end | |
54 |
|
54 | |||
55 | def draw_relations |
|
55 | def draw_relations | |
56 | r = options[:draw_relations] |
|
56 | r = options[:draw_relations] | |
57 | r.nil? || r == '1' |
|
57 | r.nil? || r == '1' | |
58 | end |
|
58 | end | |
59 |
|
59 | |||
60 | def draw_relations=(arg) |
|
60 | def draw_relations=(arg) | |
61 | options[:draw_relations] = (arg == '0' ? '0' : nil) |
|
61 | options[:draw_relations] = (arg == '0' ? '0' : nil) | |
62 | end |
|
62 | end | |
63 |
|
63 | |||
64 | def draw_progress_line |
|
64 | def draw_progress_line | |
65 | r = options[:draw_progress_line] |
|
65 | r = options[:draw_progress_line] | |
66 | r == '1' |
|
66 | r == '1' | |
67 | end |
|
67 | end | |
68 |
|
68 | |||
69 | def draw_progress_line=(arg) |
|
69 | def draw_progress_line=(arg) | |
70 | options[:draw_progress_line] = (arg == '1' ? '1' : nil) |
|
70 | options[:draw_progress_line] = (arg == '1' ? '1' : nil) | |
71 | end |
|
71 | end | |
72 |
|
72 | |||
73 | def build_from_params(params) |
|
73 | def build_from_params(params) | |
74 | super |
|
74 | super | |
75 | self.draw_relations = params[:draw_relations] || (params[:query] && params[:query][:draw_relations]) |
|
75 | self.draw_relations = params[:draw_relations] || (params[:query] && params[:query][:draw_relations]) | |
76 | self.draw_progress_line = params[:draw_progress_line] || (params[:query] && params[:query][:draw_progress_line]) |
|
76 | self.draw_progress_line = params[:draw_progress_line] || (params[:query] && params[:query][:draw_progress_line]) | |
77 | self |
|
77 | self | |
78 | end |
|
78 | end | |
79 |
|
79 | |||
80 | def initialize_available_filters |
|
80 | def initialize_available_filters | |
81 | add_available_filter "status_id", |
|
81 | add_available_filter "status_id", | |
82 | :type => :list_status, :values => lambda { IssueStatus.sorted.collect{|s| [s.name, s.id.to_s] } } |
|
82 | :type => :list_status, :values => lambda { IssueStatus.sorted.collect{|s| [s.name, s.id.to_s] } } | |
83 |
|
83 | |||
84 | add_available_filter("project_id", |
|
84 | add_available_filter("project_id", | |
85 | :type => :list, :values => lambda { project_values } |
|
85 | :type => :list, :values => lambda { project_values } | |
86 | ) if project.nil? |
|
86 | ) if project.nil? | |
87 |
|
87 | |||
88 | add_available_filter "tracker_id", |
|
88 | add_available_filter "tracker_id", | |
89 | :type => :list, :values => trackers.collect{|s| [s.name, s.id.to_s] } |
|
89 | :type => :list, :values => trackers.collect{|s| [s.name, s.id.to_s] } | |
90 |
|
90 | |||
91 | add_available_filter "priority_id", |
|
91 | add_available_filter "priority_id", | |
92 | :type => :list, :values => IssuePriority.all.collect{|s| [s.name, s.id.to_s] } |
|
92 | :type => :list, :values => IssuePriority.all.collect{|s| [s.name, s.id.to_s] } | |
93 |
|
93 | |||
94 | add_available_filter("author_id", |
|
94 | add_available_filter("author_id", | |
95 | :type => :list, :values => lambda { author_values } |
|
95 | :type => :list, :values => lambda { author_values } | |
96 | ) |
|
96 | ) | |
97 |
|
97 | |||
98 | add_available_filter("assigned_to_id", |
|
98 | add_available_filter("assigned_to_id", | |
99 | :type => :list_optional, :values => lambda { assigned_to_values } |
|
99 | :type => :list_optional, :values => lambda { assigned_to_values } | |
100 | ) |
|
100 | ) | |
101 |
|
101 | |||
102 | add_available_filter("member_of_group", |
|
102 | add_available_filter("member_of_group", | |
103 | :type => :list_optional, :values => lambda { Group.givable.visible.collect {|g| [g.name, g.id.to_s] } } |
|
103 | :type => :list_optional, :values => lambda { Group.givable.visible.collect {|g| [g.name, g.id.to_s] } } | |
104 | ) |
|
104 | ) | |
105 |
|
105 | |||
106 | add_available_filter("assigned_to_role", |
|
106 | add_available_filter("assigned_to_role", | |
107 | :type => :list_optional, :values => lambda { Role.givable.collect {|r| [r.name, r.id.to_s] } } |
|
107 | :type => :list_optional, :values => lambda { Role.givable.collect {|r| [r.name, r.id.to_s] } } | |
108 | ) |
|
108 | ) | |
109 |
|
109 | |||
110 | add_available_filter "fixed_version_id", |
|
110 | add_available_filter "fixed_version_id", | |
111 | :type => :list_optional, :values => lambda { fixed_version_values } |
|
111 | :type => :list_optional, :values => lambda { fixed_version_values } | |
112 |
|
112 | |||
113 | add_available_filter "fixed_version.due_date", |
|
113 | add_available_filter "fixed_version.due_date", | |
114 | :type => :date, |
|
114 | :type => :date, | |
115 | :name => l(:label_attribute_of_fixed_version, :name => l(:field_effective_date)) |
|
115 | :name => l(:label_attribute_of_fixed_version, :name => l(:field_effective_date)) | |
116 |
|
116 | |||
117 | add_available_filter "fixed_version.status", |
|
117 | add_available_filter "fixed_version.status", | |
118 | :type => :list, |
|
118 | :type => :list, | |
119 | :name => l(:label_attribute_of_fixed_version, :name => l(:field_status)), |
|
119 | :name => l(:label_attribute_of_fixed_version, :name => l(:field_status)), | |
120 | :values => Version::VERSION_STATUSES.map{|s| [l("version_status_#{s}"), s] } |
|
120 | :values => Version::VERSION_STATUSES.map{|s| [l("version_status_#{s}"), s] } | |
121 |
|
121 | |||
122 | add_available_filter "category_id", |
|
122 | add_available_filter "category_id", | |
123 | :type => :list_optional, |
|
123 | :type => :list_optional, | |
124 | :values => lambda { project.issue_categories.collect{|s| [s.name, s.id.to_s] } } if project |
|
124 | :values => lambda { project.issue_categories.collect{|s| [s.name, s.id.to_s] } } if project | |
125 |
|
125 | |||
126 | add_available_filter "subject", :type => :text |
|
126 | add_available_filter "subject", :type => :text | |
127 | add_available_filter "description", :type => :text |
|
127 | add_available_filter "description", :type => :text | |
128 | add_available_filter "created_on", :type => :date_past |
|
128 | add_available_filter "created_on", :type => :date_past | |
129 | add_available_filter "updated_on", :type => :date_past |
|
129 | add_available_filter "updated_on", :type => :date_past | |
130 | add_available_filter "closed_on", :type => :date_past |
|
130 | add_available_filter "closed_on", :type => :date_past | |
131 | add_available_filter "start_date", :type => :date |
|
131 | add_available_filter "start_date", :type => :date | |
132 | add_available_filter "due_date", :type => :date |
|
132 | add_available_filter "due_date", :type => :date | |
133 | add_available_filter "estimated_hours", :type => :float |
|
133 | add_available_filter "estimated_hours", :type => :float | |
134 | add_available_filter "done_ratio", :type => :integer |
|
134 | add_available_filter "done_ratio", :type => :integer | |
135 |
|
135 | |||
136 | if User.current.allowed_to?(:set_issues_private, nil, :global => true) || |
|
136 | if User.current.allowed_to?(:set_issues_private, nil, :global => true) || | |
137 | User.current.allowed_to?(:set_own_issues_private, nil, :global => true) |
|
137 | User.current.allowed_to?(:set_own_issues_private, nil, :global => true) | |
138 | add_available_filter "is_private", |
|
138 | add_available_filter "is_private", | |
139 | :type => :list, |
|
139 | :type => :list, | |
140 | :values => [[l(:general_text_yes), "1"], [l(:general_text_no), "0"]] |
|
140 | :values => [[l(:general_text_yes), "1"], [l(:general_text_no), "0"]] | |
141 | end |
|
141 | end | |
142 |
|
142 | |||
143 | if User.current.logged? |
|
143 | if User.current.logged? | |
144 | add_available_filter "watcher_id", |
|
144 | add_available_filter "watcher_id", | |
145 | :type => :list, :values => [["<< #{l(:label_me)} >>", "me"]] |
|
145 | :type => :list, :values => [["<< #{l(:label_me)} >>", "me"]] | |
146 | end |
|
146 | end | |
147 |
|
147 | |||
148 | if project && !project.leaf? |
|
148 | if project && !project.leaf? | |
149 | add_available_filter "subproject_id", |
|
149 | add_available_filter "subproject_id", | |
150 | :type => :list_subprojects, |
|
150 | :type => :list_subprojects, | |
151 | :values => lambda { subproject_values } |
|
151 | :values => lambda { subproject_values } | |
152 | end |
|
152 | end | |
153 |
|
153 | |||
154 |
|
154 | |||
155 | issue_custom_fields = project ? project.all_issue_custom_fields : IssueCustomField.where(:is_for_all => true) |
|
155 | issue_custom_fields = project ? project.all_issue_custom_fields : IssueCustomField.where(:is_for_all => true) | |
156 | add_custom_fields_filters(issue_custom_fields) |
|
156 | add_custom_fields_filters(issue_custom_fields) | |
157 |
|
157 | |||
158 | add_associations_custom_fields_filters :project, :author, :assigned_to, :fixed_version |
|
158 | add_associations_custom_fields_filters :project, :author, :assigned_to, :fixed_version | |
159 |
|
159 | |||
160 | IssueRelation::TYPES.each do |relation_type, options| |
|
160 | IssueRelation::TYPES.each do |relation_type, options| | |
161 | add_available_filter relation_type, :type => :relation, :label => options[:name], :values => lambda {all_projects_values} |
|
161 | add_available_filter relation_type, :type => :relation, :label => options[:name], :values => lambda {all_projects_values} | |
162 | end |
|
162 | end | |
163 | add_available_filter "parent_id", :type => :tree, :label => :field_parent_issue |
|
163 | add_available_filter "parent_id", :type => :tree, :label => :field_parent_issue | |
164 | add_available_filter "child_id", :type => :tree, :label => :label_subtask_plural |
|
164 | add_available_filter "child_id", :type => :tree, :label => :label_subtask_plural | |
165 |
|
165 | |||
166 | add_available_filter "issue_id", :type => :integer, :label => :label_issue |
|
166 | add_available_filter "issue_id", :type => :integer, :label => :label_issue | |
167 |
|
167 | |||
168 | Tracker.disabled_core_fields(trackers).each {|field| |
|
168 | Tracker.disabled_core_fields(trackers).each {|field| | |
169 | delete_available_filter field |
|
169 | delete_available_filter field | |
170 | } |
|
170 | } | |
171 | end |
|
171 | end | |
172 |
|
172 | |||
173 | def available_columns |
|
173 | def available_columns | |
174 | return @available_columns if @available_columns |
|
174 | return @available_columns if @available_columns | |
175 | @available_columns = self.class.available_columns.dup |
|
175 | @available_columns = self.class.available_columns.dup | |
176 | @available_columns += (project ? |
|
176 | @available_columns += (project ? | |
177 | project.all_issue_custom_fields : |
|
177 | project.all_issue_custom_fields : | |
178 | IssueCustomField |
|
178 | IssueCustomField | |
179 | ).visible.collect {|cf| QueryCustomFieldColumn.new(cf) } |
|
179 | ).visible.collect {|cf| QueryCustomFieldColumn.new(cf) } | |
180 |
|
180 | |||
181 | if User.current.allowed_to?(:view_time_entries, project, :global => true) |
|
181 | if User.current.allowed_to?(:view_time_entries, project, :global => true) | |
182 | index = @available_columns.find_index {|column| column.name == :total_estimated_hours} |
|
182 | index = @available_columns.find_index {|column| column.name == :total_estimated_hours} | |
183 | index = (index ? index + 1 : -1) |
|
183 | index = (index ? index + 1 : -1) | |
184 | # insert the column after total_estimated_hours or at the end |
|
184 | # insert the column after total_estimated_hours or at the end | |
185 | @available_columns.insert index, QueryColumn.new(:spent_hours, |
|
185 | @available_columns.insert index, QueryColumn.new(:spent_hours, | |
186 | :sortable => "COALESCE((SELECT SUM(hours) FROM #{TimeEntry.table_name} WHERE #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id), 0)", |
|
186 | :sortable => "COALESCE((SELECT SUM(hours) FROM #{TimeEntry.table_name} WHERE #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id), 0)", | |
187 | :default_order => 'desc', |
|
187 | :default_order => 'desc', | |
188 | :caption => :label_spent_time, |
|
188 | :caption => :label_spent_time, | |
189 | :totalable => true |
|
189 | :totalable => true | |
190 | ) |
|
190 | ) | |
191 | @available_columns.insert index+1, QueryColumn.new(:total_spent_hours, |
|
191 | @available_columns.insert index+1, QueryColumn.new(:total_spent_hours, | |
192 | :sortable => "COALESCE((SELECT SUM(hours) FROM #{TimeEntry.table_name} JOIN #{Issue.table_name} subtasks ON subtasks.id = #{TimeEntry.table_name}.issue_id" + |
|
192 | :sortable => "COALESCE((SELECT SUM(hours) FROM #{TimeEntry.table_name} JOIN #{Issue.table_name} subtasks ON subtasks.id = #{TimeEntry.table_name}.issue_id" + | |
193 | " WHERE subtasks.root_id = #{Issue.table_name}.root_id AND subtasks.lft >= #{Issue.table_name}.lft AND subtasks.rgt <= #{Issue.table_name}.rgt), 0)", |
|
193 | " WHERE subtasks.root_id = #{Issue.table_name}.root_id AND subtasks.lft >= #{Issue.table_name}.lft AND subtasks.rgt <= #{Issue.table_name}.rgt), 0)", | |
194 | :default_order => 'desc', |
|
194 | :default_order => 'desc', | |
195 | :caption => :label_total_spent_time |
|
195 | :caption => :label_total_spent_time | |
196 | ) |
|
196 | ) | |
197 | end |
|
197 | end | |
198 |
|
198 | |||
199 | if User.current.allowed_to?(:set_issues_private, nil, :global => true) || |
|
199 | if User.current.allowed_to?(:set_issues_private, nil, :global => true) || | |
200 | User.current.allowed_to?(:set_own_issues_private, nil, :global => true) |
|
200 | User.current.allowed_to?(:set_own_issues_private, nil, :global => true) | |
201 | @available_columns << QueryColumn.new(:is_private, :sortable => "#{Issue.table_name}.is_private") |
|
201 | @available_columns << QueryColumn.new(:is_private, :sortable => "#{Issue.table_name}.is_private") | |
202 | end |
|
202 | end | |
203 |
|
203 | |||
204 | disabled_fields = Tracker.disabled_core_fields(trackers).map {|field| field.sub(/_id$/, '')} |
|
204 | disabled_fields = Tracker.disabled_core_fields(trackers).map {|field| field.sub(/_id$/, '')} | |
205 | @available_columns.reject! {|column| |
|
205 | @available_columns.reject! {|column| | |
206 | disabled_fields.include?(column.name.to_s) |
|
206 | disabled_fields.include?(column.name.to_s) | |
207 | } |
|
207 | } | |
208 |
|
208 | |||
209 | @available_columns |
|
209 | @available_columns | |
210 | end |
|
210 | end | |
211 |
|
211 | |||
212 | def default_columns_names |
|
212 | def default_columns_names | |
213 | @default_columns_names ||= begin |
|
213 | @default_columns_names ||= begin | |
214 | default_columns = Setting.issue_list_default_columns.map(&:to_sym) |
|
214 | default_columns = Setting.issue_list_default_columns.map(&:to_sym) | |
215 |
|
215 | |||
216 | project.present? ? default_columns : [:project] | default_columns |
|
216 | project.present? ? default_columns : [:project] | default_columns | |
217 | end |
|
217 | end | |
218 | end |
|
218 | end | |
219 |
|
219 | |||
220 | def default_totalable_names |
|
220 | def default_totalable_names | |
221 | Setting.issue_list_default_totals.map(&:to_sym) |
|
221 | Setting.issue_list_default_totals.map(&:to_sym) | |
222 | end |
|
222 | end | |
223 |
|
223 | |||
224 | def base_scope |
|
224 | def base_scope | |
225 | Issue.visible.joins(:status, :project).where(statement) |
|
225 | Issue.visible.joins(:status, :project).where(statement) | |
226 | end |
|
226 | end | |
227 |
|
227 | |||
228 | # Returns the issue count |
|
228 | # Returns the issue count | |
229 | def issue_count |
|
229 | def issue_count | |
230 | base_scope.count |
|
230 | base_scope.count | |
231 | rescue ::ActiveRecord::StatementInvalid => e |
|
231 | rescue ::ActiveRecord::StatementInvalid => e | |
232 | raise StatementInvalid.new(e.message) |
|
232 | raise StatementInvalid.new(e.message) | |
233 | end |
|
233 | end | |
234 |
|
234 | |||
235 | # Returns the issue count by group or nil if query is not grouped |
|
235 | # Returns the issue count by group or nil if query is not grouped | |
236 | def issue_count_by_group |
|
236 | def issue_count_by_group | |
237 | grouped_query do |scope| |
|
237 | grouped_query do |scope| | |
238 | scope.count |
|
238 | scope.count | |
239 | end |
|
239 | end | |
240 | end |
|
240 | end | |
241 |
|
241 | |||
242 | # Returns sum of all the issue's estimated_hours |
|
242 | # Returns sum of all the issue's estimated_hours | |
243 | def total_for_estimated_hours(scope) |
|
243 | def total_for_estimated_hours(scope) | |
244 | map_total(scope.sum(:estimated_hours)) {|t| t.to_f.round(2)} |
|
244 | map_total(scope.sum(:estimated_hours)) {|t| t.to_f.round(2)} | |
245 | end |
|
245 | end | |
246 |
|
246 | |||
247 | # Returns sum of all the issue's time entries hours |
|
247 | # Returns sum of all the issue's time entries hours | |
248 | def total_for_spent_hours(scope) |
|
248 | def total_for_spent_hours(scope) | |
249 | total = if group_by_column.try(:name) == :project |
|
249 | total = if group_by_column.try(:name) == :project | |
250 | # TODO: remove this when https://github.com/rails/rails/issues/21922 is fixed |
|
250 | # TODO: remove this when https://github.com/rails/rails/issues/21922 is fixed | |
251 | # We have to do a custom join without the time_entries.project_id column |
|
251 | # We have to do a custom join without the time_entries.project_id column | |
252 | # that would trigger a ambiguous column name error |
|
252 | # that would trigger a ambiguous column name error | |
253 | scope.joins("JOIN (SELECT issue_id, hours FROM #{TimeEntry.table_name}) AS joined_time_entries ON joined_time_entries.issue_id = #{Issue.table_name}.id"). |
|
253 | scope.joins("JOIN (SELECT issue_id, hours FROM #{TimeEntry.table_name}) AS joined_time_entries ON joined_time_entries.issue_id = #{Issue.table_name}.id"). | |
254 | sum("joined_time_entries.hours") |
|
254 | sum("joined_time_entries.hours") | |
255 | else |
|
255 | else | |
256 | scope.joins(:time_entries).sum("#{TimeEntry.table_name}.hours") |
|
256 | scope.joins(:time_entries).sum("#{TimeEntry.table_name}.hours") | |
257 | end |
|
257 | end | |
258 | map_total(total) {|t| t.to_f.round(2)} |
|
258 | map_total(total) {|t| t.to_f.round(2)} | |
259 | end |
|
259 | end | |
260 |
|
260 | |||
261 | # Returns the issues |
|
261 | # Returns the issues | |
262 | # Valid options are :order, :offset, :limit, :include, :conditions |
|
262 | # Valid options are :order, :offset, :limit, :include, :conditions | |
263 | def issues(options={}) |
|
263 | def issues(options={}) | |
264 | order_option = [group_by_sort_order, options[:order]].flatten.reject(&:blank?) |
|
264 | order_option = [group_by_sort_order, options[:order]].flatten.reject(&:blank?) | |
265 |
|
265 | |||
266 | scope = Issue.visible. |
|
266 | scope = Issue.visible. | |
267 | joins(:status, :project). |
|
267 | joins(:status, :project). | |
268 | where(statement). |
|
268 | where(statement). | |
269 | includes(([:status, :project] + (options[:include] || [])).uniq). |
|
269 | includes(([:status, :project] + (options[:include] || [])).uniq). | |
270 | where(options[:conditions]). |
|
270 | where(options[:conditions]). | |
271 | order(order_option). |
|
271 | order(order_option). | |
272 | joins(joins_for_order_statement(order_option.join(','))). |
|
272 | joins(joins_for_order_statement(order_option.join(','))). | |
273 | limit(options[:limit]). |
|
273 | limit(options[:limit]). | |
274 | offset(options[:offset]) |
|
274 | offset(options[:offset]) | |
275 |
|
275 | |||
276 | scope = scope.preload([:tracker, :priority, :author, :assigned_to, :fixed_version, :category] & columns.map(&:name)) |
|
276 | scope = scope.preload([:tracker, :priority, :author, :assigned_to, :fixed_version, :category] & columns.map(&:name)) | |
277 | if has_custom_field_column? |
|
277 | if has_custom_field_column? | |
278 | scope = scope.preload(:custom_values) |
|
278 | scope = scope.preload(:custom_values) | |
279 | end |
|
279 | end | |
280 |
|
280 | |||
281 | issues = scope.to_a |
|
281 | issues = scope.to_a | |
282 |
|
282 | |||
283 | if has_column?(:spent_hours) |
|
283 | if has_column?(:spent_hours) | |
284 | Issue.load_visible_spent_hours(issues) |
|
284 | Issue.load_visible_spent_hours(issues) | |
285 | end |
|
285 | end | |
286 | if has_column?(:total_spent_hours) |
|
286 | if has_column?(:total_spent_hours) | |
287 | Issue.load_visible_total_spent_hours(issues) |
|
287 | Issue.load_visible_total_spent_hours(issues) | |
288 | end |
|
288 | end | |
289 | if has_column?(:relations) |
|
289 | if has_column?(:relations) | |
290 | Issue.load_visible_relations(issues) |
|
290 | Issue.load_visible_relations(issues) | |
291 | end |
|
291 | end | |
292 | issues |
|
292 | issues | |
293 | rescue ::ActiveRecord::StatementInvalid => e |
|
293 | rescue ::ActiveRecord::StatementInvalid => e | |
294 | raise StatementInvalid.new(e.message) |
|
294 | raise StatementInvalid.new(e.message) | |
295 | end |
|
295 | end | |
296 |
|
296 | |||
297 | # Returns the issues ids |
|
297 | # Returns the issues ids | |
298 | def issue_ids(options={}) |
|
298 | def issue_ids(options={}) | |
299 | order_option = [group_by_sort_order, options[:order]].flatten.reject(&:blank?) |
|
299 | order_option = [group_by_sort_order, options[:order]].flatten.reject(&:blank?) | |
300 |
|
300 | |||
301 | Issue.visible. |
|
301 | Issue.visible. | |
302 | joins(:status, :project). |
|
302 | joins(:status, :project). | |
303 | where(statement). |
|
303 | where(statement). | |
304 | includes(([:status, :project] + (options[:include] || [])).uniq). |
|
304 | includes(([:status, :project] + (options[:include] || [])).uniq). | |
305 | references(([:status, :project] + (options[:include] || [])).uniq). |
|
305 | references(([:status, :project] + (options[:include] || [])).uniq). | |
306 | where(options[:conditions]). |
|
306 | where(options[:conditions]). | |
307 | order(order_option). |
|
307 | order(order_option). | |
308 | joins(joins_for_order_statement(order_option.join(','))). |
|
308 | joins(joins_for_order_statement(order_option.join(','))). | |
309 | limit(options[:limit]). |
|
309 | limit(options[:limit]). | |
310 | offset(options[:offset]). |
|
310 | offset(options[:offset]). | |
311 | pluck(:id) |
|
311 | pluck(:id) | |
312 | rescue ::ActiveRecord::StatementInvalid => e |
|
312 | rescue ::ActiveRecord::StatementInvalid => e | |
313 | raise StatementInvalid.new(e.message) |
|
313 | raise StatementInvalid.new(e.message) | |
314 | end |
|
314 | end | |
315 |
|
315 | |||
316 | # Returns the journals |
|
316 | # Returns the journals | |
317 | # Valid options are :order, :offset, :limit |
|
317 | # Valid options are :order, :offset, :limit | |
318 | def journals(options={}) |
|
318 | def journals(options={}) | |
319 | Journal.visible. |
|
319 | Journal.visible. | |
320 | joins(:issue => [:project, :status]). |
|
320 | joins(:issue => [:project, :status]). | |
321 | where(statement). |
|
321 | where(statement). | |
322 | order(options[:order]). |
|
322 | order(options[:order]). | |
323 | limit(options[:limit]). |
|
323 | limit(options[:limit]). | |
324 | offset(options[:offset]). |
|
324 | offset(options[:offset]). | |
325 | preload(:details, :user, {:issue => [:project, :author, :tracker, :status]}). |
|
325 | preload(:details, :user, {:issue => [:project, :author, :tracker, :status]}). | |
326 | to_a |
|
326 | to_a | |
327 | rescue ::ActiveRecord::StatementInvalid => e |
|
327 | rescue ::ActiveRecord::StatementInvalid => e | |
328 | raise StatementInvalid.new(e.message) |
|
328 | raise StatementInvalid.new(e.message) | |
329 | end |
|
329 | end | |
330 |
|
330 | |||
331 | # Returns the versions |
|
331 | # Returns the versions | |
332 | # Valid options are :conditions |
|
332 | # Valid options are :conditions | |
333 | def versions(options={}) |
|
333 | def versions(options={}) | |
334 | Version.visible. |
|
334 | Version.visible. | |
335 | where(project_statement). |
|
335 | where(project_statement). | |
336 | where(options[:conditions]). |
|
336 | where(options[:conditions]). | |
337 | includes(:project). |
|
337 | includes(:project). | |
338 | references(:project). |
|
338 | references(:project). | |
339 | to_a |
|
339 | to_a | |
340 | rescue ::ActiveRecord::StatementInvalid => e |
|
340 | rescue ::ActiveRecord::StatementInvalid => e | |
341 | raise StatementInvalid.new(e.message) |
|
341 | raise StatementInvalid.new(e.message) | |
342 | end |
|
342 | end | |
343 |
|
343 | |||
344 | def sql_for_watcher_id_field(field, operator, value) |
|
344 | def sql_for_watcher_id_field(field, operator, value) | |
345 | db_table = Watcher.table_name |
|
345 | db_table = Watcher.table_name | |
346 | "#{Issue.table_name}.id #{ operator == '=' ? 'IN' : 'NOT IN' } (SELECT #{db_table}.watchable_id FROM #{db_table} WHERE #{db_table}.watchable_type='Issue' AND " + |
|
346 | "#{Issue.table_name}.id #{ operator == '=' ? 'IN' : 'NOT IN' } (SELECT #{db_table}.watchable_id FROM #{db_table} WHERE #{db_table}.watchable_type='Issue' AND " + | |
347 | sql_for_field(field, '=', value, db_table, 'user_id') + ')' |
|
347 | sql_for_field(field, '=', value, db_table, 'user_id') + ')' | |
348 | end |
|
348 | end | |
349 |
|
349 | |||
350 | def sql_for_member_of_group_field(field, operator, value) |
|
350 | def sql_for_member_of_group_field(field, operator, value) | |
351 | if operator == '*' # Any group |
|
351 | if operator == '*' # Any group | |
352 | groups = Group.givable |
|
352 | groups = Group.givable | |
353 | operator = '=' # Override the operator since we want to find by assigned_to |
|
353 | operator = '=' # Override the operator since we want to find by assigned_to | |
354 | elsif operator == "!*" |
|
354 | elsif operator == "!*" | |
355 | groups = Group.givable |
|
355 | groups = Group.givable | |
356 | operator = '!' # Override the operator since we want to find by assigned_to |
|
356 | operator = '!' # Override the operator since we want to find by assigned_to | |
357 | else |
|
357 | else | |
358 | groups = Group.where(:id => value).to_a |
|
358 | groups = Group.where(:id => value).to_a | |
359 | end |
|
359 | end | |
360 | groups ||= [] |
|
360 | groups ||= [] | |
361 |
|
361 | |||
362 | members_of_groups = groups.inject([]) {|user_ids, group| |
|
362 | members_of_groups = groups.inject([]) {|user_ids, group| | |
363 | user_ids + group.user_ids + [group.id] |
|
363 | user_ids + group.user_ids + [group.id] | |
364 | }.uniq.compact.sort.collect(&:to_s) |
|
364 | }.uniq.compact.sort.collect(&:to_s) | |
365 |
|
365 | |||
366 | '(' + sql_for_field("assigned_to_id", operator, members_of_groups, Issue.table_name, "assigned_to_id", false) + ')' |
|
366 | '(' + sql_for_field("assigned_to_id", operator, members_of_groups, Issue.table_name, "assigned_to_id", false) + ')' | |
367 | end |
|
367 | end | |
368 |
|
368 | |||
369 | def sql_for_assigned_to_role_field(field, operator, value) |
|
369 | def sql_for_assigned_to_role_field(field, operator, value) | |
370 | case operator |
|
370 | case operator | |
371 | when "*", "!*" # Member / Not member |
|
371 | when "*", "!*" # Member / Not member | |
372 | sw = operator == "!*" ? 'NOT' : '' |
|
372 | sw = operator == "!*" ? 'NOT' : '' | |
373 | nl = operator == "!*" ? "#{Issue.table_name}.assigned_to_id IS NULL OR" : '' |
|
373 | nl = operator == "!*" ? "#{Issue.table_name}.assigned_to_id IS NULL OR" : '' | |
374 | "(#{nl} #{Issue.table_name}.assigned_to_id #{sw} IN (SELECT DISTINCT #{Member.table_name}.user_id FROM #{Member.table_name}" + |
|
374 | "(#{nl} #{Issue.table_name}.assigned_to_id #{sw} IN (SELECT DISTINCT #{Member.table_name}.user_id FROM #{Member.table_name}" + | |
375 | " WHERE #{Member.table_name}.project_id = #{Issue.table_name}.project_id))" |
|
375 | " WHERE #{Member.table_name}.project_id = #{Issue.table_name}.project_id))" | |
376 | when "=", "!" |
|
376 | when "=", "!" | |
377 | role_cond = value.any? ? |
|
377 | role_cond = value.any? ? | |
378 | "#{MemberRole.table_name}.role_id IN (" + value.collect{|val| "'#{self.class.connection.quote_string(val)}'"}.join(",") + ")" : |
|
378 | "#{MemberRole.table_name}.role_id IN (" + value.collect{|val| "'#{self.class.connection.quote_string(val)}'"}.join(",") + ")" : | |
379 | "1=0" |
|
379 | "1=0" | |
380 |
|
380 | |||
381 | sw = operator == "!" ? 'NOT' : '' |
|
381 | sw = operator == "!" ? 'NOT' : '' | |
382 | nl = operator == "!" ? "#{Issue.table_name}.assigned_to_id IS NULL OR" : '' |
|
382 | nl = operator == "!" ? "#{Issue.table_name}.assigned_to_id IS NULL OR" : '' | |
383 | "(#{nl} #{Issue.table_name}.assigned_to_id #{sw} IN (SELECT DISTINCT #{Member.table_name}.user_id FROM #{Member.table_name}, #{MemberRole.table_name}" + |
|
383 | "(#{nl} #{Issue.table_name}.assigned_to_id #{sw} IN (SELECT DISTINCT #{Member.table_name}.user_id FROM #{Member.table_name}, #{MemberRole.table_name}" + | |
384 | " WHERE #{Member.table_name}.project_id = #{Issue.table_name}.project_id AND #{Member.table_name}.id = #{MemberRole.table_name}.member_id AND #{role_cond}))" |
|
384 | " WHERE #{Member.table_name}.project_id = #{Issue.table_name}.project_id AND #{Member.table_name}.id = #{MemberRole.table_name}.member_id AND #{role_cond}))" | |
385 | end |
|
385 | end | |
386 | end |
|
386 | end | |
387 |
|
387 | |||
388 | def sql_for_fixed_version_status_field(field, operator, value) |
|
388 | def sql_for_fixed_version_status_field(field, operator, value) | |
389 | where = sql_for_field(field, operator, value, Version.table_name, "status") |
|
389 | where = sql_for_field(field, operator, value, Version.table_name, "status") | |
390 | version_ids = versions(:conditions => [where]).map(&:id) |
|
390 | version_ids = versions(:conditions => [where]).map(&:id) | |
391 |
|
391 | |||
392 | nl = operator == "!" ? "#{Issue.table_name}.fixed_version_id IS NULL OR" : '' |
|
392 | nl = operator == "!" ? "#{Issue.table_name}.fixed_version_id IS NULL OR" : '' | |
393 | "(#{nl} #{sql_for_field("fixed_version_id", "=", version_ids, Issue.table_name, "fixed_version_id")})" |
|
393 | "(#{nl} #{sql_for_field("fixed_version_id", "=", version_ids, Issue.table_name, "fixed_version_id")})" | |
394 | end |
|
394 | end | |
395 |
|
395 | |||
396 | def sql_for_fixed_version_due_date_field(field, operator, value) |
|
396 | def sql_for_fixed_version_due_date_field(field, operator, value) | |
397 | where = sql_for_field(field, operator, value, Version.table_name, "effective_date") |
|
397 | where = sql_for_field(field, operator, value, Version.table_name, "effective_date") | |
398 | version_ids = versions(:conditions => [where]).map(&:id) |
|
398 | version_ids = versions(:conditions => [where]).map(&:id) | |
399 |
|
399 | |||
400 | nl = operator == "!*" ? "#{Issue.table_name}.fixed_version_id IS NULL OR" : '' |
|
400 | nl = operator == "!*" ? "#{Issue.table_name}.fixed_version_id IS NULL OR" : '' | |
401 | "(#{nl} #{sql_for_field("fixed_version_id", "=", version_ids, Issue.table_name, "fixed_version_id")})" |
|
401 | "(#{nl} #{sql_for_field("fixed_version_id", "=", version_ids, Issue.table_name, "fixed_version_id")})" | |
402 | end |
|
402 | end | |
403 |
|
403 | |||
404 | def sql_for_is_private_field(field, operator, value) |
|
404 | def sql_for_is_private_field(field, operator, value) | |
405 | op = (operator == "=" ? 'IN' : 'NOT IN') |
|
405 | op = (operator == "=" ? 'IN' : 'NOT IN') | |
406 | va = value.map {|v| v == '0' ? self.class.connection.quoted_false : self.class.connection.quoted_true}.uniq.join(',') |
|
406 | va = value.map {|v| v == '0' ? self.class.connection.quoted_false : self.class.connection.quoted_true}.uniq.join(',') | |
407 |
|
407 | |||
408 | "#{Issue.table_name}.is_private #{op} (#{va})" |
|
408 | "#{Issue.table_name}.is_private #{op} (#{va})" | |
409 | end |
|
409 | end | |
410 |
|
410 | |||
411 | def sql_for_parent_id_field(field, operator, value) |
|
411 | def sql_for_parent_id_field(field, operator, value) | |
412 | case operator |
|
412 | case operator | |
413 | when "=" |
|
413 | when "=" | |
414 | "#{Issue.table_name}.parent_id = #{value.first.to_i}" |
|
414 | "#{Issue.table_name}.parent_id = #{value.first.to_i}" | |
415 | when "~" |
|
415 | when "~" | |
416 | root_id, lft, rgt = Issue.where(:id => value.first.to_i).pluck(:root_id, :lft, :rgt).first |
|
416 | root_id, lft, rgt = Issue.where(:id => value.first.to_i).pluck(:root_id, :lft, :rgt).first | |
417 | if root_id && lft && rgt |
|
417 | if root_id && lft && rgt | |
418 | "#{Issue.table_name}.root_id = #{root_id} AND #{Issue.table_name}.lft > #{lft} AND #{Issue.table_name}.rgt < #{rgt}" |
|
418 | "#{Issue.table_name}.root_id = #{root_id} AND #{Issue.table_name}.lft > #{lft} AND #{Issue.table_name}.rgt < #{rgt}" | |
419 | else |
|
419 | else | |
420 | "1=0" |
|
420 | "1=0" | |
421 | end |
|
421 | end | |
422 | when "!*" |
|
422 | when "!*" | |
423 | "#{Issue.table_name}.parent_id IS NULL" |
|
423 | "#{Issue.table_name}.parent_id IS NULL" | |
424 | when "*" |
|
424 | when "*" | |
425 | "#{Issue.table_name}.parent_id IS NOT NULL" |
|
425 | "#{Issue.table_name}.parent_id IS NOT NULL" | |
426 | end |
|
426 | end | |
427 | end |
|
427 | end | |
428 |
|
428 | |||
429 | def sql_for_child_id_field(field, operator, value) |
|
429 | def sql_for_child_id_field(field, operator, value) | |
430 | case operator |
|
430 | case operator | |
431 | when "=" |
|
431 | when "=" | |
432 | parent_id = Issue.where(:id => value.first.to_i).pluck(:parent_id).first |
|
432 | parent_id = Issue.where(:id => value.first.to_i).pluck(:parent_id).first | |
433 | if parent_id |
|
433 | if parent_id | |
434 | "#{Issue.table_name}.id = #{parent_id}" |
|
434 | "#{Issue.table_name}.id = #{parent_id}" | |
435 | else |
|
435 | else | |
436 | "1=0" |
|
436 | "1=0" | |
437 | end |
|
437 | end | |
438 | when "~" |
|
438 | when "~" | |
439 | root_id, lft, rgt = Issue.where(:id => value.first.to_i).pluck(:root_id, :lft, :rgt).first |
|
439 | root_id, lft, rgt = Issue.where(:id => value.first.to_i).pluck(:root_id, :lft, :rgt).first | |
440 | if root_id && lft && rgt |
|
440 | if root_id && lft && rgt | |
441 | "#{Issue.table_name}.root_id = #{root_id} AND #{Issue.table_name}.lft < #{lft} AND #{Issue.table_name}.rgt > #{rgt}" |
|
441 | "#{Issue.table_name}.root_id = #{root_id} AND #{Issue.table_name}.lft < #{lft} AND #{Issue.table_name}.rgt > #{rgt}" | |
442 | else |
|
442 | else | |
443 | "1=0" |
|
443 | "1=0" | |
444 | end |
|
444 | end | |
445 | when "!*" |
|
445 | when "!*" | |
446 | "#{Issue.table_name}.rgt - #{Issue.table_name}.lft = 1" |
|
446 | "#{Issue.table_name}.rgt - #{Issue.table_name}.lft = 1" | |
447 | when "*" |
|
447 | when "*" | |
448 | "#{Issue.table_name}.rgt - #{Issue.table_name}.lft > 1" |
|
448 | "#{Issue.table_name}.rgt - #{Issue.table_name}.lft > 1" | |
449 | end |
|
449 | end | |
450 | end |
|
450 | end | |
451 |
|
451 | |||
452 | def sql_for_issue_id_field(field, operator, value) |
|
452 | def sql_for_issue_id_field(field, operator, value) | |
453 | if operator == "=" |
|
453 | if operator == "=" | |
454 | # accepts a comma separated list of ids |
|
454 | # accepts a comma separated list of ids | |
455 | ids = value.first.to_s.scan(/\d+/).map(&:to_i) |
|
455 | ids = value.first.to_s.scan(/\d+/).map(&:to_i) | |
456 | if ids.present? |
|
456 | if ids.present? | |
457 | "#{Issue.table_name}.id IN (#{ids.join(",")})" |
|
457 | "#{Issue.table_name}.id IN (#{ids.join(",")})" | |
458 | else |
|
458 | else | |
459 | "1=0" |
|
459 | "1=0" | |
460 | end |
|
460 | end | |
461 | else |
|
461 | else | |
462 | sql_for_field("id", operator, value, Issue.table_name, "id") |
|
462 | sql_for_field("id", operator, value, Issue.table_name, "id") | |
463 | end |
|
463 | end | |
464 | end |
|
464 | end | |
465 |
|
465 | |||
466 | def sql_for_relations(field, operator, value, options={}) |
|
466 | def sql_for_relations(field, operator, value, options={}) | |
467 | relation_options = IssueRelation::TYPES[field] |
|
467 | relation_options = IssueRelation::TYPES[field] | |
468 | return relation_options unless relation_options |
|
468 | return relation_options unless relation_options | |
469 |
|
469 | |||
470 | relation_type = field |
|
470 | relation_type = field | |
471 | join_column, target_join_column = "issue_from_id", "issue_to_id" |
|
471 | join_column, target_join_column = "issue_from_id", "issue_to_id" | |
472 | if relation_options[:reverse] || options[:reverse] |
|
472 | if relation_options[:reverse] || options[:reverse] | |
473 | relation_type = relation_options[:reverse] || relation_type |
|
473 | relation_type = relation_options[:reverse] || relation_type | |
474 | join_column, target_join_column = target_join_column, join_column |
|
474 | join_column, target_join_column = target_join_column, join_column | |
475 | end |
|
475 | end | |
476 |
|
476 | |||
477 | sql = case operator |
|
477 | sql = case operator | |
478 | when "*", "!*" |
|
478 | when "*", "!*" | |
479 | op = (operator == "*" ? 'IN' : 'NOT IN') |
|
479 | op = (operator == "*" ? 'IN' : 'NOT IN') | |
480 | "#{Issue.table_name}.id #{op} (SELECT DISTINCT #{IssueRelation.table_name}.#{join_column} FROM #{IssueRelation.table_name} WHERE #{IssueRelation.table_name}.relation_type = '#{self.class.connection.quote_string(relation_type)}')" |
|
480 | "#{Issue.table_name}.id #{op} (SELECT DISTINCT #{IssueRelation.table_name}.#{join_column} FROM #{IssueRelation.table_name} WHERE #{IssueRelation.table_name}.relation_type = '#{self.class.connection.quote_string(relation_type)}')" | |
481 | when "=", "!" |
|
481 | when "=", "!" | |
482 | op = (operator == "=" ? 'IN' : 'NOT IN') |
|
482 | op = (operator == "=" ? 'IN' : 'NOT IN') | |
483 | "#{Issue.table_name}.id #{op} (SELECT DISTINCT #{IssueRelation.table_name}.#{join_column} FROM #{IssueRelation.table_name} WHERE #{IssueRelation.table_name}.relation_type = '#{self.class.connection.quote_string(relation_type)}' AND #{IssueRelation.table_name}.#{target_join_column} = #{value.first.to_i})" |
|
483 | "#{Issue.table_name}.id #{op} (SELECT DISTINCT #{IssueRelation.table_name}.#{join_column} FROM #{IssueRelation.table_name} WHERE #{IssueRelation.table_name}.relation_type = '#{self.class.connection.quote_string(relation_type)}' AND #{IssueRelation.table_name}.#{target_join_column} = #{value.first.to_i})" | |
484 | when "=p", "=!p", "!p" |
|
484 | when "=p", "=!p", "!p" | |
485 | op = (operator == "!p" ? 'NOT IN' : 'IN') |
|
485 | op = (operator == "!p" ? 'NOT IN' : 'IN') | |
486 | comp = (operator == "=!p" ? '<>' : '=') |
|
486 | comp = (operator == "=!p" ? '<>' : '=') | |
487 | "#{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 = '#{self.class.connection.quote_string(relation_type)}' AND #{IssueRelation.table_name}.#{target_join_column} = relissues.id AND relissues.project_id #{comp} #{value.first.to_i})" |
|
487 | "#{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 = '#{self.class.connection.quote_string(relation_type)}' AND #{IssueRelation.table_name}.#{target_join_column} = relissues.id AND relissues.project_id #{comp} #{value.first.to_i})" | |
488 | when "*o", "!o" |
|
488 | when "*o", "!o" | |
489 | op = (operator == "!o" ? 'NOT IN' : 'IN') |
|
489 | op = (operator == "!o" ? 'NOT IN' : 'IN') | |
490 | "#{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 = '#{self.class.connection.quote_string(relation_type)}' AND #{IssueRelation.table_name}.#{target_join_column} = relissues.id AND relissues.status_id IN (SELECT id FROM #{IssueStatus.table_name} WHERE is_closed=#{self.class.connection.quoted_false}))" |
|
490 | "#{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 = '#{self.class.connection.quote_string(relation_type)}' AND #{IssueRelation.table_name}.#{target_join_column} = relissues.id AND relissues.status_id IN (SELECT id FROM #{IssueStatus.table_name} WHERE is_closed=#{self.class.connection.quoted_false}))" | |
491 | end |
|
491 | end | |
492 |
|
492 | |||
493 | if relation_options[:sym] == field && !options[:reverse] |
|
493 | if relation_options[:sym] == field && !options[:reverse] | |
494 | sqls = [sql, sql_for_relations(field, operator, value, :reverse => true)] |
|
494 | sqls = [sql, sql_for_relations(field, operator, value, :reverse => true)] | |
495 | sql = sqls.join(["!", "!*", "!p"].include?(operator) ? " AND " : " OR ") |
|
495 | sql = sqls.join(["!", "!*", "!p"].include?(operator) ? " AND " : " OR ") | |
496 | end |
|
496 | end | |
497 | "(#{sql})" |
|
497 | "(#{sql})" | |
498 | end |
|
498 | end | |
499 |
|
499 | |||
500 | def find_assigned_to_id_filter_values(values) |
|
500 | def find_assigned_to_id_filter_values(values) | |
501 | Principal.visible.where(:id => values).map {|p| [p.name, p.id.to_s]} |
|
501 | Principal.visible.where(:id => values).map {|p| [p.name, p.id.to_s]} | |
502 | end |
|
502 | end | |
503 | alias :find_author_id_filter_values :find_assigned_to_id_filter_values |
|
503 | alias :find_author_id_filter_values :find_assigned_to_id_filter_values | |
504 |
|
504 | |||
505 | IssueRelation::TYPES.keys.each do |relation_type| |
|
505 | IssueRelation::TYPES.keys.each do |relation_type| | |
506 | alias_method "sql_for_#{relation_type}_field".to_sym, :sql_for_relations |
|
506 | alias_method "sql_for_#{relation_type}_field".to_sym, :sql_for_relations | |
507 | end |
|
507 | end | |
508 |
|
508 | |||
509 | def joins_for_order_statement(order_options) |
|
509 | def joins_for_order_statement(order_options) | |
510 | joins = [super] |
|
510 | joins = [super] | |
511 |
|
511 | |||
512 | if order_options |
|
512 | if order_options | |
513 | if order_options.include?('authors') |
|
513 | if order_options.include?('authors') | |
514 | joins << "LEFT OUTER JOIN #{User.table_name} authors ON authors.id = #{queried_table_name}.author_id" |
|
514 | joins << "LEFT OUTER JOIN #{User.table_name} authors ON authors.id = #{queried_table_name}.author_id" | |
515 | end |
|
515 | end | |
516 | if order_options.include?('users') |
|
516 | if order_options.include?('users') | |
517 | joins << "LEFT OUTER JOIN #{User.table_name} ON #{User.table_name}.id = #{queried_table_name}.assigned_to_id" |
|
517 | joins << "LEFT OUTER JOIN #{User.table_name} ON #{User.table_name}.id = #{queried_table_name}.assigned_to_id" | |
518 | end |
|
518 | end | |
519 |
if order_options.include?(' |
|
519 | if order_options.include?('versions') | |
520 | joins << "LEFT OUTER JOIN #{Version.table_name} ON #{Version.table_name}.id = #{queried_table_name}.fixed_version_id" |
|
520 | joins << "LEFT OUTER JOIN #{Version.table_name} ON #{Version.table_name}.id = #{queried_table_name}.fixed_version_id" | |
521 | end |
|
521 | end | |
522 |
if order_options.include?('categor |
|
522 | if order_options.include?('issue_categories') | |
523 | joins << "LEFT OUTER JOIN #{IssueCategory.table_name} ON #{IssueCategory.table_name}.id = #{queried_table_name}.category_id" |
|
523 | joins << "LEFT OUTER JOIN #{IssueCategory.table_name} ON #{IssueCategory.table_name}.id = #{queried_table_name}.category_id" | |
524 | end |
|
524 | end | |
525 | if order_options.include?('tracker') |
|
525 | if order_options.include?('trackers') | |
526 | joins << "LEFT OUTER JOIN #{Tracker.table_name} ON #{Tracker.table_name}.id = #{queried_table_name}.tracker_id" |
|
526 | joins << "LEFT OUTER JOIN #{Tracker.table_name} ON #{Tracker.table_name}.id = #{queried_table_name}.tracker_id" | |
527 | end |
|
527 | end | |
528 | if order_options.include?('enumeration') |
|
528 | if order_options.include?('enumerations') | |
529 | joins << "LEFT OUTER JOIN #{IssuePriority.table_name} ON #{IssuePriority.table_name}.id = #{queried_table_name}.priority_id" |
|
529 | joins << "LEFT OUTER JOIN #{IssuePriority.table_name} ON #{IssuePriority.table_name}.id = #{queried_table_name}.priority_id" | |
530 | end |
|
530 | end | |
531 | end |
|
531 | end | |
532 |
|
532 | |||
533 | joins.any? ? joins.join(' ') : nil |
|
533 | joins.any? ? joins.join(' ') : nil | |
534 | end |
|
534 | end | |
535 | end |
|
535 | end |
General Comments 0
You need to be logged in to leave comments.
Login now