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