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