##// END OF EJS Templates
Do not copy subtasks twice when copying an issue and its descendants (#6965)....
Jean-Philippe Lang -
r10146:b72d40a4292a
parent child
Show More

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

@@ -1,431 +1,438
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2012 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]
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]
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
60 60 if @query.valid?
61 61 case params[:format]
62 62 when 'csv', 'pdf'
63 63 @limit = Setting.issues_export_limit.to_i
64 64 when 'atom'
65 65 @limit = Setting.feeds_limit.to_i
66 66 when 'xml', 'json'
67 67 @offset, @limit = api_offset_and_limit
68 68 else
69 69 @limit = per_page_option
70 70 end
71 71
72 72 @issue_count = @query.issue_count
73 73 @issue_pages = Paginator.new self, @issue_count, @limit, params['page']
74 74 @offset ||= @issue_pages.current.offset
75 75 @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version],
76 76 :order => sort_clause,
77 77 :offset => @offset,
78 78 :limit => @limit)
79 79 @issue_count_by_group = @query.issue_count_by_group
80 80
81 81 respond_to do |format|
82 82 format.html { render :template => 'issues/index', :layout => !request.xhr? }
83 83 format.api {
84 84 Issue.load_relations(@issues) if include_in_api_response?('relations')
85 85 }
86 86 format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") }
87 87 format.csv { send_data(issues_to_csv(@issues, @project, @query, params), :type => 'text/csv; header=present', :filename => 'export.csv') }
88 88 format.pdf { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'export.pdf') }
89 89 end
90 90 else
91 91 respond_to do |format|
92 92 format.html { render(:template => 'issues/index', :layout => !request.xhr?) }
93 93 format.any(:atom, :csv, :pdf) { render(:nothing => true) }
94 94 format.api { render_validation_errors(@query) }
95 95 end
96 96 end
97 97 rescue ActiveRecord::RecordNotFound
98 98 render_404
99 99 end
100 100
101 101 def show
102 102 @journals = @issue.journals.find(:all, :include => [:user, :details], :order => "#{Journal.table_name}.created_on ASC")
103 103 @journals.each_with_index {|j,i| j.indice = i+1}
104 104 @journals.reverse! if User.current.wants_comments_in_reverse_order?
105 105
106 106 @changesets = @issue.changesets.visible.all
107 107 @changesets.reverse! if User.current.wants_comments_in_reverse_order?
108 108
109 109 @relations = @issue.relations.select {|r| r.other_issue(@issue) && r.other_issue(@issue).visible? }
110 110 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
111 111 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
112 112 @priorities = IssuePriority.active
113 113 @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
114 114 respond_to do |format|
115 115 format.html {
116 116 retrieve_previous_and_next_issue_ids
117 117 render :template => 'issues/show'
118 118 }
119 119 format.api
120 120 format.atom { render :template => 'journals/index', :layout => false, :content_type => 'application/atom+xml' }
121 121 format.pdf { send_data(issue_to_pdf(@issue), :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf") }
122 122 end
123 123 end
124 124
125 125 # Add a new issue
126 126 # The new issue will be created from an existing one if copy_from parameter is given
127 127 def new
128 128 respond_to do |format|
129 129 format.html { render :action => 'new', :layout => !request.xhr? }
130 130 format.js { render :partial => 'update_form' }
131 131 end
132 132 end
133 133
134 134 def create
135 135 call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue })
136 136 @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads]))
137 137 if @issue.save
138 138 call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue})
139 139 respond_to do |format|
140 140 format.html {
141 141 render_attachment_warning_if_needed(@issue)
142 142 flash[:notice] = l(:notice_issue_successful_create, :id => view_context.link_to("##{@issue.id}", issue_path(@issue), :title => @issue.subject))
143 143 redirect_to(params[:continue] ? { :action => 'new', :project_id => @issue.project, :issue => {:tracker_id => @issue.tracker, :parent_issue_id => @issue.parent_issue_id}.reject {|k,v| v.nil?} } :
144 144 { :action => 'show', :id => @issue })
145 145 }
146 146 format.api { render :action => 'show', :status => :created, :location => issue_url(@issue) }
147 147 end
148 148 return
149 149 else
150 150 respond_to do |format|
151 151 format.html { render :action => 'new' }
152 152 format.api { render_validation_errors(@issue) }
153 153 end
154 154 end
155 155 end
156 156
157 157 def edit
158 158 return unless update_issue_from_params
159 159
160 160 respond_to do |format|
161 161 format.html { }
162 162 format.xml { }
163 163 end
164 164 end
165 165
166 166 def update
167 167 return unless update_issue_from_params
168 168 @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads]))
169 169 saved = false
170 170 begin
171 171 saved = @issue.save_issue_with_child_records(params, @time_entry)
172 172 rescue ActiveRecord::StaleObjectError
173 173 @conflict = true
174 174 if params[:last_journal_id]
175 175 @conflict_journals = @issue.journals_after(params[:last_journal_id]).all
176 176 end
177 177 end
178 178
179 179 if saved
180 180 render_attachment_warning_if_needed(@issue)
181 181 flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
182 182
183 183 respond_to do |format|
184 184 format.html { redirect_back_or_default({:action => 'show', :id => @issue}) }
185 185 format.api { render_api_ok }
186 186 end
187 187 else
188 188 respond_to do |format|
189 189 format.html { render :action => 'edit' }
190 190 format.api { render_validation_errors(@issue) }
191 191 end
192 192 end
193 193 end
194 194
195 195 # Bulk edit/copy a set of issues
196 196 def bulk_edit
197 197 @issues.sort!
198 198 @copy = params[:copy].present?
199 199 @notes = params[:notes]
200 200
201 201 if User.current.allowed_to?(:move_issues, @projects)
202 202 @allowed_projects = Issue.allowed_target_projects_on_move
203 203 if params[:issue]
204 204 @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:issue][:project_id].to_s}
205 205 if @target_project
206 206 target_projects = [@target_project]
207 207 end
208 208 end
209 209 end
210 210 target_projects ||= @projects
211 211
212 212 if @copy
213 213 @available_statuses = [IssueStatus.default]
214 214 else
215 215 @available_statuses = @issues.map(&:new_statuses_allowed_to).reduce(:&)
216 216 end
217 217 @custom_fields = target_projects.map{|p|p.all_issue_custom_fields}.reduce(:&)
218 218 @assignables = target_projects.map(&:assignable_users).reduce(:&)
219 219 @trackers = target_projects.map(&:trackers).reduce(:&)
220 220 @versions = target_projects.map {|p| p.shared_versions.open}.reduce(:&)
221 221 @categories = target_projects.map {|p| p.issue_categories}.reduce(:&)
222 222 if @copy
223 223 @attachments_present = @issues.detect {|i| i.attachments.any?}.present?
224 224 @subtasks_present = @issues.detect {|i| !i.leaf?}.present?
225 225 end
226 226
227 227 @safe_attributes = @issues.map(&:safe_attribute_names).reduce(:&)
228 228 render :layout => false if request.xhr?
229 229 end
230 230
231 231 def bulk_update
232 232 @issues.sort!
233 233 @copy = params[:copy].present?
234 234 attributes = parse_params_for_bulk_issue_attributes(params)
235 235
236 236 unsaved_issue_ids = []
237 237 moved_issues = []
238
239 if @copy && params[:copy_subtasks].present?
240 # Descendant issues will be copied with the parent task
241 # Don't copy them twice
242 @issues.reject! {|issue| @issues.detect {|other| issue.is_descendant_of?(other)}}
243 end
244
238 245 @issues.each do |issue|
239 246 issue.reload
240 247 if @copy
241 248 issue = issue.copy({},
242 249 :attachments => params[:copy_attachments].present?,
243 250 :subtasks => params[:copy_subtasks].present?
244 251 )
245 252 end
246 253 journal = issue.init_journal(User.current, params[:notes])
247 254 issue.safe_attributes = attributes
248 255 call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue })
249 256 if issue.save
250 257 moved_issues << issue
251 258 else
252 259 # Keep unsaved issue ids to display them in flash error
253 260 unsaved_issue_ids << issue.id
254 261 end
255 262 end
256 263 set_flash_from_bulk_issue_save(@issues, unsaved_issue_ids)
257 264
258 265 if params[:follow]
259 266 if @issues.size == 1 && moved_issues.size == 1
260 267 redirect_to :controller => 'issues', :action => 'show', :id => moved_issues.first
261 268 elsif moved_issues.map(&:project).uniq.size == 1
262 269 redirect_to :controller => 'issues', :action => 'index', :project_id => moved_issues.map(&:project).first
263 270 end
264 271 else
265 272 redirect_back_or_default({:controller => 'issues', :action => 'index', :project_id => @project})
266 273 end
267 274 end
268 275
269 276 def destroy
270 277 @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f
271 278 if @hours > 0
272 279 case params[:todo]
273 280 when 'destroy'
274 281 # nothing to do
275 282 when 'nullify'
276 283 TimeEntry.update_all('issue_id = NULL', ['issue_id IN (?)', @issues])
277 284 when 'reassign'
278 285 reassign_to = @project.issues.find_by_id(params[:reassign_to_id])
279 286 if reassign_to.nil?
280 287 flash.now[:error] = l(:error_issue_not_found_in_project)
281 288 return
282 289 else
283 290 TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues])
284 291 end
285 292 else
286 293 # display the destroy form if it's a user request
287 294 return unless api_request?
288 295 end
289 296 end
290 297 @issues.each do |issue|
291 298 begin
292 299 issue.reload.destroy
293 300 rescue ::ActiveRecord::RecordNotFound # raised by #reload if issue no longer exists
294 301 # nothing to do, issue was already deleted (eg. by a parent)
295 302 end
296 303 end
297 304 respond_to do |format|
298 305 format.html { redirect_back_or_default(:action => 'index', :project_id => @project) }
299 306 format.api { render_api_ok }
300 307 end
301 308 end
302 309
303 310 private
304 311 def find_issue
305 312 # Issue.visible.find(...) can not be used to redirect user to the login form
306 313 # if the issue actually exists but requires authentication
307 314 @issue = Issue.find(params[:id], :include => [:project, :tracker, :status, :author, :priority, :category])
308 315 unless @issue.visible?
309 316 deny_access
310 317 return
311 318 end
312 319 @project = @issue.project
313 320 rescue ActiveRecord::RecordNotFound
314 321 render_404
315 322 end
316 323
317 324 def find_project
318 325 project_id = params[:project_id] || (params[:issue] && params[:issue][:project_id])
319 326 @project = Project.find(project_id)
320 327 rescue ActiveRecord::RecordNotFound
321 328 render_404
322 329 end
323 330
324 331 def retrieve_previous_and_next_issue_ids
325 332 retrieve_query_from_session
326 333 if @query
327 334 sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
328 335 sort_update(@query.sortable_columns, 'issues_index_sort')
329 336 limit = 500
330 337 issue_ids = @query.issue_ids(:order => sort_clause, :limit => (limit + 1), :include => [:assigned_to, :tracker, :priority, :category, :fixed_version])
331 338 if (idx = issue_ids.index(@issue.id)) && idx < limit
332 339 if issue_ids.size < 500
333 340 @issue_position = idx + 1
334 341 @issue_count = issue_ids.size
335 342 end
336 343 @prev_issue_id = issue_ids[idx - 1] if idx > 0
337 344 @next_issue_id = issue_ids[idx + 1] if idx < (issue_ids.size - 1)
338 345 end
339 346 end
340 347 end
341 348
342 349 # Used by #edit and #update to set some common instance variables
343 350 # from the params
344 351 # TODO: Refactor, not everything in here is needed by #edit
345 352 def update_issue_from_params
346 353 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
347 354 @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
348 355 @time_entry.attributes = params[:time_entry]
349 356
350 357 @notes = params[:notes] || (params[:issue].present? ? params[:issue][:notes] : nil)
351 358 @issue.init_journal(User.current, @notes)
352 359
353 360 issue_attributes = params[:issue]
354 361 if issue_attributes && params[:conflict_resolution]
355 362 case params[:conflict_resolution]
356 363 when 'overwrite'
357 364 issue_attributes = issue_attributes.dup
358 365 issue_attributes.delete(:lock_version)
359 366 when 'add_notes'
360 367 issue_attributes = {}
361 368 when 'cancel'
362 369 redirect_to issue_path(@issue)
363 370 return false
364 371 end
365 372 end
366 373 @issue.safe_attributes = issue_attributes
367 374 @priorities = IssuePriority.active
368 375 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
369 376 true
370 377 end
371 378
372 379 # TODO: Refactor, lots of extra code in here
373 380 # TODO: Changing tracker on an existing issue should not trigger this
374 381 def build_new_issue_from_params
375 382 if params[:id].blank?
376 383 @issue = Issue.new
377 384 if params[:copy_from]
378 385 begin
379 386 @copy_from = Issue.visible.find(params[:copy_from])
380 387 @copy_attachments = params[:copy_attachments].present? || request.get?
381 388 @copy_subtasks = params[:copy_subtasks].present? || request.get?
382 389 @issue.copy_from(@copy_from, :attachments => @copy_attachments, :subtasks => @copy_subtasks)
383 390 rescue ActiveRecord::RecordNotFound
384 391 render_404
385 392 return
386 393 end
387 394 end
388 395 @issue.project = @project
389 396 else
390 397 @issue = @project.issues.visible.find(params[:id])
391 398 end
392 399
393 400 @issue.project = @project
394 401 @issue.author = User.current
395 402 # Tracker must be set before custom field values
396 403 @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first)
397 404 if @issue.tracker.nil?
398 405 render_error l(:error_no_tracker_in_project)
399 406 return false
400 407 end
401 408 @issue.start_date ||= Date.today if Setting.default_issue_start_date_to_creation_date?
402 409 @issue.safe_attributes = params[:issue]
403 410
404 411 @priorities = IssuePriority.active
405 412 @allowed_statuses = @issue.new_statuses_allowed_to(User.current, true)
406 413 @available_watchers = (@issue.project.users.sort + @issue.watcher_users).uniq
407 414 end
408 415
409 416 def check_for_default_issue_status
410 417 if IssueStatus.default.nil?
411 418 render_error l(:error_no_default_issue_status)
412 419 return false
413 420 end
414 421 end
415 422
416 423 def parse_params_for_bulk_issue_attributes(params)
417 424 attributes = (params[:issue] || {}).reject {|k,v| v.blank?}
418 425 attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'}
419 426 if custom = attributes[:custom_field_values]
420 427 custom.reject! {|k,v| v.blank?}
421 428 custom.keys.each do |k|
422 429 if custom[k].is_a?(Array)
423 430 custom[k] << '' if custom[k].delete('__none__')
424 431 else
425 432 custom[k] = '' if custom[k] == '__none__'
426 433 end
427 434 end
428 435 end
429 436 attributes
430 437 end
431 438 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