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