##// END OF EJS Templates
Updated code comments....
Jean-Philippe Lang -
r13618:8534fc04a0fc
parent child
Show More
@@ -1,515 +1,515
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2015 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 menu_item :new_issue, :only => [:new, :create]
20 20 default_search_scope :issues
21 21
22 22 before_filter :find_issue, :only => [:show, :edit, :update]
23 23 before_filter :find_issues, :only => [:bulk_edit, :bulk_update, :destroy]
24 24 before_filter :authorize, :except => [:index, :new, :create]
25 25 before_filter :find_optional_project, :only => [:index, :new, :create]
26 26 before_filter :build_new_issue_from_params, :only => [:new, :create]
27 27 accept_rss_auth :index, :show
28 28 accept_api_auth :index, :show, :create, :update, :destroy
29 29
30 30 rescue_from Query::StatementInvalid, :with => :query_statement_invalid
31 31
32 32 helper :journals
33 33 helper :projects
34 34 include ProjectsHelper
35 35 helper :custom_fields
36 36 include CustomFieldsHelper
37 37 helper :issue_relations
38 38 include IssueRelationsHelper
39 39 helper :watchers
40 40 include WatchersHelper
41 41 helper :attachments
42 42 include AttachmentsHelper
43 43 helper :queries
44 44 include QueriesHelper
45 45 helper :repositories
46 46 include RepositoriesHelper
47 47 helper :sort
48 48 include SortHelper
49 49 include IssuesHelper
50 50 helper :timelog
51 51
52 52 def index
53 53 retrieve_query
54 54 sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
55 55 sort_update(@query.sortable_columns)
56 56 @query.sort_criteria = sort_criteria.to_a
57 57
58 58 if @query.valid?
59 59 case params[:format]
60 60 when 'csv', 'pdf'
61 61 @limit = Setting.issues_export_limit.to_i
62 62 if params[:columns] == 'all'
63 63 @query.column_names = @query.available_inline_columns.map(&:name)
64 64 end
65 65 when 'atom'
66 66 @limit = Setting.feeds_limit.to_i
67 67 when 'xml', 'json'
68 68 @offset, @limit = api_offset_and_limit
69 69 @query.column_names = %w(author)
70 70 else
71 71 @limit = per_page_option
72 72 end
73 73
74 74 @issue_count = @query.issue_count
75 75 @issue_pages = Paginator.new @issue_count, @limit, params['page']
76 76 @offset ||= @issue_pages.offset
77 77 @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version],
78 78 :order => sort_clause,
79 79 :offset => @offset,
80 80 :limit => @limit)
81 81 @issue_count_by_group = @query.issue_count_by_group
82 82
83 83 respond_to do |format|
84 84 format.html { render :template => 'issues/index', :layout => !request.xhr? }
85 85 format.api {
86 86 Issue.load_visible_relations(@issues) if include_in_api_response?('relations')
87 87 }
88 88 format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") }
89 89 format.csv { send_data(query_to_csv(@issues, @query, params), :type => 'text/csv; header=present', :filename => 'issues.csv') }
90 90 format.pdf { send_file_headers! :type => 'application/pdf', :filename => 'issues.pdf' }
91 91 end
92 92 else
93 93 respond_to do |format|
94 94 format.html { render(:template => 'issues/index', :layout => !request.xhr?) }
95 95 format.any(:atom, :csv, :pdf) { render(:nothing => true) }
96 96 format.api { render_validation_errors(@query) }
97 97 end
98 98 end
99 99 rescue ActiveRecord::RecordNotFound
100 100 render_404
101 101 end
102 102
103 103 def show
104 104 @journals = @issue.journals.includes(:user, :details).
105 105 references(:user, :details).
106 106 reorder("#{Journal.table_name}.id ASC").to_a
107 107 @journals.each_with_index {|j,i| j.indice = i+1}
108 108 @journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project)
109 109 Journal.preload_journals_details_custom_fields(@journals)
110 110 @journals.select! {|journal| journal.notes? || journal.visible_details.any?}
111 111 @journals.reverse! if User.current.wants_comments_in_reverse_order?
112 112
113 113 @changesets = @issue.changesets.visible.to_a
114 114 @changesets.reverse! if User.current.wants_comments_in_reverse_order?
115 115
116 116 @relations = @issue.relations.select {|r| r.other_issue(@issue) && r.other_issue(@issue).visible? }
117 117 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
118 118 @priorities = IssuePriority.active
119 119 @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
120 120 @relation = IssueRelation.new
121 121
122 122 respond_to do |format|
123 123 format.html {
124 124 retrieve_previous_and_next_issue_ids
125 125 render :template => 'issues/show'
126 126 }
127 127 format.api
128 128 format.atom { render :template => 'journals/index', :layout => false, :content_type => 'application/atom+xml' }
129 129 format.pdf {
130 130 send_file_headers! :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf"
131 131 }
132 132 end
133 133 end
134 134
135 # Add a new issue
136 # The new issue will be created from an existing one if copy_from parameter is given
137 135 def new
138 136 respond_to do |format|
139 137 format.html { render :action => 'new', :layout => !request.xhr? }
140 138 format.js
141 139 end
142 140 end
143 141
144 142 def create
145 143 unless User.current.allowed_to?(:add_issues, @issue.project)
146 144 raise ::Unauthorized
147 145 end
148 146 call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue })
149 147 @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads]))
150 148 if @issue.save
151 149 call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue})
152 150 respond_to do |format|
153 151 format.html {
154 152 render_attachment_warning_if_needed(@issue)
155 153 flash[:notice] = l(:notice_issue_successful_create, :id => view_context.link_to("##{@issue.id}", issue_path(@issue), :title => @issue.subject))
156 154 redirect_after_create
157 155 }
158 156 format.api { render :action => 'show', :status => :created, :location => issue_url(@issue) }
159 157 end
160 158 return
161 159 else
162 160 respond_to do |format|
163 161 format.html { render :action => 'new' }
164 162 format.api { render_validation_errors(@issue) }
165 163 end
166 164 end
167 165 end
168 166
169 167 def edit
170 168 return unless update_issue_from_params
171 169
172 170 respond_to do |format|
173 171 format.html { }
174 172 format.js
175 173 end
176 174 end
177 175
178 176 def update
179 177 return unless update_issue_from_params
180 178 @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads]))
181 179 saved = false
182 180 begin
183 181 saved = save_issue_with_child_records
184 182 rescue ActiveRecord::StaleObjectError
185 183 @conflict = true
186 184 if params[:last_journal_id]
187 185 @conflict_journals = @issue.journals_after(params[:last_journal_id]).to_a
188 186 @conflict_journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project)
189 187 end
190 188 end
191 189
192 190 if saved
193 191 render_attachment_warning_if_needed(@issue)
194 192 flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
195 193
196 194 respond_to do |format|
197 195 format.html { redirect_back_or_default issue_path(@issue) }
198 196 format.api { render_api_ok }
199 197 end
200 198 else
201 199 respond_to do |format|
202 200 format.html { render :action => 'edit' }
203 201 format.api { render_validation_errors(@issue) }
204 202 end
205 203 end
206 204 end
207 205
208 206 # Bulk edit/copy a set of issues
209 207 def bulk_edit
210 208 @issues.sort!
211 209 @copy = params[:copy].present?
212 210 @notes = params[:notes]
213 211
214 212 if @copy
215 213 unless User.current.allowed_to?(:copy_issues, @projects)
216 214 raise ::Unauthorized
217 215 end
218 216 end
219 217
220 218 @allowed_projects = Issue.allowed_target_projects
221 219 if params[:issue]
222 220 @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:issue][:project_id].to_s}
223 221 if @target_project
224 222 target_projects = [@target_project]
225 223 end
226 224 end
227 225 target_projects ||= @projects
228 226
229 227 if @copy
230 228 # Copied issues will get their default statuses
231 229 @available_statuses = []
232 230 else
233 231 @available_statuses = @issues.map(&:new_statuses_allowed_to).reduce(:&)
234 232 end
235 233 @custom_fields = target_projects.map{|p|p.all_issue_custom_fields.visible}.reduce(:&)
236 234 @assignables = target_projects.map(&:assignable_users).reduce(:&)
237 235 @trackers = target_projects.map(&:trackers).reduce(:&)
238 236 @versions = target_projects.map {|p| p.shared_versions.open}.reduce(:&)
239 237 @categories = target_projects.map {|p| p.issue_categories}.reduce(:&)
240 238 if @copy
241 239 @attachments_present = @issues.detect {|i| i.attachments.any?}.present?
242 240 @subtasks_present = @issues.detect {|i| !i.leaf?}.present?
243 241 end
244 242
245 243 @safe_attributes = @issues.map(&:safe_attribute_names).reduce(:&)
246 244
247 245 @issue_params = params[:issue] || {}
248 246 @issue_params[:custom_field_values] ||= {}
249 247 end
250 248
251 249 def bulk_update
252 250 @issues.sort!
253 251 @copy = params[:copy].present?
254 252 attributes = parse_params_for_bulk_issue_attributes(params)
255 253
256 254 if @copy
257 255 unless User.current.allowed_to?(:copy_issues, @projects)
258 256 raise ::Unauthorized
259 257 end
260 258 target_projects = @projects
261 259 if attributes['project_id'].present?
262 260 target_projects = Project.where(:id => attributes['project_id']).to_a
263 261 end
264 262 unless User.current.allowed_to?(:add_issues, target_projects)
265 263 raise ::Unauthorized
266 264 end
267 265 end
268 266
269 267 unsaved_issues = []
270 268 saved_issues = []
271 269
272 270 if @copy && params[:copy_subtasks].present?
273 271 # Descendant issues will be copied with the parent task
274 272 # Don't copy them twice
275 273 @issues.reject! {|issue| @issues.detect {|other| issue.is_descendant_of?(other)}}
276 274 end
277 275
278 276 @issues.each do |orig_issue|
279 277 orig_issue.reload
280 278 if @copy
281 279 issue = orig_issue.copy({},
282 280 :attachments => params[:copy_attachments].present?,
283 281 :subtasks => params[:copy_subtasks].present?,
284 282 :link => link_copy?(params[:link_copy])
285 283 )
286 284 else
287 285 issue = orig_issue
288 286 end
289 287 journal = issue.init_journal(User.current, params[:notes])
290 288 issue.safe_attributes = attributes
291 289 call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue })
292 290 if issue.save
293 291 saved_issues << issue
294 292 else
295 293 unsaved_issues << orig_issue
296 294 end
297 295 end
298 296
299 297 if unsaved_issues.empty?
300 298 flash[:notice] = l(:notice_successful_update) unless saved_issues.empty?
301 299 if params[:follow]
302 300 if @issues.size == 1 && saved_issues.size == 1
303 301 redirect_to issue_path(saved_issues.first)
304 302 elsif saved_issues.map(&:project).uniq.size == 1
305 303 redirect_to project_issues_path(saved_issues.map(&:project).first)
306 304 end
307 305 else
308 306 redirect_back_or_default _project_issues_path(@project)
309 307 end
310 308 else
311 309 @saved_issues = @issues
312 310 @unsaved_issues = unsaved_issues
313 311 @issues = Issue.visible.where(:id => @unsaved_issues.map(&:id)).to_a
314 312 bulk_edit
315 313 render :action => 'bulk_edit'
316 314 end
317 315 end
318 316
319 317 def destroy
320 318 @hours = TimeEntry.where(:issue_id => @issues.map(&:id)).sum(:hours).to_f
321 319 if @hours > 0
322 320 case params[:todo]
323 321 when 'destroy'
324 322 # nothing to do
325 323 when 'nullify'
326 324 TimeEntry.where(['issue_id IN (?)', @issues]).update_all('issue_id = NULL')
327 325 when 'reassign'
328 326 reassign_to = @project.issues.find_by_id(params[:reassign_to_id])
329 327 if reassign_to.nil?
330 328 flash.now[:error] = l(:error_issue_not_found_in_project)
331 329 return
332 330 else
333 331 TimeEntry.where(['issue_id IN (?)', @issues]).
334 332 update_all("issue_id = #{reassign_to.id}")
335 333 end
336 334 else
337 335 # display the destroy form if it's a user request
338 336 return unless api_request?
339 337 end
340 338 end
341 339 @issues.each do |issue|
342 340 begin
343 341 issue.reload.destroy
344 342 rescue ::ActiveRecord::RecordNotFound # raised by #reload if issue no longer exists
345 343 # nothing to do, issue was already deleted (eg. by a parent)
346 344 end
347 345 end
348 346 respond_to do |format|
349 347 format.html { redirect_back_or_default _project_issues_path(@project) }
350 348 format.api { render_api_ok }
351 349 end
352 350 end
353 351
354 352 private
355 353
356 354 def retrieve_previous_and_next_issue_ids
357 355 retrieve_query_from_session
358 356 if @query
359 357 sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
360 358 sort_update(@query.sortable_columns, 'issues_index_sort')
361 359 limit = 500
362 360 issue_ids = @query.issue_ids(:order => sort_clause, :limit => (limit + 1), :include => [:assigned_to, :tracker, :priority, :category, :fixed_version])
363 361 if (idx = issue_ids.index(@issue.id)) && idx < limit
364 362 if issue_ids.size < 500
365 363 @issue_position = idx + 1
366 364 @issue_count = issue_ids.size
367 365 end
368 366 @prev_issue_id = issue_ids[idx - 1] if idx > 0
369 367 @next_issue_id = issue_ids[idx + 1] if idx < (issue_ids.size - 1)
370 368 end
371 369 end
372 370 end
373 371
374 372 # Used by #edit and #update to set some common instance variables
375 373 # from the params
376 # TODO: Refactor, not everything in here is needed by #edit
377 374 def update_issue_from_params
378 375 @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
379 376 if params[:time_entry]
380 377 @time_entry.attributes = params[:time_entry]
381 378 end
382 379
383 380 @issue.init_journal(User.current)
384 381
385 382 issue_attributes = params[:issue]
386 383 if issue_attributes && params[:conflict_resolution]
387 384 case params[:conflict_resolution]
388 385 when 'overwrite'
389 386 issue_attributes = issue_attributes.dup
390 387 issue_attributes.delete(:lock_version)
391 388 when 'add_notes'
392 389 issue_attributes = issue_attributes.slice(:notes)
393 390 when 'cancel'
394 391 redirect_to issue_path(@issue)
395 392 return false
396 393 end
397 394 end
398 395 @issue.safe_attributes = issue_attributes
399 396 @priorities = IssuePriority.active
400 397 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
401 398 true
402 399 end
403 400
404 # TODO: Refactor, lots of extra code in here
401 # Used by #new and #create to build a new issue from the params
402 # The new issue will be copied from an existing one if copy_from parameter is given
405 403 def build_new_issue_from_params
406 404 @issue = Issue.new
407 405 if params[:copy_from]
408 406 begin
409 407 @issue.init_journal(User.current)
410 408 @copy_from = Issue.visible.find(params[:copy_from])
411 409 unless User.current.allowed_to?(:copy_issues, @copy_from.project)
412 410 raise ::Unauthorized
413 411 end
414 412 @link_copy = link_copy?(params[:link_copy]) || request.get?
415 413 @copy_attachments = params[:copy_attachments].present? || request.get?
416 414 @copy_subtasks = params[:copy_subtasks].present? || request.get?
417 415 @issue.copy_from(@copy_from, :attachments => @copy_attachments, :subtasks => @copy_subtasks, :link => @link_copy)
418 416 rescue ActiveRecord::RecordNotFound
419 417 render_404
420 418 return
421 419 end
422 420 end
423 421 @issue.project = @project
424 422 if request.get?
425 423 @issue.project ||= @issue.allowed_target_projects.first
426 424 end
427 425 @issue.author ||= User.current
428 426 @issue.start_date ||= Date.today if Setting.default_issue_start_date_to_creation_date?
429 427
430 428 if attrs = params[:issue].deep_dup
431 429 if params[:was_default_status] == attrs[:status_id]
432 430 attrs.delete(:status_id)
433 431 end
434 432 @issue.safe_attributes = attrs
435 433 end
436 434 if @issue.project
437 435 @issue.tracker ||= @issue.project.trackers.first
438 436 if @issue.tracker.nil?
439 437 render_error l(:error_no_tracker_in_project)
440 438 return false
441 439 end
442 440 if @issue.status.nil?
443 441 render_error l(:error_no_default_issue_status)
444 442 return false
445 443 end
446 444 end
447 445
448 446 @priorities = IssuePriority.active
449 447 @allowed_statuses = @issue.new_statuses_allowed_to(User.current, @issue.new_record?)
450 448 end
451 449
452 450 def parse_params_for_bulk_issue_attributes(params)
453 451 attributes = (params[:issue] || {}).reject {|k,v| v.blank?}
454 452 attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'}
455 453 if custom = attributes[:custom_field_values]
456 454 custom.reject! {|k,v| v.blank?}
457 455 custom.keys.each do |k|
458 456 if custom[k].is_a?(Array)
459 457 custom[k] << '' if custom[k].delete('__none__')
460 458 else
461 459 custom[k] = '' if custom[k] == '__none__'
462 460 end
463 461 end
464 462 end
465 463 attributes
466 464 end
467 465
468 466 # Saves @issue and a time_entry from the parameters
469 467 def save_issue_with_child_records
470 468 Issue.transaction do
471 469 if params[:time_entry] && (params[:time_entry][:hours].present? || params[:time_entry][:comments].present?) && User.current.allowed_to?(:log_time, @issue.project)
472 470 time_entry = @time_entry || TimeEntry.new
473 471 time_entry.project = @issue.project
474 472 time_entry.issue = @issue
475 473 time_entry.user = User.current
476 474 time_entry.spent_on = User.current.today
477 475 time_entry.attributes = params[:time_entry]
478 476 @issue.time_entries << time_entry
479 477 end
480 478
481 479 call_hook(:controller_issues_edit_before_save, { :params => params, :issue => @issue, :time_entry => time_entry, :journal => @issue.current_journal})
482 480 if @issue.save
483 481 call_hook(:controller_issues_edit_after_save, { :params => params, :issue => @issue, :time_entry => time_entry, :journal => @issue.current_journal})
484 482 else
485 483 raise ActiveRecord::Rollback
486 484 end
487 485 end
488 486 end
489 487
488 # Returns true if the issue copy should be linked
489 # to the original issue
490 490 def link_copy?(param)
491 491 case Setting.link_copied_issue
492 492 when 'yes'
493 493 true
494 494 when 'no'
495 495 false
496 496 when 'ask'
497 497 param == '1'
498 498 end
499 499 end
500 500
501 501 # Redirects user after a successful issue creation
502 502 def redirect_after_create
503 503 if params[:continue]
504 504 attrs = {:tracker_id => @issue.tracker, :parent_issue_id => @issue.parent_issue_id}.reject {|k,v| v.nil?}
505 505 if params[:project_id]
506 506 redirect_to new_project_issue_path(@issue.project, :issue => attrs)
507 507 else
508 508 attrs.merge! :project_id => @issue.project_id
509 509 redirect_to new_issue_path(:issue => attrs)
510 510 end
511 511 else
512 512 redirect_to issue_path(@issue)
513 513 end
514 514 end
515 515 end
General Comments 0
You need to be logged in to leave comments. Login now