##// END OF EJS Templates
Merged r4181 from trunk....
Eric Davis -
r4086:c4e123b2f002
parent child
Show More
@@ -1,325 +1,328
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 include Redmine::Export::PDF
51 51
52 52 verify :method => [:post, :delete],
53 53 :only => :destroy,
54 54 :render => { :nothing => true, :status => :method_not_allowed }
55 55
56 56 verify :method => :post, :only => :create, :render => {:nothing => true, :status => :method_not_allowed }
57 57 verify :method => :post, :only => :bulk_update, :render => {:nothing => true, :status => :method_not_allowed }
58 58 verify :method => :put, :only => :update, :render => {:nothing => true, :status => :method_not_allowed }
59 59
60 60 def index
61 61 retrieve_query
62 62 sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
63 63 sort_update(@query.sortable_columns)
64 64
65 65 if @query.valid?
66 66 limit = case params[:format]
67 67 when 'csv', 'pdf'
68 68 Setting.issues_export_limit.to_i
69 69 when 'atom'
70 70 Setting.feeds_limit.to_i
71 71 else
72 72 per_page_option
73 73 end
74 74
75 75 @issue_count = @query.issue_count
76 76 @issue_pages = Paginator.new self, @issue_count, limit, params['page']
77 77 @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version],
78 78 :order => sort_clause,
79 79 :offset => @issue_pages.current.offset,
80 80 :limit => limit)
81 81 @issue_count_by_group = @query.issue_count_by_group
82 82
83 83 respond_to do |format|
84 84 format.html { render :template => 'issues/index.rhtml', :layout => !request.xhr? }
85 85 format.xml { render :layout => false }
86 86 format.json { render :text => @issues.to_json, :layout => false }
87 87 format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") }
88 88 format.csv { send_data(issues_to_csv(@issues, @project), :type => 'text/csv; header=present', :filename => 'export.csv') }
89 89 format.pdf { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'export.pdf') }
90 90 end
91 91 else
92 92 # Send html if the query is not valid
93 93 render(:template => 'issues/index.rhtml', :layout => !request.xhr?)
94 94 end
95 95 rescue ActiveRecord::RecordNotFound
96 96 render_404
97 97 end
98 98
99 99 def show
100 100 @journals = @issue.journals.find(:all, :include => [:user, :details], :order => "#{Journal.table_name}.created_on ASC")
101 101 @journals.each_with_index {|j,i| j.indice = i+1}
102 102 @journals.reverse! if User.current.wants_comments_in_reverse_order?
103 103 @changesets = @issue.changesets.visible.all
104 104 @changesets.reverse! if User.current.wants_comments_in_reverse_order?
105 105 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
106 106 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
107 107 @priorities = IssuePriority.all
108 108 @time_entry = TimeEntry.new
109 109 respond_to do |format|
110 110 format.html { render :template => 'issues/show.rhtml' }
111 111 format.xml { render :layout => false }
112 112 format.json { render :text => @issue.to_json, :layout => false }
113 113 format.atom { render :template => 'journals/index', :layout => false, :content_type => 'application/atom+xml' }
114 114 format.pdf { send_data(issue_to_pdf(@issue), :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf") }
115 115 end
116 116 end
117 117
118 118 # Add a new issue
119 119 # The new issue will be created from an existing one if copy_from parameter is given
120 120 def new
121 121 respond_to do |format|
122 122 format.html { render :action => 'new', :layout => !request.xhr? }
123 123 format.js { render :partial => 'attributes' }
124 124 end
125 125 end
126 126
127 127 def create
128 128 call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue })
129 129 if @issue.save
130 130 attachments = Attachment.attach_files(@issue, params[:attachments])
131 131 render_attachment_warning_if_needed(@issue)
132 132 flash[:notice] = l(:notice_successful_create)
133 133 call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue})
134 134 respond_to do |format|
135 135 format.html {
136 136 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?} } :
137 137 { :action => 'show', :id => @issue })
138 138 }
139 139 format.xml { render :action => 'show', :status => :created, :location => url_for(:controller => 'issues', :action => 'show', :id => @issue) }
140 140 format.json { render :text => @issue.to_json, :status => :created, :location => url_for(:controller => 'issues', :action => 'show'), :layout => false }
141 141 end
142 142 return
143 143 else
144 144 respond_to do |format|
145 145 format.html { render :action => 'new' }
146 146 format.xml { render(:xml => @issue.errors, :status => :unprocessable_entity); return }
147 147 format.json { render :text => object_errors_to_json(@issue), :status => :unprocessable_entity, :layout => false }
148 148 end
149 149 end
150 150 end
151 151
152 152 # Attributes that can be updated on workflow transition (without :edit permission)
153 153 # TODO: make it configurable (at least per role)
154 154 UPDATABLE_ATTRS_ON_TRANSITION = %w(status_id assigned_to_id fixed_version_id done_ratio) unless const_defined?(:UPDATABLE_ATTRS_ON_TRANSITION)
155 155
156 156 def edit
157 157 update_issue_from_params
158 158
159 159 @journal = @issue.current_journal
160 160
161 161 respond_to do |format|
162 162 format.html { }
163 163 format.xml { }
164 164 end
165 165 end
166 166
167 167 def update
168 168 update_issue_from_params
169 169
170 170 if @issue.save_issue_with_child_records(params, @time_entry)
171 171 render_attachment_warning_if_needed(@issue)
172 172 flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
173 173
174 174 respond_to do |format|
175 175 format.html { redirect_back_or_default({:action => 'show', :id => @issue}) }
176 176 format.xml { head :ok }
177 177 format.json { head :ok }
178 178 end
179 179 else
180 180 render_attachment_warning_if_needed(@issue)
181 181 flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
182 182 @journal = @issue.current_journal
183 183
184 184 respond_to do |format|
185 185 format.html { render :action => 'edit' }
186 186 format.xml { render :xml => @issue.errors, :status => :unprocessable_entity }
187 187 format.json { render :text => object_errors_to_json(@issue), :status => :unprocessable_entity, :layout => false }
188 188 end
189 189 end
190 190 end
191 191
192 192 # Bulk edit a set of issues
193 193 def bulk_edit
194 194 @issues.sort!
195 195 @available_statuses = Workflow.available_statuses(@project)
196 196 @custom_fields = @project.all_issue_custom_fields
197 197 end
198 198
199 199 def bulk_update
200 200 @issues.sort!
201 201 attributes = parse_params_for_bulk_issue_attributes(params)
202 202
203 203 unsaved_issue_ids = []
204 204 @issues.each do |issue|
205 205 issue.reload
206 206 journal = issue.init_journal(User.current, params[:notes])
207 207 issue.safe_attributes = attributes
208 208 call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue })
209 209 unless issue.save
210 210 # Keep unsaved issue ids to display them in flash error
211 211 unsaved_issue_ids << issue.id
212 212 end
213 213 end
214 214 set_flash_from_bulk_issue_save(@issues, unsaved_issue_ids)
215 215 redirect_back_or_default({:controller => 'issues', :action => 'index', :project_id => @project})
216 216 end
217 217
218 218 def destroy
219 219 @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f
220 220 if @hours > 0
221 221 case params[:todo]
222 222 when 'destroy'
223 223 # nothing to do
224 224 when 'nullify'
225 225 TimeEntry.update_all('issue_id = NULL', ['issue_id IN (?)', @issues])
226 226 when 'reassign'
227 227 reassign_to = @project.issues.find_by_id(params[:reassign_to_id])
228 228 if reassign_to.nil?
229 229 flash.now[:error] = l(:error_issue_not_found_in_project)
230 230 return
231 231 else
232 232 TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues])
233 233 end
234 234 else
235 235 unless params[:format] == 'xml' || params[:format] == 'json'
236 236 # display the destroy form if it's a user request
237 237 return
238 238 end
239 239 end
240 240 end
241 241 @issues.each(&:destroy)
242 242 respond_to do |format|
243 243 format.html { redirect_to :action => 'index', :project_id => @project }
244 244 format.xml { head :ok }
245 245 format.json { head :ok }
246 246 end
247 247 end
248 248
249 249 private
250 250 def find_issue
251 251 @issue = Issue.find(params[:id], :include => [:project, :tracker, :status, :author, :priority, :category])
252 252 @project = @issue.project
253 253 rescue ActiveRecord::RecordNotFound
254 254 render_404
255 255 end
256 256
257 257 def find_project
258 258 project_id = (params[:issue] && params[:issue][:project_id]) || params[:project_id]
259 259 @project = Project.find(project_id)
260 260 rescue ActiveRecord::RecordNotFound
261 261 render_404
262 262 end
263 263
264 264 # Used by #edit and #update to set some common instance variables
265 265 # from the params
266 266 # TODO: Refactor, not everything in here is needed by #edit
267 267 def update_issue_from_params
268 268 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
269 269 @priorities = IssuePriority.all
270 270 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
271 271 @time_entry = TimeEntry.new
272 272
273 273 @notes = params[:notes] || (params[:issue].present? ? params[:issue][:notes] : nil)
274 274 @issue.init_journal(User.current, @notes)
275 275 # User can change issue attributes only if he has :edit permission or if a workflow transition is allowed
276 276 if (@edit_allowed || !@allowed_statuses.empty?) && params[:issue]
277 277 attrs = params[:issue].dup
278 278 attrs.delete_if {|k,v| !UPDATABLE_ATTRS_ON_TRANSITION.include?(k) } unless @edit_allowed
279 279 attrs.delete(:status_id) unless @allowed_statuses.detect {|s| s.id.to_s == attrs[:status_id].to_s}
280 280 @issue.safe_attributes = attrs
281 281 end
282 282
283 283 end
284 284
285 285 # TODO: Refactor, lots of extra code in here
286 # TODO: Changing tracker on an existing issue should not trigger this
286 287 def build_new_issue_from_params
287 288 if params[:id].blank?
288 289 @issue = Issue.new
289 290 @issue.copy_from(params[:copy_from]) if params[:copy_from]
290 291 @issue.project = @project
291 292 else
292 293 @issue = @project.issues.visible.find(params[:id])
293 294 end
294 295
295 296 @issue.project = @project
296 297 # Tracker must be set before custom field values
297 298 @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first)
298 299 if @issue.tracker.nil?
299 300 render_error l(:error_no_tracker_in_project)
300 301 return false
301 302 end
302 303 if params[:issue].is_a?(Hash)
303 304 @issue.safe_attributes = params[:issue]
304 @issue.watcher_user_ids = params[:issue]['watcher_user_ids'] if User.current.allowed_to?(:add_issue_watchers, @project)
305 if User.current.allowed_to?(:add_issue_watchers, @project) && @issue.new_record?
306 @issue.watcher_user_ids = params[:issue]['watcher_user_ids']
307 end
305 308 end
306 309 @issue.author = User.current
307 310 @issue.start_date ||= Date.today
308 311 @priorities = IssuePriority.all
309 312 @allowed_statuses = @issue.new_statuses_allowed_to(User.current, true)
310 313 end
311 314
312 315 def check_for_default_issue_status
313 316 if IssueStatus.default.nil?
314 317 render_error l(:error_no_default_issue_status)
315 318 return false
316 319 end
317 320 end
318 321
319 322 def parse_params_for_bulk_issue_attributes(params)
320 323 attributes = (params[:issue] || {}).reject {|k,v| v.blank?}
321 324 attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'}
322 325 attributes[:custom_field_values].reject! {|k,v| v.blank?} if attributes[:custom_field_values]
323 326 attributes
324 327 end
325 328 end
General Comments 0
You need to be logged in to leave comments. Login now