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