##// END OF EJS Templates
Refactor: Extract method for setting the flash on bulk issue saves....
Eric Davis -
r3446:c7c3c0cc7c7d
parent child
Show More
@@ -1,571 +1,571
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2008 Jean-Philippe Lang
2 # Copyright (C) 2006-2008 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
19 menu_item :new_issue, :only => :new
20 default_search_scope :issues
20 default_search_scope :issues
21
21
22 before_filter :find_issue, :only => [:show, :edit, :update, :reply]
22 before_filter :find_issue, :only => [:show, :edit, :update, :reply]
23 before_filter :find_issues, :only => [:bulk_edit, :move, :destroy]
23 before_filter :find_issues, :only => [:bulk_edit, :move, :destroy]
24 before_filter :find_project, :only => [:new, :update_form, :preview]
24 before_filter :find_project, :only => [:new, :update_form, :preview]
25 before_filter :authorize, :except => [:index, :changes, :gantt, :calendar, :preview, :context_menu]
25 before_filter :authorize, :except => [:index, :changes, :gantt, :calendar, :preview, :context_menu]
26 before_filter :find_optional_project, :only => [:index, :changes, :gantt, :calendar]
26 before_filter :find_optional_project, :only => [:index, :changes, :gantt, :calendar]
27 accept_key_auth :index, :show, :changes
27 accept_key_auth :index, :show, :changes
28
28
29 rescue_from Query::StatementInvalid, :with => :query_statement_invalid
29 rescue_from Query::StatementInvalid, :with => :query_statement_invalid
30
30
31 helper :journals
31 helper :journals
32 helper :projects
32 helper :projects
33 include ProjectsHelper
33 include ProjectsHelper
34 helper :custom_fields
34 helper :custom_fields
35 include CustomFieldsHelper
35 include CustomFieldsHelper
36 helper :issue_relations
36 helper :issue_relations
37 include IssueRelationsHelper
37 include IssueRelationsHelper
38 helper :watchers
38 helper :watchers
39 include WatchersHelper
39 include WatchersHelper
40 helper :attachments
40 helper :attachments
41 include AttachmentsHelper
41 include AttachmentsHelper
42 helper :queries
42 helper :queries
43 include QueriesHelper
43 include QueriesHelper
44 helper :sort
44 helper :sort
45 include SortHelper
45 include SortHelper
46 include IssuesHelper
46 include IssuesHelper
47 helper :timelog
47 helper :timelog
48 include Redmine::Export::PDF
48 include Redmine::Export::PDF
49
49
50 verify :method => [:post, :delete],
50 verify :method => [:post, :delete],
51 :only => :destroy,
51 :only => :destroy,
52 :render => { :nothing => true, :status => :method_not_allowed }
52 :render => { :nothing => true, :status => :method_not_allowed }
53
53
54 verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
54 verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
55
55
56 def index
56 def index
57 retrieve_query
57 retrieve_query
58 sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
58 sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
59 sort_update({'id' => "#{Issue.table_name}.id"}.merge(@query.available_columns.inject({}) {|h, c| h[c.name.to_s] = c.sortable; h}))
59 sort_update({'id' => "#{Issue.table_name}.id"}.merge(@query.available_columns.inject({}) {|h, c| h[c.name.to_s] = c.sortable; h}))
60
60
61 if @query.valid?
61 if @query.valid?
62 limit = case params[:format]
62 limit = case params[:format]
63 when 'csv', 'pdf'
63 when 'csv', 'pdf'
64 Setting.issues_export_limit.to_i
64 Setting.issues_export_limit.to_i
65 when 'atom'
65 when 'atom'
66 Setting.feeds_limit.to_i
66 Setting.feeds_limit.to_i
67 else
67 else
68 per_page_option
68 per_page_option
69 end
69 end
70
70
71 @issue_count = @query.issue_count
71 @issue_count = @query.issue_count
72 @issue_pages = Paginator.new self, @issue_count, limit, params['page']
72 @issue_pages = Paginator.new self, @issue_count, limit, params['page']
73 @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version],
73 @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version],
74 :order => sort_clause,
74 :order => sort_clause,
75 :offset => @issue_pages.current.offset,
75 :offset => @issue_pages.current.offset,
76 :limit => limit)
76 :limit => limit)
77 @issue_count_by_group = @query.issue_count_by_group
77 @issue_count_by_group = @query.issue_count_by_group
78
78
79 respond_to do |format|
79 respond_to do |format|
80 format.html { render :template => 'issues/index.rhtml', :layout => !request.xhr? }
80 format.html { render :template => 'issues/index.rhtml', :layout => !request.xhr? }
81 format.xml { render :layout => false }
81 format.xml { render :layout => false }
82 format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") }
82 format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") }
83 format.csv { send_data(issues_to_csv(@issues, @project), :type => 'text/csv; header=present', :filename => 'export.csv') }
83 format.csv { send_data(issues_to_csv(@issues, @project), :type => 'text/csv; header=present', :filename => 'export.csv') }
84 format.pdf { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'export.pdf') }
84 format.pdf { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'export.pdf') }
85 end
85 end
86 else
86 else
87 # Send html if the query is not valid
87 # Send html if the query is not valid
88 render(:template => 'issues/index.rhtml', :layout => !request.xhr?)
88 render(:template => 'issues/index.rhtml', :layout => !request.xhr?)
89 end
89 end
90 rescue ActiveRecord::RecordNotFound
90 rescue ActiveRecord::RecordNotFound
91 render_404
91 render_404
92 end
92 end
93
93
94 def changes
94 def changes
95 retrieve_query
95 retrieve_query
96 sort_init 'id', 'desc'
96 sort_init 'id', 'desc'
97 sort_update({'id' => "#{Issue.table_name}.id"}.merge(@query.available_columns.inject({}) {|h, c| h[c.name.to_s] = c.sortable; h}))
97 sort_update({'id' => "#{Issue.table_name}.id"}.merge(@query.available_columns.inject({}) {|h, c| h[c.name.to_s] = c.sortable; h}))
98
98
99 if @query.valid?
99 if @query.valid?
100 @journals = @query.journals(:order => "#{Journal.table_name}.created_on DESC",
100 @journals = @query.journals(:order => "#{Journal.table_name}.created_on DESC",
101 :limit => 25)
101 :limit => 25)
102 end
102 end
103 @title = (@project ? @project.name : Setting.app_title) + ": " + (@query.new_record? ? l(:label_changes_details) : @query.name)
103 @title = (@project ? @project.name : Setting.app_title) + ": " + (@query.new_record? ? l(:label_changes_details) : @query.name)
104 render :layout => false, :content_type => 'application/atom+xml'
104 render :layout => false, :content_type => 'application/atom+xml'
105 rescue ActiveRecord::RecordNotFound
105 rescue ActiveRecord::RecordNotFound
106 render_404
106 render_404
107 end
107 end
108
108
109 def show
109 def show
110 @journals = @issue.journals.find(:all, :include => [:user, :details], :order => "#{Journal.table_name}.created_on ASC")
110 @journals = @issue.journals.find(:all, :include => [:user, :details], :order => "#{Journal.table_name}.created_on ASC")
111 @journals.each_with_index {|j,i| j.indice = i+1}
111 @journals.each_with_index {|j,i| j.indice = i+1}
112 @journals.reverse! if User.current.wants_comments_in_reverse_order?
112 @journals.reverse! if User.current.wants_comments_in_reverse_order?
113 @changesets = @issue.changesets.visible.all
113 @changesets = @issue.changesets.visible.all
114 @changesets.reverse! if User.current.wants_comments_in_reverse_order?
114 @changesets.reverse! if User.current.wants_comments_in_reverse_order?
115 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
115 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
116 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
116 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
117 @priorities = IssuePriority.all
117 @priorities = IssuePriority.all
118 @time_entry = TimeEntry.new
118 @time_entry = TimeEntry.new
119 respond_to do |format|
119 respond_to do |format|
120 format.html { render :template => 'issues/show.rhtml' }
120 format.html { render :template => 'issues/show.rhtml' }
121 format.xml { render :layout => false }
121 format.xml { render :layout => false }
122 format.atom { render :action => 'changes', :layout => false, :content_type => 'application/atom+xml' }
122 format.atom { render :action => 'changes', :layout => false, :content_type => 'application/atom+xml' }
123 format.pdf { send_data(issue_to_pdf(@issue), :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf") }
123 format.pdf { send_data(issue_to_pdf(@issue), :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf") }
124 end
124 end
125 end
125 end
126
126
127 # Add a new issue
127 # Add a new issue
128 # The new issue will be created from an existing one if copy_from parameter is given
128 # The new issue will be created from an existing one if copy_from parameter is given
129 def new
129 def new
130 @issue = Issue.new
130 @issue = Issue.new
131 @issue.copy_from(params[:copy_from]) if params[:copy_from]
131 @issue.copy_from(params[:copy_from]) if params[:copy_from]
132 @issue.project = @project
132 @issue.project = @project
133 # Tracker must be set before custom field values
133 # Tracker must be set before custom field values
134 @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first)
134 @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first)
135 if @issue.tracker.nil?
135 if @issue.tracker.nil?
136 render_error l(:error_no_tracker_in_project)
136 render_error l(:error_no_tracker_in_project)
137 return
137 return
138 end
138 end
139 if params[:issue].is_a?(Hash)
139 if params[:issue].is_a?(Hash)
140 @issue.safe_attributes = params[:issue]
140 @issue.safe_attributes = params[:issue]
141 @issue.watcher_user_ids = params[:issue]['watcher_user_ids'] if User.current.allowed_to?(:add_issue_watchers, @project)
141 @issue.watcher_user_ids = params[:issue]['watcher_user_ids'] if User.current.allowed_to?(:add_issue_watchers, @project)
142 end
142 end
143 @issue.author = User.current
143 @issue.author = User.current
144
144
145 default_status = IssueStatus.default
145 default_status = IssueStatus.default
146 unless default_status
146 unless default_status
147 render_error l(:error_no_default_issue_status)
147 render_error l(:error_no_default_issue_status)
148 return
148 return
149 end
149 end
150 @issue.status = default_status
150 @issue.status = default_status
151 @allowed_statuses = @issue.new_statuses_allowed_to(User.current, true)
151 @allowed_statuses = @issue.new_statuses_allowed_to(User.current, true)
152
152
153 if request.get? || request.xhr?
153 if request.get? || request.xhr?
154 @issue.start_date ||= Date.today
154 @issue.start_date ||= Date.today
155 else
155 else
156 requested_status = IssueStatus.find_by_id(params[:issue][:status_id])
156 requested_status = IssueStatus.find_by_id(params[:issue][:status_id])
157 # Check that the user is allowed to apply the requested status
157 # Check that the user is allowed to apply the requested status
158 @issue.status = (@allowed_statuses.include? requested_status) ? requested_status : default_status
158 @issue.status = (@allowed_statuses.include? requested_status) ? requested_status : default_status
159 call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue })
159 call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue })
160 if @issue.save
160 if @issue.save
161 attachments = Attachment.attach_files(@issue, params[:attachments])
161 attachments = Attachment.attach_files(@issue, params[:attachments])
162 render_attachment_warning_if_needed(@issue)
162 render_attachment_warning_if_needed(@issue)
163 flash[:notice] = l(:notice_successful_create)
163 flash[:notice] = l(:notice_successful_create)
164 call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue})
164 call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue})
165 respond_to do |format|
165 respond_to do |format|
166 format.html {
166 format.html {
167 redirect_to(params[:continue] ? { :action => 'new', :tracker_id => @issue.tracker } :
167 redirect_to(params[:continue] ? { :action => 'new', :tracker_id => @issue.tracker } :
168 { :action => 'show', :id => @issue })
168 { :action => 'show', :id => @issue })
169 }
169 }
170 format.xml { render :action => 'show', :status => :created, :location => url_for(:controller => 'issues', :action => 'show', :id => @issue) }
170 format.xml { render :action => 'show', :status => :created, :location => url_for(:controller => 'issues', :action => 'show', :id => @issue) }
171 end
171 end
172 return
172 return
173 else
173 else
174 respond_to do |format|
174 respond_to do |format|
175 format.html { }
175 format.html { }
176 format.xml { render(:xml => @issue.errors, :status => :unprocessable_entity); return }
176 format.xml { render(:xml => @issue.errors, :status => :unprocessable_entity); return }
177 end
177 end
178 end
178 end
179 end
179 end
180 @priorities = IssuePriority.all
180 @priorities = IssuePriority.all
181 render :layout => !request.xhr?
181 render :layout => !request.xhr?
182 end
182 end
183
183
184 # Attributes that can be updated on workflow transition (without :edit permission)
184 # Attributes that can be updated on workflow transition (without :edit permission)
185 # TODO: make it configurable (at least per role)
185 # TODO: make it configurable (at least per role)
186 UPDATABLE_ATTRS_ON_TRANSITION = %w(status_id assigned_to_id fixed_version_id done_ratio) unless const_defined?(:UPDATABLE_ATTRS_ON_TRANSITION)
186 UPDATABLE_ATTRS_ON_TRANSITION = %w(status_id assigned_to_id fixed_version_id done_ratio) unless const_defined?(:UPDATABLE_ATTRS_ON_TRANSITION)
187
187
188 def edit
188 def edit
189 update_issue_from_params
189 update_issue_from_params
190
190
191 @journal = @issue.current_journal
191 @journal = @issue.current_journal
192
192
193 respond_to do |format|
193 respond_to do |format|
194 format.html { }
194 format.html { }
195 format.xml { }
195 format.xml { }
196 end
196 end
197 end
197 end
198
198
199 def update
199 def update
200 update_issue_from_params
200 update_issue_from_params
201
201
202 if @issue.save_issue_with_child_records(params, @time_entry)
202 if @issue.save_issue_with_child_records(params, @time_entry)
203 render_attachment_warning_if_needed(@issue)
203 render_attachment_warning_if_needed(@issue)
204 flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
204 flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
205
205
206 respond_to do |format|
206 respond_to do |format|
207 format.html { redirect_back_or_default({:action => 'show', :id => @issue}) }
207 format.html { redirect_back_or_default({:action => 'show', :id => @issue}) }
208 format.xml { head :ok }
208 format.xml { head :ok }
209 end
209 end
210 else
210 else
211 render_attachment_warning_if_needed(@issue)
211 render_attachment_warning_if_needed(@issue)
212 flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
212 flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
213 @journal = @issue.current_journal
213 @journal = @issue.current_journal
214
214
215 respond_to do |format|
215 respond_to do |format|
216 format.html { render :action => 'edit' }
216 format.html { render :action => 'edit' }
217 format.xml { render :xml => @issue.errors, :status => :unprocessable_entity }
217 format.xml { render :xml => @issue.errors, :status => :unprocessable_entity }
218 end
218 end
219 end
219 end
220
220
221 rescue ActiveRecord::StaleObjectError
221 rescue ActiveRecord::StaleObjectError
222 # Optimistic locking exception
222 # Optimistic locking exception
223 flash.now[:error] = l(:notice_locking_conflict)
223 flash.now[:error] = l(:notice_locking_conflict)
224 # Remove the previously added attachments if issue was not updated
224 # Remove the previously added attachments if issue was not updated
225 attachments[:files].each(&:destroy) if attachments[:files]
225 attachments[:files].each(&:destroy) if attachments[:files]
226 end
226 end
227
227
228 def reply
228 def reply
229 journal = Journal.find(params[:journal_id]) if params[:journal_id]
229 journal = Journal.find(params[:journal_id]) if params[:journal_id]
230 if journal
230 if journal
231 user = journal.user
231 user = journal.user
232 text = journal.notes
232 text = journal.notes
233 else
233 else
234 user = @issue.author
234 user = @issue.author
235 text = @issue.description
235 text = @issue.description
236 end
236 end
237 content = "#{ll(Setting.default_language, :text_user_wrote, user)}\\n> "
237 content = "#{ll(Setting.default_language, :text_user_wrote, user)}\\n> "
238 content << text.to_s.strip.gsub(%r{<pre>((.|\s)*?)</pre>}m, '[...]').gsub('"', '\"').gsub(/(\r?\n|\r\n?)/, "\\n> ") + "\\n\\n"
238 content << text.to_s.strip.gsub(%r{<pre>((.|\s)*?)</pre>}m, '[...]').gsub('"', '\"').gsub(/(\r?\n|\r\n?)/, "\\n> ") + "\\n\\n"
239 render(:update) { |page|
239 render(:update) { |page|
240 page.<< "$('notes').value = \"#{content}\";"
240 page.<< "$('notes').value = \"#{content}\";"
241 page.show 'update'
241 page.show 'update'
242 page << "Form.Element.focus('notes');"
242 page << "Form.Element.focus('notes');"
243 page << "Element.scrollTo('update');"
243 page << "Element.scrollTo('update');"
244 page << "$('notes').scrollTop = $('notes').scrollHeight - $('notes').clientHeight;"
244 page << "$('notes').scrollTop = $('notes').scrollHeight - $('notes').clientHeight;"
245 }
245 }
246 end
246 end
247
247
248 # Bulk edit a set of issues
248 # Bulk edit a set of issues
249 def bulk_edit
249 def bulk_edit
250 if request.post?
250 if request.post?
251 attributes = (params[:issue] || {}).reject {|k,v| v.blank?}
251 attributes = (params[:issue] || {}).reject {|k,v| v.blank?}
252 attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'}
252 attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'}
253 attributes[:custom_field_values].reject! {|k,v| v.blank?} if attributes[:custom_field_values]
253 attributes[:custom_field_values].reject! {|k,v| v.blank?} if attributes[:custom_field_values]
254
254
255 unsaved_issue_ids = []
255 unsaved_issue_ids = []
256 @issues.each do |issue|
256 @issues.each do |issue|
257 journal = issue.init_journal(User.current, params[:notes])
257 journal = issue.init_journal(User.current, params[:notes])
258 issue.safe_attributes = attributes
258 issue.safe_attributes = attributes
259 call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue })
259 call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue })
260 unless issue.save
260 unless issue.save
261 # Keep unsaved issue ids to display them in flash error
261 # Keep unsaved issue ids to display them in flash error
262 unsaved_issue_ids << issue.id
262 unsaved_issue_ids << issue.id
263 end
263 end
264 end
264 end
265 if unsaved_issue_ids.empty?
265 set_flash_from_bulk_issue_save(@issues, unsaved_issue_ids)
266 flash[:notice] = l(:notice_successful_update) unless @issues.empty?
267 else
268 flash[:error] = l(:notice_failed_to_save_issues, :count => unsaved_issue_ids.size,
269 :total => @issues.size,
270 :ids => '#' + unsaved_issue_ids.join(', #'))
271 end
272 redirect_back_or_default({:controller => 'issues', :action => 'index', :project_id => @project})
266 redirect_back_or_default({:controller => 'issues', :action => 'index', :project_id => @project})
273 return
267 return
274 end
268 end
275 @available_statuses = Workflow.available_statuses(@project)
269 @available_statuses = Workflow.available_statuses(@project)
276 @custom_fields = @project.all_issue_custom_fields
270 @custom_fields = @project.all_issue_custom_fields
277 end
271 end
278
272
279 def move
273 def move
280 @copy = params[:copy_options] && params[:copy_options][:copy]
274 @copy = params[:copy_options] && params[:copy_options][:copy]
281 @allowed_projects = []
275 @allowed_projects = []
282 # find projects to which the user is allowed to move the issue
276 # find projects to which the user is allowed to move the issue
283 if User.current.admin?
277 if User.current.admin?
284 # admin is allowed to move issues to any active (visible) project
278 # admin is allowed to move issues to any active (visible) project
285 @allowed_projects = Project.find(:all, :conditions => Project.visible_by(User.current))
279 @allowed_projects = Project.find(:all, :conditions => Project.visible_by(User.current))
286 else
280 else
287 User.current.memberships.each {|m| @allowed_projects << m.project if m.roles.detect {|r| r.allowed_to?(:move_issues)}}
281 User.current.memberships.each {|m| @allowed_projects << m.project if m.roles.detect {|r| r.allowed_to?(:move_issues)}}
288 end
282 end
289 @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:new_project_id]} if params[:new_project_id]
283 @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:new_project_id]} if params[:new_project_id]
290 @target_project ||= @project
284 @target_project ||= @project
291 @trackers = @target_project.trackers
285 @trackers = @target_project.trackers
292 @available_statuses = Workflow.available_statuses(@project)
286 @available_statuses = Workflow.available_statuses(@project)
293 if request.post?
287 if request.post?
294 new_tracker = params[:new_tracker_id].blank? ? nil : @target_project.trackers.find_by_id(params[:new_tracker_id])
288 new_tracker = params[:new_tracker_id].blank? ? nil : @target_project.trackers.find_by_id(params[:new_tracker_id])
295 unsaved_issue_ids = []
289 unsaved_issue_ids = []
296 moved_issues = []
290 moved_issues = []
297 @issues.each do |issue|
291 @issues.each do |issue|
298 changed_attributes = {}
292 changed_attributes = {}
299 [:assigned_to_id, :status_id, :start_date, :due_date].each do |valid_attribute|
293 [:assigned_to_id, :status_id, :start_date, :due_date].each do |valid_attribute|
300 unless params[valid_attribute].blank?
294 unless params[valid_attribute].blank?
301 changed_attributes[valid_attribute] = (params[valid_attribute] == 'none' ? nil : params[valid_attribute])
295 changed_attributes[valid_attribute] = (params[valid_attribute] == 'none' ? nil : params[valid_attribute])
302 end
296 end
303 end
297 end
304 issue.init_journal(User.current)
298 issue.init_journal(User.current)
305 call_hook(:controller_issues_move_before_save, { :params => params, :issue => issue, :target_project => @target_project, :copy => !!@copy })
299 call_hook(:controller_issues_move_before_save, { :params => params, :issue => issue, :target_project => @target_project, :copy => !!@copy })
306 if r = issue.move_to(@target_project, new_tracker, {:copy => @copy, :attributes => changed_attributes})
300 if r = issue.move_to(@target_project, new_tracker, {:copy => @copy, :attributes => changed_attributes})
307 moved_issues << r
301 moved_issues << r
308 else
302 else
309 unsaved_issue_ids << issue.id
303 unsaved_issue_ids << issue.id
310 end
304 end
311 end
305 end
312 if unsaved_issue_ids.empty?
306 set_flash_from_bulk_issue_save(@issues, unsaved_issue_ids)
313 flash[:notice] = l(:notice_successful_update) unless @issues.empty?
307
314 else
315 flash[:error] = l(:notice_failed_to_save_issues, :count => unsaved_issue_ids.size,
316 :total => @issues.size,
317 :ids => '#' + unsaved_issue_ids.join(', #'))
318 end
319 if params[:follow]
308 if params[:follow]
320 if @issues.size == 1 && moved_issues.size == 1
309 if @issues.size == 1 && moved_issues.size == 1
321 redirect_to :controller => 'issues', :action => 'show', :id => moved_issues.first
310 redirect_to :controller => 'issues', :action => 'show', :id => moved_issues.first
322 else
311 else
323 redirect_to :controller => 'issues', :action => 'index', :project_id => (@target_project || @project)
312 redirect_to :controller => 'issues', :action => 'index', :project_id => (@target_project || @project)
324 end
313 end
325 else
314 else
326 redirect_to :controller => 'issues', :action => 'index', :project_id => @project
315 redirect_to :controller => 'issues', :action => 'index', :project_id => @project
327 end
316 end
328 return
317 return
329 end
318 end
330 render :layout => false if request.xhr?
319 render :layout => false if request.xhr?
331 end
320 end
332
321
333 def destroy
322 def destroy
334 @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f
323 @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f
335 if @hours > 0
324 if @hours > 0
336 case params[:todo]
325 case params[:todo]
337 when 'destroy'
326 when 'destroy'
338 # nothing to do
327 # nothing to do
339 when 'nullify'
328 when 'nullify'
340 TimeEntry.update_all('issue_id = NULL', ['issue_id IN (?)', @issues])
329 TimeEntry.update_all('issue_id = NULL', ['issue_id IN (?)', @issues])
341 when 'reassign'
330 when 'reassign'
342 reassign_to = @project.issues.find_by_id(params[:reassign_to_id])
331 reassign_to = @project.issues.find_by_id(params[:reassign_to_id])
343 if reassign_to.nil?
332 if reassign_to.nil?
344 flash.now[:error] = l(:error_issue_not_found_in_project)
333 flash.now[:error] = l(:error_issue_not_found_in_project)
345 return
334 return
346 else
335 else
347 TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues])
336 TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues])
348 end
337 end
349 else
338 else
350 unless params[:format] == 'xml'
339 unless params[:format] == 'xml'
351 # display the destroy form if it's a user request
340 # display the destroy form if it's a user request
352 return
341 return
353 end
342 end
354 end
343 end
355 end
344 end
356 @issues.each(&:destroy)
345 @issues.each(&:destroy)
357 respond_to do |format|
346 respond_to do |format|
358 format.html { redirect_to :action => 'index', :project_id => @project }
347 format.html { redirect_to :action => 'index', :project_id => @project }
359 format.xml { head :ok }
348 format.xml { head :ok }
360 end
349 end
361 end
350 end
362
351
363 def gantt
352 def gantt
364 @gantt = Redmine::Helpers::Gantt.new(params)
353 @gantt = Redmine::Helpers::Gantt.new(params)
365 retrieve_query
354 retrieve_query
366 @query.group_by = nil
355 @query.group_by = nil
367 if @query.valid?
356 if @query.valid?
368 events = []
357 events = []
369 # Issues that have start and due dates
358 # Issues that have start and due dates
370 events += @query.issues(:include => [:tracker, :assigned_to, :priority],
359 events += @query.issues(:include => [:tracker, :assigned_to, :priority],
371 :order => "start_date, due_date",
360 :order => "start_date, due_date",
372 :conditions => ["(((start_date>=? and start_date<=?) or (due_date>=? and due_date<=?) or (start_date<? and due_date>?)) and start_date is not null and due_date is not null)", @gantt.date_from, @gantt.date_to, @gantt.date_from, @gantt.date_to, @gantt.date_from, @gantt.date_to]
361 :conditions => ["(((start_date>=? and start_date<=?) or (due_date>=? and due_date<=?) or (start_date<? and due_date>?)) and start_date is not null and due_date is not null)", @gantt.date_from, @gantt.date_to, @gantt.date_from, @gantt.date_to, @gantt.date_from, @gantt.date_to]
373 )
362 )
374 # Issues that don't have a due date but that are assigned to a version with a date
363 # Issues that don't have a due date but that are assigned to a version with a date
375 events += @query.issues(:include => [:tracker, :assigned_to, :priority, :fixed_version],
364 events += @query.issues(:include => [:tracker, :assigned_to, :priority, :fixed_version],
376 :order => "start_date, effective_date",
365 :order => "start_date, effective_date",
377 :conditions => ["(((start_date>=? and start_date<=?) or (effective_date>=? and effective_date<=?) or (start_date<? and effective_date>?)) and start_date is not null and due_date is null and effective_date is not null)", @gantt.date_from, @gantt.date_to, @gantt.date_from, @gantt.date_to, @gantt.date_from, @gantt.date_to]
366 :conditions => ["(((start_date>=? and start_date<=?) or (effective_date>=? and effective_date<=?) or (start_date<? and effective_date>?)) and start_date is not null and due_date is null and effective_date is not null)", @gantt.date_from, @gantt.date_to, @gantt.date_from, @gantt.date_to, @gantt.date_from, @gantt.date_to]
378 )
367 )
379 # Versions
368 # Versions
380 events += @query.versions(:conditions => ["effective_date BETWEEN ? AND ?", @gantt.date_from, @gantt.date_to])
369 events += @query.versions(:conditions => ["effective_date BETWEEN ? AND ?", @gantt.date_from, @gantt.date_to])
381
370
382 @gantt.events = events
371 @gantt.events = events
383 end
372 end
384
373
385 basename = (@project ? "#{@project.identifier}-" : '') + 'gantt'
374 basename = (@project ? "#{@project.identifier}-" : '') + 'gantt'
386
375
387 respond_to do |format|
376 respond_to do |format|
388 format.html { render :template => "issues/gantt.rhtml", :layout => !request.xhr? }
377 format.html { render :template => "issues/gantt.rhtml", :layout => !request.xhr? }
389 format.png { send_data(@gantt.to_image, :disposition => 'inline', :type => 'image/png', :filename => "#{basename}.png") } if @gantt.respond_to?('to_image')
378 format.png { send_data(@gantt.to_image, :disposition => 'inline', :type => 'image/png', :filename => "#{basename}.png") } if @gantt.respond_to?('to_image')
390 format.pdf { send_data(gantt_to_pdf(@gantt, @project), :type => 'application/pdf', :filename => "#{basename}.pdf") }
379 format.pdf { send_data(gantt_to_pdf(@gantt, @project), :type => 'application/pdf', :filename => "#{basename}.pdf") }
391 end
380 end
392 end
381 end
393
382
394 def calendar
383 def calendar
395 if params[:year] and params[:year].to_i > 1900
384 if params[:year] and params[:year].to_i > 1900
396 @year = params[:year].to_i
385 @year = params[:year].to_i
397 if params[:month] and params[:month].to_i > 0 and params[:month].to_i < 13
386 if params[:month] and params[:month].to_i > 0 and params[:month].to_i < 13
398 @month = params[:month].to_i
387 @month = params[:month].to_i
399 end
388 end
400 end
389 end
401 @year ||= Date.today.year
390 @year ||= Date.today.year
402 @month ||= Date.today.month
391 @month ||= Date.today.month
403
392
404 @calendar = Redmine::Helpers::Calendar.new(Date.civil(@year, @month, 1), current_language, :month)
393 @calendar = Redmine::Helpers::Calendar.new(Date.civil(@year, @month, 1), current_language, :month)
405 retrieve_query
394 retrieve_query
406 @query.group_by = nil
395 @query.group_by = nil
407 if @query.valid?
396 if @query.valid?
408 events = []
397 events = []
409 events += @query.issues(:include => [:tracker, :assigned_to, :priority],
398 events += @query.issues(:include => [:tracker, :assigned_to, :priority],
410 :conditions => ["((start_date BETWEEN ? AND ?) OR (due_date BETWEEN ? AND ?))", @calendar.startdt, @calendar.enddt, @calendar.startdt, @calendar.enddt]
399 :conditions => ["((start_date BETWEEN ? AND ?) OR (due_date BETWEEN ? AND ?))", @calendar.startdt, @calendar.enddt, @calendar.startdt, @calendar.enddt]
411 )
400 )
412 events += @query.versions(:conditions => ["effective_date BETWEEN ? AND ?", @calendar.startdt, @calendar.enddt])
401 events += @query.versions(:conditions => ["effective_date BETWEEN ? AND ?", @calendar.startdt, @calendar.enddt])
413
402
414 @calendar.events = events
403 @calendar.events = events
415 end
404 end
416
405
417 render :layout => false if request.xhr?
406 render :layout => false if request.xhr?
418 end
407 end
419
408
420 def context_menu
409 def context_menu
421 @issues = Issue.find_all_by_id(params[:ids], :include => :project)
410 @issues = Issue.find_all_by_id(params[:ids], :include => :project)
422 if (@issues.size == 1)
411 if (@issues.size == 1)
423 @issue = @issues.first
412 @issue = @issues.first
424 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
413 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
425 end
414 end
426 projects = @issues.collect(&:project).compact.uniq
415 projects = @issues.collect(&:project).compact.uniq
427 @project = projects.first if projects.size == 1
416 @project = projects.first if projects.size == 1
428
417
429 @can = {:edit => (@project && User.current.allowed_to?(:edit_issues, @project)),
418 @can = {:edit => (@project && User.current.allowed_to?(:edit_issues, @project)),
430 :log_time => (@project && User.current.allowed_to?(:log_time, @project)),
419 :log_time => (@project && User.current.allowed_to?(:log_time, @project)),
431 :update => (@project && (User.current.allowed_to?(:edit_issues, @project) || (User.current.allowed_to?(:change_status, @project) && @allowed_statuses && !@allowed_statuses.empty?))),
420 :update => (@project && (User.current.allowed_to?(:edit_issues, @project) || (User.current.allowed_to?(:change_status, @project) && @allowed_statuses && !@allowed_statuses.empty?))),
432 :move => (@project && User.current.allowed_to?(:move_issues, @project)),
421 :move => (@project && User.current.allowed_to?(:move_issues, @project)),
433 :copy => (@issue && @project.trackers.include?(@issue.tracker) && User.current.allowed_to?(:add_issues, @project)),
422 :copy => (@issue && @project.trackers.include?(@issue.tracker) && User.current.allowed_to?(:add_issues, @project)),
434 :delete => (@project && User.current.allowed_to?(:delete_issues, @project))
423 :delete => (@project && User.current.allowed_to?(:delete_issues, @project))
435 }
424 }
436 if @project
425 if @project
437 @assignables = @project.assignable_users
426 @assignables = @project.assignable_users
438 @assignables << @issue.assigned_to if @issue && @issue.assigned_to && !@assignables.include?(@issue.assigned_to)
427 @assignables << @issue.assigned_to if @issue && @issue.assigned_to && !@assignables.include?(@issue.assigned_to)
439 @trackers = @project.trackers
428 @trackers = @project.trackers
440 end
429 end
441
430
442 @priorities = IssuePriority.all.reverse
431 @priorities = IssuePriority.all.reverse
443 @statuses = IssueStatus.find(:all, :order => 'position')
432 @statuses = IssueStatus.find(:all, :order => 'position')
444 @back = params[:back_url] || request.env['HTTP_REFERER']
433 @back = params[:back_url] || request.env['HTTP_REFERER']
445
434
446 render :layout => false
435 render :layout => false
447 end
436 end
448
437
449 def update_form
438 def update_form
450 if params[:id].blank?
439 if params[:id].blank?
451 @issue = Issue.new
440 @issue = Issue.new
452 @issue.project = @project
441 @issue.project = @project
453 else
442 else
454 @issue = @project.issues.visible.find(params[:id])
443 @issue = @project.issues.visible.find(params[:id])
455 end
444 end
456 @issue.attributes = params[:issue]
445 @issue.attributes = params[:issue]
457 @allowed_statuses = ([@issue.status] + @issue.status.find_new_statuses_allowed_to(User.current.roles_for_project(@project), @issue.tracker)).uniq
446 @allowed_statuses = ([@issue.status] + @issue.status.find_new_statuses_allowed_to(User.current.roles_for_project(@project), @issue.tracker)).uniq
458 @priorities = IssuePriority.all
447 @priorities = IssuePriority.all
459
448
460 render :partial => 'attributes'
449 render :partial => 'attributes'
461 end
450 end
462
451
463 def preview
452 def preview
464 @issue = @project.issues.find_by_id(params[:id]) unless params[:id].blank?
453 @issue = @project.issues.find_by_id(params[:id]) unless params[:id].blank?
465 @attachements = @issue.attachments if @issue
454 @attachements = @issue.attachments if @issue
466 @text = params[:notes] || (params[:issue] ? params[:issue][:description] : nil)
455 @text = params[:notes] || (params[:issue] ? params[:issue][:description] : nil)
467 render :partial => 'common/preview'
456 render :partial => 'common/preview'
468 end
457 end
469
458
470 private
459 private
471 def find_issue
460 def find_issue
472 @issue = Issue.find(params[:id], :include => [:project, :tracker, :status, :author, :priority, :category])
461 @issue = Issue.find(params[:id], :include => [:project, :tracker, :status, :author, :priority, :category])
473 @project = @issue.project
462 @project = @issue.project
474 rescue ActiveRecord::RecordNotFound
463 rescue ActiveRecord::RecordNotFound
475 render_404
464 render_404
476 end
465 end
477
466
478 # Filter for bulk operations
467 # Filter for bulk operations
479 def find_issues
468 def find_issues
480 @issues = Issue.find_all_by_id(params[:id] || params[:ids])
469 @issues = Issue.find_all_by_id(params[:id] || params[:ids])
481 raise ActiveRecord::RecordNotFound if @issues.empty?
470 raise ActiveRecord::RecordNotFound if @issues.empty?
482 projects = @issues.collect(&:project).compact.uniq
471 projects = @issues.collect(&:project).compact.uniq
483 if projects.size == 1
472 if projects.size == 1
484 @project = projects.first
473 @project = projects.first
485 else
474 else
486 # TODO: let users bulk edit/move/destroy issues from different projects
475 # TODO: let users bulk edit/move/destroy issues from different projects
487 render_error 'Can not bulk edit/move/destroy issues from different projects'
476 render_error 'Can not bulk edit/move/destroy issues from different projects'
488 return false
477 return false
489 end
478 end
490 rescue ActiveRecord::RecordNotFound
479 rescue ActiveRecord::RecordNotFound
491 render_404
480 render_404
492 end
481 end
493
482
494 def find_project
483 def find_project
495 project_id = (params[:issue] && params[:issue][:project_id]) || params[:project_id]
484 project_id = (params[:issue] && params[:issue][:project_id]) || params[:project_id]
496 @project = Project.find(project_id)
485 @project = Project.find(project_id)
497 rescue ActiveRecord::RecordNotFound
486 rescue ActiveRecord::RecordNotFound
498 render_404
487 render_404
499 end
488 end
500
489
501 def find_optional_project
490 def find_optional_project
502 @project = Project.find(params[:project_id]) unless params[:project_id].blank?
491 @project = Project.find(params[:project_id]) unless params[:project_id].blank?
503 allowed = User.current.allowed_to?({:controller => params[:controller], :action => params[:action]}, @project, :global => true)
492 allowed = User.current.allowed_to?({:controller => params[:controller], :action => params[:action]}, @project, :global => true)
504 allowed ? true : deny_access
493 allowed ? true : deny_access
505 rescue ActiveRecord::RecordNotFound
494 rescue ActiveRecord::RecordNotFound
506 render_404
495 render_404
507 end
496 end
508
497
509 # Retrieve query from session or build a new query
498 # Retrieve query from session or build a new query
510 def retrieve_query
499 def retrieve_query
511 if !params[:query_id].blank?
500 if !params[:query_id].blank?
512 cond = "project_id IS NULL"
501 cond = "project_id IS NULL"
513 cond << " OR project_id = #{@project.id}" if @project
502 cond << " OR project_id = #{@project.id}" if @project
514 @query = Query.find(params[:query_id], :conditions => cond)
503 @query = Query.find(params[:query_id], :conditions => cond)
515 @query.project = @project
504 @query.project = @project
516 session[:query] = {:id => @query.id, :project_id => @query.project_id}
505 session[:query] = {:id => @query.id, :project_id => @query.project_id}
517 sort_clear
506 sort_clear
518 else
507 else
519 if api_request? || params[:set_filter] || session[:query].nil? || session[:query][:project_id] != (@project ? @project.id : nil)
508 if api_request? || params[:set_filter] || session[:query].nil? || session[:query][:project_id] != (@project ? @project.id : nil)
520 # Give it a name, required to be valid
509 # Give it a name, required to be valid
521 @query = Query.new(:name => "_")
510 @query = Query.new(:name => "_")
522 @query.project = @project
511 @query.project = @project
523 if params[:fields] and params[:fields].is_a? Array
512 if params[:fields] and params[:fields].is_a? Array
524 params[:fields].each do |field|
513 params[:fields].each do |field|
525 @query.add_filter(field, params[:operators][field], params[:values][field])
514 @query.add_filter(field, params[:operators][field], params[:values][field])
526 end
515 end
527 else
516 else
528 @query.available_filters.keys.each do |field|
517 @query.available_filters.keys.each do |field|
529 @query.add_short_filter(field, params[field]) if params[field]
518 @query.add_short_filter(field, params[field]) if params[field]
530 end
519 end
531 end
520 end
532 @query.group_by = params[:group_by]
521 @query.group_by = params[:group_by]
533 @query.column_names = params[:query] && params[:query][:column_names]
522 @query.column_names = params[:query] && params[:query][:column_names]
534 session[:query] = {:project_id => @query.project_id, :filters => @query.filters, :group_by => @query.group_by, :column_names => @query.column_names}
523 session[:query] = {:project_id => @query.project_id, :filters => @query.filters, :group_by => @query.group_by, :column_names => @query.column_names}
535 else
524 else
536 @query = Query.find_by_id(session[:query][:id]) if session[:query][:id]
525 @query = Query.find_by_id(session[:query][:id]) if session[:query][:id]
537 @query ||= Query.new(:name => "_", :project => @project, :filters => session[:query][:filters], :group_by => session[:query][:group_by], :column_names => session[:query][:column_names])
526 @query ||= Query.new(:name => "_", :project => @project, :filters => session[:query][:filters], :group_by => session[:query][:group_by], :column_names => session[:query][:column_names])
538 @query.project = @project
527 @query.project = @project
539 end
528 end
540 end
529 end
541 end
530 end
542
531
543 # Rescues an invalid query statement. Just in case...
532 # Rescues an invalid query statement. Just in case...
544 def query_statement_invalid(exception)
533 def query_statement_invalid(exception)
545 logger.error "Query::StatementInvalid: #{exception.message}" if logger
534 logger.error "Query::StatementInvalid: #{exception.message}" if logger
546 session.delete(:query)
535 session.delete(:query)
547 sort_clear
536 sort_clear
548 render_error "An error occurred while executing the query and has been logged. Please report this error to your Redmine administrator."
537 render_error "An error occurred while executing the query and has been logged. Please report this error to your Redmine administrator."
549 end
538 end
550
539
551 # Used by #edit and #update to set some common instance variables
540 # Used by #edit and #update to set some common instance variables
552 # from the params
541 # from the params
553 # TODO: Refactor, not everything in here is needed by #edit
542 # TODO: Refactor, not everything in here is needed by #edit
554 def update_issue_from_params
543 def update_issue_from_params
555 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
544 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
556 @priorities = IssuePriority.all
545 @priorities = IssuePriority.all
557 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
546 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
558 @time_entry = TimeEntry.new
547 @time_entry = TimeEntry.new
559
548
560 @notes = params[:notes]
549 @notes = params[:notes]
561 @issue.init_journal(User.current, @notes)
550 @issue.init_journal(User.current, @notes)
562 # User can change issue attributes only if he has :edit permission or if a workflow transition is allowed
551 # User can change issue attributes only if he has :edit permission or if a workflow transition is allowed
563 if (@edit_allowed || !@allowed_statuses.empty?) && params[:issue]
552 if (@edit_allowed || !@allowed_statuses.empty?) && params[:issue]
564 attrs = params[:issue].dup
553 attrs = params[:issue].dup
565 attrs.delete_if {|k,v| !UPDATABLE_ATTRS_ON_TRANSITION.include?(k) } unless @edit_allowed
554 attrs.delete_if {|k,v| !UPDATABLE_ATTRS_ON_TRANSITION.include?(k) } unless @edit_allowed
566 attrs.delete(:status_id) unless @allowed_statuses.detect {|s| s.id.to_s == attrs[:status_id].to_s}
555 attrs.delete(:status_id) unless @allowed_statuses.detect {|s| s.id.to_s == attrs[:status_id].to_s}
567 @issue.safe_attributes = attrs
556 @issue.safe_attributes = attrs
568 end
557 end
569
558
570 end
559 end
560
561 def set_flash_from_bulk_issue_save(issues, unsaved_issue_ids)
562 if unsaved_issue_ids.empty?
563 flash[:notice] = l(:notice_successful_update) unless issues.empty?
564 else
565 flash[:error] = l(:notice_failed_to_save_issues,
566 :count => unsaved_issue_ids.size,
567 :total => issues.size,
568 :ids => '#' + unsaved_issue_ids.join(', #'))
569 end
570 end
571 end
571 end
General Comments 0
You need to be logged in to leave comments. Login now