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