##// END OF EJS Templates
Fixed: don't reset watchers when changing tracker. #6479...
Jean-Baptiste Barth -
r4067:2f380a798592
parent child
Show More
@@ -1,326 +1,329
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2008 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class IssuesController < ApplicationController
19 19 menu_item :new_issue, :only => [:new, :create]
20 20 default_search_scope :issues
21 21
22 22 before_filter :find_issue, :only => [:show, :edit, :update]
23 23 before_filter :find_issues, :only => [:bulk_edit, :bulk_update, :move, :perform_move, :destroy]
24 24 before_filter :find_project, :only => [:new, :create]
25 25 before_filter :authorize, :except => [:index]
26 26 before_filter :find_optional_project, :only => [:index]
27 27 before_filter :check_for_default_issue_status, :only => [:new, :create]
28 28 before_filter :build_new_issue_from_params, :only => [:new, :create]
29 29 accept_key_auth :index, :show
30 30
31 31 rescue_from Query::StatementInvalid, :with => :query_statement_invalid
32 32
33 33 helper :journals
34 34 helper :projects
35 35 include ProjectsHelper
36 36 helper :custom_fields
37 37 include CustomFieldsHelper
38 38 helper :issue_relations
39 39 include IssueRelationsHelper
40 40 helper :watchers
41 41 include WatchersHelper
42 42 helper :attachments
43 43 include AttachmentsHelper
44 44 helper :queries
45 45 include QueriesHelper
46 46 helper :sort
47 47 include SortHelper
48 48 include IssuesHelper
49 49 helper :timelog
50 50 helper :gantt
51 51 include Redmine::Export::PDF
52 52
53 53 verify :method => [:post, :delete],
54 54 :only => :destroy,
55 55 :render => { :nothing => true, :status => :method_not_allowed }
56 56
57 57 verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed }
58 58 verify :method => :post, :only => :bulk_update, :render => {:nothing => true, :status => :method_not_allowed }
59 59 verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
60 60
61 61 def index
62 62 retrieve_query
63 63 sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
64 64 sort_update(@query.sortable_columns)
65 65
66 66 if @query.valid?
67 67 limit = case params[:format]
68 68 when 'csv', 'pdf'
69 69 Setting.issues_export_limit.to_i
70 70 when 'atom'
71 71 Setting.feeds_limit.to_i
72 72 else
73 73 per_page_option
74 74 end
75 75
76 76 @issue_count = @query.issue_count
77 77 @issue_pages = Paginator.new self, @issue_count, limit, params['page']
78 78 @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version],
79 79 :order => sort_clause,
80 80 :offset => @issue_pages.current.offset,
81 81 :limit => limit)
82 82 @issue_count_by_group = @query.issue_count_by_group
83 83
84 84 respond_to do |format|
85 85 format.html { render :template => 'issues/index.rhtml', :layout => !request.xhr? }
86 86 format.xml { render :layout => false }
87 87 format.json { render :text => @issues.to_json, :layout => false }
88 88 format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") }
89 89 format.csv { send_data(issues_to_csv(@issues, @project), :type => 'text/csv; header=present', :filename => 'export.csv') }
90 90 format.pdf { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'export.pdf') }
91 91 end
92 92 else
93 93 # Send html if the query is not valid
94 94 render(:template => 'issues/index.rhtml', :layout => !request.xhr?)
95 95 end
96 96 rescue ActiveRecord::RecordNotFound
97 97 render_404
98 98 end
99 99
100 100 def show
101 101 @journals = @issue.journals.find(:all, :include => [:user, :details], :order => "#{Journal.table_name}.created_on ASC")
102 102 @journals.each_with_index {|j,i| j.indice = i+1}
103 103 @journals.reverse! if User.current.wants_comments_in_reverse_order?
104 104 @changesets = @issue.changesets.visible.all
105 105 @changesets.reverse! if User.current.wants_comments_in_reverse_order?
106 106 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
107 107 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
108 108 @priorities = IssuePriority.all
109 109 @time_entry = TimeEntry.new
110 110 respond_to do |format|
111 111 format.html { render :template => 'issues/show.rhtml' }
112 112 format.xml { render :layout => false }
113 113 format.json { render :text => @issue.to_json, :layout => false }
114 114 format.atom { render :template => 'journals/index', :layout => false, :content_type => 'application/atom+xml' }
115 115 format.pdf { send_data(issue_to_pdf(@issue), :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf") }
116 116 end
117 117 end
118 118
119 119 # Add a new issue
120 120 # The new issue will be created from an existing one if copy_from parameter is given
121 121 def new
122 122 respond_to do |format|
123 123 format.html { render :action => 'new', :layout => !request.xhr? }
124 124 format.js { render :partial => 'attributes' }
125 125 end
126 126 end
127 127
128 128 def create
129 129 call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue })
130 130 if @issue.save
131 131 attachments = Attachment.attach_files(@issue, params[:attachments])
132 132 render_attachment_warning_if_needed(@issue)
133 133 flash[:notice] = l(:notice_successful_create)
134 134 call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue})
135 135 respond_to do |format|
136 136 format.html {
137 137 redirect_to(params[:continue] ? { :action => 'new', :project_id => @project, :issue => {:tracker_id => @issue.tracker, :parent_issue_id => @issue.parent_issue_id}.reject {|k,v| v.nil?} } :
138 138 { :action => 'show', :id => @issue })
139 139 }
140 140 format.xml { render :action => 'show', :status => :created, :location => url_for(:controller => 'issues', :action => 'show', :id => @issue) }
141 141 format.json { render :text => @issue.to_json, :status => :created, :location => url_for(:controller => 'issues', :action => 'show'), :layout => false }
142 142 end
143 143 return
144 144 else
145 145 respond_to do |format|
146 146 format.html { render :action => 'new' }
147 147 format.xml { render(:xml => @issue.errors, :status => :unprocessable_entity); return }
148 148 format.json { render :text => object_errors_to_json(@issue), :status => :unprocessable_entity, :layout => false }
149 149 end
150 150 end
151 151 end
152 152
153 153 # Attributes that can be updated on workflow transition (without :edit permission)
154 154 # TODO: make it configurable (at least per role)
155 155 UPDATABLE_ATTRS_ON_TRANSITION = %w(status_id assigned_to_id fixed_version_id done_ratio) unless const_defined?(:UPDATABLE_ATTRS_ON_TRANSITION)
156 156
157 157 def edit
158 158 update_issue_from_params
159 159
160 160 @journal = @issue.current_journal
161 161
162 162 respond_to do |format|
163 163 format.html { }
164 164 format.xml { }
165 165 end
166 166 end
167 167
168 168 def update
169 169 update_issue_from_params
170 170
171 171 if @issue.save_issue_with_child_records(params, @time_entry)
172 172 render_attachment_warning_if_needed(@issue)
173 173 flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
174 174
175 175 respond_to do |format|
176 176 format.html { redirect_back_or_default({:action => 'show', :id => @issue}) }
177 177 format.xml { head :ok }
178 178 format.json { head :ok }
179 179 end
180 180 else
181 181 render_attachment_warning_if_needed(@issue)
182 182 flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
183 183 @journal = @issue.current_journal
184 184
185 185 respond_to do |format|
186 186 format.html { render :action => 'edit' }
187 187 format.xml { render :xml => @issue.errors, :status => :unprocessable_entity }
188 188 format.json { render :text => object_errors_to_json(@issue), :status => :unprocessable_entity, :layout => false }
189 189 end
190 190 end
191 191 end
192 192
193 193 # Bulk edit a set of issues
194 194 def bulk_edit
195 195 @issues.sort!
196 196 @available_statuses = Workflow.available_statuses(@project)
197 197 @custom_fields = @project.all_issue_custom_fields
198 198 end
199 199
200 200 def bulk_update
201 201 @issues.sort!
202 202 attributes = parse_params_for_bulk_issue_attributes(params)
203 203
204 204 unsaved_issue_ids = []
205 205 @issues.each do |issue|
206 206 issue.reload
207 207 journal = issue.init_journal(User.current, params[:notes])
208 208 issue.safe_attributes = attributes
209 209 call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue })
210 210 unless issue.save
211 211 # Keep unsaved issue ids to display them in flash error
212 212 unsaved_issue_ids << issue.id
213 213 end
214 214 end
215 215 set_flash_from_bulk_issue_save(@issues, unsaved_issue_ids)
216 216 redirect_back_or_default({:controller => 'issues', :action => 'index', :project_id => @project})
217 217 end
218 218
219 219 def destroy
220 220 @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f
221 221 if @hours > 0
222 222 case params[:todo]
223 223 when 'destroy'
224 224 # nothing to do
225 225 when 'nullify'
226 226 TimeEntry.update_all('issue_id = NULL', ['issue_id IN (?)', @issues])
227 227 when 'reassign'
228 228 reassign_to = @project.issues.find_by_id(params[:reassign_to_id])
229 229 if reassign_to.nil?
230 230 flash.now[:error] = l(:error_issue_not_found_in_project)
231 231 return
232 232 else
233 233 TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues])
234 234 end
235 235 else
236 236 unless params[:format] == 'xml' || params[:format] == 'json'
237 237 # display the destroy form if it's a user request
238 238 return
239 239 end
240 240 end
241 241 end
242 242 @issues.each(&:destroy)
243 243 respond_to do |format|
244 244 format.html { redirect_to :action => 'index', :project_id => @project }
245 245 format.xml { head :ok }
246 246 format.json { head :ok }
247 247 end
248 248 end
249 249
250 250 private
251 251 def find_issue
252 252 @issue = Issue.find(params[:id], :include => [:project, :tracker, :status, :author, :priority, :category])
253 253 @project = @issue.project
254 254 rescue ActiveRecord::RecordNotFound
255 255 render_404
256 256 end
257 257
258 258 def find_project
259 259 project_id = (params[:issue] && params[:issue][:project_id]) || params[:project_id]
260 260 @project = Project.find(project_id)
261 261 rescue ActiveRecord::RecordNotFound
262 262 render_404
263 263 end
264 264
265 265 # Used by #edit and #update to set some common instance variables
266 266 # from the params
267 267 # TODO: Refactor, not everything in here is needed by #edit
268 268 def update_issue_from_params
269 269 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
270 270 @priorities = IssuePriority.all
271 271 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
272 272 @time_entry = TimeEntry.new
273 273
274 274 @notes = params[:notes] || (params[:issue].present? ? params[:issue][:notes] : nil)
275 275 @issue.init_journal(User.current, @notes)
276 276 # User can change issue attributes only if he has :edit permission or if a workflow transition is allowed
277 277 if (@edit_allowed || !@allowed_statuses.empty?) && params[:issue]
278 278 attrs = params[:issue].dup
279 279 attrs.delete_if {|k,v| !UPDATABLE_ATTRS_ON_TRANSITION.include?(k) } unless @edit_allowed
280 280 attrs.delete(:status_id) unless @allowed_statuses.detect {|s| s.id.to_s == attrs[:status_id].to_s}
281 281 @issue.safe_attributes = attrs
282 282 end
283 283
284 284 end
285 285
286 286 # TODO: Refactor, lots of extra code in here
287 # TODO: Changing tracker on an existing issue should not trigger this
287 288 def build_new_issue_from_params
288 289 if params[:id].blank?
289 290 @issue = Issue.new
290 291 @issue.copy_from(params[:copy_from]) if params[:copy_from]
291 292 @issue.project = @project
292 293 else
293 294 @issue = @project.issues.visible.find(params[:id])
294 295 end
295 296
296 297 @issue.project = @project
297 298 # Tracker must be set before custom field values
298 299 @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first)
299 300 if @issue.tracker.nil?
300 301 render_error l(:error_no_tracker_in_project)
301 302 return false
302 303 end
303 304 if params[:issue].is_a?(Hash)
304 305 @issue.safe_attributes = params[:issue]
305 @issue.watcher_user_ids = params[:issue]['watcher_user_ids'] if User.current.allowed_to?(:add_issue_watchers, @project)
306 if User.current.allowed_to?(:add_issue_watchers, @project) && @issue.new_record?
307 @issue.watcher_user_ids = params[:issue]['watcher_user_ids']
308 end
306 309 end
307 310 @issue.author = User.current
308 311 @issue.start_date ||= Date.today
309 312 @priorities = IssuePriority.all
310 313 @allowed_statuses = @issue.new_statuses_allowed_to(User.current, true)
311 314 end
312 315
313 316 def check_for_default_issue_status
314 317 if IssueStatus.default.nil?
315 318 render_error l(:error_no_default_issue_status)
316 319 return false
317 320 end
318 321 end
319 322
320 323 def parse_params_for_bulk_issue_attributes(params)
321 324 attributes = (params[:issue] || {}).reject {|k,v| v.blank?}
322 325 attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'}
323 326 attributes[:custom_field_values].reject! {|k,v| v.blank?} if attributes[:custom_field_values]
324 327 attributes
325 328 end
326 329 end
General Comments 0
You need to be logged in to leave comments. Login now