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