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