##// END OF EJS Templates
Merged r14268 (#19815)....
Jean-Philippe Lang -
r13896:84a40b16f18c
parent child
Show More

The requested changes are too big and content was truncated. Show full diff

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