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