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