##// END OF EJS Templates
Merged r14239 and r14240 (#19706)....
Jean-Philippe Lang -
r13872:30e1513b2343
parent child
Show More
@@ -1,485 +1,485
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2015 Jean-Philippe Lang
2 # Copyright (C) 2006-2015 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, :create]
19 menu_item :new_issue, :only => [:new, :create]
20 default_search_scope :issues
20 default_search_scope :issues
21
21
22 before_filter :find_issue, :only => [:show, :edit, :update]
22 before_filter :find_issue, :only => [:show, :edit, :update]
23 before_filter :find_issues, :only => [:bulk_edit, :bulk_update, :destroy]
23 before_filter :find_issues, :only => [:bulk_edit, :bulk_update, :destroy]
24 before_filter :find_project, :only => [:new, :create, :update_form]
24 before_filter :find_project, :only => [:new, :create, :update_form]
25 before_filter :authorize, :except => [:index]
25 before_filter :authorize, :except => [:index]
26 before_filter :find_optional_project, :only => [:index]
26 before_filter :find_optional_project, :only => [:index]
27 before_filter :check_for_default_issue_status, :only => [:new, :create]
27 before_filter :check_for_default_issue_status, :only => [:new, :create]
28 before_filter :build_new_issue_from_params, :only => [:new, :create, :update_form]
28 before_filter :build_new_issue_from_params, :only => [:new, :create, :update_form]
29 accept_rss_auth :index, :show
29 accept_rss_auth :index, :show
30 accept_api_auth :index, :show, :create, :update, :destroy
30 accept_api_auth :index, :show, :create, :update, :destroy
31
31
32 rescue_from Query::StatementInvalid, :with => :query_statement_invalid
32 rescue_from Query::StatementInvalid, :with => :query_statement_invalid
33
33
34 helper :journals
34 helper :journals
35 helper :projects
35 helper :projects
36 include ProjectsHelper
36 include ProjectsHelper
37 helper :custom_fields
37 helper :custom_fields
38 include CustomFieldsHelper
38 include CustomFieldsHelper
39 helper :issue_relations
39 helper :issue_relations
40 include IssueRelationsHelper
40 include IssueRelationsHelper
41 helper :watchers
41 helper :watchers
42 include WatchersHelper
42 include WatchersHelper
43 helper :attachments
43 helper :attachments
44 include AttachmentsHelper
44 include AttachmentsHelper
45 helper :queries
45 helper :queries
46 include QueriesHelper
46 include QueriesHelper
47 helper :repositories
47 helper :repositories
48 include RepositoriesHelper
48 include RepositoriesHelper
49 helper :sort
49 helper :sort
50 include SortHelper
50 include SortHelper
51 include IssuesHelper
51 include IssuesHelper
52 helper :timelog
52 helper :timelog
53 include Redmine::Export::PDF
53 include Redmine::Export::PDF
54
54
55 def index
55 def index
56 retrieve_query
56 retrieve_query
57 sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
57 sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
58 sort_update(@query.sortable_columns)
58 sort_update(@query.sortable_columns)
59 @query.sort_criteria = sort_criteria.to_a
59 @query.sort_criteria = sort_criteria.to_a
60
60
61 if @query.valid?
61 if @query.valid?
62 case params[:format]
62 case params[:format]
63 when 'csv', 'pdf'
63 when 'csv', 'pdf'
64 @limit = Setting.issues_export_limit.to_i
64 @limit = Setting.issues_export_limit.to_i
65 if params[:columns] == 'all'
65 if params[:columns] == 'all'
66 @query.column_names = @query.available_inline_columns.map(&:name)
66 @query.column_names = @query.available_inline_columns.map(&:name)
67 end
67 end
68 when 'atom'
68 when 'atom'
69 @limit = Setting.feeds_limit.to_i
69 @limit = Setting.feeds_limit.to_i
70 when 'xml', 'json'
70 when 'xml', 'json'
71 @offset, @limit = api_offset_and_limit
71 @offset, @limit = api_offset_and_limit
72 @query.column_names = %w(author)
72 @query.column_names = %w(author)
73 else
73 else
74 @limit = per_page_option
74 @limit = per_page_option
75 end
75 end
76
76
77 @issue_count = @query.issue_count
77 @issue_count = @query.issue_count
78 @issue_pages = Paginator.new @issue_count, @limit, params['page']
78 @issue_pages = Paginator.new @issue_count, @limit, params['page']
79 @offset ||= @issue_pages.offset
79 @offset ||= @issue_pages.offset
80 @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version],
80 @issues = @query.issues(:include => [:assigned_to, :tracker, :priority, :category, :fixed_version],
81 :order => sort_clause,
81 :order => sort_clause,
82 :offset => @offset,
82 :offset => @offset,
83 :limit => @limit)
83 :limit => @limit)
84 @issue_count_by_group = @query.issue_count_by_group
84 @issue_count_by_group = @query.issue_count_by_group
85
85
86 respond_to do |format|
86 respond_to do |format|
87 format.html { render :template => 'issues/index', :layout => !request.xhr? }
87 format.html { render :template => 'issues/index', :layout => !request.xhr? }
88 format.api {
88 format.api {
89 Issue.load_visible_relations(@issues) if include_in_api_response?('relations')
89 Issue.load_visible_relations(@issues) if include_in_api_response?('relations')
90 }
90 }
91 format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") }
91 format.atom { render_feed(@issues, :title => "#{@project || Setting.app_title}: #{l(:label_issue_plural)}") }
92 format.csv { send_data(query_to_csv(@issues, @query, params), :type => 'text/csv; header=present', :filename => 'issues.csv') }
92 format.csv { send_data(query_to_csv(@issues, @query, params), :type => 'text/csv; header=present', :filename => 'issues.csv') }
93 format.pdf { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'issues.pdf') }
93 format.pdf { send_data(issues_to_pdf(@issues, @project, @query), :type => 'application/pdf', :filename => 'issues.pdf') }
94 end
94 end
95 else
95 else
96 respond_to do |format|
96 respond_to do |format|
97 format.html { render(:template => 'issues/index', :layout => !request.xhr?) }
97 format.html { render(:template => 'issues/index', :layout => !request.xhr?) }
98 format.any(:atom, :csv, :pdf) { render(:nothing => true) }
98 format.any(:atom, :csv, :pdf) { render(:nothing => true) }
99 format.api { render_validation_errors(@query) }
99 format.api { render_validation_errors(@query) }
100 end
100 end
101 end
101 end
102 rescue ActiveRecord::RecordNotFound
102 rescue ActiveRecord::RecordNotFound
103 render_404
103 render_404
104 end
104 end
105
105
106 def show
106 def show
107 @journals = @issue.journals.includes(:user, :details).reorder("#{Journal.table_name}.id ASC").all
107 @journals = @issue.journals.includes(:user, :details).reorder("#{Journal.table_name}.id ASC").all
108 @journals.each_with_index {|j,i| j.indice = i+1}
108 @journals.each_with_index {|j,i| j.indice = i+1}
109 @journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project)
109 @journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project)
110 Journal.preload_journals_details_custom_fields(@journals)
110 Journal.preload_journals_details_custom_fields(@journals)
111 # TODO: use #select! when ruby1.8 support is dropped
111 # TODO: use #select! when ruby1.8 support is dropped
112 @journals.reject! {|journal| !journal.notes? && journal.visible_details.empty?}
112 @journals.reject! {|journal| !journal.notes? && journal.visible_details.empty?}
113 @journals.reverse! if User.current.wants_comments_in_reverse_order?
113 @journals.reverse! if User.current.wants_comments_in_reverse_order?
114
114
115 @changesets = @issue.changesets.visible.all
115 @changesets = @issue.changesets.visible.preload(:repository, :user).to_a
116 @changesets.reverse! if User.current.wants_comments_in_reverse_order?
116 @changesets.reverse! if User.current.wants_comments_in_reverse_order?
117
117
118 @relations = @issue.relations.select {|r| r.other_issue(@issue) && r.other_issue(@issue).visible? }
118 @relations = @issue.relations.select {|r| r.other_issue(@issue) && r.other_issue(@issue).visible? }
119 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
119 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
120 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
120 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
121 @priorities = IssuePriority.active
121 @priorities = IssuePriority.active
122 @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
122 @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
123 @relation = IssueRelation.new
123 @relation = IssueRelation.new
124
124
125 respond_to do |format|
125 respond_to do |format|
126 format.html {
126 format.html {
127 retrieve_previous_and_next_issue_ids
127 retrieve_previous_and_next_issue_ids
128 render :template => 'issues/show'
128 render :template => 'issues/show'
129 }
129 }
130 format.api
130 format.api
131 format.atom { render :template => 'journals/index', :layout => false, :content_type => 'application/atom+xml' }
131 format.atom { render :template => 'journals/index', :layout => false, :content_type => 'application/atom+xml' }
132 format.pdf {
132 format.pdf {
133 pdf = issue_to_pdf(@issue, :journals => @journals)
133 pdf = issue_to_pdf(@issue, :journals => @journals)
134 send_data(pdf, :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf")
134 send_data(pdf, :type => 'application/pdf', :filename => "#{@project.identifier}-#{@issue.id}.pdf")
135 }
135 }
136 end
136 end
137 end
137 end
138
138
139 # Add a new issue
139 # Add a new issue
140 # The new issue will be created from an existing one if copy_from parameter is given
140 # The new issue will be created from an existing one if copy_from parameter is given
141 def new
141 def new
142 respond_to do |format|
142 respond_to do |format|
143 format.html { render :action => 'new', :layout => !request.xhr? }
143 format.html { render :action => 'new', :layout => !request.xhr? }
144 end
144 end
145 end
145 end
146
146
147 def create
147 def create
148 call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue })
148 call_hook(:controller_issues_new_before_save, { :params => params, :issue => @issue })
149 @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads]))
149 @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads]))
150 if @issue.save
150 if @issue.save
151 call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue})
151 call_hook(:controller_issues_new_after_save, { :params => params, :issue => @issue})
152 respond_to do |format|
152 respond_to do |format|
153 format.html {
153 format.html {
154 render_attachment_warning_if_needed(@issue)
154 render_attachment_warning_if_needed(@issue)
155 flash[:notice] = l(:notice_issue_successful_create, :id => view_context.link_to("##{@issue.id}", issue_path(@issue), :title => @issue.subject))
155 flash[:notice] = l(:notice_issue_successful_create, :id => view_context.link_to("##{@issue.id}", issue_path(@issue), :title => @issue.subject))
156 if params[:continue]
156 if params[:continue]
157 attrs = {:tracker_id => @issue.tracker, :parent_issue_id => @issue.parent_issue_id}.reject {|k,v| v.nil?}
157 attrs = {:tracker_id => @issue.tracker, :parent_issue_id => @issue.parent_issue_id}.reject {|k,v| v.nil?}
158 redirect_to new_project_issue_path(@issue.project, :issue => attrs)
158 redirect_to new_project_issue_path(@issue.project, :issue => attrs)
159 else
159 else
160 redirect_to issue_path(@issue)
160 redirect_to issue_path(@issue)
161 end
161 end
162 }
162 }
163 format.api { render :action => 'show', :status => :created, :location => issue_url(@issue) }
163 format.api { render :action => 'show', :status => :created, :location => issue_url(@issue) }
164 end
164 end
165 return
165 return
166 else
166 else
167 respond_to do |format|
167 respond_to do |format|
168 format.html { render :action => 'new' }
168 format.html { render :action => 'new' }
169 format.api { render_validation_errors(@issue) }
169 format.api { render_validation_errors(@issue) }
170 end
170 end
171 end
171 end
172 end
172 end
173
173
174 def edit
174 def edit
175 return unless update_issue_from_params
175 return unless update_issue_from_params
176
176
177 respond_to do |format|
177 respond_to do |format|
178 format.html { }
178 format.html { }
179 format.xml { }
179 format.xml { }
180 end
180 end
181 end
181 end
182
182
183 def update
183 def update
184 return unless update_issue_from_params
184 return unless update_issue_from_params
185 @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads]))
185 @issue.save_attachments(params[:attachments] || (params[:issue] && params[:issue][:uploads]))
186 saved = false
186 saved = false
187 begin
187 begin
188 saved = save_issue_with_child_records
188 saved = save_issue_with_child_records
189 rescue ActiveRecord::StaleObjectError
189 rescue ActiveRecord::StaleObjectError
190 @conflict = true
190 @conflict = true
191 if params[:last_journal_id]
191 if params[:last_journal_id]
192 @conflict_journals = @issue.journals_after(params[:last_journal_id]).all
192 @conflict_journals = @issue.journals_after(params[:last_journal_id]).all
193 @conflict_journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project)
193 @conflict_journals.reject!(&:private_notes?) unless User.current.allowed_to?(:view_private_notes, @issue.project)
194 end
194 end
195 end
195 end
196
196
197 if saved
197 if saved
198 render_attachment_warning_if_needed(@issue)
198 render_attachment_warning_if_needed(@issue)
199 flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
199 flash[:notice] = l(:notice_successful_update) unless @issue.current_journal.new_record?
200
200
201 respond_to do |format|
201 respond_to do |format|
202 format.html { redirect_back_or_default issue_path(@issue) }
202 format.html { redirect_back_or_default issue_path(@issue) }
203 format.api { render_api_ok }
203 format.api { render_api_ok }
204 end
204 end
205 else
205 else
206 respond_to do |format|
206 respond_to do |format|
207 format.html { render :action => 'edit' }
207 format.html { render :action => 'edit' }
208 format.api { render_validation_errors(@issue) }
208 format.api { render_validation_errors(@issue) }
209 end
209 end
210 end
210 end
211 end
211 end
212
212
213 # Updates the issue form when changing the project, status or tracker
213 # Updates the issue form when changing the project, status or tracker
214 # on issue creation/update
214 # on issue creation/update
215 def update_form
215 def update_form
216 end
216 end
217
217
218 # Bulk edit/copy a set of issues
218 # Bulk edit/copy a set of issues
219 def bulk_edit
219 def bulk_edit
220 @issues.sort!
220 @issues.sort!
221 @copy = params[:copy].present?
221 @copy = params[:copy].present?
222 @notes = params[:notes]
222 @notes = params[:notes]
223
223
224 if User.current.allowed_to?(:move_issues, @projects)
224 if User.current.allowed_to?(:move_issues, @projects)
225 @allowed_projects = Issue.allowed_target_projects_on_move
225 @allowed_projects = Issue.allowed_target_projects_on_move
226 if params[:issue]
226 if params[:issue]
227 @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:issue][:project_id].to_s}
227 @target_project = @allowed_projects.detect {|p| p.id.to_s == params[:issue][:project_id].to_s}
228 if @target_project
228 if @target_project
229 target_projects = [@target_project]
229 target_projects = [@target_project]
230 end
230 end
231 end
231 end
232 end
232 end
233 target_projects ||= @projects
233 target_projects ||= @projects
234
234
235 if @copy
235 if @copy
236 @available_statuses = [IssueStatus.default]
236 @available_statuses = [IssueStatus.default]
237 else
237 else
238 @available_statuses = @issues.map(&:new_statuses_allowed_to).reduce(:&)
238 @available_statuses = @issues.map(&:new_statuses_allowed_to).reduce(:&)
239 end
239 end
240 @custom_fields = target_projects.map{|p|p.all_issue_custom_fields.visible}.reduce(:&)
240 @custom_fields = target_projects.map{|p|p.all_issue_custom_fields.visible}.reduce(:&)
241 @assignables = target_projects.map(&:assignable_users).reduce(:&)
241 @assignables = target_projects.map(&:assignable_users).reduce(:&)
242 @trackers = target_projects.map(&:trackers).reduce(:&)
242 @trackers = target_projects.map(&:trackers).reduce(:&)
243 @versions = target_projects.map {|p| p.shared_versions.open}.reduce(:&)
243 @versions = target_projects.map {|p| p.shared_versions.open}.reduce(:&)
244 @categories = target_projects.map {|p| p.issue_categories}.reduce(:&)
244 @categories = target_projects.map {|p| p.issue_categories}.reduce(:&)
245 if @copy
245 if @copy
246 @attachments_present = @issues.detect {|i| i.attachments.any?}.present?
246 @attachments_present = @issues.detect {|i| i.attachments.any?}.present?
247 @subtasks_present = @issues.detect {|i| !i.leaf?}.present?
247 @subtasks_present = @issues.detect {|i| !i.leaf?}.present?
248 end
248 end
249
249
250 @safe_attributes = @issues.map(&:safe_attribute_names).reduce(:&)
250 @safe_attributes = @issues.map(&:safe_attribute_names).reduce(:&)
251
251
252 @issue_params = params[:issue] || {}
252 @issue_params = params[:issue] || {}
253 @issue_params[:custom_field_values] ||= {}
253 @issue_params[:custom_field_values] ||= {}
254 end
254 end
255
255
256 def bulk_update
256 def bulk_update
257 @issues.sort!
257 @issues.sort!
258 @copy = params[:copy].present?
258 @copy = params[:copy].present?
259 attributes = parse_params_for_bulk_issue_attributes(params)
259 attributes = parse_params_for_bulk_issue_attributes(params)
260
260
261 unsaved_issues = []
261 unsaved_issues = []
262 saved_issues = []
262 saved_issues = []
263
263
264 if @copy && params[:copy_subtasks].present?
264 if @copy && params[:copy_subtasks].present?
265 # Descendant issues will be copied with the parent task
265 # Descendant issues will be copied with the parent task
266 # Don't copy them twice
266 # Don't copy them twice
267 @issues.reject! {|issue| @issues.detect {|other| issue.is_descendant_of?(other)}}
267 @issues.reject! {|issue| @issues.detect {|other| issue.is_descendant_of?(other)}}
268 end
268 end
269
269
270 @issues.each do |orig_issue|
270 @issues.each do |orig_issue|
271 orig_issue.reload
271 orig_issue.reload
272 if @copy
272 if @copy
273 issue = orig_issue.copy({},
273 issue = orig_issue.copy({},
274 :attachments => params[:copy_attachments].present?,
274 :attachments => params[:copy_attachments].present?,
275 :subtasks => params[:copy_subtasks].present?
275 :subtasks => params[:copy_subtasks].present?
276 )
276 )
277 else
277 else
278 issue = orig_issue
278 issue = orig_issue
279 end
279 end
280 journal = issue.init_journal(User.current, params[:notes])
280 journal = issue.init_journal(User.current, params[:notes])
281 issue.safe_attributes = attributes
281 issue.safe_attributes = attributes
282 call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue })
282 call_hook(:controller_issues_bulk_edit_before_save, { :params => params, :issue => issue })
283 if issue.save
283 if issue.save
284 saved_issues << issue
284 saved_issues << issue
285 else
285 else
286 unsaved_issues << orig_issue
286 unsaved_issues << orig_issue
287 end
287 end
288 end
288 end
289
289
290 if unsaved_issues.empty?
290 if unsaved_issues.empty?
291 flash[:notice] = l(:notice_successful_update) unless saved_issues.empty?
291 flash[:notice] = l(:notice_successful_update) unless saved_issues.empty?
292 if params[:follow]
292 if params[:follow]
293 if @issues.size == 1 && saved_issues.size == 1
293 if @issues.size == 1 && saved_issues.size == 1
294 redirect_to issue_path(saved_issues.first)
294 redirect_to issue_path(saved_issues.first)
295 elsif saved_issues.map(&:project).uniq.size == 1
295 elsif saved_issues.map(&:project).uniq.size == 1
296 redirect_to project_issues_path(saved_issues.map(&:project).first)
296 redirect_to project_issues_path(saved_issues.map(&:project).first)
297 end
297 end
298 else
298 else
299 redirect_back_or_default _project_issues_path(@project)
299 redirect_back_or_default _project_issues_path(@project)
300 end
300 end
301 else
301 else
302 @saved_issues = @issues
302 @saved_issues = @issues
303 @unsaved_issues = unsaved_issues
303 @unsaved_issues = unsaved_issues
304 @issues = Issue.visible.where(:id => @unsaved_issues.map(&:id)).all
304 @issues = Issue.visible.where(:id => @unsaved_issues.map(&:id)).all
305 bulk_edit
305 bulk_edit
306 render :action => 'bulk_edit'
306 render :action => 'bulk_edit'
307 end
307 end
308 end
308 end
309
309
310 def destroy
310 def destroy
311 @hours = TimeEntry.where(:issue_id => @issues.map(&:id)).sum(:hours).to_f
311 @hours = TimeEntry.where(:issue_id => @issues.map(&:id)).sum(:hours).to_f
312 if @hours > 0
312 if @hours > 0
313 case params[:todo]
313 case params[:todo]
314 when 'destroy'
314 when 'destroy'
315 # nothing to do
315 # nothing to do
316 when 'nullify'
316 when 'nullify'
317 TimeEntry.where(['issue_id IN (?)', @issues]).update_all('issue_id = NULL')
317 TimeEntry.where(['issue_id IN (?)', @issues]).update_all('issue_id = NULL')
318 when 'reassign'
318 when 'reassign'
319 reassign_to = @project.issues.find_by_id(params[:reassign_to_id])
319 reassign_to = @project.issues.find_by_id(params[:reassign_to_id])
320 if reassign_to.nil?
320 if reassign_to.nil?
321 flash.now[:error] = l(:error_issue_not_found_in_project)
321 flash.now[:error] = l(:error_issue_not_found_in_project)
322 return
322 return
323 else
323 else
324 TimeEntry.where(['issue_id IN (?)', @issues]).
324 TimeEntry.where(['issue_id IN (?)', @issues]).
325 update_all("issue_id = #{reassign_to.id}")
325 update_all("issue_id = #{reassign_to.id}")
326 end
326 end
327 else
327 else
328 # display the destroy form if it's a user request
328 # display the destroy form if it's a user request
329 return unless api_request?
329 return unless api_request?
330 end
330 end
331 end
331 end
332 @issues.each do |issue|
332 @issues.each do |issue|
333 begin
333 begin
334 issue.reload.destroy
334 issue.reload.destroy
335 rescue ::ActiveRecord::RecordNotFound # raised by #reload if issue no longer exists
335 rescue ::ActiveRecord::RecordNotFound # raised by #reload if issue no longer exists
336 # nothing to do, issue was already deleted (eg. by a parent)
336 # nothing to do, issue was already deleted (eg. by a parent)
337 end
337 end
338 end
338 end
339 respond_to do |format|
339 respond_to do |format|
340 format.html { redirect_back_or_default _project_issues_path(@project) }
340 format.html { redirect_back_or_default _project_issues_path(@project) }
341 format.api { render_api_ok }
341 format.api { render_api_ok }
342 end
342 end
343 end
343 end
344
344
345 private
345 private
346
346
347 def find_project
347 def find_project
348 project_id = params[:project_id] || (params[:issue] && params[:issue][:project_id])
348 project_id = params[:project_id] || (params[:issue] && params[:issue][:project_id])
349 @project = Project.find(project_id)
349 @project = Project.find(project_id)
350 rescue ActiveRecord::RecordNotFound
350 rescue ActiveRecord::RecordNotFound
351 render_404
351 render_404
352 end
352 end
353
353
354 def retrieve_previous_and_next_issue_ids
354 def retrieve_previous_and_next_issue_ids
355 retrieve_query_from_session
355 retrieve_query_from_session
356 if @query
356 if @query
357 sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
357 sort_init(@query.sort_criteria.empty? ? [['id', 'desc']] : @query.sort_criteria)
358 sort_update(@query.sortable_columns, 'issues_index_sort')
358 sort_update(@query.sortable_columns, 'issues_index_sort')
359 limit = 500
359 limit = 500
360 issue_ids = @query.issue_ids(:order => sort_clause, :limit => (limit + 1), :include => [:assigned_to, :tracker, :priority, :category, :fixed_version])
360 issue_ids = @query.issue_ids(:order => sort_clause, :limit => (limit + 1), :include => [:assigned_to, :tracker, :priority, :category, :fixed_version])
361 if (idx = issue_ids.index(@issue.id)) && idx < limit
361 if (idx = issue_ids.index(@issue.id)) && idx < limit
362 if issue_ids.size < 500
362 if issue_ids.size < 500
363 @issue_position = idx + 1
363 @issue_position = idx + 1
364 @issue_count = issue_ids.size
364 @issue_count = issue_ids.size
365 end
365 end
366 @prev_issue_id = issue_ids[idx - 1] if idx > 0
366 @prev_issue_id = issue_ids[idx - 1] if idx > 0
367 @next_issue_id = issue_ids[idx + 1] if idx < (issue_ids.size - 1)
367 @next_issue_id = issue_ids[idx + 1] if idx < (issue_ids.size - 1)
368 end
368 end
369 end
369 end
370 end
370 end
371
371
372 # Used by #edit and #update to set some common instance variables
372 # Used by #edit and #update to set some common instance variables
373 # from the params
373 # from the params
374 # TODO: Refactor, not everything in here is needed by #edit
374 # TODO: Refactor, not everything in here is needed by #edit
375 def update_issue_from_params
375 def update_issue_from_params
376 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
376 @edit_allowed = User.current.allowed_to?(:edit_issues, @project)
377 @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
377 @time_entry = TimeEntry.new(:issue => @issue, :project => @issue.project)
378 @time_entry.attributes = params[:time_entry]
378 @time_entry.attributes = params[:time_entry]
379
379
380 @issue.init_journal(User.current)
380 @issue.init_journal(User.current)
381
381
382 issue_attributes = params[:issue]
382 issue_attributes = params[:issue]
383 if issue_attributes && params[:conflict_resolution]
383 if issue_attributes && params[:conflict_resolution]
384 case params[:conflict_resolution]
384 case params[:conflict_resolution]
385 when 'overwrite'
385 when 'overwrite'
386 issue_attributes = issue_attributes.dup
386 issue_attributes = issue_attributes.dup
387 issue_attributes.delete(:lock_version)
387 issue_attributes.delete(:lock_version)
388 when 'add_notes'
388 when 'add_notes'
389 issue_attributes = issue_attributes.slice(:notes)
389 issue_attributes = issue_attributes.slice(:notes)
390 when 'cancel'
390 when 'cancel'
391 redirect_to issue_path(@issue)
391 redirect_to issue_path(@issue)
392 return false
392 return false
393 end
393 end
394 end
394 end
395 @issue.safe_attributes = issue_attributes
395 @issue.safe_attributes = issue_attributes
396 @priorities = IssuePriority.active
396 @priorities = IssuePriority.active
397 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
397 @allowed_statuses = @issue.new_statuses_allowed_to(User.current)
398 true
398 true
399 end
399 end
400
400
401 # TODO: Refactor, lots of extra code in here
401 # TODO: Refactor, lots of extra code in here
402 # TODO: Changing tracker on an existing issue should not trigger this
402 # TODO: Changing tracker on an existing issue should not trigger this
403 def build_new_issue_from_params
403 def build_new_issue_from_params
404 if params[:id].blank?
404 if params[:id].blank?
405 @issue = Issue.new
405 @issue = Issue.new
406 if params[:copy_from]
406 if params[:copy_from]
407 begin
407 begin
408 @copy_from = Issue.visible.find(params[:copy_from])
408 @copy_from = Issue.visible.find(params[:copy_from])
409 @copy_attachments = params[:copy_attachments].present? || request.get?
409 @copy_attachments = params[:copy_attachments].present? || request.get?
410 @copy_subtasks = params[:copy_subtasks].present? || request.get?
410 @copy_subtasks = params[:copy_subtasks].present? || request.get?
411 @issue.copy_from(@copy_from, :attachments => @copy_attachments, :subtasks => @copy_subtasks)
411 @issue.copy_from(@copy_from, :attachments => @copy_attachments, :subtasks => @copy_subtasks)
412 rescue ActiveRecord::RecordNotFound
412 rescue ActiveRecord::RecordNotFound
413 render_404
413 render_404
414 return
414 return
415 end
415 end
416 end
416 end
417 @issue.project = @project
417 @issue.project = @project
418 else
418 else
419 @issue = @project.issues.visible.find(params[:id])
419 @issue = @project.issues.visible.find(params[:id])
420 end
420 end
421
421
422 @issue.project = @project
422 @issue.project = @project
423 @issue.author ||= User.current
423 @issue.author ||= User.current
424 # Tracker must be set before custom field values
424 # Tracker must be set before custom field values
425 @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first)
425 @issue.tracker ||= @project.trackers.find((params[:issue] && params[:issue][:tracker_id]) || params[:tracker_id] || :first)
426 if @issue.tracker.nil?
426 if @issue.tracker.nil?
427 render_error l(:error_no_tracker_in_project)
427 render_error l(:error_no_tracker_in_project)
428 return false
428 return false
429 end
429 end
430 @issue.start_date ||= Date.today if Setting.default_issue_start_date_to_creation_date?
430 @issue.start_date ||= Date.today if Setting.default_issue_start_date_to_creation_date?
431 @issue.safe_attributes = params[:issue]
431 @issue.safe_attributes = params[:issue]
432
432
433 @priorities = IssuePriority.active
433 @priorities = IssuePriority.active
434 @allowed_statuses = @issue.new_statuses_allowed_to(User.current, @issue.new_record?)
434 @allowed_statuses = @issue.new_statuses_allowed_to(User.current, @issue.new_record?)
435 @available_watchers = @issue.watcher_users
435 @available_watchers = @issue.watcher_users
436 if @issue.project.users.count <= 20
436 if @issue.project.users.count <= 20
437 @available_watchers = (@available_watchers + @issue.project.users.sort).uniq
437 @available_watchers = (@available_watchers + @issue.project.users.sort).uniq
438 end
438 end
439 end
439 end
440
440
441 def check_for_default_issue_status
441 def check_for_default_issue_status
442 if IssueStatus.default.nil?
442 if IssueStatus.default.nil?
443 render_error l(:error_no_default_issue_status)
443 render_error l(:error_no_default_issue_status)
444 return false
444 return false
445 end
445 end
446 end
446 end
447
447
448 def parse_params_for_bulk_issue_attributes(params)
448 def parse_params_for_bulk_issue_attributes(params)
449 attributes = (params[:issue] || {}).reject {|k,v| v.blank?}
449 attributes = (params[:issue] || {}).reject {|k,v| v.blank?}
450 attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'}
450 attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'}
451 if custom = attributes[:custom_field_values]
451 if custom = attributes[:custom_field_values]
452 custom.reject! {|k,v| v.blank?}
452 custom.reject! {|k,v| v.blank?}
453 custom.keys.each do |k|
453 custom.keys.each do |k|
454 if custom[k].is_a?(Array)
454 if custom[k].is_a?(Array)
455 custom[k] << '' if custom[k].delete('__none__')
455 custom[k] << '' if custom[k].delete('__none__')
456 else
456 else
457 custom[k] = '' if custom[k] == '__none__'
457 custom[k] = '' if custom[k] == '__none__'
458 end
458 end
459 end
459 end
460 end
460 end
461 attributes
461 attributes
462 end
462 end
463
463
464 # Saves @issue and a time_entry from the parameters
464 # Saves @issue and a time_entry from the parameters
465 def save_issue_with_child_records
465 def save_issue_with_child_records
466 Issue.transaction do
466 Issue.transaction do
467 if params[:time_entry] && (params[:time_entry][:hours].present? || params[:time_entry][:comments].present?) && User.current.allowed_to?(:log_time, @issue.project)
467 if params[:time_entry] && (params[:time_entry][:hours].present? || params[:time_entry][:comments].present?) && User.current.allowed_to?(:log_time, @issue.project)
468 time_entry = @time_entry || TimeEntry.new
468 time_entry = @time_entry || TimeEntry.new
469 time_entry.project = @issue.project
469 time_entry.project = @issue.project
470 time_entry.issue = @issue
470 time_entry.issue = @issue
471 time_entry.user = User.current
471 time_entry.user = User.current
472 time_entry.spent_on = User.current.today
472 time_entry.spent_on = User.current.today
473 time_entry.attributes = params[:time_entry]
473 time_entry.attributes = params[:time_entry]
474 @issue.time_entries << time_entry
474 @issue.time_entries << time_entry
475 end
475 end
476
476
477 call_hook(:controller_issues_edit_before_save, { :params => params, :issue => @issue, :time_entry => time_entry, :journal => @issue.current_journal})
477 call_hook(:controller_issues_edit_before_save, { :params => params, :issue => @issue, :time_entry => time_entry, :journal => @issue.current_journal})
478 if @issue.save
478 if @issue.save
479 call_hook(:controller_issues_edit_after_save, { :params => params, :issue => @issue, :time_entry => time_entry, :journal => @issue.current_journal})
479 call_hook(:controller_issues_edit_after_save, { :params => params, :issue => @issue, :time_entry => time_entry, :journal => @issue.current_journal})
480 else
480 else
481 raise ActiveRecord::Rollback
481 raise ActiveRecord::Rollback
482 end
482 end
483 end
483 end
484 end
484 end
485 end
485 end
@@ -1,430 +1,430
1 # encoding: utf-8
1 # encoding: utf-8
2 #
2 #
3 # Redmine - project management software
3 # Redmine - project management software
4 # Copyright (C) 2006-2015 Jean-Philippe Lang
4 # Copyright (C) 2006-2015 Jean-Philippe Lang
5 #
5 #
6 # This program is free software; you can redistribute it and/or
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License
7 # modify it under the terms of the GNU General Public License
8 # as published by the Free Software Foundation; either version 2
8 # as published by the Free Software Foundation; either version 2
9 # of the License, or (at your option) any later version.
9 # of the License, or (at your option) any later version.
10 #
10 #
11 # This program is distributed in the hope that it will be useful,
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
14 # GNU General Public License for more details.
15 #
15 #
16 # You should have received a copy of the GNU General Public License
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19
19
20 module IssuesHelper
20 module IssuesHelper
21 include ApplicationHelper
21 include ApplicationHelper
22
22
23 def issue_list(issues, &block)
23 def issue_list(issues, &block)
24 ancestors = []
24 ancestors = []
25 issues.each do |issue|
25 issues.each do |issue|
26 while (ancestors.any? && !issue.is_descendant_of?(ancestors.last))
26 while (ancestors.any? && !issue.is_descendant_of?(ancestors.last))
27 ancestors.pop
27 ancestors.pop
28 end
28 end
29 yield issue, ancestors.size
29 yield issue, ancestors.size
30 ancestors << issue unless issue.leaf?
30 ancestors << issue unless issue.leaf?
31 end
31 end
32 end
32 end
33
33
34 # Renders a HTML/CSS tooltip
34 # Renders a HTML/CSS tooltip
35 #
35 #
36 # To use, a trigger div is needed. This is a div with the class of "tooltip"
36 # To use, a trigger div is needed. This is a div with the class of "tooltip"
37 # that contains this method wrapped in a span with the class of "tip"
37 # that contains this method wrapped in a span with the class of "tip"
38 #
38 #
39 # <div class="tooltip"><%= link_to_issue(issue) %>
39 # <div class="tooltip"><%= link_to_issue(issue) %>
40 # <span class="tip"><%= render_issue_tooltip(issue) %></span>
40 # <span class="tip"><%= render_issue_tooltip(issue) %></span>
41 # </div>
41 # </div>
42 #
42 #
43 def render_issue_tooltip(issue)
43 def render_issue_tooltip(issue)
44 @cached_label_status ||= l(:field_status)
44 @cached_label_status ||= l(:field_status)
45 @cached_label_start_date ||= l(:field_start_date)
45 @cached_label_start_date ||= l(:field_start_date)
46 @cached_label_due_date ||= l(:field_due_date)
46 @cached_label_due_date ||= l(:field_due_date)
47 @cached_label_assigned_to ||= l(:field_assigned_to)
47 @cached_label_assigned_to ||= l(:field_assigned_to)
48 @cached_label_priority ||= l(:field_priority)
48 @cached_label_priority ||= l(:field_priority)
49 @cached_label_project ||= l(:field_project)
49 @cached_label_project ||= l(:field_project)
50
50
51 link_to_issue(issue) + "<br /><br />".html_safe +
51 link_to_issue(issue) + "<br /><br />".html_safe +
52 "<strong>#{@cached_label_project}</strong>: #{link_to_project(issue.project)}<br />".html_safe +
52 "<strong>#{@cached_label_project}</strong>: #{link_to_project(issue.project)}<br />".html_safe +
53 "<strong>#{@cached_label_status}</strong>: #{h(issue.status.name)}<br />".html_safe +
53 "<strong>#{@cached_label_status}</strong>: #{h(issue.status.name)}<br />".html_safe +
54 "<strong>#{@cached_label_start_date}</strong>: #{format_date(issue.start_date)}<br />".html_safe +
54 "<strong>#{@cached_label_start_date}</strong>: #{format_date(issue.start_date)}<br />".html_safe +
55 "<strong>#{@cached_label_due_date}</strong>: #{format_date(issue.due_date)}<br />".html_safe +
55 "<strong>#{@cached_label_due_date}</strong>: #{format_date(issue.due_date)}<br />".html_safe +
56 "<strong>#{@cached_label_assigned_to}</strong>: #{h(issue.assigned_to)}<br />".html_safe +
56 "<strong>#{@cached_label_assigned_to}</strong>: #{h(issue.assigned_to)}<br />".html_safe +
57 "<strong>#{@cached_label_priority}</strong>: #{h(issue.priority.name)}".html_safe
57 "<strong>#{@cached_label_priority}</strong>: #{h(issue.priority.name)}".html_safe
58 end
58 end
59
59
60 def issue_heading(issue)
60 def issue_heading(issue)
61 h("#{issue.tracker} ##{issue.id}")
61 h("#{issue.tracker} ##{issue.id}")
62 end
62 end
63
63
64 def render_issue_subject_with_tree(issue)
64 def render_issue_subject_with_tree(issue)
65 s = ''
65 s = ''
66 ancestors = issue.root? ? [] : issue.ancestors.visible.all
66 ancestors = issue.root? ? [] : issue.ancestors.visible.all
67 ancestors.each do |ancestor|
67 ancestors.each do |ancestor|
68 s << '<div>' + content_tag('p', link_to_issue(ancestor, :project => (issue.project_id != ancestor.project_id)))
68 s << '<div>' + content_tag('p', link_to_issue(ancestor, :project => (issue.project_id != ancestor.project_id)))
69 end
69 end
70 s << '<div>'
70 s << '<div>'
71 subject = h(issue.subject)
71 subject = h(issue.subject)
72 if issue.is_private?
72 if issue.is_private?
73 subject = content_tag('span', l(:field_is_private), :class => 'private') + ' ' + subject
73 subject = content_tag('span', l(:field_is_private), :class => 'private') + ' ' + subject
74 end
74 end
75 s << content_tag('h3', subject)
75 s << content_tag('h3', subject)
76 s << '</div>' * (ancestors.size + 1)
76 s << '</div>' * (ancestors.size + 1)
77 s.html_safe
77 s.html_safe
78 end
78 end
79
79
80 def render_descendants_tree(issue)
80 def render_descendants_tree(issue)
81 s = '<form><table class="list issues">'
81 s = '<form><table class="list issues">'
82 issue_list(issue.descendants.visible.sort_by(&:lft)) do |child, level|
82 issue_list(issue.descendants.visible.preload(:status, :priority, :tracker).sort_by(&:lft)) do |child, level|
83 css = "issue issue-#{child.id} hascontextmenu"
83 css = "issue issue-#{child.id} hascontextmenu"
84 css << " idnt idnt-#{level}" if level > 0
84 css << " idnt idnt-#{level}" if level > 0
85 s << content_tag('tr',
85 s << content_tag('tr',
86 content_tag('td', check_box_tag("ids[]", child.id, false, :id => nil), :class => 'checkbox') +
86 content_tag('td', check_box_tag("ids[]", child.id, false, :id => nil), :class => 'checkbox') +
87 content_tag('td', link_to_issue(child, :truncate => 60, :project => (issue.project_id != child.project_id)), :class => 'subject') +
87 content_tag('td', link_to_issue(child, :truncate => 60, :project => (issue.project_id != child.project_id)), :class => 'subject') +
88 content_tag('td', h(child.status)) +
88 content_tag('td', h(child.status)) +
89 content_tag('td', link_to_user(child.assigned_to)) +
89 content_tag('td', link_to_user(child.assigned_to)) +
90 content_tag('td', progress_bar(child.done_ratio, :width => '80px')),
90 content_tag('td', progress_bar(child.done_ratio, :width => '80px')),
91 :class => css)
91 :class => css)
92 end
92 end
93 s << '</table></form>'
93 s << '</table></form>'
94 s.html_safe
94 s.html_safe
95 end
95 end
96
96
97 # Returns an array of error messages for bulk edited issues
97 # Returns an array of error messages for bulk edited issues
98 def bulk_edit_error_messages(issues)
98 def bulk_edit_error_messages(issues)
99 messages = {}
99 messages = {}
100 issues.each do |issue|
100 issues.each do |issue|
101 issue.errors.full_messages.each do |message|
101 issue.errors.full_messages.each do |message|
102 messages[message] ||= []
102 messages[message] ||= []
103 messages[message] << issue
103 messages[message] << issue
104 end
104 end
105 end
105 end
106 messages.map { |message, issues|
106 messages.map { |message, issues|
107 "#{message}: " + issues.map {|i| "##{i.id}"}.join(', ')
107 "#{message}: " + issues.map {|i| "##{i.id}"}.join(', ')
108 }
108 }
109 end
109 end
110
110
111 # Returns a link for adding a new subtask to the given issue
111 # Returns a link for adding a new subtask to the given issue
112 def link_to_new_subtask(issue)
112 def link_to_new_subtask(issue)
113 attrs = {
113 attrs = {
114 :tracker_id => issue.tracker,
114 :tracker_id => issue.tracker,
115 :parent_issue_id => issue
115 :parent_issue_id => issue
116 }
116 }
117 link_to(l(:button_add), new_project_issue_path(issue.project, :issue => attrs))
117 link_to(l(:button_add), new_project_issue_path(issue.project, :issue => attrs))
118 end
118 end
119
119
120 class IssueFieldsRows
120 class IssueFieldsRows
121 include ActionView::Helpers::TagHelper
121 include ActionView::Helpers::TagHelper
122
122
123 def initialize
123 def initialize
124 @left = []
124 @left = []
125 @right = []
125 @right = []
126 end
126 end
127
127
128 def left(*args)
128 def left(*args)
129 args.any? ? @left << cells(*args) : @left
129 args.any? ? @left << cells(*args) : @left
130 end
130 end
131
131
132 def right(*args)
132 def right(*args)
133 args.any? ? @right << cells(*args) : @right
133 args.any? ? @right << cells(*args) : @right
134 end
134 end
135
135
136 def size
136 def size
137 @left.size > @right.size ? @left.size : @right.size
137 @left.size > @right.size ? @left.size : @right.size
138 end
138 end
139
139
140 def to_html
140 def to_html
141 html = ''.html_safe
141 html = ''.html_safe
142 blank = content_tag('th', '') + content_tag('td', '')
142 blank = content_tag('th', '') + content_tag('td', '')
143 size.times do |i|
143 size.times do |i|
144 left = @left[i] || blank
144 left = @left[i] || blank
145 right = @right[i] || blank
145 right = @right[i] || blank
146 html << content_tag('tr', left + right)
146 html << content_tag('tr', left + right)
147 end
147 end
148 html
148 html
149 end
149 end
150
150
151 def cells(label, text, options={})
151 def cells(label, text, options={})
152 content_tag('th', "#{label}:", options) + content_tag('td', text, options)
152 content_tag('th', "#{label}:", options) + content_tag('td', text, options)
153 end
153 end
154 end
154 end
155
155
156 def issue_fields_rows
156 def issue_fields_rows
157 r = IssueFieldsRows.new
157 r = IssueFieldsRows.new
158 yield r
158 yield r
159 r.to_html
159 r.to_html
160 end
160 end
161
161
162 def render_custom_fields_rows(issue)
162 def render_custom_fields_rows(issue)
163 values = issue.visible_custom_field_values
163 values = issue.visible_custom_field_values
164 return if values.empty?
164 return if values.empty?
165 ordered_values = []
165 ordered_values = []
166 half = (values.size / 2.0).ceil
166 half = (values.size / 2.0).ceil
167 half.times do |i|
167 half.times do |i|
168 ordered_values << values[i]
168 ordered_values << values[i]
169 ordered_values << values[i + half]
169 ordered_values << values[i + half]
170 end
170 end
171 s = "<tr>\n"
171 s = "<tr>\n"
172 n = 0
172 n = 0
173 ordered_values.compact.each do |value|
173 ordered_values.compact.each do |value|
174 css = "cf_#{value.custom_field.id}"
174 css = "cf_#{value.custom_field.id}"
175 s << "</tr>\n<tr>\n" if n > 0 && (n % 2) == 0
175 s << "</tr>\n<tr>\n" if n > 0 && (n % 2) == 0
176 s << "\t<th class=\"#{css}\">#{ h(value.custom_field.name) }:</th><td class=\"#{css}\">#{ h(show_value(value)) }</td>\n"
176 s << "\t<th class=\"#{css}\">#{ h(value.custom_field.name) }:</th><td class=\"#{css}\">#{ h(show_value(value)) }</td>\n"
177 n += 1
177 n += 1
178 end
178 end
179 s << "</tr>\n"
179 s << "</tr>\n"
180 s.html_safe
180 s.html_safe
181 end
181 end
182
182
183 def issues_destroy_confirmation_message(issues)
183 def issues_destroy_confirmation_message(issues)
184 issues = [issues] unless issues.is_a?(Array)
184 issues = [issues] unless issues.is_a?(Array)
185 message = l(:text_issues_destroy_confirmation)
185 message = l(:text_issues_destroy_confirmation)
186 descendant_count = issues.inject(0) {|memo, i| memo += (i.right - i.left - 1)/2}
186 descendant_count = issues.inject(0) {|memo, i| memo += (i.right - i.left - 1)/2}
187 if descendant_count > 0
187 if descendant_count > 0
188 issues.each do |issue|
188 issues.each do |issue|
189 next if issue.root?
189 next if issue.root?
190 issues.each do |other_issue|
190 issues.each do |other_issue|
191 descendant_count -= 1 if issue.is_descendant_of?(other_issue)
191 descendant_count -= 1 if issue.is_descendant_of?(other_issue)
192 end
192 end
193 end
193 end
194 if descendant_count > 0
194 if descendant_count > 0
195 message << "\n" + l(:text_issues_destroy_descendants_confirmation, :count => descendant_count)
195 message << "\n" + l(:text_issues_destroy_descendants_confirmation, :count => descendant_count)
196 end
196 end
197 end
197 end
198 message
198 message
199 end
199 end
200
200
201 def sidebar_queries
201 def sidebar_queries
202 unless @sidebar_queries
202 unless @sidebar_queries
203 @sidebar_queries = IssueQuery.visible.
203 @sidebar_queries = IssueQuery.visible.
204 order("#{Query.table_name}.name ASC").
204 order("#{Query.table_name}.name ASC").
205 # Project specific queries and global queries
205 # Project specific queries and global queries
206 where(@project.nil? ? ["project_id IS NULL"] : ["project_id IS NULL OR project_id = ?", @project.id]).
206 where(@project.nil? ? ["project_id IS NULL"] : ["project_id IS NULL OR project_id = ?", @project.id]).
207 all
207 all
208 end
208 end
209 @sidebar_queries
209 @sidebar_queries
210 end
210 end
211
211
212 def query_links(title, queries)
212 def query_links(title, queries)
213 return '' if queries.empty?
213 return '' if queries.empty?
214 # links to #index on issues/show
214 # links to #index on issues/show
215 url_params = controller_name == 'issues' ? {:controller => 'issues', :action => 'index', :project_id => @project} : params
215 url_params = controller_name == 'issues' ? {:controller => 'issues', :action => 'index', :project_id => @project} : params
216
216
217 content_tag('h3', title) + "\n" +
217 content_tag('h3', title) + "\n" +
218 content_tag('ul',
218 content_tag('ul',
219 queries.collect {|query|
219 queries.collect {|query|
220 css = 'query'
220 css = 'query'
221 css << ' selected' if query == @query
221 css << ' selected' if query == @query
222 content_tag('li', link_to(query.name, url_params.merge(:query_id => query), :class => css))
222 content_tag('li', link_to(query.name, url_params.merge(:query_id => query), :class => css))
223 }.join("\n").html_safe,
223 }.join("\n").html_safe,
224 :class => 'queries'
224 :class => 'queries'
225 ) + "\n"
225 ) + "\n"
226 end
226 end
227
227
228 def render_sidebar_queries
228 def render_sidebar_queries
229 out = ''.html_safe
229 out = ''.html_safe
230 out << query_links(l(:label_my_queries), sidebar_queries.select(&:is_private?))
230 out << query_links(l(:label_my_queries), sidebar_queries.select(&:is_private?))
231 out << query_links(l(:label_query_plural), sidebar_queries.reject(&:is_private?))
231 out << query_links(l(:label_query_plural), sidebar_queries.reject(&:is_private?))
232 out
232 out
233 end
233 end
234
234
235 def email_issue_attributes(issue, user)
235 def email_issue_attributes(issue, user)
236 items = []
236 items = []
237 %w(author status priority assigned_to category fixed_version).each do |attribute|
237 %w(author status priority assigned_to category fixed_version).each do |attribute|
238 unless issue.disabled_core_fields.include?(attribute+"_id")
238 unless issue.disabled_core_fields.include?(attribute+"_id")
239 items << "#{l("field_#{attribute}")}: #{issue.send attribute}"
239 items << "#{l("field_#{attribute}")}: #{issue.send attribute}"
240 end
240 end
241 end
241 end
242 issue.visible_custom_field_values(user).each do |value|
242 issue.visible_custom_field_values(user).each do |value|
243 items << "#{value.custom_field.name}: #{show_value(value, false)}"
243 items << "#{value.custom_field.name}: #{show_value(value, false)}"
244 end
244 end
245 items
245 items
246 end
246 end
247
247
248 def render_email_issue_attributes(issue, user, html=false)
248 def render_email_issue_attributes(issue, user, html=false)
249 items = email_issue_attributes(issue, user)
249 items = email_issue_attributes(issue, user)
250 if html
250 if html
251 content_tag('ul', items.map{|s| content_tag('li', s)}.join("\n").html_safe)
251 content_tag('ul', items.map{|s| content_tag('li', s)}.join("\n").html_safe)
252 else
252 else
253 items.map{|s| "* #{s}"}.join("\n")
253 items.map{|s| "* #{s}"}.join("\n")
254 end
254 end
255 end
255 end
256
256
257 # Returns the textual representation of a journal details
257 # Returns the textual representation of a journal details
258 # as an array of strings
258 # as an array of strings
259 def details_to_strings(details, no_html=false, options={})
259 def details_to_strings(details, no_html=false, options={})
260 options[:only_path] = (options[:only_path] == false ? false : true)
260 options[:only_path] = (options[:only_path] == false ? false : true)
261 strings = []
261 strings = []
262 values_by_field = {}
262 values_by_field = {}
263 details.each do |detail|
263 details.each do |detail|
264 if detail.property == 'cf'
264 if detail.property == 'cf'
265 field = detail.custom_field
265 field = detail.custom_field
266 if field && field.multiple?
266 if field && field.multiple?
267 values_by_field[field] ||= {:added => [], :deleted => []}
267 values_by_field[field] ||= {:added => [], :deleted => []}
268 if detail.old_value
268 if detail.old_value
269 values_by_field[field][:deleted] << detail.old_value
269 values_by_field[field][:deleted] << detail.old_value
270 end
270 end
271 if detail.value
271 if detail.value
272 values_by_field[field][:added] << detail.value
272 values_by_field[field][:added] << detail.value
273 end
273 end
274 next
274 next
275 end
275 end
276 end
276 end
277 strings << show_detail(detail, no_html, options)
277 strings << show_detail(detail, no_html, options)
278 end
278 end
279 values_by_field.each do |field, changes|
279 values_by_field.each do |field, changes|
280 detail = JournalDetail.new(:property => 'cf', :prop_key => field.id.to_s)
280 detail = JournalDetail.new(:property => 'cf', :prop_key => field.id.to_s)
281 detail.instance_variable_set "@custom_field", field
281 detail.instance_variable_set "@custom_field", field
282 if changes[:added].any?
282 if changes[:added].any?
283 detail.value = changes[:added]
283 detail.value = changes[:added]
284 strings << show_detail(detail, no_html, options)
284 strings << show_detail(detail, no_html, options)
285 elsif changes[:deleted].any?
285 elsif changes[:deleted].any?
286 detail.old_value = changes[:deleted]
286 detail.old_value = changes[:deleted]
287 strings << show_detail(detail, no_html, options)
287 strings << show_detail(detail, no_html, options)
288 end
288 end
289 end
289 end
290 strings
290 strings
291 end
291 end
292
292
293 # Returns the textual representation of a single journal detail
293 # Returns the textual representation of a single journal detail
294 def show_detail(detail, no_html=false, options={})
294 def show_detail(detail, no_html=false, options={})
295 multiple = false
295 multiple = false
296 case detail.property
296 case detail.property
297 when 'attr'
297 when 'attr'
298 field = detail.prop_key.to_s.gsub(/\_id$/, "")
298 field = detail.prop_key.to_s.gsub(/\_id$/, "")
299 label = l(("field_" + field).to_sym)
299 label = l(("field_" + field).to_sym)
300 case detail.prop_key
300 case detail.prop_key
301 when 'due_date', 'start_date'
301 when 'due_date', 'start_date'
302 value = format_date(detail.value.to_date) if detail.value
302 value = format_date(detail.value.to_date) if detail.value
303 old_value = format_date(detail.old_value.to_date) if detail.old_value
303 old_value = format_date(detail.old_value.to_date) if detail.old_value
304
304
305 when 'project_id', 'status_id', 'tracker_id', 'assigned_to_id',
305 when 'project_id', 'status_id', 'tracker_id', 'assigned_to_id',
306 'priority_id', 'category_id', 'fixed_version_id'
306 'priority_id', 'category_id', 'fixed_version_id'
307 value = find_name_by_reflection(field, detail.value)
307 value = find_name_by_reflection(field, detail.value)
308 old_value = find_name_by_reflection(field, detail.old_value)
308 old_value = find_name_by_reflection(field, detail.old_value)
309
309
310 when 'estimated_hours'
310 when 'estimated_hours'
311 value = "%0.02f" % detail.value.to_f unless detail.value.blank?
311 value = "%0.02f" % detail.value.to_f unless detail.value.blank?
312 old_value = "%0.02f" % detail.old_value.to_f unless detail.old_value.blank?
312 old_value = "%0.02f" % detail.old_value.to_f unless detail.old_value.blank?
313
313
314 when 'parent_id'
314 when 'parent_id'
315 label = l(:field_parent_issue)
315 label = l(:field_parent_issue)
316 value = "##{detail.value}" unless detail.value.blank?
316 value = "##{detail.value}" unless detail.value.blank?
317 old_value = "##{detail.old_value}" unless detail.old_value.blank?
317 old_value = "##{detail.old_value}" unless detail.old_value.blank?
318
318
319 when 'is_private'
319 when 'is_private'
320 value = l(detail.value == "0" ? :general_text_No : :general_text_Yes) unless detail.value.blank?
320 value = l(detail.value == "0" ? :general_text_No : :general_text_Yes) unless detail.value.blank?
321 old_value = l(detail.old_value == "0" ? :general_text_No : :general_text_Yes) unless detail.old_value.blank?
321 old_value = l(detail.old_value == "0" ? :general_text_No : :general_text_Yes) unless detail.old_value.blank?
322 end
322 end
323 when 'cf'
323 when 'cf'
324 custom_field = detail.custom_field
324 custom_field = detail.custom_field
325 if custom_field
325 if custom_field
326 multiple = custom_field.multiple?
326 multiple = custom_field.multiple?
327 label = custom_field.name
327 label = custom_field.name
328 value = format_value(detail.value, custom_field) if detail.value
328 value = format_value(detail.value, custom_field) if detail.value
329 old_value = format_value(detail.old_value, custom_field) if detail.old_value
329 old_value = format_value(detail.old_value, custom_field) if detail.old_value
330 end
330 end
331 when 'attachment'
331 when 'attachment'
332 label = l(:label_attachment)
332 label = l(:label_attachment)
333 when 'relation'
333 when 'relation'
334 if detail.value && !detail.old_value
334 if detail.value && !detail.old_value
335 rel_issue = Issue.visible.find_by_id(detail.value)
335 rel_issue = Issue.visible.find_by_id(detail.value)
336 value = rel_issue.nil? ? "#{l(:label_issue)} ##{detail.value}" :
336 value = rel_issue.nil? ? "#{l(:label_issue)} ##{detail.value}" :
337 (no_html ? rel_issue : link_to_issue(rel_issue, :only_path => options[:only_path]))
337 (no_html ? rel_issue : link_to_issue(rel_issue, :only_path => options[:only_path]))
338 elsif detail.old_value && !detail.value
338 elsif detail.old_value && !detail.value
339 rel_issue = Issue.visible.find_by_id(detail.old_value)
339 rel_issue = Issue.visible.find_by_id(detail.old_value)
340 old_value = rel_issue.nil? ? "#{l(:label_issue)} ##{detail.old_value}" :
340 old_value = rel_issue.nil? ? "#{l(:label_issue)} ##{detail.old_value}" :
341 (no_html ? rel_issue : link_to_issue(rel_issue, :only_path => options[:only_path]))
341 (no_html ? rel_issue : link_to_issue(rel_issue, :only_path => options[:only_path]))
342 end
342 end
343 relation_type = IssueRelation::TYPES[detail.prop_key]
343 relation_type = IssueRelation::TYPES[detail.prop_key]
344 label = l(relation_type[:name]) if relation_type
344 label = l(relation_type[:name]) if relation_type
345 end
345 end
346 call_hook(:helper_issues_show_detail_after_setting,
346 call_hook(:helper_issues_show_detail_after_setting,
347 {:detail => detail, :label => label, :value => value, :old_value => old_value })
347 {:detail => detail, :label => label, :value => value, :old_value => old_value })
348
348
349 label ||= detail.prop_key
349 label ||= detail.prop_key
350 value ||= detail.value
350 value ||= detail.value
351 old_value ||= detail.old_value
351 old_value ||= detail.old_value
352
352
353 unless no_html
353 unless no_html
354 label = content_tag('strong', label)
354 label = content_tag('strong', label)
355 old_value = content_tag("i", h(old_value)) if detail.old_value
355 old_value = content_tag("i", h(old_value)) if detail.old_value
356 if detail.old_value && detail.value.blank? && detail.property != 'relation'
356 if detail.old_value && detail.value.blank? && detail.property != 'relation'
357 old_value = content_tag("del", old_value)
357 old_value = content_tag("del", old_value)
358 end
358 end
359 if detail.property == 'attachment' && !value.blank? && atta = Attachment.find_by_id(detail.prop_key)
359 if detail.property == 'attachment' && !value.blank? && atta = Attachment.find_by_id(detail.prop_key)
360 # Link to the attachment if it has not been removed
360 # Link to the attachment if it has not been removed
361 value = link_to_attachment(atta, :download => true, :only_path => options[:only_path])
361 value = link_to_attachment(atta, :download => true, :only_path => options[:only_path])
362 if options[:only_path] != false && atta.is_text?
362 if options[:only_path] != false && atta.is_text?
363 value += link_to(
363 value += link_to(
364 image_tag('magnifier.png'),
364 image_tag('magnifier.png'),
365 :controller => 'attachments', :action => 'show',
365 :controller => 'attachments', :action => 'show',
366 :id => atta, :filename => atta.filename
366 :id => atta, :filename => atta.filename
367 )
367 )
368 end
368 end
369 else
369 else
370 value = content_tag("i", h(value)) if value
370 value = content_tag("i", h(value)) if value
371 end
371 end
372 end
372 end
373
373
374 if detail.property == 'attr' && detail.prop_key == 'description'
374 if detail.property == 'attr' && detail.prop_key == 'description'
375 s = l(:text_journal_changed_no_detail, :label => label)
375 s = l(:text_journal_changed_no_detail, :label => label)
376 unless no_html
376 unless no_html
377 diff_link = link_to 'diff',
377 diff_link = link_to 'diff',
378 {:controller => 'journals', :action => 'diff', :id => detail.journal_id,
378 {:controller => 'journals', :action => 'diff', :id => detail.journal_id,
379 :detail_id => detail.id, :only_path => options[:only_path]},
379 :detail_id => detail.id, :only_path => options[:only_path]},
380 :title => l(:label_view_diff)
380 :title => l(:label_view_diff)
381 s << " (#{ diff_link })"
381 s << " (#{ diff_link })"
382 end
382 end
383 s.html_safe
383 s.html_safe
384 elsif detail.value.present?
384 elsif detail.value.present?
385 case detail.property
385 case detail.property
386 when 'attr', 'cf'
386 when 'attr', 'cf'
387 if detail.old_value.present?
387 if detail.old_value.present?
388 l(:text_journal_changed, :label => label, :old => old_value, :new => value).html_safe
388 l(:text_journal_changed, :label => label, :old => old_value, :new => value).html_safe
389 elsif multiple
389 elsif multiple
390 l(:text_journal_added, :label => label, :value => value).html_safe
390 l(:text_journal_added, :label => label, :value => value).html_safe
391 else
391 else
392 l(:text_journal_set_to, :label => label, :value => value).html_safe
392 l(:text_journal_set_to, :label => label, :value => value).html_safe
393 end
393 end
394 when 'attachment', 'relation'
394 when 'attachment', 'relation'
395 l(:text_journal_added, :label => label, :value => value).html_safe
395 l(:text_journal_added, :label => label, :value => value).html_safe
396 end
396 end
397 else
397 else
398 l(:text_journal_deleted, :label => label, :old => old_value).html_safe
398 l(:text_journal_deleted, :label => label, :old => old_value).html_safe
399 end
399 end
400 end
400 end
401
401
402 # Find the name of an associated record stored in the field attribute
402 # Find the name of an associated record stored in the field attribute
403 def find_name_by_reflection(field, id)
403 def find_name_by_reflection(field, id)
404 unless id.present?
404 unless id.present?
405 return nil
405 return nil
406 end
406 end
407 association = Issue.reflect_on_association(field.to_sym)
407 association = Issue.reflect_on_association(field.to_sym)
408 if association
408 if association
409 record = association.class_name.constantize.find_by_id(id)
409 record = association.class_name.constantize.find_by_id(id)
410 if record
410 if record
411 record.name.force_encoding('UTF-8') if record.name.respond_to?(:force_encoding)
411 record.name.force_encoding('UTF-8') if record.name.respond_to?(:force_encoding)
412 return record.name
412 return record.name
413 end
413 end
414 end
414 end
415 end
415 end
416
416
417 # Renders issue children recursively
417 # Renders issue children recursively
418 def render_api_issue_children(issue, api)
418 def render_api_issue_children(issue, api)
419 return if issue.leaf?
419 return if issue.leaf?
420 api.array :children do
420 api.array :children do
421 issue.children.each do |child|
421 issue.children.each do |child|
422 api.issue(:id => child.id) do
422 api.issue(:id => child.id) do
423 api.tracker(:id => child.tracker_id, :name => child.tracker.name) unless child.tracker.nil?
423 api.tracker(:id => child.tracker_id, :name => child.tracker.name) unless child.tracker.nil?
424 api.subject child.subject
424 api.subject child.subject
425 render_api_issue_children(child, api)
425 render_api_issue_children(child, api)
426 end
426 end
427 end
427 end
428 end
428 end
429 end
429 end
430 end
430 end
General Comments 0
You need to be logged in to leave comments. Login now