##// END OF EJS Templates
Merged r1376 from trunk....
Jean-Philippe Lang -
r1527:a55317f09c39
parent child
Show More
@@ -1,421 +1,421
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2007 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 layout 'base'
19 layout 'base'
20 menu_item :new_issue, :only => :new
20 menu_item :new_issue, :only => :new
21
21
22 before_filter :find_issue, :only => [:show, :edit, :destroy_attachment]
22 before_filter :find_issue, :only => [:show, :edit, :destroy_attachment]
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, :preview, :update_form, :context_menu]
25 before_filter :authorize, :except => [:index, :changes, :preview, :update_form, :context_menu]
26 before_filter :find_optional_project, :only => [:index, :changes]
26 before_filter :find_optional_project, :only => [:index, :changes]
27 accept_key_auth :index, :changes
27 accept_key_auth :index, :changes
28
28
29 helper :journals
29 helper :journals
30 helper :projects
30 helper :projects
31 include ProjectsHelper
31 include ProjectsHelper
32 helper :custom_fields
32 helper :custom_fields
33 include CustomFieldsHelper
33 include CustomFieldsHelper
34 helper :ifpdf
34 helper :ifpdf
35 include IfpdfHelper
35 include IfpdfHelper
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 helper :sort
43 helper :sort
44 include SortHelper
44 include SortHelper
45 include IssuesHelper
45 include IssuesHelper
46
46
47 def index
47 def index
48 sort_init "#{Issue.table_name}.id", "desc"
48 sort_init "#{Issue.table_name}.id", "desc"
49 sort_update
49 sort_update
50 retrieve_query
50 retrieve_query
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 => l(:label_issue_plural)) }
68 format.atom { render_feed(@issues, :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(render(:template => 'issues/index.rfpdf', :layout => false), :type => 'application/pdf', :filename => 'export.pdf') }
70 format.pdf { send_data(render(:template => 'issues/index.rfpdf', :layout => false), :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 sort_init "#{Issue.table_name}.id", "desc"
81 sort_init "#{Issue.table_name}.id", "desc"
82 sort_update
82 sort_update
83 retrieve_query
83 retrieve_query
84 if @query.valid?
84 if @query.valid?
85 @journals = Journal.find :all, :include => [ :details, :user, {:issue => [:project, :author, :tracker, :status]} ],
85 @journals = Journal.find :all, :include => [ :details, :user, {:issue => [:project, :author, :tracker, :status]} ],
86 :conditions => @query.statement,
86 :conditions => @query.statement,
87 :limit => 25,
87 :limit => 25,
88 :order => "#{Journal.table_name}.created_on DESC"
88 :order => "#{Journal.table_name}.created_on DESC"
89 end
89 end
90 @title = (@project ? @project.name : Setting.app_title) + ": " + (@query.new_record? ? l(:label_changes_details) : @query.name)
90 @title = (@project ? @project.name : Setting.app_title) + ": " + (@query.new_record? ? l(:label_changes_details) : @query.name)
91 render :layout => false, :content_type => 'application/atom+xml'
91 render :layout => false, :content_type => 'application/atom+xml'
92 rescue ActiveRecord::RecordNotFound
92 rescue ActiveRecord::RecordNotFound
93 render_404
93 render_404
94 end
94 end
95
95
96 def show
96 def show
97 @custom_values = @project.custom_fields_for_issues(@issue.tracker).collect { |x| @issue.custom_values.find_by_custom_field_id(x.id) || CustomValue.new(:custom_field => x, :customized => @issue) }
97 @custom_values = @project.custom_fields_for_issues(@issue.tracker).collect { |x| @issue.custom_values.find_by_custom_field_id(x.id) || CustomValue.new(:custom_field => x, :customized => @issue) }
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 @activities = Enumeration::get_values('ACTI')
103 @activities = Enumeration::get_values('ACTI')
104 @priorities = Enumeration::get_values('IPRI')
104 @priorities = Enumeration::get_values('IPRI')
105 @time_entry = TimeEntry.new
105 @time_entry = TimeEntry.new
106 respond_to do |format|
106 respond_to do |format|
107 format.html { render :template => 'issues/show.rhtml' }
107 format.html { render :template => 'issues/show.rhtml' }
108 format.atom { render :action => 'changes', :layout => false, :content_type => 'application/atom+xml' }
108 format.atom { render :action => 'changes', :layout => false, :content_type => 'application/atom+xml' }
109 format.pdf { send_data(render(:template => 'issues/show.rfpdf', :layout => false), :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf") }
109 format.pdf { send_data(render(:template => 'issues/show.rfpdf', :layout => false), :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf") }
110 end
110 end
111 end
111 end
112
112
113 # Add a new issue
113 # Add a new issue
114 # The new issue will be created from an existing one if copy_from parameter is given
114 # The new issue will be created from an existing one if copy_from parameter is given
115 def new
115 def new
116 @issue = params[:copy_from] ? Issue.new.copy_from(params[:copy_from]) : Issue.new(params[:issue])
116 @issue = params[:copy_from] ? Issue.new.copy_from(params[:copy_from]) : Issue.new(params[:issue])
117 @issue.project = @project
117 @issue.project = @project
118 @issue.author = User.current
118 @issue.author = User.current
119 @issue.tracker ||= @project.trackers.find(params[:tracker_id] ? params[:tracker_id] : :first)
119 @issue.tracker ||= @project.trackers.find(params[: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
125
126 default_status = IssueStatus.default
126 default_status = IssueStatus.default
127 unless default_status
127 unless default_status
128 flash.now[:error] = 'No default issue status is defined. Please check your configuration (Go to "Administration -> Issue statuses").'
128 flash.now[:error] = 'No default issue status is defined. Please check your configuration (Go to "Administration -> Issue statuses").'
129 render :nothing => true, :layout => true
129 render :nothing => true, :layout => true
130 return
130 return
131 end
131 end
132 @issue.status = default_status
132 @issue.status = default_status
133 @allowed_statuses = ([default_status] + default_status.find_new_statuses_allowed_to(User.current.role_for_project(@project), @issue.tracker)).uniq
133 @allowed_statuses = ([default_status] + default_status.find_new_statuses_allowed_to(User.current.role_for_project(@project), @issue.tracker)).uniq
134
134
135 if request.get? || request.xhr?
135 if request.get? || request.xhr?
136 @issue.start_date ||= Date.today
136 @issue.start_date ||= Date.today
137 @custom_values = @issue.custom_values.empty? ?
137 @custom_values = @issue.custom_values.empty? ?
138 @project.custom_fields_for_issues(@issue.tracker).collect { |x| CustomValue.new(:custom_field => x, :customized => @issue) } :
138 @project.custom_fields_for_issues(@issue.tracker).collect { |x| CustomValue.new(:custom_field => x, :customized => @issue) } :
139 @issue.custom_values
139 @issue.custom_values
140 else
140 else
141 requested_status = IssueStatus.find_by_id(params[:issue][:status_id])
141 requested_status = (params[:issue] && params[:issue][:status_id] ? IssueStatus.find_by_id(params[:issue][:status_id]) : default_status)
142 # Check that the user is allowed to apply the requested status
142 # Check that the user is allowed to apply the requested status
143 @issue.status = (@allowed_statuses.include? requested_status) ? requested_status : default_status
143 @issue.status = (@allowed_statuses.include? requested_status) ? requested_status : default_status
144 @custom_values = @project.custom_fields_for_issues(@issue.tracker).collect { |x| CustomValue.new(:custom_field => x,
144 @custom_values = @project.custom_fields_for_issues(@issue.tracker).collect { |x| CustomValue.new(:custom_field => x,
145 :customized => @issue,
145 :customized => @issue,
146 :value => (params[:custom_fields] ? params[:custom_fields][x.id.to_s] : nil)) }
146 :value => (params[:custom_fields] ? params[:custom_fields][x.id.to_s] : nil)) }
147 @issue.custom_values = @custom_values
147 @issue.custom_values = @custom_values
148 if @issue.save
148 if @issue.save
149 attach_files(@issue, params[:attachments])
149 attach_files(@issue, params[:attachments])
150 flash[:notice] = l(:notice_successful_create)
150 flash[:notice] = l(:notice_successful_create)
151 Mailer.deliver_issue_add(@issue) if Setting.notified_events.include?('issue_added')
151 Mailer.deliver_issue_add(@issue) if Setting.notified_events.include?('issue_added')
152 redirect_to :controller => 'issues', :action => 'show', :id => @issue
152 redirect_to :controller => 'issues', :action => 'show', :id => @issue
153 return
153 return
154 end
154 end
155 end
155 end
156 @priorities = Enumeration::get_values('IPRI')
156 @priorities = Enumeration::get_values('IPRI')
157 render :layout => !request.xhr?
157 render :layout => !request.xhr?
158 end
158 end
159
159
160 # Attributes that can be updated on workflow transition (without :edit permission)
160 # Attributes that can be updated on workflow transition (without :edit permission)
161 # TODO: make it configurable (at least per role)
161 # TODO: make it configurable (at least per role)
162 UPDATABLE_ATTRS_ON_TRANSITION = %w(status_id assigned_to_id fixed_version_id done_ratio) unless const_defined?(:UPDATABLE_ATTRS_ON_TRANSITION)
162 UPDATABLE_ATTRS_ON_TRANSITION = %w(status_id assigned_to_id fixed_version_id done_ratio) unless const_defined?(:UPDATABLE_ATTRS_ON_TRANSITION)
163
163
164 def edit
164 def edit
165 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
165 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
166 @activities = Enumeration::get_values('ACTI')
166 @activities = Enumeration::get_values('ACTI')
167 @priorities = Enumeration::get_values('IPRI')
167 @priorities = Enumeration::get_values('IPRI')
168 @custom_values = []
168 @custom_values = []
169 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
169 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
170
170
171 @notes = params[:notes]
171 @notes = params[:notes]
172 journal = @issue.init_journal(User.current, @notes)
172 journal = @issue.init_journal(User.current, @notes)
173 # User can change issue attributes only if he has :edit permission or if a workflow transition is allowed
173 # User can change issue attributes only if he has :edit permission or if a workflow transition is allowed
174 if (@edit_allowed || !@allowed_statuses.empty?) && params[:issue]
174 if (@edit_allowed || !@allowed_statuses.empty?) && params[:issue]
175 attrs = params[:issue].dup
175 attrs = params[:issue].dup
176 attrs.delete_if {|k,v| !UPDATABLE_ATTRS_ON_TRANSITION.include?(k) } unless @edit_allowed
176 attrs.delete_if {|k,v| !UPDATABLE_ATTRS_ON_TRANSITION.include?(k) } unless @edit_allowed
177 attrs.delete(:status_id) unless @allowed_statuses.detect {|s| s.id.to_s == attrs[:status_id].to_s}
177 attrs.delete(:status_id) unless @allowed_statuses.detect {|s| s.id.to_s == attrs[:status_id].to_s}
178 @issue.attributes = attrs
178 @issue.attributes = attrs
179 end
179 end
180
180
181 if request.get?
181 if request.get?
182 @custom_values = @project.custom_fields_for_issues(@issue.tracker).collect { |x| @issue.custom_values.find_by_custom_field_id(x.id) || CustomValue.new(:custom_field => x, :customized => @issue) }
182 @custom_values = @project.custom_fields_for_issues(@issue.tracker).collect { |x| @issue.custom_values.find_by_custom_field_id(x.id) || CustomValue.new(:custom_field => x, :customized => @issue) }
183 else
183 else
184 # Update custom fields if user has :edit permission
184 # Update custom fields if user has :edit permission
185 if @edit_allowed && params[:custom_fields]
185 if @edit_allowed && params[:custom_fields]
186 @custom_values = @project.custom_fields_for_issues(@issue.tracker).collect { |x| CustomValue.new(:custom_field => x, :customized => @issue, :value => params["custom_fields"][x.id.to_s]) }
186 @custom_values = @project.custom_fields_for_issues(@issue.tracker).collect { |x| CustomValue.new(:custom_field => x, :customized => @issue, :value => params["custom_fields"][x.id.to_s]) }
187 @issue.custom_values = @custom_values
187 @issue.custom_values = @custom_values
188 end
188 end
189 @time_entry = TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => Date.today)
189 @time_entry = TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => Date.today)
190 @time_entry.attributes = params[:time_entry]
190 @time_entry.attributes = params[:time_entry]
191 attachments = attach_files(@issue, params[:attachments])
191 attachments = attach_files(@issue, params[:attachments])
192 attachments.each {|a| journal.details << JournalDetail.new(:property => 'attachment', :prop_key => a.id, :value => a.filename)}
192 attachments.each {|a| journal.details << JournalDetail.new(:property => 'attachment', :prop_key => a.id, :value => a.filename)}
193 if @issue.save
193 if @issue.save
194 # Log spend time
194 # Log spend time
195 if current_role.allowed_to?(:log_time)
195 if current_role.allowed_to?(:log_time)
196 @time_entry.save
196 @time_entry.save
197 end
197 end
198 if !journal.new_record?
198 if !journal.new_record?
199 # Only send notification if something was actually changed
199 # Only send notification if something was actually changed
200 flash[:notice] = l(:notice_successful_update)
200 flash[:notice] = l(:notice_successful_update)
201 Mailer.deliver_issue_edit(journal) if Setting.notified_events.include?('issue_updated')
201 Mailer.deliver_issue_edit(journal) if Setting.notified_events.include?('issue_updated')
202 end
202 end
203 redirect_to(params[:back_to] || {:action => 'show', :id => @issue})
203 redirect_to(params[:back_to] || {:action => 'show', :id => @issue})
204 end
204 end
205 end
205 end
206 rescue ActiveRecord::StaleObjectError
206 rescue ActiveRecord::StaleObjectError
207 # Optimistic locking exception
207 # Optimistic locking exception
208 flash.now[:error] = l(:notice_locking_conflict)
208 flash.now[:error] = l(:notice_locking_conflict)
209 end
209 end
210
210
211 # Bulk edit a set of issues
211 # Bulk edit a set of issues
212 def bulk_edit
212 def bulk_edit
213 if request.post?
213 if request.post?
214 status = params[:status_id].blank? ? nil : IssueStatus.find_by_id(params[:status_id])
214 status = params[:status_id].blank? ? nil : IssueStatus.find_by_id(params[:status_id])
215 priority = params[:priority_id].blank? ? nil : Enumeration.find_by_id(params[:priority_id])
215 priority = params[:priority_id].blank? ? nil : Enumeration.find_by_id(params[:priority_id])
216 assigned_to = (params[:assigned_to_id].blank? || params[:assigned_to_id] == 'none') ? nil : User.find_by_id(params[:assigned_to_id])
216 assigned_to = (params[:assigned_to_id].blank? || params[:assigned_to_id] == 'none') ? nil : User.find_by_id(params[:assigned_to_id])
217 category = (params[:category_id].blank? || params[:category_id] == 'none') ? nil : @project.issue_categories.find_by_id(params[:category_id])
217 category = (params[:category_id].blank? || params[:category_id] == 'none') ? nil : @project.issue_categories.find_by_id(params[:category_id])
218 fixed_version = (params[:fixed_version_id].blank? || params[:fixed_version_id] == 'none') ? nil : @project.versions.find_by_id(params[:fixed_version_id])
218 fixed_version = (params[:fixed_version_id].blank? || params[:fixed_version_id] == 'none') ? nil : @project.versions.find_by_id(params[:fixed_version_id])
219
219
220 unsaved_issue_ids = []
220 unsaved_issue_ids = []
221 @issues.each do |issue|
221 @issues.each do |issue|
222 journal = issue.init_journal(User.current, params[:notes])
222 journal = issue.init_journal(User.current, params[:notes])
223 issue.priority = priority if priority
223 issue.priority = priority if priority
224 issue.assigned_to = assigned_to if assigned_to || params[:assigned_to_id] == 'none'
224 issue.assigned_to = assigned_to if assigned_to || params[:assigned_to_id] == 'none'
225 issue.category = category if category || params[:category_id] == 'none'
225 issue.category = category if category || params[:category_id] == 'none'
226 issue.fixed_version = fixed_version if fixed_version || params[:fixed_version_id] == 'none'
226 issue.fixed_version = fixed_version if fixed_version || params[:fixed_version_id] == 'none'
227 issue.start_date = params[:start_date] unless params[:start_date].blank?
227 issue.start_date = params[:start_date] unless params[:start_date].blank?
228 issue.due_date = params[:due_date] unless params[:due_date].blank?
228 issue.due_date = params[:due_date] unless params[:due_date].blank?
229 issue.done_ratio = params[:done_ratio] unless params[:done_ratio].blank?
229 issue.done_ratio = params[:done_ratio] unless params[:done_ratio].blank?
230 # Don't save any change to the issue if the user is not authorized to apply the requested status
230 # Don't save any change to the issue if the user is not authorized to apply the requested status
231 if (status.nil? || (issue.status.new_status_allowed_to?(status, current_role, issue.tracker) && issue.status = status)) && issue.save
231 if (status.nil? || (issue.status.new_status_allowed_to?(status, current_role, issue.tracker) && issue.status = status)) && issue.save
232 # Send notification for each issue (if changed)
232 # Send notification for each issue (if changed)
233 Mailer.deliver_issue_edit(journal) if journal.details.any? && Setting.notified_events.include?('issue_updated')
233 Mailer.deliver_issue_edit(journal) if journal.details.any? && Setting.notified_events.include?('issue_updated')
234 else
234 else
235 # Keep unsaved issue ids to display them in flash error
235 # Keep unsaved issue ids to display them in flash error
236 unsaved_issue_ids << issue.id
236 unsaved_issue_ids << issue.id
237 end
237 end
238 end
238 end
239 if unsaved_issue_ids.empty?
239 if unsaved_issue_ids.empty?
240 flash[:notice] = l(:notice_successful_update) unless @issues.empty?
240 flash[:notice] = l(:notice_successful_update) unless @issues.empty?
241 else
241 else
242 flash[:error] = l(:notice_failed_to_save_issues, unsaved_issue_ids.size, @issues.size, '#' + unsaved_issue_ids.join(', #'))
242 flash[:error] = l(:notice_failed_to_save_issues, unsaved_issue_ids.size, @issues.size, '#' + unsaved_issue_ids.join(', #'))
243 end
243 end
244 redirect_to :controller => 'issues', :action => 'index', :project_id => @project
244 redirect_to :controller => 'issues', :action => 'index', :project_id => @project
245 return
245 return
246 end
246 end
247 # Find potential statuses the user could be allowed to switch issues to
247 # Find potential statuses the user could be allowed to switch issues to
248 @available_statuses = Workflow.find(:all, :include => :new_status,
248 @available_statuses = Workflow.find(:all, :include => :new_status,
249 :conditions => {:role_id => current_role.id}).collect(&:new_status).compact.uniq
249 :conditions => {:role_id => current_role.id}).collect(&:new_status).compact.uniq
250 end
250 end
251
251
252 def move
252 def move
253 @allowed_projects = []
253 @allowed_projects = []
254 # find projects to which the user is allowed to move the issue
254 # find projects to which the user is allowed to move the issue
255 if User.current.admin?
255 if User.current.admin?
256 # admin is allowed to move issues to any active (visible) project
256 # admin is allowed to move issues to any active (visible) project
257 @allowed_projects = Project.find(:all, :conditions => Project.visible_by(User.current), :order => 'name')
257 @allowed_projects = Project.find(:all, :conditions => Project.visible_by(User.current), :order => 'name')
258 else
258 else
259 User.current.memberships.each {|m| @allowed_projects << m.project if m.role.allowed_to?(:move_issues)}
259 User.current.memberships.each {|m| @allowed_projects << m.project if m.role.allowed_to?(:move_issues)}
260 end
260 end
261 @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:new_project_id]} if params[:new_project_id]
261 @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:new_project_id]} if params[:new_project_id]
262 @target_project ||= @project
262 @target_project ||= @project
263 @trackers = @target_project.trackers
263 @trackers = @target_project.trackers
264 if request.post?
264 if request.post?
265 new_tracker = params[:new_tracker_id].blank? ? nil : @target_project.trackers.find_by_id(params[:new_tracker_id])
265 new_tracker = params[:new_tracker_id].blank? ? nil : @target_project.trackers.find_by_id(params[:new_tracker_id])
266 unsaved_issue_ids = []
266 unsaved_issue_ids = []
267 @issues.each do |issue|
267 @issues.each do |issue|
268 unsaved_issue_ids << issue.id unless issue.move_to(@target_project, new_tracker)
268 unsaved_issue_ids << issue.id unless issue.move_to(@target_project, new_tracker)
269 end
269 end
270 if unsaved_issue_ids.empty?
270 if unsaved_issue_ids.empty?
271 flash[:notice] = l(:notice_successful_update) unless @issues.empty?
271 flash[:notice] = l(:notice_successful_update) unless @issues.empty?
272 else
272 else
273 flash[:error] = l(:notice_failed_to_save_issues, unsaved_issue_ids.size, @issues.size, '#' + unsaved_issue_ids.join(', #'))
273 flash[:error] = l(:notice_failed_to_save_issues, unsaved_issue_ids.size, @issues.size, '#' + unsaved_issue_ids.join(', #'))
274 end
274 end
275 redirect_to :controller => 'issues', :action => 'index', :project_id => @project
275 redirect_to :controller => 'issues', :action => 'index', :project_id => @project
276 return
276 return
277 end
277 end
278 render :layout => false if request.xhr?
278 render :layout => false if request.xhr?
279 end
279 end
280
280
281 def destroy
281 def destroy
282 @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f
282 @hours = TimeEntry.sum(:hours, :conditions => ['issue_id IN (?)', @issues]).to_f
283 if @hours > 0
283 if @hours > 0
284 case params[:todo]
284 case params[:todo]
285 when 'destroy'
285 when 'destroy'
286 # nothing to do
286 # nothing to do
287 when 'nullify'
287 when 'nullify'
288 TimeEntry.update_all('issue_id = NULL', ['issue_id IN (?)', @issues])
288 TimeEntry.update_all('issue_id = NULL', ['issue_id IN (?)', @issues])
289 when 'reassign'
289 when 'reassign'
290 reassign_to = @project.issues.find_by_id(params[:reassign_to_id])
290 reassign_to = @project.issues.find_by_id(params[:reassign_to_id])
291 if reassign_to.nil?
291 if reassign_to.nil?
292 flash.now[:error] = l(:error_issue_not_found_in_project)
292 flash.now[:error] = l(:error_issue_not_found_in_project)
293 return
293 return
294 else
294 else
295 TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues])
295 TimeEntry.update_all("issue_id = #{reassign_to.id}", ['issue_id IN (?)', @issues])
296 end
296 end
297 else
297 else
298 # display the destroy form
298 # display the destroy form
299 return
299 return
300 end
300 end
301 end
301 end
302 @issues.each(&:destroy)
302 @issues.each(&:destroy)
303 redirect_to :action => 'index', :project_id => @project
303 redirect_to :action => 'index', :project_id => @project
304 end
304 end
305
305
306 def destroy_attachment
306 def destroy_attachment
307 a = @issue.attachments.find(params[:attachment_id])
307 a = @issue.attachments.find(params[:attachment_id])
308 a.destroy
308 a.destroy
309 journal = @issue.init_journal(User.current)
309 journal = @issue.init_journal(User.current)
310 journal.details << JournalDetail.new(:property => 'attachment',
310 journal.details << JournalDetail.new(:property => 'attachment',
311 :prop_key => a.id,
311 :prop_key => a.id,
312 :old_value => a.filename)
312 :old_value => a.filename)
313 journal.save
313 journal.save
314 redirect_to :action => 'show', :id => @issue
314 redirect_to :action => 'show', :id => @issue
315 end
315 end
316
316
317 def context_menu
317 def context_menu
318 @issues = Issue.find_all_by_id(params[:ids], :include => :project)
318 @issues = Issue.find_all_by_id(params[:ids], :include => :project)
319 if (@issues.size == 1)
319 if (@issues.size == 1)
320 @issue = @issues.first
320 @issue = @issues.first
321 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
321 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
322 @assignables = @issue.assignable_users
322 @assignables = @issue.assignable_users
323 @assignables << @issue.assigned_to if @issue.assigned_to && !@assignables.include?(@issue.assigned_to)
323 @assignables << @issue.assigned_to if @issue.assigned_to && !@assignables.include?(@issue.assigned_to)
324 end
324 end
325 projects = @issues.collect(&:project).compact.uniq
325 projects = @issues.collect(&:project).compact.uniq
326 @project = projects.first if projects.size == 1
326 @project = projects.first if projects.size == 1
327
327
328 @can = {:edit => (@project && User.current.allowed_to?(:edit_issues, @project)),
328 @can = {:edit => (@project && User.current.allowed_to?(:edit_issues, @project)),
329 :update => (@issue && (User.current.allowed_to?(:edit_issues, @project) || (User.current.allowed_to?(:change_status, @project) && !@allowed_statuses.empty?))),
329 :update => (@issue && (User.current.allowed_to?(:edit_issues, @project) || (User.current.allowed_to?(:change_status, @project) && !@allowed_statuses.empty?))),
330 :move => (@project && User.current.allowed_to?(:move_issues, @project)),
330 :move => (@project && User.current.allowed_to?(:move_issues, @project)),
331 :copy => (@issue && @project.trackers.include?(@issue.tracker) && User.current.allowed_to?(:add_issues, @project)),
331 :copy => (@issue && @project.trackers.include?(@issue.tracker) && User.current.allowed_to?(:add_issues, @project)),
332 :delete => (@project && User.current.allowed_to?(:delete_issues, @project))
332 :delete => (@project && User.current.allowed_to?(:delete_issues, @project))
333 }
333 }
334
334
335 @priorities = Enumeration.get_values('IPRI').reverse
335 @priorities = Enumeration.get_values('IPRI').reverse
336 @statuses = IssueStatus.find(:all, :order => 'position')
336 @statuses = IssueStatus.find(:all, :order => 'position')
337 @back = request.env['HTTP_REFERER']
337 @back = request.env['HTTP_REFERER']
338
338
339 render :layout => false
339 render :layout => false
340 end
340 end
341
341
342 def update_form
342 def update_form
343 @issue = Issue.new(params[:issue])
343 @issue = Issue.new(params[:issue])
344 render :action => :new, :layout => false
344 render :action => :new, :layout => false
345 end
345 end
346
346
347 def preview
347 def preview
348 @issue = @project.issues.find_by_id(params[:id]) unless params[:id].blank?
348 @issue = @project.issues.find_by_id(params[:id]) unless params[:id].blank?
349 @attachements = @issue.attachments if @issue
349 @attachements = @issue.attachments if @issue
350 @text = params[:notes] || (params[:issue] ? params[:issue][:description] : nil)
350 @text = params[:notes] || (params[:issue] ? params[:issue][:description] : nil)
351 render :partial => 'common/preview'
351 render :partial => 'common/preview'
352 end
352 end
353
353
354 private
354 private
355 def find_issue
355 def find_issue
356 @issue = Issue.find(params[:id], :include => [:project, :tracker, :status, :author, :priority, :category])
356 @issue = Issue.find(params[:id], :include => [:project, :tracker, :status, :author, :priority, :category])
357 @project = @issue.project
357 @project = @issue.project
358 rescue ActiveRecord::RecordNotFound
358 rescue ActiveRecord::RecordNotFound
359 render_404
359 render_404
360 end
360 end
361
361
362 # Filter for bulk operations
362 # Filter for bulk operations
363 def find_issues
363 def find_issues
364 @issues = Issue.find_all_by_id(params[:id] || params[:ids])
364 @issues = Issue.find_all_by_id(params[:id] || params[:ids])
365 raise ActiveRecord::RecordNotFound if @issues.empty?
365 raise ActiveRecord::RecordNotFound if @issues.empty?
366 projects = @issues.collect(&:project).compact.uniq
366 projects = @issues.collect(&:project).compact.uniq
367 if projects.size == 1
367 if projects.size == 1
368 @project = projects.first
368 @project = projects.first
369 else
369 else
370 # TODO: let users bulk edit/move/destroy issues from different projects
370 # TODO: let users bulk edit/move/destroy issues from different projects
371 render_error 'Can not bulk edit/move/destroy issues from different projects' and return false
371 render_error 'Can not bulk edit/move/destroy issues from different projects' and return false
372 end
372 end
373 rescue ActiveRecord::RecordNotFound
373 rescue ActiveRecord::RecordNotFound
374 render_404
374 render_404
375 end
375 end
376
376
377 def find_project
377 def find_project
378 @project = Project.find(params[:project_id])
378 @project = Project.find(params[:project_id])
379 rescue ActiveRecord::RecordNotFound
379 rescue ActiveRecord::RecordNotFound
380 render_404
380 render_404
381 end
381 end
382
382
383 def find_optional_project
383 def find_optional_project
384 return true unless params[:project_id]
384 return true unless params[:project_id]
385 @project = Project.find(params[:project_id])
385 @project = Project.find(params[:project_id])
386 authorize
386 authorize
387 rescue ActiveRecord::RecordNotFound
387 rescue ActiveRecord::RecordNotFound
388 render_404
388 render_404
389 end
389 end
390
390
391 # Retrieve query from session or build a new query
391 # Retrieve query from session or build a new query
392 def retrieve_query
392 def retrieve_query
393 if !params[:query_id].blank?
393 if !params[:query_id].blank?
394 cond = "project_id IS NULL"
394 cond = "project_id IS NULL"
395 cond << " OR project_id = #{@project.id}" if @project
395 cond << " OR project_id = #{@project.id}" if @project
396 @query = Query.find(params[:query_id], :conditions => cond)
396 @query = Query.find(params[:query_id], :conditions => cond)
397 @query.project = @project
397 @query.project = @project
398 session[:query] = {:id => @query.id, :project_id => @query.project_id}
398 session[:query] = {:id => @query.id, :project_id => @query.project_id}
399 else
399 else
400 if params[:set_filter] || session[:query].nil? || session[:query][:project_id] != (@project ? @project.id : nil)
400 if params[:set_filter] || session[:query].nil? || session[:query][:project_id] != (@project ? @project.id : nil)
401 # Give it a name, required to be valid
401 # Give it a name, required to be valid
402 @query = Query.new(:name => "_")
402 @query = Query.new(:name => "_")
403 @query.project = @project
403 @query.project = @project
404 if params[:fields] and params[:fields].is_a? Array
404 if params[:fields] and params[:fields].is_a? Array
405 params[:fields].each do |field|
405 params[:fields].each do |field|
406 @query.add_filter(field, params[:operators][field], params[:values][field])
406 @query.add_filter(field, params[:operators][field], params[:values][field])
407 end
407 end
408 else
408 else
409 @query.available_filters.keys.each do |field|
409 @query.available_filters.keys.each do |field|
410 @query.add_short_filter(field, params[field]) if params[field]
410 @query.add_short_filter(field, params[field]) if params[field]
411 end
411 end
412 end
412 end
413 session[:query] = {:project_id => @query.project_id, :filters => @query.filters}
413 session[:query] = {:project_id => @query.project_id, :filters => @query.filters}
414 else
414 else
415 @query = Query.find_by_id(session[:query][:id]) if session[:query][:id]
415 @query = Query.find_by_id(session[:query][:id]) if session[:query][:id]
416 @query ||= Query.new(:name => "_", :project => @project, :filters => session[:query][:filters])
416 @query ||= Query.new(:name => "_", :project => @project, :filters => session[:query][:filters])
417 @query.project = @project
417 @query.project = @project
418 end
418 end
419 end
419 end
420 end
420 end
421 end
421 end
General Comments 0
You need to be logged in to leave comments. Login now