comparison .svn/pristine/f4/f440a7eb20f70f4e22fe7c8f34d5eed9370c603a.svn-base @ 1296:038ba2d95de8 redmine-2.2

Fix redmine-2.2 branch update (add missing svn files)
author Chris Cannam
date Fri, 14 Jun 2013 09:05:06 +0100
parents
children
comparison
equal deleted inserted replaced
1294:3e4c3460b6ca 1296:038ba2d95de8
1 # Redmine - project management software
2 # Copyright (C) 2006-2012 Jean-Philippe Lang
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
13 #
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
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
18 class IssuesController < ApplicationController
19 menu_item :new_issue, :only => [:new, :create]
20 default_search_scope :issues
21
22 before_filter :find_issue, :only => [:show, :edit, :update]
23 before_filter :find_issues, :only => [:bulk_edit, :bulk_update, :destroy]
24 before_filter :find_project, :only => [:new, :create]
25 before_filter :authorize, :except => [:index]
26 before_filter :find_optional_project, :only => [:index]
27 before_filter :check_for_default_issue_status, :only => [:new, :create]
28 before_filter :build_new_issue_from_params, :only => [:new, :create]
29 accept_rss_auth :index, :show
30 accept_api_auth :index, :show, :create, :update, :destroy
31
32 rescue_from Query::StatementInvalid, :with => :query_statement_invalid
33
34 helper :journals
35 helper :projects
36 include ProjectsHelper
37 helper :custom_fields
38 include CustomFieldsHelper
39 helper :issue_relations
40 include IssueRelationsHelper
41 helper :watchers
42 include WatchersHelper
43 helper :attachments
44 include AttachmentsHelper
45 helper :queries
46 include QueriesHelper
47 helper :repositories
48 include RepositoriesHelper
49 helper :sort
50 include SortHelper
51 include IssuesHelper
52 helper :timelog
53 include Redmine::Export::PDF
54
55 def index
56 retrieve_query
57 sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
58 sort_update(@query.sortable_columns)
59 @query.sort_criteria = sort_criteria.to_a
60
61 if @query.valid?
62 case params[:format]
63 when 'csv', 'pdf'
64 @limit = Setting.issues_export_limit.to_i
65 when 'atom'
66 @limit = Setting.feeds_limit.to_i
67 when 'xml', 'json'
68 @offset, @limit = api_offset_and_limit
69 else
70 @limit = per_page_option
71 end
72
73 @issue_count = @query.issue_count
74 @issue_pages = Paginator.new self, @issue_count, @limit, params['page']
75 @offset ||= @issue_pages.current.offset
76 @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version],
77 :order => sort_clause,
78 :offset => @offset,
79 :limit => @limit)
80 @issue_count_by_group = @query.issue_count_by_group
81
82 respond_to do |format|
83 format.html { render :template => 'issues/index', :layout => !request.xhr? }
84 format.api {
85 Issue.load_visible_relations(@issues) if include_in_api_response?('relations')
86 }
87 format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") }
88 format.csv { send_data(issues_to_csv(@issues, @project, @query, params), :type => 'text/csv; header=present', :filename => 'export.csv') }
89 format.pdf { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'export.pdf') }
90 end
91 else
92 respond_to do |format|
93 format.html { render(:template => 'issues/index', :layout => !request.xhr?) }
94 format.any(:atom, :csv, :pdf) { render(:nothing => true) }
95 format.api { render_validation_errors(@query) }
96 end
97 end
98 rescue ActiveRecord::RecordNotFound
99 render_404
100 end
101
102 def show
103 @journals = @issue.journals.includes(:user, :details).reorder("#{Journal.table_name}.id ASC").all
104 @journals.each_with_index {|j,i| j.indice = i+1}
105 @journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project)
106 @journals.reverse! if User.current.wants_comments_in_reverse_order?
107
108 @changesets = @issue.changesets.visible.all
109 @changesets.reverse! if User.current.wants_comments_in_reverse_order?
110
111 @relations = @issue.relations.select {|r| r.other_issue(@issue) && r.other_issue(@issue).visible? }
112 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
113 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
114 @priorities = IssuePriority.active
115 @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
116 respond_to do |format|
117 format.html {
118 retrieve_previous_and_next_issue_ids
119 render :template => 'issues/show'
120 }
121 format.api
122 format.atom { render :template => 'journals/index', :layout => false, :content_type => 'application/atom+xml' }
123 format.pdf {
124 pdf = issue_to_pdf(@issue, :journals => @journals)
125 send_data(pdf, :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf")
126 }
127 end
128 end
129
130 # Add a new issue
131 # The new issue will be created from an existing one if copy_from parameter is given
132 def new
133 respond_to do |format|
134 format.html { render :action => 'new', :layout => !request.xhr? }
135 format.js { render :partial => 'update_form' }
136 end
137 end
138
139 def create
140 call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue })
141 @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads]))
142 if @issue.save
143 call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue})
144 respond_to do |format|
145 format.html {
146 render_attachment_warning_if_needed(@issue)
147 flash[:notice] = l(:notice_issue_successful_create, :id => view_context.link_to("##{@issue.id}", issue_path(@issue), :title => @issue.subject))
148 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?} } :
149 { :action => 'show', :id => @issue })
150 }
151 format.api { render :action => 'show', :status => :created, :location => issue_url(@issue) }
152 end
153 return
154 else
155 respond_to do |format|
156 format.html { render :action => 'new' }
157 format.api { render_validation_errors(@issue) }
158 end
159 end
160 end
161
162 def edit
163 return unless update_issue_from_params
164
165 respond_to do |format|
166 format.html { }
167 format.xml { }
168 end
169 end
170
171 def update
172 return unless update_issue_from_params
173 @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads]))
174 saved = false
175 begin
176 saved = @issue.save_issue_with_child_records(params, @time_entry)
177 rescue ActiveRecord::StaleObjectError
178 @conflict = true
179 if params[:last_journal_id]
180 @conflict_journals = @issue.journals_after(params[:last_journal_id]).all
181 @conflict_journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project)
182 end
183 end
184
185 if saved
186 render_attachment_warning_if_needed(@issue)
187 flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
188
189 respond_to do |format|
190 format.html { redirect_back_or_default({:action => 'show', :id => @issue}) }
191 format.api { render_api_ok }
192 end
193 else
194 respond_to do |format|
195 format.html { render :action => 'edit' }
196 format.api { render_validation_errors(@issue) }
197 end
198 end
199 end
200
201 # Bulk edit/copy a set of issues
202 def bulk_edit
203 @issues.sort!
204 @copy = params[:copy].present?
205 @notes = params[:notes]
206
207 if User.current.allowed_to?(:move_issues, @projects)
208 @allowed_projects = Issue.allowed_target_projects_on_move
209 if params[:issue]
210 @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:issue][:project_id].to_s}
211 if @target_project
212 target_projects = [@target_project]
213 end
214 end
215 end
216 target_projects ||= @projects
217
218 if @copy
219 @available_statuses = [IssueStatus.default]
220 else
221 @available_statuses = @issues.map(&:new_statuses_allowed_to).reduce(:&)
222 end
223 @custom_fields = target_projects.map{|p|p.all_issue_custom_fields}.reduce(:&)
224 @assignables = target_projects.map(&:assignable_users).reduce(:&)
225 @trackers = target_projects.map(&:trackers).reduce(:&)
226 @versions = target_projects.map {|p| p.shared_versions.open}.reduce(:&)
227 @categories = target_projects.map {|p| p.issue_categories}.reduce(:&)
228 if @copy
229 @attachments_present = @issues.detect {|i| i.attachments.any?}.present?
230 @subtasks_present = @issues.detect {|i| !i.leaf?}.present?
231 end
232
233 @safe_attributes = @issues.map(&:safe_attribute_names).reduce(:&)
234 render :layout => false if request.xhr?
235 end
236
237 def bulk_update
238 @issues.sort!
239 @copy = params[:copy].present?
240 attributes = parse_params_for_bulk_issue_attributes(params)
241
242 unsaved_issue_ids = []
243 moved_issues = []
244
245 if @copy && params[:copy_subtasks].present?
246 # Descendant issues will be copied with the parent task
247 # Don't copy them twice
248 @issues.reject! {|issue| @issues.detect {|other| issue.is_descendant_of?(other)}}
249 end
250
251 @issues.each do |issue|
252 issue.reload
253 if @copy
254 issue = issue.copy({},
255 :attachments => params[:copy_attachments].present?,
256 :subtasks => params[:copy_subtasks].present?
257 )
258 end
259 journal = issue.init_journal(User.current, params[:notes])
260 issue.safe_attributes = attributes
261 call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue })
262 if issue.save
263 moved_issues << issue
264 else
265 # Keep unsaved issue ids to display them in flash error
266 unsaved_issue_ids << issue.id
267 end
268 end
269 set_flash_from_bulk_issue_save(@issues, unsaved_issue_ids)
270
271 if params[:follow]
272 if @issues.size == 1 && moved_issues.size == 1
273 redirect_to :controller => 'issues', :action => 'show', :id => moved_issues.first
274 elsif moved_issues.map(&:project).uniq.size == 1
275 redirect_to :controller => 'issues', :action => 'index', :project_id => moved_issues.map(&:project).first
276 end
277 else
278 redirect_back_or_default({:controller => 'issues', :action => 'index', :project_id => @project})
279 end
280 end
281
282 def destroy
283 @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f
284 if @hours > 0
285 case params[:todo]
286 when 'destroy'
287 # nothing to do
288 when 'nullify'
289 TimeEntry.update_all('issue_id = NULL', ['issue_id IN (?)', @issues])
290 when 'reassign'
291 reassign_to = @project.issues.find_by_id(params[:reassign_to_id])
292 if reassign_to.nil?
293 flash.now[:error] = l(:error_issue_not_found_in_project)
294 return
295 else
296 TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues])
297 end
298 else
299 # display the destroy form if it's a user request
300 return unless api_request?
301 end
302 end
303 @issues.each do |issue|
304 begin
305 issue.reload.destroy
306 rescue ::ActiveRecord::RecordNotFound # raised by #reload if issue no longer exists
307 # nothing to do, issue was already deleted (eg. by a parent)
308 end
309 end
310 respond_to do |format|
311 format.html { redirect_back_or_default(:action => 'index', :project_id => @project) }
312 format.api { render_api_ok }
313 end
314 end
315
316 private
317
318 def find_project
319 project_id = params[:project_id] || (params[:issue] && params[:issue][:project_id])
320 @project = Project.find(project_id)
321 rescue ActiveRecord::RecordNotFound
322 render_404
323 end
324
325 def retrieve_previous_and_next_issue_ids
326 retrieve_query_from_session
327 if @query
328 sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
329 sort_update(@query.sortable_columns, 'issues_index_sort')
330 limit = 500
331 issue_ids = @query.issue_ids(:order => sort_clause, :limit => (limit + 1), :include => [:assigned_to, :tracker, :priority, :category, :fixed_version])
332 if (idx = issue_ids.index(@issue.id)) && idx < limit
333 if issue_ids.size < 500
334 @issue_position = idx + 1
335 @issue_count = issue_ids.size
336 end
337 @prev_issue_id = issue_ids[idx - 1] if idx > 0
338 @next_issue_id = issue_ids[idx + 1] if idx < (issue_ids.size - 1)
339 end
340 end
341 end
342
343 # Used by #edit and #update to set some common instance variables
344 # from the params
345 # TODO: Refactor, not everything in here is needed by #edit
346 def update_issue_from_params
347 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
348 @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
349 @time_entry.attributes = params[:time_entry]
350
351 @issue.init_journal(User.current)
352
353 issue_attributes = params[:issue]
354 if issue_attributes && params[:conflict_resolution]
355 case params[:conflict_resolution]
356 when 'overwrite'
357 issue_attributes = issue_attributes.dup
358 issue_attributes.delete(:lock_version)
359 when 'add_notes'
360 issue_attributes = issue_attributes.slice(:notes)
361 when 'cancel'
362 redirect_to issue_path(@issue)
363 return false
364 end
365 end
366 @issue.safe_attributes = issue_attributes
367 @priorities = IssuePriority.active
368 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
369 true
370 end
371
372 # TODO: Refactor, lots of extra code in here
373 # TODO: Changing tracker on an existing issue should not trigger this
374 def build_new_issue_from_params
375 if params[:id].blank?
376 @issue = Issue.new
377 if params[:copy_from]
378 begin
379 @copy_from = Issue.visible.find(params[:copy_from])
380 @copy_attachments = params[:copy_attachments].present? || request.get?
381 @copy_subtasks = params[:copy_subtasks].present? || request.get?
382 @issue.copy_from(@copy_from, :attachments => @copy_attachments, :subtasks => @copy_subtasks)
383 rescue ActiveRecord::RecordNotFound
384 render_404
385 return
386 end
387 end
388 @issue.project = @project
389 else
390 @issue = @project.issues.visible.find(params[:id])
391 end
392
393 @issue.project = @project
394 @issue.author ||= User.current
395 # Tracker must be set before custom field values
396 @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first)
397 if @issue.tracker.nil?
398 render_error l(:error_no_tracker_in_project)
399 return false
400 end
401 @issue.start_date ||= Date.today if Setting.default_issue_start_date_to_creation_date?
402 @issue.safe_attributes = params[:issue]
403
404 @priorities = IssuePriority.active
405 @allowed_statuses = @issue.new_statuses_allowed_to(User.current, true)
406 @available_watchers = (@issue.project.users.sort + @issue.watcher_users).uniq
407 end
408
409 def check_for_default_issue_status
410 if IssueStatus.default.nil?
411 render_error l(:error_no_default_issue_status)
412 return false
413 end
414 end
415
416 def parse_params_for_bulk_issue_attributes(params)
417 attributes = (params[:issue] || {}).reject {|k,v| v.blank?}
418 attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'}
419 if custom = attributes[:custom_field_values]
420 custom.reject! {|k,v| v.blank?}
421 custom.keys.each do |k|
422 if custom[k].is_a?(Array)
423 custom[k] << '' if custom[k].delete('__none__')
424 else
425 custom[k] = '' if custom[k] == '__none__'
426 end
427 end
428 end
429 attributes
430 end
431 end