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