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