@@ -1,569 +1,568 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2016 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | class IssuesController < ApplicationController |
|
19 | 19 | default_search_scope :issues |
|
20 | 20 | |
|
21 | 21 | before_action :find_issue, :only => [:show, :edit, :update] |
|
22 | 22 | before_action :find_issues, :only => [:bulk_edit, :bulk_update, :destroy] |
|
23 | 23 | before_action :authorize, :except => [:index, :new, :create] |
|
24 | 24 | before_action :find_optional_project, :only => [:index, :new, :create] |
|
25 | 25 | before_action :build_new_issue_from_params, :only => [:new, :create] |
|
26 | 26 | accept_rss_auth :index, :show |
|
27 | 27 | accept_api_auth :index, :show, :create, :update, :destroy |
|
28 | 28 | |
|
29 | 29 | rescue_from Query::StatementInvalid, :with => :query_statement_invalid |
|
30 | 30 | |
|
31 | 31 | helper :journals |
|
32 | 32 | helper :projects |
|
33 | 33 | helper :custom_fields |
|
34 | 34 | helper :issue_relations |
|
35 | 35 | helper :watchers |
|
36 | 36 | helper :attachments |
|
37 | 37 | helper :queries |
|
38 | 38 | include QueriesHelper |
|
39 | 39 | helper :repositories |
|
40 | 40 | helper :sort |
|
41 | 41 | include SortHelper |
|
42 | 42 | helper :timelog |
|
43 | 43 | |
|
44 | 44 | def index |
|
45 | 45 | retrieve_query |
|
46 | 46 | sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria) |
|
47 | 47 | sort_update(@query.sortable_columns) |
|
48 | 48 | @query.sort_criteria = sort_criteria.to_a |
|
49 | 49 | |
|
50 | 50 | if @query.valid? |
|
51 | 51 | case params[:format] |
|
52 | 52 | when 'csv', 'pdf' |
|
53 | 53 | @limit = Setting.issues_export_limit.to_i |
|
54 | 54 | if params[:columns] == 'all' |
|
55 | 55 | @query.column_names = @query.available_inline_columns.map(&:name) |
|
56 | 56 | end |
|
57 | 57 | when 'atom' |
|
58 | 58 | @limit = Setting.feeds_limit.to_i |
|
59 | 59 | when 'xml', 'json' |
|
60 | 60 | @offset, @limit = api_offset_and_limit |
|
61 | 61 | @query.column_names = %w(author) |
|
62 | 62 | else |
|
63 | 63 | @limit = per_page_option |
|
64 | 64 | end |
|
65 | 65 | |
|
66 | 66 | @issue_count = @query.issue_count |
|
67 | 67 | @issue_pages = Paginator.new @issue_count, @limit, params['page'] |
|
68 | 68 | @offset ||= @issue_pages.offset |
|
69 | @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version], | |
|
70 | :order => sort_clause, | |
|
69 | @issues = @query.issues(:order => sort_clause, | |
|
71 | 70 | :offset => @offset, |
|
72 | 71 | :limit => @limit) |
|
73 | 72 | @issue_count_by_group = @query.issue_count_by_group |
|
74 | 73 | |
|
75 | 74 | respond_to do |format| |
|
76 | 75 | format.html { render :template => 'issues/index', :layout => !request.xhr? } |
|
77 | 76 | format.api { |
|
78 | 77 | Issue.load_visible_relations(@issues) if include_in_api_response?('relations') |
|
79 | 78 | } |
|
80 | 79 | format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") } |
|
81 | 80 | format.csv { send_data(query_to_csv(@issues, @query, params[:csv]), :type => 'text/csv; header=present', :filename => 'issues.csv') } |
|
82 | 81 | format.pdf { send_file_headers! :type => 'application/pdf', :filename => 'issues.pdf' } |
|
83 | 82 | end |
|
84 | 83 | else |
|
85 | 84 | respond_to do |format| |
|
86 | 85 | format.html { render(:template => 'issues/index', :layout => !request.xhr?) } |
|
87 | 86 | format.any(:atom, :csv, :pdf) { head 422 } |
|
88 | 87 | format.api { render_validation_errors(@query) } |
|
89 | 88 | end |
|
90 | 89 | end |
|
91 | 90 | rescue ActiveRecord::RecordNotFound |
|
92 | 91 | render_404 |
|
93 | 92 | end |
|
94 | 93 | |
|
95 | 94 | def show |
|
96 | 95 | @journals = @issue.visible_journals_with_index |
|
97 | 96 | @changesets = @issue.changesets.visible.preload(:repository, :user).to_a |
|
98 | 97 | @relations = @issue.relations.select {|r| r.other_issue(@issue) && r.other_issue(@issue).visible? } |
|
99 | 98 | |
|
100 | 99 | if User.current.wants_comments_in_reverse_order? |
|
101 | 100 | @journals.reverse! |
|
102 | 101 | @changesets.reverse! |
|
103 | 102 | end |
|
104 | 103 | |
|
105 | 104 | respond_to do |format| |
|
106 | 105 | format.html { |
|
107 | 106 | @allowed_statuses = @issue.new_statuses_allowed_to(User.current) |
|
108 | 107 | @priorities = IssuePriority.active |
|
109 | 108 | @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project) |
|
110 | 109 | @relation = IssueRelation.new |
|
111 | 110 | retrieve_previous_and_next_issue_ids |
|
112 | 111 | render :template => 'issues/show' |
|
113 | 112 | } |
|
114 | 113 | format.api |
|
115 | 114 | format.atom { render :template => 'journals/index', :layout => false, :content_type => 'application/atom+xml' } |
|
116 | 115 | format.pdf { |
|
117 | 116 | send_file_headers! :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf" |
|
118 | 117 | } |
|
119 | 118 | end |
|
120 | 119 | end |
|
121 | 120 | |
|
122 | 121 | def new |
|
123 | 122 | respond_to do |format| |
|
124 | 123 | format.html { render :action => 'new', :layout => !request.xhr? } |
|
125 | 124 | format.js |
|
126 | 125 | end |
|
127 | 126 | end |
|
128 | 127 | |
|
129 | 128 | def create |
|
130 | 129 | unless User.current.allowed_to?(:add_issues, @issue.project, :global => true) |
|
131 | 130 | raise ::Unauthorized |
|
132 | 131 | end |
|
133 | 132 | call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue }) |
|
134 | 133 | @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads])) |
|
135 | 134 | if @issue.save |
|
136 | 135 | call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue}) |
|
137 | 136 | respond_to do |format| |
|
138 | 137 | format.html { |
|
139 | 138 | render_attachment_warning_if_needed(@issue) |
|
140 | 139 | flash[:notice] = l(:notice_issue_successful_create, :id => view_context.link_to("##{@issue.id}", issue_path(@issue), :title => @issue.subject)) |
|
141 | 140 | redirect_after_create |
|
142 | 141 | } |
|
143 | 142 | format.api { render :action => 'show', :status => :created, :location => issue_url(@issue) } |
|
144 | 143 | end |
|
145 | 144 | return |
|
146 | 145 | else |
|
147 | 146 | respond_to do |format| |
|
148 | 147 | format.html { |
|
149 | 148 | if @issue.project.nil? |
|
150 | 149 | render_error :status => 422 |
|
151 | 150 | else |
|
152 | 151 | render :action => 'new' |
|
153 | 152 | end |
|
154 | 153 | } |
|
155 | 154 | format.api { render_validation_errors(@issue) } |
|
156 | 155 | end |
|
157 | 156 | end |
|
158 | 157 | end |
|
159 | 158 | |
|
160 | 159 | def edit |
|
161 | 160 | return unless update_issue_from_params |
|
162 | 161 | |
|
163 | 162 | respond_to do |format| |
|
164 | 163 | format.html { } |
|
165 | 164 | format.js |
|
166 | 165 | end |
|
167 | 166 | end |
|
168 | 167 | |
|
169 | 168 | def update |
|
170 | 169 | return unless update_issue_from_params |
|
171 | 170 | @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads])) |
|
172 | 171 | saved = false |
|
173 | 172 | begin |
|
174 | 173 | saved = save_issue_with_child_records |
|
175 | 174 | rescue ActiveRecord::StaleObjectError |
|
176 | 175 | @conflict = true |
|
177 | 176 | if params[:last_journal_id] |
|
178 | 177 | @conflict_journals = @issue.journals_after(params[:last_journal_id]).to_a |
|
179 | 178 | @conflict_journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project) |
|
180 | 179 | end |
|
181 | 180 | end |
|
182 | 181 | |
|
183 | 182 | if saved |
|
184 | 183 | render_attachment_warning_if_needed(@issue) |
|
185 | 184 | flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record? |
|
186 | 185 | |
|
187 | 186 | respond_to do |format| |
|
188 | 187 | format.html { redirect_back_or_default issue_path(@issue, previous_and_next_issue_ids_params) } |
|
189 | 188 | format.api { render_api_ok } |
|
190 | 189 | end |
|
191 | 190 | else |
|
192 | 191 | respond_to do |format| |
|
193 | 192 | format.html { render :action => 'edit' } |
|
194 | 193 | format.api { render_validation_errors(@issue) } |
|
195 | 194 | end |
|
196 | 195 | end |
|
197 | 196 | end |
|
198 | 197 | |
|
199 | 198 | # Bulk edit/copy a set of issues |
|
200 | 199 | def bulk_edit |
|
201 | 200 | @issues.sort! |
|
202 | 201 | @copy = params[:copy].present? |
|
203 | 202 | @notes = params[:notes] |
|
204 | 203 | |
|
205 | 204 | if @copy |
|
206 | 205 | unless User.current.allowed_to?(:copy_issues, @projects) |
|
207 | 206 | raise ::Unauthorized |
|
208 | 207 | end |
|
209 | 208 | else |
|
210 | 209 | unless @issues.all?(&:attributes_editable?) |
|
211 | 210 | raise ::Unauthorized |
|
212 | 211 | end |
|
213 | 212 | end |
|
214 | 213 | |
|
215 | 214 | edited_issues = Issue.where(:id => @issues.map(&:id)).to_a |
|
216 | 215 | |
|
217 | 216 | @allowed_projects = Issue.allowed_target_projects |
|
218 | 217 | if params[:issue] |
|
219 | 218 | @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:issue][:project_id].to_s} |
|
220 | 219 | if @target_project |
|
221 | 220 | target_projects = [@target_project] |
|
222 | 221 | edited_issues.each {|issue| issue.project = @target_project} |
|
223 | 222 | end |
|
224 | 223 | end |
|
225 | 224 | target_projects ||= @projects |
|
226 | 225 | |
|
227 | 226 | @trackers = target_projects.map {|p| Issue.allowed_target_trackers(p) }.reduce(:&) |
|
228 | 227 | if params[:issue] |
|
229 | 228 | @target_tracker = @trackers.detect {|t| t.id.to_s == params[:issue][:tracker_id].to_s} |
|
230 | 229 | if @target_tracker |
|
231 | 230 | edited_issues.each {|issue| issue.tracker = @target_tracker} |
|
232 | 231 | end |
|
233 | 232 | end |
|
234 | 233 | |
|
235 | 234 | if @copy |
|
236 | 235 | # Copied issues will get their default statuses |
|
237 | 236 | @available_statuses = [] |
|
238 | 237 | else |
|
239 | 238 | @available_statuses = edited_issues.map(&:new_statuses_allowed_to).reduce(:&) |
|
240 | 239 | end |
|
241 | 240 | if params[:issue] |
|
242 | 241 | @target_status = @available_statuses.detect {|t| t.id.to_s == params[:issue][:status_id].to_s} |
|
243 | 242 | if @target_status |
|
244 | 243 | edited_issues.each {|issue| issue.status = @target_status} |
|
245 | 244 | end |
|
246 | 245 | end |
|
247 | 246 | |
|
248 | 247 | @custom_fields = edited_issues.map{|i|i.editable_custom_fields}.reduce(:&).select {|field| field.format.bulk_edit_supported} |
|
249 | 248 | @assignables = target_projects.map(&:assignable_users).reduce(:&) |
|
250 | 249 | @versions = target_projects.map {|p| p.shared_versions.open}.reduce(:&) |
|
251 | 250 | @categories = target_projects.map {|p| p.issue_categories}.reduce(:&) |
|
252 | 251 | if @copy |
|
253 | 252 | @attachments_present = @issues.detect {|i| i.attachments.any?}.present? |
|
254 | 253 | @subtasks_present = @issues.detect {|i| !i.leaf?}.present? |
|
255 | 254 | end |
|
256 | 255 | |
|
257 | 256 | @safe_attributes = edited_issues.map(&:safe_attribute_names).reduce(:&) |
|
258 | 257 | |
|
259 | 258 | @issue_params = params[:issue] || {} |
|
260 | 259 | @issue_params[:custom_field_values] ||= {} |
|
261 | 260 | end |
|
262 | 261 | |
|
263 | 262 | def bulk_update |
|
264 | 263 | @issues.sort! |
|
265 | 264 | @copy = params[:copy].present? |
|
266 | 265 | |
|
267 | 266 | attributes = parse_params_for_bulk_update(params[:issue]) |
|
268 | 267 | copy_subtasks = (params[:copy_subtasks] == '1') |
|
269 | 268 | copy_attachments = (params[:copy_attachments] == '1') |
|
270 | 269 | |
|
271 | 270 | if @copy |
|
272 | 271 | unless User.current.allowed_to?(:copy_issues, @projects) |
|
273 | 272 | raise ::Unauthorized |
|
274 | 273 | end |
|
275 | 274 | target_projects = @projects |
|
276 | 275 | if attributes['project_id'].present? |
|
277 | 276 | target_projects = Project.where(:id => attributes['project_id']).to_a |
|
278 | 277 | end |
|
279 | 278 | unless User.current.allowed_to?(:add_issues, target_projects) |
|
280 | 279 | raise ::Unauthorized |
|
281 | 280 | end |
|
282 | 281 | else |
|
283 | 282 | unless @issues.all?(&:attributes_editable?) |
|
284 | 283 | raise ::Unauthorized |
|
285 | 284 | end |
|
286 | 285 | end |
|
287 | 286 | |
|
288 | 287 | unsaved_issues = [] |
|
289 | 288 | saved_issues = [] |
|
290 | 289 | |
|
291 | 290 | if @copy && copy_subtasks |
|
292 | 291 | # Descendant issues will be copied with the parent task |
|
293 | 292 | # Don't copy them twice |
|
294 | 293 | @issues.reject! {|issue| @issues.detect {|other| issue.is_descendant_of?(other)}} |
|
295 | 294 | end |
|
296 | 295 | |
|
297 | 296 | @issues.each do |orig_issue| |
|
298 | 297 | orig_issue.reload |
|
299 | 298 | if @copy |
|
300 | 299 | issue = orig_issue.copy({}, |
|
301 | 300 | :attachments => copy_attachments, |
|
302 | 301 | :subtasks => copy_subtasks, |
|
303 | 302 | :link => link_copy?(params[:link_copy]) |
|
304 | 303 | ) |
|
305 | 304 | else |
|
306 | 305 | issue = orig_issue |
|
307 | 306 | end |
|
308 | 307 | journal = issue.init_journal(User.current, params[:notes]) |
|
309 | 308 | issue.safe_attributes = attributes |
|
310 | 309 | call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue }) |
|
311 | 310 | if issue.save |
|
312 | 311 | saved_issues << issue |
|
313 | 312 | else |
|
314 | 313 | unsaved_issues << orig_issue |
|
315 | 314 | end |
|
316 | 315 | end |
|
317 | 316 | |
|
318 | 317 | if unsaved_issues.empty? |
|
319 | 318 | flash[:notice] = l(:notice_successful_update) unless saved_issues.empty? |
|
320 | 319 | if params[:follow] |
|
321 | 320 | if @issues.size == 1 && saved_issues.size == 1 |
|
322 | 321 | redirect_to issue_path(saved_issues.first) |
|
323 | 322 | elsif saved_issues.map(&:project).uniq.size == 1 |
|
324 | 323 | redirect_to project_issues_path(saved_issues.map(&:project).first) |
|
325 | 324 | end |
|
326 | 325 | else |
|
327 | 326 | redirect_back_or_default _project_issues_path(@project) |
|
328 | 327 | end |
|
329 | 328 | else |
|
330 | 329 | @saved_issues = @issues |
|
331 | 330 | @unsaved_issues = unsaved_issues |
|
332 | 331 | @issues = Issue.visible.where(:id => @unsaved_issues.map(&:id)).to_a |
|
333 | 332 | bulk_edit |
|
334 | 333 | render :action => 'bulk_edit' |
|
335 | 334 | end |
|
336 | 335 | end |
|
337 | 336 | |
|
338 | 337 | def destroy |
|
339 | 338 | raise Unauthorized unless @issues.all?(&:deletable?) |
|
340 | 339 | |
|
341 | 340 | # all issues and their descendants are about to be deleted |
|
342 | 341 | issues_and_descendants_ids = Issue.self_and_descendants(@issues).pluck(:id) |
|
343 | 342 | time_entries = TimeEntry.where(:issue_id => issues_and_descendants_ids) |
|
344 | 343 | @hours = time_entries.sum(:hours).to_f |
|
345 | 344 | |
|
346 | 345 | if @hours > 0 |
|
347 | 346 | case params[:todo] |
|
348 | 347 | when 'destroy' |
|
349 | 348 | # nothing to do |
|
350 | 349 | when 'nullify' |
|
351 | 350 | time_entries.update_all(:issue_id => nil) |
|
352 | 351 | when 'reassign' |
|
353 | 352 | reassign_to = @project && @project.issues.find_by_id(params[:reassign_to_id]) |
|
354 | 353 | if reassign_to.nil? |
|
355 | 354 | flash.now[:error] = l(:error_issue_not_found_in_project) |
|
356 | 355 | return |
|
357 | 356 | elsif issues_and_descendants_ids.include?(reassign_to.id) |
|
358 | 357 | flash.now[:error] = l(:error_cannot_reassign_time_entries_to_an_issue_about_to_be_deleted) |
|
359 | 358 | return |
|
360 | 359 | else |
|
361 | 360 | time_entries.update_all(:issue_id => reassign_to.id, :project_id => reassign_to.project_id) |
|
362 | 361 | end |
|
363 | 362 | else |
|
364 | 363 | # display the destroy form if it's a user request |
|
365 | 364 | return unless api_request? |
|
366 | 365 | end |
|
367 | 366 | end |
|
368 | 367 | @issues.each do |issue| |
|
369 | 368 | begin |
|
370 | 369 | issue.reload.destroy |
|
371 | 370 | rescue ::ActiveRecord::RecordNotFound # raised by #reload if issue no longer exists |
|
372 | 371 | # nothing to do, issue was already deleted (eg. by a parent) |
|
373 | 372 | end |
|
374 | 373 | end |
|
375 | 374 | respond_to do |format| |
|
376 | 375 | format.html { redirect_back_or_default _project_issues_path(@project) } |
|
377 | 376 | format.api { render_api_ok } |
|
378 | 377 | end |
|
379 | 378 | end |
|
380 | 379 | |
|
381 | 380 | # Overrides Redmine::MenuManager::MenuController::ClassMethods for |
|
382 | 381 | # when the "New issue" tab is enabled |
|
383 | 382 | def current_menu_item |
|
384 | 383 | if Setting.new_item_menu_tab == '1' && [:new, :create].include?(action_name.to_sym) |
|
385 | 384 | :new_issue |
|
386 | 385 | else |
|
387 | 386 | super |
|
388 | 387 | end |
|
389 | 388 | end |
|
390 | 389 | |
|
391 | 390 | private |
|
392 | 391 | |
|
393 | 392 | def retrieve_previous_and_next_issue_ids |
|
394 | 393 | if params[:prev_issue_id].present? || params[:next_issue_id].present? |
|
395 | 394 | @prev_issue_id = params[:prev_issue_id].presence.try(:to_i) |
|
396 | 395 | @next_issue_id = params[:next_issue_id].presence.try(:to_i) |
|
397 | 396 | @issue_position = params[:issue_position].presence.try(:to_i) |
|
398 | 397 | @issue_count = params[:issue_count].presence.try(:to_i) |
|
399 | 398 | else |
|
400 | 399 | retrieve_query_from_session |
|
401 | 400 | if @query |
|
402 | 401 | sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria) |
|
403 | 402 | sort_update(@query.sortable_columns, 'issues_index_sort') |
|
404 | 403 | limit = 500 |
|
405 |
issue_ids = @query.issue_ids(:order => sort_clause, :limit => (limit + 1) |
|
|
404 | issue_ids = @query.issue_ids(:order => sort_clause, :limit => (limit + 1)) | |
|
406 | 405 | if (idx = issue_ids.index(@issue.id)) && idx < limit |
|
407 | 406 | if issue_ids.size < 500 |
|
408 | 407 | @issue_position = idx + 1 |
|
409 | 408 | @issue_count = issue_ids.size |
|
410 | 409 | end |
|
411 | 410 | @prev_issue_id = issue_ids[idx - 1] if idx > 0 |
|
412 | 411 | @next_issue_id = issue_ids[idx + 1] if idx < (issue_ids.size - 1) |
|
413 | 412 | end |
|
414 | 413 | end |
|
415 | 414 | end |
|
416 | 415 | end |
|
417 | 416 | |
|
418 | 417 | def previous_and_next_issue_ids_params |
|
419 | 418 | { |
|
420 | 419 | :prev_issue_id => params[:prev_issue_id], |
|
421 | 420 | :next_issue_id => params[:next_issue_id], |
|
422 | 421 | :issue_position => params[:issue_position], |
|
423 | 422 | :issue_count => params[:issue_count] |
|
424 | 423 | }.reject {|k,v| k.blank?} |
|
425 | 424 | end |
|
426 | 425 | |
|
427 | 426 | # Used by #edit and #update to set some common instance variables |
|
428 | 427 | # from the params |
|
429 | 428 | def update_issue_from_params |
|
430 | 429 | @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project) |
|
431 | 430 | if params[:time_entry] |
|
432 | 431 | @time_entry.safe_attributes = params[:time_entry] |
|
433 | 432 | end |
|
434 | 433 | |
|
435 | 434 | @issue.init_journal(User.current) |
|
436 | 435 | |
|
437 | 436 | issue_attributes = params[:issue] |
|
438 | 437 | if issue_attributes && params[:conflict_resolution] |
|
439 | 438 | case params[:conflict_resolution] |
|
440 | 439 | when 'overwrite' |
|
441 | 440 | issue_attributes = issue_attributes.dup |
|
442 | 441 | issue_attributes.delete(:lock_version) |
|
443 | 442 | when 'add_notes' |
|
444 | 443 | issue_attributes = issue_attributes.slice(:notes, :private_notes) |
|
445 | 444 | when 'cancel' |
|
446 | 445 | redirect_to issue_path(@issue) |
|
447 | 446 | return false |
|
448 | 447 | end |
|
449 | 448 | end |
|
450 | 449 | @issue.safe_attributes = issue_attributes |
|
451 | 450 | @priorities = IssuePriority.active |
|
452 | 451 | @allowed_statuses = @issue.new_statuses_allowed_to(User.current) |
|
453 | 452 | true |
|
454 | 453 | end |
|
455 | 454 | |
|
456 | 455 | # Used by #new and #create to build a new issue from the params |
|
457 | 456 | # The new issue will be copied from an existing one if copy_from parameter is given |
|
458 | 457 | def build_new_issue_from_params |
|
459 | 458 | @issue = Issue.new |
|
460 | 459 | if params[:copy_from] |
|
461 | 460 | begin |
|
462 | 461 | @issue.init_journal(User.current) |
|
463 | 462 | @copy_from = Issue.visible.find(params[:copy_from]) |
|
464 | 463 | unless User.current.allowed_to?(:copy_issues, @copy_from.project) |
|
465 | 464 | raise ::Unauthorized |
|
466 | 465 | end |
|
467 | 466 | @link_copy = link_copy?(params[:link_copy]) || request.get? |
|
468 | 467 | @copy_attachments = params[:copy_attachments].present? || request.get? |
|
469 | 468 | @copy_subtasks = params[:copy_subtasks].present? || request.get? |
|
470 | 469 | @issue.copy_from(@copy_from, :attachments => @copy_attachments, :subtasks => @copy_subtasks, :link => @link_copy) |
|
471 | 470 | @issue.parent_issue_id = @copy_from.parent_id |
|
472 | 471 | rescue ActiveRecord::RecordNotFound |
|
473 | 472 | render_404 |
|
474 | 473 | return |
|
475 | 474 | end |
|
476 | 475 | end |
|
477 | 476 | @issue.project = @project |
|
478 | 477 | if request.get? |
|
479 | 478 | @issue.project ||= @issue.allowed_target_projects.first |
|
480 | 479 | end |
|
481 | 480 | @issue.author ||= User.current |
|
482 | 481 | @issue.start_date ||= User.current.today if Setting.default_issue_start_date_to_creation_date? |
|
483 | 482 | |
|
484 | 483 | attrs = (params[:issue] || {}).deep_dup |
|
485 | 484 | if action_name == 'new' && params[:was_default_status] == attrs[:status_id] |
|
486 | 485 | attrs.delete(:status_id) |
|
487 | 486 | end |
|
488 | 487 | if action_name == 'new' && params[:form_update_triggered_by] == 'issue_project_id' |
|
489 | 488 | # Discard submitted version when changing the project on the issue form |
|
490 | 489 | # so we can use the default version for the new project |
|
491 | 490 | attrs.delete(:fixed_version_id) |
|
492 | 491 | end |
|
493 | 492 | @issue.safe_attributes = attrs |
|
494 | 493 | |
|
495 | 494 | if @issue.project |
|
496 | 495 | @issue.tracker ||= @issue.allowed_target_trackers.first |
|
497 | 496 | if @issue.tracker.nil? |
|
498 | 497 | if @issue.project.trackers.any? |
|
499 | 498 | # None of the project trackers is allowed to the user |
|
500 | 499 | render_error :message => l(:error_no_tracker_allowed_for_new_issue_in_project), :status => 403 |
|
501 | 500 | else |
|
502 | 501 | # Project has no trackers |
|
503 | 502 | render_error l(:error_no_tracker_in_project) |
|
504 | 503 | end |
|
505 | 504 | return false |
|
506 | 505 | end |
|
507 | 506 | if @issue.status.nil? |
|
508 | 507 | render_error l(:error_no_default_issue_status) |
|
509 | 508 | return false |
|
510 | 509 | end |
|
511 | 510 | elsif request.get? |
|
512 | 511 | render_error :message => l(:error_no_projects_with_tracker_allowed_for_new_issue), :status => 403 |
|
513 | 512 | return false |
|
514 | 513 | end |
|
515 | 514 | |
|
516 | 515 | @priorities = IssuePriority.active |
|
517 | 516 | @allowed_statuses = @issue.new_statuses_allowed_to(User.current) |
|
518 | 517 | end |
|
519 | 518 | |
|
520 | 519 | # Saves @issue and a time_entry from the parameters |
|
521 | 520 | def save_issue_with_child_records |
|
522 | 521 | Issue.transaction do |
|
523 | 522 | if params[:time_entry] && (params[:time_entry][:hours].present? || params[:time_entry][:comments].present?) && User.current.allowed_to?(:log_time, @issue.project) |
|
524 | 523 | time_entry = @time_entry || TimeEntry.new |
|
525 | 524 | time_entry.project = @issue.project |
|
526 | 525 | time_entry.issue = @issue |
|
527 | 526 | time_entry.user = User.current |
|
528 | 527 | time_entry.spent_on = User.current.today |
|
529 | 528 | time_entry.attributes = params[:time_entry] |
|
530 | 529 | @issue.time_entries << time_entry |
|
531 | 530 | end |
|
532 | 531 | |
|
533 | 532 | call_hook(:controller_issues_edit_before_save, { :params => params, :issue => @issue, :time_entry => time_entry, :journal => @issue.current_journal}) |
|
534 | 533 | if @issue.save |
|
535 | 534 | call_hook(:controller_issues_edit_after_save, { :params => params, :issue => @issue, :time_entry => time_entry, :journal => @issue.current_journal}) |
|
536 | 535 | else |
|
537 | 536 | raise ActiveRecord::Rollback |
|
538 | 537 | end |
|
539 | 538 | end |
|
540 | 539 | end |
|
541 | 540 | |
|
542 | 541 | # Returns true if the issue copy should be linked |
|
543 | 542 | # to the original issue |
|
544 | 543 | def link_copy?(param) |
|
545 | 544 | case Setting.link_copied_issue |
|
546 | 545 | when 'yes' |
|
547 | 546 | true |
|
548 | 547 | when 'no' |
|
549 | 548 | false |
|
550 | 549 | when 'ask' |
|
551 | 550 | param == '1' |
|
552 | 551 | end |
|
553 | 552 | end |
|
554 | 553 | |
|
555 | 554 | # Redirects user after a successful issue creation |
|
556 | 555 | def redirect_after_create |
|
557 | 556 | if params[:continue] |
|
558 | 557 | attrs = {:tracker_id => @issue.tracker, :parent_issue_id => @issue.parent_issue_id}.reject {|k,v| v.nil?} |
|
559 | 558 | if params[:project_id] |
|
560 | 559 | redirect_to new_project_issue_path(@issue.project, :issue => attrs) |
|
561 | 560 | else |
|
562 | 561 | attrs.merge! :project_id => @issue.project_id |
|
563 | 562 | redirect_to new_issue_path(:issue => attrs) |
|
564 | 563 | end |
|
565 | 564 | else |
|
566 | 565 | redirect_to issue_path(@issue) |
|
567 | 566 | end |
|
568 | 567 | end |
|
569 | 568 | end |
@@ -1,520 +1,535 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2016 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | class IssueQuery < Query |
|
19 | 19 | |
|
20 | 20 | self.queried_class = Issue |
|
21 | 21 | self.view_permission = :view_issues |
|
22 | 22 | |
|
23 | 23 | self.available_columns = [ |
|
24 | 24 | QueryColumn.new(:id, :sortable => "#{Issue.table_name}.id", :default_order => 'desc', :caption => '#', :frozen => true), |
|
25 | 25 | QueryColumn.new(:project, :sortable => "#{Project.table_name}.name", :groupable => true), |
|
26 | 26 | QueryColumn.new(:tracker, :sortable => "#{Tracker.table_name}.position", :groupable => true), |
|
27 | 27 | QueryColumn.new(:parent, :sortable => ["#{Issue.table_name}.root_id", "#{Issue.table_name}.lft ASC"], :default_order => 'desc', :caption => :field_parent_issue), |
|
28 | 28 | QueryColumn.new(:status, :sortable => "#{IssueStatus.table_name}.position", :groupable => true), |
|
29 | 29 | QueryColumn.new(:priority, :sortable => "#{IssuePriority.table_name}.position", :default_order => 'desc', :groupable => true), |
|
30 | 30 | QueryColumn.new(:subject, :sortable => "#{Issue.table_name}.subject"), |
|
31 | 31 | QueryColumn.new(:author, :sortable => lambda {User.fields_for_order_statement("authors")}, :groupable => true), |
|
32 | 32 | QueryColumn.new(:assigned_to, :sortable => lambda {User.fields_for_order_statement}, :groupable => true), |
|
33 | 33 | QueryColumn.new(:updated_on, :sortable => "#{Issue.table_name}.updated_on", :default_order => 'desc'), |
|
34 | 34 | QueryColumn.new(:category, :sortable => "#{IssueCategory.table_name}.name", :groupable => true), |
|
35 | 35 | QueryColumn.new(:fixed_version, :sortable => lambda {Version.fields_for_order_statement}, :groupable => true), |
|
36 | 36 | QueryColumn.new(:start_date, :sortable => "#{Issue.table_name}.start_date"), |
|
37 | 37 | QueryColumn.new(:due_date, :sortable => "#{Issue.table_name}.due_date"), |
|
38 | 38 | QueryColumn.new(:estimated_hours, :sortable => "#{Issue.table_name}.estimated_hours", :totalable => true), |
|
39 | 39 | QueryColumn.new(:total_estimated_hours, |
|
40 | 40 | :sortable => "COALESCE((SELECT SUM(estimated_hours) FROM #{Issue.table_name} subtasks" + |
|
41 | 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 | 42 | :default_order => 'desc'), |
|
43 | 43 | QueryColumn.new(:done_ratio, :sortable => "#{Issue.table_name}.done_ratio", :groupable => true), |
|
44 | 44 | QueryColumn.new(:created_on, :sortable => "#{Issue.table_name}.created_on", :default_order => 'desc'), |
|
45 | 45 | QueryColumn.new(:closed_on, :sortable => "#{Issue.table_name}.closed_on", :default_order => 'desc'), |
|
46 | 46 | QueryColumn.new(:relations, :caption => :label_related_issues), |
|
47 | 47 | QueryColumn.new(:description, :inline => false) |
|
48 | 48 | ] |
|
49 | 49 | |
|
50 | 50 | def initialize(attributes=nil, *args) |
|
51 | 51 | super attributes |
|
52 | 52 | self.filters ||= { 'status_id' => {:operator => "o", :values => [""]} } |
|
53 | 53 | end |
|
54 | 54 | |
|
55 | 55 | def draw_relations |
|
56 | 56 | r = options[:draw_relations] |
|
57 | 57 | r.nil? || r == '1' |
|
58 | 58 | end |
|
59 | 59 | |
|
60 | 60 | def draw_relations=(arg) |
|
61 | 61 | options[:draw_relations] = (arg == '0' ? '0' : nil) |
|
62 | 62 | end |
|
63 | 63 | |
|
64 | 64 | def draw_progress_line |
|
65 | 65 | r = options[:draw_progress_line] |
|
66 | 66 | r == '1' |
|
67 | 67 | end |
|
68 | 68 | |
|
69 | 69 | def draw_progress_line=(arg) |
|
70 | 70 | options[:draw_progress_line] = (arg == '1' ? '1' : nil) |
|
71 | 71 | end |
|
72 | 72 | |
|
73 | 73 | def build_from_params(params) |
|
74 | 74 | super |
|
75 | 75 | self.draw_relations = params[:draw_relations] || (params[:query] && params[:query][:draw_relations]) |
|
76 | 76 | self.draw_progress_line = params[:draw_progress_line] || (params[:query] && params[:query][:draw_progress_line]) |
|
77 | 77 | self |
|
78 | 78 | end |
|
79 | 79 | |
|
80 | 80 | def initialize_available_filters |
|
81 | 81 | add_available_filter "status_id", |
|
82 | 82 | :type => :list_status, :values => lambda { IssueStatus.sorted.collect{|s| [s.name, s.id.to_s] } } |
|
83 | 83 | |
|
84 | 84 | add_available_filter("project_id", |
|
85 | 85 | :type => :list, :values => lambda { project_values } |
|
86 | 86 | ) if project.nil? |
|
87 | 87 | |
|
88 | 88 | add_available_filter "tracker_id", |
|
89 | 89 | :type => :list, :values => trackers.collect{|s| [s.name, s.id.to_s] } |
|
90 | 90 | |
|
91 | 91 | add_available_filter "priority_id", |
|
92 | 92 | :type => :list, :values => IssuePriority.all.collect{|s| [s.name, s.id.to_s] } |
|
93 | 93 | |
|
94 | 94 | add_available_filter("author_id", |
|
95 | 95 | :type => :list, :values => lambda { author_values } |
|
96 | 96 | ) |
|
97 | 97 | |
|
98 | 98 | add_available_filter("assigned_to_id", |
|
99 | 99 | :type => :list_optional, :values => lambda { assigned_to_values } |
|
100 | 100 | ) |
|
101 | 101 | |
|
102 | 102 | add_available_filter("member_of_group", |
|
103 | 103 | :type => :list_optional, :values => lambda { Group.givable.visible.collect {|g| [g.name, g.id.to_s] } } |
|
104 | 104 | ) |
|
105 | 105 | |
|
106 | 106 | add_available_filter("assigned_to_role", |
|
107 | 107 | :type => :list_optional, :values => lambda { Role.givable.collect {|r| [r.name, r.id.to_s] } } |
|
108 | 108 | ) |
|
109 | 109 | |
|
110 | 110 | add_available_filter "fixed_version_id", |
|
111 | 111 | :type => :list_optional, :values => lambda { fixed_version_values } |
|
112 | 112 | |
|
113 | 113 | add_available_filter "fixed_version.due_date", |
|
114 | 114 | :type => :date, |
|
115 | 115 | :name => l(:label_attribute_of_fixed_version, :name => l(:field_effective_date)) |
|
116 | 116 | |
|
117 | 117 | add_available_filter "fixed_version.status", |
|
118 | 118 | :type => :list, |
|
119 | 119 | :name => l(:label_attribute_of_fixed_version, :name => l(:field_status)), |
|
120 | 120 | :values => Version::VERSION_STATUSES.map{|s| [l("version_status_#{s}"), s] } |
|
121 | 121 | |
|
122 | 122 | add_available_filter "category_id", |
|
123 | 123 | :type => :list_optional, |
|
124 | 124 | :values => lambda { project.issue_categories.collect{|s| [s.name, s.id.to_s] } } if project |
|
125 | 125 | |
|
126 | 126 | add_available_filter "subject", :type => :text |
|
127 | 127 | add_available_filter "description", :type => :text |
|
128 | 128 | add_available_filter "created_on", :type => :date_past |
|
129 | 129 | add_available_filter "updated_on", :type => :date_past |
|
130 | 130 | add_available_filter "closed_on", :type => :date_past |
|
131 | 131 | add_available_filter "start_date", :type => :date |
|
132 | 132 | add_available_filter "due_date", :type => :date |
|
133 | 133 | add_available_filter "estimated_hours", :type => :float |
|
134 | 134 | add_available_filter "done_ratio", :type => :integer |
|
135 | 135 | |
|
136 | 136 | if User.current.allowed_to?(:set_issues_private, nil, :global => true) || |
|
137 | 137 | User.current.allowed_to?(:set_own_issues_private, nil, :global => true) |
|
138 | 138 | add_available_filter "is_private", |
|
139 | 139 | :type => :list, |
|
140 | 140 | :values => [[l(:general_text_yes), "1"], [l(:general_text_no), "0"]] |
|
141 | 141 | end |
|
142 | 142 | |
|
143 | 143 | if User.current.logged? |
|
144 | 144 | add_available_filter "watcher_id", |
|
145 | 145 | :type => :list, :values => [["<< #{l(:label_me)} >>", "me"]] |
|
146 | 146 | end |
|
147 | 147 | |
|
148 | 148 | if project && !project.leaf? |
|
149 | 149 | add_available_filter "subproject_id", |
|
150 | 150 | :type => :list_subprojects, |
|
151 | 151 | :values => lambda { subproject_values } |
|
152 | 152 | end |
|
153 | 153 | |
|
154 | 154 | |
|
155 | 155 | issue_custom_fields = project ? project.all_issue_custom_fields : IssueCustomField.where(:is_for_all => true) |
|
156 | 156 | add_custom_fields_filters(issue_custom_fields) |
|
157 | 157 | |
|
158 | 158 | add_associations_custom_fields_filters :project, :author, :assigned_to, :fixed_version |
|
159 | 159 | |
|
160 | 160 | IssueRelation::TYPES.each do |relation_type, options| |
|
161 | 161 | add_available_filter relation_type, :type => :relation, :label => options[:name], :values => lambda {all_projects_values} |
|
162 | 162 | end |
|
163 | 163 | add_available_filter "parent_id", :type => :tree, :label => :field_parent_issue |
|
164 | 164 | add_available_filter "child_id", :type => :tree, :label => :label_subtask_plural |
|
165 | 165 | |
|
166 | 166 | add_available_filter "issue_id", :type => :integer, :label => :label_issue |
|
167 | 167 | |
|
168 | 168 | Tracker.disabled_core_fields(trackers).each {|field| |
|
169 | 169 | delete_available_filter field |
|
170 | 170 | } |
|
171 | 171 | end |
|
172 | 172 | |
|
173 | 173 | def available_columns |
|
174 | 174 | return @available_columns if @available_columns |
|
175 | 175 | @available_columns = self.class.available_columns.dup |
|
176 | 176 | @available_columns += (project ? |
|
177 | 177 | project.all_issue_custom_fields : |
|
178 | 178 | IssueCustomField |
|
179 | 179 | ).visible.collect {|cf| QueryCustomFieldColumn.new(cf) } |
|
180 | 180 | |
|
181 | 181 | if User.current.allowed_to?(:view_time_entries, project, :global => true) |
|
182 | 182 | index = @available_columns.find_index {|column| column.name == :total_estimated_hours} |
|
183 | 183 | index = (index ? index + 1 : -1) |
|
184 | 184 | # insert the column after total_estimated_hours or at the end |
|
185 | 185 | @available_columns.insert index, QueryColumn.new(:spent_hours, |
|
186 | 186 | :sortable => "COALESCE((SELECT SUM(hours) FROM #{TimeEntry.table_name} WHERE #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id), 0)", |
|
187 | 187 | :default_order => 'desc', |
|
188 | 188 | :caption => :label_spent_time, |
|
189 | 189 | :totalable => true |
|
190 | 190 | ) |
|
191 | 191 | @available_columns.insert index+1, QueryColumn.new(:total_spent_hours, |
|
192 | 192 | :sortable => "COALESCE((SELECT SUM(hours) FROM #{TimeEntry.table_name} JOIN #{Issue.table_name} subtasks ON subtasks.id = #{TimeEntry.table_name}.issue_id" + |
|
193 | 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 | 194 | :default_order => 'desc', |
|
195 | 195 | :caption => :label_total_spent_time |
|
196 | 196 | ) |
|
197 | 197 | end |
|
198 | 198 | |
|
199 | 199 | if User.current.allowed_to?(:set_issues_private, nil, :global => true) || |
|
200 | 200 | User.current.allowed_to?(:set_own_issues_private, nil, :global => true) |
|
201 | 201 | @available_columns << QueryColumn.new(:is_private, :sortable => "#{Issue.table_name}.is_private") |
|
202 | 202 | end |
|
203 | 203 | |
|
204 | 204 | disabled_fields = Tracker.disabled_core_fields(trackers).map {|field| field.sub(/_id$/, '')} |
|
205 | 205 | @available_columns.reject! {|column| |
|
206 | 206 | disabled_fields.include?(column.name.to_s) |
|
207 | 207 | } |
|
208 | 208 | |
|
209 | 209 | @available_columns |
|
210 | 210 | end |
|
211 | 211 | |
|
212 | 212 | def default_columns_names |
|
213 | 213 | @default_columns_names ||= begin |
|
214 | 214 | default_columns = Setting.issue_list_default_columns.map(&:to_sym) |
|
215 | 215 | |
|
216 | 216 | project.present? ? default_columns : [:project] | default_columns |
|
217 | 217 | end |
|
218 | 218 | end |
|
219 | 219 | |
|
220 | 220 | def default_totalable_names |
|
221 | 221 | Setting.issue_list_default_totals.map(&:to_sym) |
|
222 | 222 | end |
|
223 | 223 | |
|
224 | 224 | def base_scope |
|
225 | 225 | Issue.visible.joins(:status, :project).where(statement) |
|
226 | 226 | end |
|
227 | 227 | |
|
228 | 228 | # Returns the issue count |
|
229 | 229 | def issue_count |
|
230 | 230 | base_scope.count |
|
231 | 231 | rescue ::ActiveRecord::StatementInvalid => e |
|
232 | 232 | raise StatementInvalid.new(e.message) |
|
233 | 233 | end |
|
234 | 234 | |
|
235 | 235 | # Returns the issue count by group or nil if query is not grouped |
|
236 | 236 | def issue_count_by_group |
|
237 | 237 | grouped_query do |scope| |
|
238 | 238 | scope.count |
|
239 | 239 | end |
|
240 | 240 | end |
|
241 | 241 | |
|
242 | 242 | # Returns sum of all the issue's estimated_hours |
|
243 | 243 | def total_for_estimated_hours(scope) |
|
244 | 244 | map_total(scope.sum(:estimated_hours)) {|t| t.to_f.round(2)} |
|
245 | 245 | end |
|
246 | 246 | |
|
247 | 247 | # Returns sum of all the issue's time entries hours |
|
248 | 248 | def total_for_spent_hours(scope) |
|
249 | 249 | total = if group_by_column.try(:name) == :project |
|
250 | 250 | # TODO: remove this when https://github.com/rails/rails/issues/21922 is fixed |
|
251 | 251 | # We have to do a custom join without the time_entries.project_id column |
|
252 | 252 | # that would trigger a ambiguous column name error |
|
253 | 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 | 254 | sum("joined_time_entries.hours") |
|
255 | 255 | else |
|
256 | 256 | scope.joins(:time_entries).sum("#{TimeEntry.table_name}.hours") |
|
257 | 257 | end |
|
258 | 258 | map_total(total) {|t| t.to_f.round(2)} |
|
259 | 259 | end |
|
260 | 260 | |
|
261 | 261 | # Returns the issues |
|
262 | 262 | # Valid options are :order, :offset, :limit, :include, :conditions |
|
263 | 263 | def issues(options={}) |
|
264 | 264 | order_option = [group_by_sort_order, options[:order]].flatten.reject(&:blank?) |
|
265 | 265 | |
|
266 | 266 | scope = Issue.visible. |
|
267 | 267 | joins(:status, :project). |
|
268 | 268 | where(statement). |
|
269 | 269 | includes(([:status, :project] + (options[:include] || [])).uniq). |
|
270 | 270 | where(options[:conditions]). |
|
271 | 271 | order(order_option). |
|
272 | 272 | joins(joins_for_order_statement(order_option.join(','))). |
|
273 | 273 | limit(options[:limit]). |
|
274 | 274 | offset(options[:offset]) |
|
275 | 275 | |
|
276 | scope = scope.preload(:custom_values) | |
|
277 |
if has_column? |
|
|
278 |
scope = scope.preload(: |
|
|
276 | scope = scope.preload([:tracker, :priority, :author, :assigned_to, :fixed_version, :category] & columns.map(&:name)) | |
|
277 | if has_custom_field_column? | |
|
278 | scope = scope.preload(:custom_values) | |
|
279 | 279 | end |
|
280 | 280 | |
|
281 | 281 | issues = scope.to_a |
|
282 | 282 | |
|
283 | 283 | if has_column?(:spent_hours) |
|
284 | 284 | Issue.load_visible_spent_hours(issues) |
|
285 | 285 | end |
|
286 | 286 | if has_column?(:total_spent_hours) |
|
287 | 287 | Issue.load_visible_total_spent_hours(issues) |
|
288 | 288 | end |
|
289 | 289 | if has_column?(:relations) |
|
290 | 290 | Issue.load_visible_relations(issues) |
|
291 | 291 | end |
|
292 | 292 | issues |
|
293 | 293 | rescue ::ActiveRecord::StatementInvalid => e |
|
294 | 294 | raise StatementInvalid.new(e.message) |
|
295 | 295 | end |
|
296 | 296 | |
|
297 | 297 | # Returns the issues ids |
|
298 | 298 | def issue_ids(options={}) |
|
299 | 299 | order_option = [group_by_sort_order, options[:order]].flatten.reject(&:blank?) |
|
300 | 300 | |
|
301 | 301 | Issue.visible. |
|
302 | 302 | joins(:status, :project). |
|
303 | 303 | where(statement). |
|
304 | 304 | includes(([:status, :project] + (options[:include] || [])).uniq). |
|
305 | 305 | references(([:status, :project] + (options[:include] || [])).uniq). |
|
306 | 306 | where(options[:conditions]). |
|
307 | 307 | order(order_option). |
|
308 | 308 | joins(joins_for_order_statement(order_option.join(','))). |
|
309 | 309 | limit(options[:limit]). |
|
310 | 310 | offset(options[:offset]). |
|
311 | 311 | pluck(:id) |
|
312 | 312 | rescue ::ActiveRecord::StatementInvalid => e |
|
313 | 313 | raise StatementInvalid.new(e.message) |
|
314 | 314 | end |
|
315 | 315 | |
|
316 | 316 | # Returns the journals |
|
317 | 317 | # Valid options are :order, :offset, :limit |
|
318 | 318 | def journals(options={}) |
|
319 | 319 | Journal.visible. |
|
320 | 320 | joins(:issue => [:project, :status]). |
|
321 | 321 | where(statement). |
|
322 | 322 | order(options[:order]). |
|
323 | 323 | limit(options[:limit]). |
|
324 | 324 | offset(options[:offset]). |
|
325 | 325 | preload(:details, :user, {:issue => [:project, :author, :tracker, :status]}). |
|
326 | 326 | to_a |
|
327 | 327 | rescue ::ActiveRecord::StatementInvalid => e |
|
328 | 328 | raise StatementInvalid.new(e.message) |
|
329 | 329 | end |
|
330 | 330 | |
|
331 | 331 | # Returns the versions |
|
332 | 332 | # Valid options are :conditions |
|
333 | 333 | def versions(options={}) |
|
334 | 334 | Version.visible. |
|
335 | 335 | where(project_statement). |
|
336 | 336 | where(options[:conditions]). |
|
337 | 337 | includes(:project). |
|
338 | 338 | references(:project). |
|
339 | 339 | to_a |
|
340 | 340 | rescue ::ActiveRecord::StatementInvalid => e |
|
341 | 341 | raise StatementInvalid.new(e.message) |
|
342 | 342 | end |
|
343 | 343 | |
|
344 | 344 | def sql_for_watcher_id_field(field, operator, value) |
|
345 | 345 | db_table = Watcher.table_name |
|
346 | 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 | 347 | sql_for_field(field, '=', value, db_table, 'user_id') + ')' |
|
348 | 348 | end |
|
349 | 349 | |
|
350 | 350 | def sql_for_member_of_group_field(field, operator, value) |
|
351 | 351 | if operator == '*' # Any group |
|
352 | 352 | groups = Group.givable |
|
353 | 353 | operator = '=' # Override the operator since we want to find by assigned_to |
|
354 | 354 | elsif operator == "!*" |
|
355 | 355 | groups = Group.givable |
|
356 | 356 | operator = '!' # Override the operator since we want to find by assigned_to |
|
357 | 357 | else |
|
358 | 358 | groups = Group.where(:id => value).to_a |
|
359 | 359 | end |
|
360 | 360 | groups ||= [] |
|
361 | 361 | |
|
362 | 362 | members_of_groups = groups.inject([]) {|user_ids, group| |
|
363 | 363 | user_ids + group.user_ids + [group.id] |
|
364 | 364 | }.uniq.compact.sort.collect(&:to_s) |
|
365 | 365 | |
|
366 | 366 | '(' + sql_for_field("assigned_to_id", operator, members_of_groups, Issue.table_name, "assigned_to_id", false) + ')' |
|
367 | 367 | end |
|
368 | 368 | |
|
369 | 369 | def sql_for_assigned_to_role_field(field, operator, value) |
|
370 | 370 | case operator |
|
371 | 371 | when "*", "!*" # Member / Not member |
|
372 | 372 | sw = operator == "!*" ? 'NOT' : '' |
|
373 | 373 | nl = operator == "!*" ? "#{Issue.table_name}.assigned_to_id IS NULL OR" : '' |
|
374 | 374 | "(#{nl} #{Issue.table_name}.assigned_to_id #{sw} IN (SELECT DISTINCT #{Member.table_name}.user_id FROM #{Member.table_name}" + |
|
375 | 375 | " WHERE #{Member.table_name}.project_id = #{Issue.table_name}.project_id))" |
|
376 | 376 | when "=", "!" |
|
377 | 377 | role_cond = value.any? ? |
|
378 | 378 | "#{MemberRole.table_name}.role_id IN (" + value.collect{|val| "'#{self.class.connection.quote_string(val)}'"}.join(",") + ")" : |
|
379 | 379 | "1=0" |
|
380 | 380 | |
|
381 | 381 | sw = operator == "!" ? 'NOT' : '' |
|
382 | 382 | nl = operator == "!" ? "#{Issue.table_name}.assigned_to_id IS NULL OR" : '' |
|
383 | 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 | 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 | 385 | end |
|
386 | 386 | end |
|
387 | 387 | |
|
388 | 388 | def sql_for_fixed_version_status_field(field, operator, value) |
|
389 | 389 | where = sql_for_field(field, operator, value, Version.table_name, "status") |
|
390 | 390 | version_ids = versions(:conditions => [where]).map(&:id) |
|
391 | 391 | |
|
392 | 392 | nl = operator == "!" ? "#{Issue.table_name}.fixed_version_id IS NULL OR" : '' |
|
393 | 393 | "(#{nl} #{sql_for_field("fixed_version_id", "=", version_ids, Issue.table_name, "fixed_version_id")})" |
|
394 | 394 | end |
|
395 | 395 | |
|
396 | 396 | def sql_for_fixed_version_due_date_field(field, operator, value) |
|
397 | 397 | where = sql_for_field(field, operator, value, Version.table_name, "effective_date") |
|
398 | 398 | version_ids = versions(:conditions => [where]).map(&:id) |
|
399 | 399 | |
|
400 | 400 | nl = operator == "!*" ? "#{Issue.table_name}.fixed_version_id IS NULL OR" : '' |
|
401 | 401 | "(#{nl} #{sql_for_field("fixed_version_id", "=", version_ids, Issue.table_name, "fixed_version_id")})" |
|
402 | 402 | end |
|
403 | 403 | |
|
404 | 404 | def sql_for_is_private_field(field, operator, value) |
|
405 | 405 | op = (operator == "=" ? 'IN' : 'NOT IN') |
|
406 | 406 | va = value.map {|v| v == '0' ? self.class.connection.quoted_false : self.class.connection.quoted_true}.uniq.join(',') |
|
407 | 407 | |
|
408 | 408 | "#{Issue.table_name}.is_private #{op} (#{va})" |
|
409 | 409 | end |
|
410 | 410 | |
|
411 | 411 | def sql_for_parent_id_field(field, operator, value) |
|
412 | 412 | case operator |
|
413 | 413 | when "=" |
|
414 | 414 | "#{Issue.table_name}.parent_id = #{value.first.to_i}" |
|
415 | 415 | when "~" |
|
416 | 416 | root_id, lft, rgt = Issue.where(:id => value.first.to_i).pluck(:root_id, :lft, :rgt).first |
|
417 | 417 | if root_id && lft && rgt |
|
418 | 418 | "#{Issue.table_name}.root_id = #{root_id} AND #{Issue.table_name}.lft > #{lft} AND #{Issue.table_name}.rgt < #{rgt}" |
|
419 | 419 | else |
|
420 | 420 | "1=0" |
|
421 | 421 | end |
|
422 | 422 | when "!*" |
|
423 | 423 | "#{Issue.table_name}.parent_id IS NULL" |
|
424 | 424 | when "*" |
|
425 | 425 | "#{Issue.table_name}.parent_id IS NOT NULL" |
|
426 | 426 | end |
|
427 | 427 | end |
|
428 | 428 | |
|
429 | 429 | def sql_for_child_id_field(field, operator, value) |
|
430 | 430 | case operator |
|
431 | 431 | when "=" |
|
432 | 432 | parent_id = Issue.where(:id => value.first.to_i).pluck(:parent_id).first |
|
433 | 433 | if parent_id |
|
434 | 434 | "#{Issue.table_name}.id = #{parent_id}" |
|
435 | 435 | else |
|
436 | 436 | "1=0" |
|
437 | 437 | end |
|
438 | 438 | when "~" |
|
439 | 439 | root_id, lft, rgt = Issue.where(:id => value.first.to_i).pluck(:root_id, :lft, :rgt).first |
|
440 | 440 | if root_id && lft && rgt |
|
441 | 441 | "#{Issue.table_name}.root_id = #{root_id} AND #{Issue.table_name}.lft < #{lft} AND #{Issue.table_name}.rgt > #{rgt}" |
|
442 | 442 | else |
|
443 | 443 | "1=0" |
|
444 | 444 | end |
|
445 | 445 | when "!*" |
|
446 | 446 | "#{Issue.table_name}.rgt - #{Issue.table_name}.lft = 1" |
|
447 | 447 | when "*" |
|
448 | 448 | "#{Issue.table_name}.rgt - #{Issue.table_name}.lft > 1" |
|
449 | 449 | end |
|
450 | 450 | end |
|
451 | 451 | |
|
452 | 452 | def sql_for_issue_id_field(field, operator, value) |
|
453 | 453 | if operator == "=" |
|
454 | 454 | # accepts a comma separated list of ids |
|
455 | 455 | ids = value.first.to_s.scan(/\d+/).map(&:to_i) |
|
456 | 456 | if ids.present? |
|
457 | 457 | "#{Issue.table_name}.id IN (#{ids.join(",")})" |
|
458 | 458 | else |
|
459 | 459 | "1=0" |
|
460 | 460 | end |
|
461 | 461 | else |
|
462 | 462 | sql_for_field("id", operator, value, Issue.table_name, "id") |
|
463 | 463 | end |
|
464 | 464 | end |
|
465 | 465 | |
|
466 | 466 | def sql_for_relations(field, operator, value, options={}) |
|
467 | 467 | relation_options = IssueRelation::TYPES[field] |
|
468 | 468 | return relation_options unless relation_options |
|
469 | 469 | |
|
470 | 470 | relation_type = field |
|
471 | 471 | join_column, target_join_column = "issue_from_id", "issue_to_id" |
|
472 | 472 | if relation_options[:reverse] || options[:reverse] |
|
473 | 473 | relation_type = relation_options[:reverse] || relation_type |
|
474 | 474 | join_column, target_join_column = target_join_column, join_column |
|
475 | 475 | end |
|
476 | 476 | |
|
477 | 477 | sql = case operator |
|
478 | 478 | when "*", "!*" |
|
479 | 479 | op = (operator == "*" ? 'IN' : 'NOT IN') |
|
480 | 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 | 481 | when "=", "!" |
|
482 | 482 | op = (operator == "=" ? 'IN' : 'NOT IN') |
|
483 | 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 | 484 | when "=p", "=!p", "!p" |
|
485 | 485 | op = (operator == "!p" ? 'NOT IN' : 'IN') |
|
486 | 486 | comp = (operator == "=!p" ? '<>' : '=') |
|
487 | 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 | 488 | when "*o", "!o" |
|
489 | 489 | op = (operator == "!o" ? 'NOT IN' : 'IN') |
|
490 | 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 | 491 | end |
|
492 | 492 | |
|
493 | 493 | if relation_options[:sym] == field && !options[:reverse] |
|
494 | 494 | sqls = [sql, sql_for_relations(field, operator, value, :reverse => true)] |
|
495 | 495 | sql = sqls.join(["!", "!*", "!p"].include?(operator) ? " AND " : " OR ") |
|
496 | 496 | end |
|
497 | 497 | "(#{sql})" |
|
498 | 498 | end |
|
499 | 499 | |
|
500 | 500 | def find_assigned_to_id_filter_values(values) |
|
501 | 501 | Principal.visible.where(:id => values).map {|p| [p.name, p.id.to_s]} |
|
502 | 502 | end |
|
503 | 503 | alias :find_author_id_filter_values :find_assigned_to_id_filter_values |
|
504 | 504 | |
|
505 | 505 | IssueRelation::TYPES.keys.each do |relation_type| |
|
506 | 506 | alias_method "sql_for_#{relation_type}_field".to_sym, :sql_for_relations |
|
507 | 507 | end |
|
508 | 508 | |
|
509 | 509 | def joins_for_order_statement(order_options) |
|
510 | 510 | joins = [super] |
|
511 | 511 | |
|
512 | 512 | if order_options |
|
513 | 513 | if order_options.include?('authors') |
|
514 | 514 | joins << "LEFT OUTER JOIN #{User.table_name} authors ON authors.id = #{queried_table_name}.author_id" |
|
515 | 515 | end |
|
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" | |
|
518 | end | |
|
519 | if order_options.include?('fixed_version') | |
|
520 | joins << "LEFT OUTER JOIN #{Version.table_name} ON #{Version.table_name}.id = #{queried_table_name}.fixed_version_id" | |
|
521 | end | |
|
522 | if order_options.include?('category') | |
|
523 | joins << "LEFT OUTER JOIN #{IssueCategory.table_name} ON #{IssueCategory.table_name}.id = #{queried_table_name}.category_id" | |
|
524 | end | |
|
525 | if order_options.include?('tracker') | |
|
526 | joins << "LEFT OUTER JOIN #{Tracker.table_name} ON #{Tracker.table_name}.id = #{queried_table_name}.tracker_id" | |
|
527 | end | |
|
528 | if order_options.include?('enumeration') | |
|
529 | joins << "LEFT OUTER JOIN #{IssuePriority.table_name} ON #{IssuePriority.table_name}.id = #{queried_table_name}.priority_id" | |
|
530 | end | |
|
516 | 531 | end |
|
517 | 532 | |
|
518 | 533 | joins.any? ? joins.join(' ') : nil |
|
519 | 534 | end |
|
520 | 535 | end |
General Comments 0
You need to be logged in to leave comments.
Login now