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