##// END OF EJS Templates
Code cleanup....
Jean-Philippe Lang -
r9724:86617fc43788
parent child
Show More
@@ -1,442 +1,442
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2012 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, :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_rss_auth :index, :show
30 30 accept_api_auth :index, :show, :create, :update, :destroy
31 31
32 32 rescue_from Query::StatementInvalid, :with => :query_statement_invalid
33 33
34 34 helper :journals
35 35 helper :projects
36 36 include ProjectsHelper
37 37 helper :custom_fields
38 38 include CustomFieldsHelper
39 39 helper :issue_relations
40 40 include IssueRelationsHelper
41 41 helper :watchers
42 42 include WatchersHelper
43 43 helper :attachments
44 44 include AttachmentsHelper
45 45 helper :queries
46 46 include QueriesHelper
47 47 helper :repositories
48 48 include RepositoriesHelper
49 49 helper :sort
50 50 include SortHelper
51 51 include IssuesHelper
52 52 helper :timelog
53 53 helper :gantt
54 54 include Redmine::Export::PDF
55 55
56 56 def index
57 57 retrieve_query
58 58 sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
59 59 sort_update(@query.sortable_columns)
60 60
61 61 if @query.valid?
62 62 case params[:format]
63 63 when 'csv', 'pdf'
64 64 @limit = Setting.issues_export_limit.to_i
65 65 when 'atom'
66 66 @limit = Setting.feeds_limit.to_i
67 67 when 'xml', 'json'
68 68 @offset, @limit = api_offset_and_limit
69 69 else
70 70 @limit = per_page_option
71 71 end
72 72
73 73 @issue_count = @query.issue_count
74 74 @issue_pages = Paginator.new self, @issue_count, @limit, params['page']
75 75 @offset ||= @issue_pages.current.offset
76 76 @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version],
77 77 :order => sort_clause,
78 78 :offset => @offset,
79 79 :limit => @limit)
80 80 @issue_count_by_group = @query.issue_count_by_group
81 81
82 82 respond_to do |format|
83 83 format.html { render :template => 'issues/index', :layout => !request.xhr? }
84 84 format.api {
85 85 Issue.load_relations(@issues) if include_in_api_response?('relations')
86 86 }
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, @query, params), :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 respond_to do |format|
93 93 format.html { render(:template => 'issues/index', :layout => !request.xhr?) }
94 94 format.any(:atom, :csv, :pdf) { render(:nothing => true) }
95 95 format.api { render_validation_errors(@query) }
96 96 end
97 97 end
98 98 rescue ActiveRecord::RecordNotFound
99 99 render_404
100 100 end
101 101
102 102 def show
103 103 @journals = @issue.journals.find(:all, :include => [:user, :details], :order => "#{Journal.table_name}.created_on ASC")
104 104 @journals.each_with_index {|j,i| j.indice = i+1}
105 105 @journals.reverse! if User.current.wants_comments_in_reverse_order?
106 106
107 107 @changesets = @issue.changesets.visible.all
108 108 @changesets.reverse! if User.current.wants_comments_in_reverse_order?
109 109
110 110 @relations = @issue.relations.select {|r| r.other_issue(@issue) && r.other_issue(@issue).visible? }
111 111 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
112 112 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
113 113 @priorities = IssuePriority.active
114 114 @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
115 115 respond_to do |format|
116 116 format.html {
117 117 retrieve_previous_and_next_issue_ids
118 118 render :template => 'issues/show'
119 119 }
120 120 format.api
121 121 format.atom { render :template => 'journals/index', :layout => false, :content_type => 'application/atom+xml' }
122 122 format.pdf { send_data(issue_to_pdf(@issue), :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf") }
123 123 end
124 124 end
125 125
126 126 # Add a new issue
127 127 # The new issue will be created from an existing one if copy_from parameter is given
128 128 def new
129 129 respond_to do |format|
130 130 format.html { render :action => 'new', :layout => !request.xhr? }
131 131 format.js {
132 132 render(:update) { |page|
133 133 if params[:project_change]
134 134 page.replace_html 'all_attributes', :partial => 'form'
135 135 else
136 136 page.replace_html 'attributes', :partial => 'attributes'
137 137 end
138 138 m = User.current.allowed_to?(:log_time, @issue.project) ? 'show' : 'hide'
139 139 page << "if ($('log_time')) {Element.#{m}('log_time');}"
140 140 }
141 141 }
142 142 end
143 143 end
144 144
145 145 def create
146 146 call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue })
147 147 @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads]))
148 148 if @issue.save
149 149 call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue})
150 150 respond_to do |format|
151 151 format.html {
152 152 render_attachment_warning_if_needed(@issue)
153 flash[:notice] = l(:notice_issue_successful_create, :id => "<a href='#{issue_path(@issue)}'>##{@issue.id}</a>")
153 flash[:notice] = l(:notice_issue_successful_create, :id => view_context.link_to("##{@issue.id}", issue_path(@issue)))
154 154 redirect_to(params[:continue] ? { :action => 'new', :project_id => @issue.project, :issue => {:tracker_id => @issue.tracker, :parent_issue_id => @issue.parent_issue_id}.reject {|k,v| v.nil?} } :
155 155 { :action => 'show', :id => @issue })
156 156 }
157 157 format.api { render :action => 'show', :status => :created, :location => issue_url(@issue) }
158 158 end
159 159 return
160 160 else
161 161 respond_to do |format|
162 162 format.html { render :action => 'new' }
163 163 format.api { render_validation_errors(@issue) }
164 164 end
165 165 end
166 166 end
167 167
168 168 def edit
169 169 return unless update_issue_from_params
170 170
171 171 respond_to do |format|
172 172 format.html { }
173 173 format.xml { }
174 174 end
175 175 end
176 176
177 177 def update
178 178 return unless update_issue_from_params
179 179 @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads]))
180 180 saved = false
181 181 begin
182 182 saved = @issue.save_issue_with_child_records(params, @time_entry)
183 183 rescue ActiveRecord::StaleObjectError
184 184 @conflict = true
185 185 if params[:last_journal_id]
186 186 if params[:last_journal_id].present?
187 187 last_journal_id = params[:last_journal_id].to_i
188 188 @conflict_journals = @issue.journals.all(:conditions => ["#{Journal.table_name}.id > ?", last_journal_id])
189 189 else
190 190 @conflict_journals = @issue.journals.all
191 191 end
192 192 end
193 193 end
194 194
195 195 if saved
196 196 render_attachment_warning_if_needed(@issue)
197 197 flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
198 198
199 199 respond_to do |format|
200 200 format.html { redirect_back_or_default({:action => 'show', :id => @issue}) }
201 201 format.api { head :ok }
202 202 end
203 203 else
204 204 respond_to do |format|
205 205 format.html { render :action => 'edit' }
206 206 format.api { render_validation_errors(@issue) }
207 207 end
208 208 end
209 209 end
210 210
211 211 # Bulk edit/copy a set of issues
212 212 def bulk_edit
213 213 @issues.sort!
214 214 @copy = params[:copy].present?
215 215 @notes = params[:notes]
216 216
217 217 if User.current.allowed_to?(:move_issues, @projects)
218 218 @allowed_projects = Issue.allowed_target_projects_on_move
219 219 if params[:issue]
220 220 @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:issue][:project_id].to_s}
221 221 if @target_project
222 222 target_projects = [@target_project]
223 223 end
224 224 end
225 225 end
226 226 target_projects ||= @projects
227 227
228 228 if @copy
229 229 @available_statuses = [IssueStatus.default]
230 230 else
231 231 @available_statuses = @issues.map(&:new_statuses_allowed_to).reduce(:&)
232 232 end
233 233 @custom_fields = target_projects.map{|p|p.all_issue_custom_fields}.reduce(:&)
234 234 @assignables = target_projects.map(&:assignable_users).reduce(:&)
235 235 @trackers = target_projects.map(&:trackers).reduce(:&)
236 236 @versions = target_projects.map {|p| p.shared_versions.open}.reduce(:&)
237 237 @categories = target_projects.map {|p| p.issue_categories}.reduce(:&)
238 238 if @copy
239 239 @attachments_present = @issues.detect {|i| i.attachments.any?}.present?
240 240 end
241 241
242 242 @safe_attributes = @issues.map(&:safe_attribute_names).reduce(:&)
243 243 render :layout => false if request.xhr?
244 244 end
245 245
246 246 def bulk_update
247 247 @issues.sort!
248 248 @copy = params[:copy].present?
249 249 attributes = parse_params_for_bulk_issue_attributes(params)
250 250
251 251 unsaved_issue_ids = []
252 252 moved_issues = []
253 253 @issues.each do |issue|
254 254 issue.reload
255 255 if @copy
256 256 issue = issue.copy({}, :attachments => params[:copy_attachments].present?)
257 257 end
258 258 journal = issue.init_journal(User.current, params[:notes])
259 259 issue.safe_attributes = attributes
260 260 call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue })
261 261 if issue.save
262 262 moved_issues << issue
263 263 else
264 264 # Keep unsaved issue ids to display them in flash error
265 265 unsaved_issue_ids << issue.id
266 266 end
267 267 end
268 268 set_flash_from_bulk_issue_save(@issues, unsaved_issue_ids)
269 269
270 270 if params[:follow]
271 271 if @issues.size == 1 && moved_issues.size == 1
272 272 redirect_to :controller => 'issues', :action => 'show', :id => moved_issues.first
273 273 elsif moved_issues.map(&:project).uniq.size == 1
274 274 redirect_to :controller => 'issues', :action => 'index', :project_id => moved_issues.map(&:project).first
275 275 end
276 276 else
277 277 redirect_back_or_default({:controller => 'issues', :action => 'index', :project_id => @project})
278 278 end
279 279 end
280 280
281 281 def destroy
282 282 @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f
283 283 if @hours > 0
284 284 case params[:todo]
285 285 when 'destroy'
286 286 # nothing to do
287 287 when 'nullify'
288 288 TimeEntry.update_all('issue_id = NULL', ['issue_id IN (?)', @issues])
289 289 when 'reassign'
290 290 reassign_to = @project.issues.find_by_id(params[:reassign_to_id])
291 291 if reassign_to.nil?
292 292 flash.now[:error] = l(:error_issue_not_found_in_project)
293 293 return
294 294 else
295 295 TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues])
296 296 end
297 297 else
298 298 # display the destroy form if it's a user request
299 299 return unless api_request?
300 300 end
301 301 end
302 302 @issues.each do |issue|
303 303 begin
304 304 issue.reload.destroy
305 305 rescue ::ActiveRecord::RecordNotFound # raised by #reload if issue no longer exists
306 306 # nothing to do, issue was already deleted (eg. by a parent)
307 307 end
308 308 end
309 309 respond_to do |format|
310 310 format.html { redirect_back_or_default(:action => 'index', :project_id => @project) }
311 311 format.api { head :ok }
312 312 end
313 313 end
314 314
315 315 private
316 316 def find_issue
317 317 # Issue.visible.find(...) can not be used to redirect user to the login form
318 318 # if the issue actually exists but requires authentication
319 319 @issue = Issue.find(params[:id], :include => [:project, :tracker, :status, :author, :priority, :category])
320 320 unless @issue.visible?
321 321 deny_access
322 322 return
323 323 end
324 324 @project = @issue.project
325 325 rescue ActiveRecord::RecordNotFound
326 326 render_404
327 327 end
328 328
329 329 def find_project
330 330 project_id = params[:project_id] || (params[:issue] && params[:issue][:project_id])
331 331 @project = Project.find(project_id)
332 332 rescue ActiveRecord::RecordNotFound
333 333 render_404
334 334 end
335 335
336 336 def retrieve_previous_and_next_issue_ids
337 337 retrieve_query_from_session
338 338 if @query
339 339 sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
340 340 sort_update(@query.sortable_columns, 'issues_index_sort')
341 341 limit = 500
342 342 issue_ids = @query.issue_ids(:order => sort_clause, :limit => (limit + 1), :include => [:assigned_to, :tracker, :priority, :category, :fixed_version])
343 343 if (idx = issue_ids.index(@issue.id)) && idx < limit
344 344 if issue_ids.size < 500
345 345 @issue_position = idx + 1
346 346 @issue_count = issue_ids.size
347 347 end
348 348 @prev_issue_id = issue_ids[idx - 1] if idx > 0
349 349 @next_issue_id = issue_ids[idx + 1] if idx < (issue_ids.size - 1)
350 350 end
351 351 end
352 352 end
353 353
354 354 # Used by #edit and #update to set some common instance variables
355 355 # from the params
356 356 # TODO: Refactor, not everything in here is needed by #edit
357 357 def update_issue_from_params
358 358 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
359 359 @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
360 360 @time_entry.attributes = params[:time_entry]
361 361
362 362 @notes = params[:notes] || (params[:issue].present? ? params[:issue][:notes] : nil)
363 363 @issue.init_journal(User.current, @notes)
364 364
365 365 issue_attributes = params[:issue]
366 366 if issue_attributes && params[:conflict_resolution]
367 367 case params[:conflict_resolution]
368 368 when 'overwrite'
369 369 issue_attributes = issue_attributes.dup
370 370 issue_attributes.delete(:lock_version)
371 371 when 'add_notes'
372 372 issue_attributes = {}
373 373 when 'cancel'
374 374 redirect_to issue_path(@issue)
375 375 return false
376 376 end
377 377 end
378 378 @issue.safe_attributes = issue_attributes
379 379 @priorities = IssuePriority.active
380 380 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
381 381 true
382 382 end
383 383
384 384 # TODO: Refactor, lots of extra code in here
385 385 # TODO: Changing tracker on an existing issue should not trigger this
386 386 def build_new_issue_from_params
387 387 if params[:id].blank?
388 388 @issue = Issue.new
389 389 if params[:copy_from]
390 390 begin
391 391 @copy_from = Issue.visible.find(params[:copy_from])
392 392 @copy_attachments = params[:copy_attachments].present? || request.get?
393 393 @issue.copy_from(@copy_from, :attachments => @copy_attachments)
394 394 rescue ActiveRecord::RecordNotFound
395 395 render_404
396 396 return
397 397 end
398 398 end
399 399 @issue.project = @project
400 400 else
401 401 @issue = @project.issues.visible.find(params[:id])
402 402 end
403 403
404 404 @issue.project = @project
405 405 @issue.author = User.current
406 406 # Tracker must be set before custom field values
407 407 @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first)
408 408 if @issue.tracker.nil?
409 409 render_error l(:error_no_tracker_in_project)
410 410 return false
411 411 end
412 412 @issue.start_date ||= Date.today if Setting.default_issue_start_date_to_creation_date?
413 413 @issue.safe_attributes = params[:issue]
414 414
415 415 @priorities = IssuePriority.active
416 416 @allowed_statuses = @issue.new_statuses_allowed_to(User.current, true)
417 417 @available_watchers = (@issue.project.users.sort + @issue.watcher_users).uniq
418 418 end
419 419
420 420 def check_for_default_issue_status
421 421 if IssueStatus.default.nil?
422 422 render_error l(:error_no_default_issue_status)
423 423 return false
424 424 end
425 425 end
426 426
427 427 def parse_params_for_bulk_issue_attributes(params)
428 428 attributes = (params[:issue] || {}).reject {|k,v| v.blank?}
429 429 attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'}
430 430 if custom = attributes[:custom_field_values]
431 431 custom.reject! {|k,v| v.blank?}
432 432 custom.keys.each do |k|
433 433 if custom[k].is_a?(Array)
434 434 custom[k] << '' if custom[k].delete('__none__')
435 435 else
436 436 custom[k] = '' if custom[k] == '__none__'
437 437 end
438 438 end
439 439 end
440 440 attributes
441 441 end
442 442 end
General Comments 0
You need to be logged in to leave comments. Login now