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