comparison app/helpers/issues_helper.rb @ 1338:25603efa57b5

Merge from live branch
author Chris Cannam
date Thu, 20 Jun 2013 13:14:14 +0100
parents 3e4c3460b6ca
children 622f24f53b42 261b3d9a4903
comparison
equal deleted inserted replaced
1209:1b1138f6f55e 1338:25603efa57b5
1 # encoding: utf-8 1 # encoding: utf-8
2 # 2 #
3 # Redmine - project management software 3 # Redmine - project management software
4 # Copyright (C) 2006-2011 Jean-Philippe Lang 4 # Copyright (C) 2006-2012 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.
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 />" + 51 link_to_issue(issue) + "<br /><br />".html_safe +
52 "<strong>#{@cached_label_project}</strong>: #{link_to_project(issue.project)}<br />" + 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 />" + 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 />" + 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 />" + 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 />" + 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)) 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
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.sort_by(&:lft)) do |child, level|
83 css = "issue issue-#{child.id} hascontextmenu"
84 css << " idnt idnt-#{level}" if level > 0
83 s << content_tag('tr', 85 s << content_tag('tr',
84 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') +
85 content_tag('td', link_to_issue(child, :truncate => 60), :class => 'subject') + 87 content_tag('td', link_to_issue(child, :truncate => 60, :project => (issue.project_id != child.project_id)), :class => 'subject') +
86 content_tag('td', h(child.status)) + 88 content_tag('td', h(child.status)) +
87 content_tag('td', link_to_user(child.assigned_to)) + 89 content_tag('td', link_to_user(child.assigned_to)) +
88 content_tag('td', progress_bar(child.done_ratio, :width => '80px')), 90 content_tag('td', progress_bar(child.done_ratio, :width => '80px')),
89 :class => "issue issue-#{child.id} hascontextmenu #{level > 0 ? "idnt idnt-#{level}" : nil}") 91 :class => css)
90 end 92 end
91 s << '</form></table>' 93 s << '</table></form>'
92 s.html_safe 94 s.html_safe
95 end
96
97 # Returns a link for adding a new subtask to the given issue
98 def link_to_new_subtask(issue)
99 attrs = {
100 :tracker_id => issue.tracker,
101 :parent_issue_id => issue
102 }
103 link_to(l(:button_add), new_project_issue_path(issue.project, :issue => attrs))
104 end
105
106 class IssueFieldsRows
107 include ActionView::Helpers::TagHelper
108
109 def initialize
110 @left = []
111 @right = []
112 end
113
114 def left(*args)
115 args.any? ? @left << cells(*args) : @left
116 end
117
118 def right(*args)
119 args.any? ? @right << cells(*args) : @right
120 end
121
122 def size
123 @left.size > @right.size ? @left.size : @right.size
124 end
125
126 def to_html
127 html = ''.html_safe
128 blank = content_tag('th', '') + content_tag('td', '')
129 size.times do |i|
130 left = @left[i] || blank
131 right = @right[i] || blank
132 html << content_tag('tr', left + right)
133 end
134 html
135 end
136
137 def cells(label, text, options={})
138 content_tag('th', "#{label}:", options) + content_tag('td', text, options)
139 end
140 end
141
142 def issue_fields_rows
143 r = IssueFieldsRows.new
144 yield r
145 r.to_html
93 end 146 end
94 147
95 def render_custom_fields_rows(issue) 148 def render_custom_fields_rows(issue)
96 return if issue.custom_field_values.empty? 149 return if issue.custom_field_values.empty?
97 ordered_values = [] 150 ordered_values = []
129 message 182 message
130 end 183 end
131 184
132 def sidebar_queries 185 def sidebar_queries
133 unless @sidebar_queries 186 unless @sidebar_queries
134 # User can see public queries and his own queries 187 @sidebar_queries = Query.visible.all(
135 visible = ARCondition.new(["is_public = ? OR user_id = ?", true, (User.current.logged? ? User.current.id : 0)]) 188 :order => "#{Query.table_name}.name ASC",
136 # Project specific queries and global queries 189 # Project specific queries and global queries
137 visible << (@project.nil? ? ["project_id IS NULL"] : ["project_id IS NULL OR project_id = ?", @project.id]) 190 :conditions => (@project.nil? ? ["project_id IS NULL"] : ["project_id IS NULL OR project_id = ?", @project.id])
138 @sidebar_queries = Query.find(:all, 191 )
139 :select => 'id, name, is_public',
140 :order => "name ASC",
141 :conditions => visible.conditions)
142 end 192 end
143 @sidebar_queries 193 @sidebar_queries
144 end 194 end
145 195
146 def query_links(title, queries) 196 def query_links(title, queries)
147 # links to #index on issues/show 197 # links to #index on issues/show
148 url_params = controller_name == 'issues' ? {:controller => 'issues', :action => 'index', :project_id => @project} : params 198 url_params = controller_name == 'issues' ? {:controller => 'issues', :action => 'index', :project_id => @project} : params
149 199
150 content_tag('h3', h(title)) + 200 content_tag('h3', h(title)) +
151 queries.collect {|query| 201 queries.collect {|query|
152 link_to(h(query.name), url_params.merge(:query_id => query)) 202 css = 'query'
153 }.join('<br />') 203 css << ' selected' if query == @query
204 link_to(h(query.name), url_params.merge(:query_id => query), :class => css)
205 }.join('<br />').html_safe
154 end 206 end
155 207
156 def render_sidebar_queries 208 def render_sidebar_queries
157 out = '' 209 out = ''.html_safe
158 queries = sidebar_queries.select {|q| !q.is_public?} 210 queries = sidebar_queries.select {|q| !q.is_public?}
159 out << query_links(l(:label_my_queries), queries) if queries.any? 211 out << query_links(l(:label_my_queries), queries) if queries.any?
160 queries = sidebar_queries.select {|q| q.is_public?} 212 queries = sidebar_queries.select {|q| q.is_public?}
161 out << query_links(l(:label_query_plural), queries) if queries.any? 213 out << query_links(l(:label_query_plural), queries) if queries.any?
162 out 214 out
163 end 215 end
164 216
165 def show_detail(detail, no_html=false) 217 # Returns the textual representation of a journal details
218 # as an array of strings
219 def details_to_strings(details, no_html=false, options={})
220 options[:only_path] = (options[:only_path] == false ? false : true)
221 strings = []
222 values_by_field = {}
223 details.each do |detail|
224 if detail.property == 'cf'
225 field_id = detail.prop_key
226 field = CustomField.find_by_id(field_id)
227 if field && field.multiple?
228 values_by_field[field_id] ||= {:added => [], :deleted => []}
229 if detail.old_value
230 values_by_field[field_id][:deleted] << detail.old_value
231 end
232 if detail.value
233 values_by_field[field_id][:added] << detail.value
234 end
235 next
236 end
237 end
238 strings << show_detail(detail, no_html, options)
239 end
240 values_by_field.each do |field_id, changes|
241 detail = JournalDetail.new(:property => 'cf', :prop_key => field_id)
242 if changes[:added].any?
243 detail.value = changes[:added]
244 strings << show_detail(detail, no_html, options)
245 elsif changes[:deleted].any?
246 detail.old_value = changes[:deleted]
247 strings << show_detail(detail, no_html, options)
248 end
249 end
250 strings
251 end
252
253 # Returns the textual representation of a single journal detail
254 def show_detail(detail, no_html=false, options={})
255 multiple = false
166 case detail.property 256 case detail.property
167 when 'attr' 257 when 'attr'
168 field = detail.prop_key.to_s.gsub(/\_id$/, "") 258 field = detail.prop_key.to_s.gsub(/\_id$/, "")
169 label = l(("field_" + field).to_sym) 259 label = l(("field_" + field).to_sym)
170 case 260 case detail.prop_key
171 when ['due_date', 'start_date'].include?(detail.prop_key) 261 when 'due_date', 'start_date'
172 value = format_date(detail.value.to_date) if detail.value 262 value = format_date(detail.value.to_date) if detail.value
173 old_value = format_date(detail.old_value.to_date) if detail.old_value 263 old_value = format_date(detail.old_value.to_date) if detail.old_value
174 264
175 when ['project_id', 'status_id', 'tracker_id', 'assigned_to_id', 'priority_id', 'category_id', 'fixed_version_id'].include?(detail.prop_key) 265 when 'project_id', 'status_id', 'tracker_id', 'assigned_to_id',
266 'priority_id', 'category_id', 'fixed_version_id'
176 value = find_name_by_reflection(field, detail.value) 267 value = find_name_by_reflection(field, detail.value)
177 old_value = find_name_by_reflection(field, detail.old_value) 268 old_value = find_name_by_reflection(field, detail.old_value)
178 269
179 when detail.prop_key == 'estimated_hours' 270 when 'estimated_hours'
180 value = "%0.02f" % detail.value.to_f unless detail.value.blank? 271 value = "%0.02f" % detail.value.to_f unless detail.value.blank?
181 old_value = "%0.02f" % detail.old_value.to_f unless detail.old_value.blank? 272 old_value = "%0.02f" % detail.old_value.to_f unless detail.old_value.blank?
182 273
183 when detail.prop_key == 'parent_id' 274 when 'parent_id'
184 label = l(:field_parent_issue) 275 label = l(:field_parent_issue)
185 value = "##{detail.value}" unless detail.value.blank? 276 value = "##{detail.value}" unless detail.value.blank?
186 old_value = "##{detail.old_value}" unless detail.old_value.blank? 277 old_value = "##{detail.old_value}" unless detail.old_value.blank?
187 278
188 when detail.prop_key == 'is_private' 279 when 'is_private'
189 value = l(detail.value == "0" ? :general_text_No : :general_text_Yes) unless detail.value.blank? 280 value = l(detail.value == "0" ? :general_text_No : :general_text_Yes) unless detail.value.blank?
190 old_value = l(detail.old_value == "0" ? :general_text_No : :general_text_Yes) unless detail.old_value.blank? 281 old_value = l(detail.old_value == "0" ? :general_text_No : :general_text_Yes) unless detail.old_value.blank?
191 end 282 end
192 when 'cf' 283 when 'cf'
193 custom_field = CustomField.find_by_id(detail.prop_key) 284 custom_field = CustomField.find_by_id(detail.prop_key)
194 if custom_field 285 if custom_field
286 multiple = custom_field.multiple?
195 label = custom_field.name 287 label = custom_field.name
196 value = format_value(detail.value, custom_field.field_format) if detail.value 288 value = format_value(detail.value, custom_field.field_format) if detail.value
197 old_value = format_value(detail.old_value, custom_field.field_format) if detail.old_value 289 old_value = format_value(detail.old_value, custom_field.field_format) if detail.old_value
198 end 290 end
199 when 'attachment' 291 when 'attachment'
200 label = l(:label_attachment) 292 label = l(:label_attachment)
201 end 293 end
202 call_hook(:helper_issues_show_detail_after_setting, {:detail => detail, :label => label, :value => value, :old_value => old_value }) 294 call_hook(:helper_issues_show_detail_after_setting,
295 {:detail => detail, :label => label, :value => value, :old_value => old_value })
203 296
204 label ||= detail.prop_key 297 label ||= detail.prop_key
205 value ||= detail.value 298 value ||= detail.value
206 old_value ||= detail.old_value 299 old_value ||= detail.old_value
207 300
208 unless no_html 301 unless no_html
209 label = content_tag('strong', label) 302 label = content_tag('strong', label)
210 old_value = content_tag("i", h(old_value)) if detail.old_value 303 old_value = content_tag("i", h(old_value)) if detail.old_value
211 old_value = content_tag("strike", old_value) if detail.old_value and detail.value.blank? 304 old_value = content_tag("del", old_value) if detail.old_value and detail.value.blank?
212 if detail.property == 'attachment' && !value.blank? && a = Attachment.find_by_id(detail.prop_key) 305 if detail.property == 'attachment' && !value.blank? && atta = Attachment.find_by_id(detail.prop_key)
213 # Link to the attachment if it has not been removed 306 # Link to the attachment if it has not been removed
214 value = link_to_attachment(a) 307 value = link_to_attachment(atta, :download => true, :only_path => options[:only_path])
308 if options[:only_path] != false && atta.is_text?
309 value += link_to(
310 image_tag('magnifier.png'),
311 :controller => 'attachments', :action => 'show',
312 :id => atta, :filename => atta.filename
313 )
314 end
215 else 315 else
216 value = content_tag("i", h(value)) if value 316 value = content_tag("i", h(value)) if value
217 end 317 end
218 end 318 end
219 319
220 if detail.property == 'attr' && detail.prop_key == 'description' 320 if detail.property == 'attr' && detail.prop_key == 'description'
221 s = l(:text_journal_changed_no_detail, :label => label) 321 s = l(:text_journal_changed_no_detail, :label => label)
222 unless no_html 322 unless no_html
223 diff_link = link_to 'diff', 323 diff_link = link_to 'diff',
224 {:controller => 'journals', :action => 'diff', :id => detail.journal_id, :detail_id => detail.id}, 324 {:controller => 'journals', :action => 'diff', :id => detail.journal_id,
325 :detail_id => detail.id, :only_path => options[:only_path]},
225 :title => l(:label_view_diff) 326 :title => l(:label_view_diff)
226 s << " (#{ diff_link })" 327 s << " (#{ diff_link })"
227 end 328 end
228 s 329 s.html_safe
229 elsif !detail.value.blank? 330 elsif detail.value.present?
230 case detail.property 331 case detail.property
231 when 'attr', 'cf' 332 when 'attr', 'cf'
232 if !detail.old_value.blank? 333 if detail.old_value.present?
233 l(:text_journal_changed, :label => label, :old => old_value, :new => value) 334 l(:text_journal_changed, :label => label, :old => old_value, :new => value).html_safe
335 elsif multiple
336 l(:text_journal_added, :label => label, :value => value).html_safe
234 else 337 else
235 l(:text_journal_set_to, :label => label, :value => value) 338 l(:text_journal_set_to, :label => label, :value => value).html_safe
236 end 339 end
237 when 'attachment' 340 when 'attachment'
238 l(:text_journal_added, :label => label, :value => value) 341 l(:text_journal_added, :label => label, :value => value).html_safe
239 end 342 end
240 else 343 else
241 l(:text_journal_deleted, :label => label, :old => old_value) 344 l(:text_journal_deleted, :label => label, :old => old_value).html_safe
242 end 345 end
243 end 346 end
244 347
245 # Find the name of an associated record stored in the field attribute 348 # Find the name of an associated record stored in the field attribute
246 def find_name_by_reflection(field, id) 349 def find_name_by_reflection(field, id)
247 association = Issue.reflect_on_association(field.to_sym) 350 association = Issue.reflect_on_association(field.to_sym)
248 if association 351 if association
249 record = association.class_name.constantize.find_by_id(id) 352 record = association.class_name.constantize.find_by_id(id)
250 return record.name if record 353 if record
354 record.name.force_encoding('UTF-8') if record.name.respond_to?(:force_encoding)
355 return record.name
356 end
251 end 357 end
252 end 358 end
253 359
254 # Renders issue children recursively 360 # Renders issue children recursively
255 def render_api_issue_children(issue, api) 361 def render_api_issue_children(issue, api)
266 end 372 end
267 373
268 def issues_to_csv(issues, project, query, options={}) 374 def issues_to_csv(issues, project, query, options={})
269 decimal_separator = l(:general_csv_decimal_separator) 375 decimal_separator = l(:general_csv_decimal_separator)
270 encoding = l(:general_csv_encoding) 376 encoding = l(:general_csv_encoding)
271 columns = (options[:columns] == 'all' ? query.available_columns : query.columns) 377 columns = (options[:columns] == 'all' ? query.available_inline_columns : query.inline_columns)
378 if options[:description]
379 if description = query.available_columns.detect {|q| q.name == :description}
380 columns << description
381 end
382 end
272 383
273 export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv| 384 export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv|
274 # csv header fields 385 # csv header fields
275 csv << [ "#" ] + columns.collect {|c| Redmine::CodesetUtil.from_utf8(c.caption.to_s, encoding) } + 386 csv << [ "#" ] + columns.collect {|c| Redmine::CodesetUtil.from_utf8(c.caption.to_s, encoding) }
276 (options[:description] ? [Redmine::CodesetUtil.from_utf8(l(:field_description), encoding)] : [])
277 387
278 # csv lines 388 # csv lines
279 issues.each do |issue| 389 issues.each do |issue|
280 col_values = columns.collect do |column| 390 col_values = columns.collect do |column|
281 s = if column.is_a?(QueryCustomFieldColumn) 391 s = if column.is_a?(QueryCustomFieldColumn)
282 cv = issue.custom_values.detect {|v| v.custom_field_id == column.custom_field.id} 392 cv = issue.custom_field_values.detect {|v| v.custom_field_id == column.custom_field.id}
283 show_value(cv) 393 show_value(cv)
284 else 394 else
285 value = issue.send(column.name) 395 value = column.value(issue)
286 if value.is_a?(Date) 396 if value.is_a?(Date)
287 format_date(value) 397 format_date(value)
288 elsif value.is_a?(Time) 398 elsif value.is_a?(Time)
289 format_time(value) 399 format_time(value)
290 elsif value.is_a?(Float) 400 elsif value.is_a?(Float)
291 value.to_s.gsub('.', decimal_separator) 401 ("%.2f" % value).gsub('.', decimal_separator)
292 else 402 else
293 value 403 value
294 end 404 end
295 end 405 end
296 s.to_s 406 s.to_s
297 end 407 end
298 csv << [ issue.id.to_s ] + col_values.collect {|c| Redmine::CodesetUtil.from_utf8(c.to_s, encoding) } + 408 csv << [ issue.id.to_s ] + col_values.collect {|c| Redmine::CodesetUtil.from_utf8(c.to_s, encoding) }
299 (options[:description] ? [Redmine::CodesetUtil.from_utf8(issue.description, encoding)] : [])
300 end 409 end
301 end 410 end
302 export 411 export
303 end 412 end
304 end 413 end