##// END OF EJS Templates
Use #issue_fields_rows to render custom fields values....
Jean-Philippe Lang -
r14464:2350bc29f417
parent child
Show More
@@ -1,524 +1,516
1 1 # encoding: utf-8
2 2 #
3 3 # Redmine - project management software
4 4 # Copyright (C) 2006-2015 Jean-Philippe Lang
5 5 #
6 6 # This program is free software; you can redistribute it and/or
7 7 # modify it under the terms of the GNU General Public License
8 8 # as published by the Free Software Foundation; either version 2
9 9 # of the License, or (at your option) any later version.
10 10 #
11 11 # This program is distributed in the hope that it will be useful,
12 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 14 # GNU General Public License for more details.
15 15 #
16 16 # You should have received a copy of the GNU General Public License
17 17 # along with this program; if not, write to the Free Software
18 18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 19
20 20 module IssuesHelper
21 21 include ApplicationHelper
22 22 include Redmine::Export::PDF::IssuesPdfHelper
23 23
24 24 def issue_list(issues, &block)
25 25 ancestors = []
26 26 issues.each do |issue|
27 27 while (ancestors.any? && !issue.is_descendant_of?(ancestors.last))
28 28 ancestors.pop
29 29 end
30 30 yield issue, ancestors.size
31 31 ancestors << issue unless issue.leaf?
32 32 end
33 33 end
34 34
35 35 def grouped_issue_list(issues, query, issue_count_by_group, &block)
36 36 previous_group, first = false, true
37 37 totals_by_group = query.totalable_columns.inject({}) do |h, column|
38 38 h[column] = query.total_by_group_for(column)
39 39 h
40 40 end
41 41 issue_list(issues) do |issue, level|
42 42 group_name = group_count = nil
43 43 if query.grouped?
44 44 group = query.group_by_column.value(issue)
45 45 if first || group != previous_group
46 46 if group.blank? && group != false
47 47 group_name = "(#{l(:label_blank_value)})"
48 48 else
49 49 group_name = format_object(group)
50 50 end
51 51 group_name ||= ""
52 52 group_count = issue_count_by_group[group]
53 53 group_totals = totals_by_group.map {|column, t| total_tag(column, t[group] || 0)}.join(" ").html_safe
54 54 end
55 55 end
56 56 yield issue, level, group_name, group_count, group_totals
57 57 previous_group, first = group, false
58 58 end
59 59 end
60 60
61 61 # Renders a HTML/CSS tooltip
62 62 #
63 63 # To use, a trigger div is needed. This is a div with the class of "tooltip"
64 64 # that contains this method wrapped in a span with the class of "tip"
65 65 #
66 66 # <div class="tooltip"><%= link_to_issue(issue) %>
67 67 # <span class="tip"><%= render_issue_tooltip(issue) %></span>
68 68 # </div>
69 69 #
70 70 def render_issue_tooltip(issue)
71 71 @cached_label_status ||= l(:field_status)
72 72 @cached_label_start_date ||= l(:field_start_date)
73 73 @cached_label_due_date ||= l(:field_due_date)
74 74 @cached_label_assigned_to ||= l(:field_assigned_to)
75 75 @cached_label_priority ||= l(:field_priority)
76 76 @cached_label_project ||= l(:field_project)
77 77
78 78 link_to_issue(issue) + "<br /><br />".html_safe +
79 79 "<strong>#{@cached_label_project}</strong>: #{link_to_project(issue.project)}<br />".html_safe +
80 80 "<strong>#{@cached_label_status}</strong>: #{h(issue.status.name)}<br />".html_safe +
81 81 "<strong>#{@cached_label_start_date}</strong>: #{format_date(issue.start_date)}<br />".html_safe +
82 82 "<strong>#{@cached_label_due_date}</strong>: #{format_date(issue.due_date)}<br />".html_safe +
83 83 "<strong>#{@cached_label_assigned_to}</strong>: #{h(issue.assigned_to)}<br />".html_safe +
84 84 "<strong>#{@cached_label_priority}</strong>: #{h(issue.priority.name)}".html_safe
85 85 end
86 86
87 87 def issue_heading(issue)
88 88 h("#{issue.tracker} ##{issue.id}")
89 89 end
90 90
91 91 def render_issue_subject_with_tree(issue)
92 92 s = ''
93 93 ancestors = issue.root? ? [] : issue.ancestors.visible.to_a
94 94 ancestors.each do |ancestor|
95 95 s << '<div>' + content_tag('p', link_to_issue(ancestor, :project => (issue.project_id != ancestor.project_id)))
96 96 end
97 97 s << '<div>'
98 98 subject = h(issue.subject)
99 99 if issue.is_private?
100 100 subject = content_tag('span', l(:field_is_private), :class => 'private') + ' ' + subject
101 101 end
102 102 s << content_tag('h3', subject)
103 103 s << '</div>' * (ancestors.size + 1)
104 104 s.html_safe
105 105 end
106 106
107 107 def render_descendants_tree(issue)
108 108 s = '<form><table class="list issues">'
109 109 issue_list(issue.descendants.visible.preload(:status, :priority, :tracker).sort_by(&:lft)) do |child, level|
110 110 css = "issue issue-#{child.id} hascontextmenu"
111 111 css << " idnt idnt-#{level}" if level > 0
112 112 s << content_tag('tr',
113 113 content_tag('td', check_box_tag("ids[]", child.id, false, :id => nil), :class => 'checkbox') +
114 114 content_tag('td', link_to_issue(child, :project => (issue.project_id != child.project_id)), :class => 'subject', :style => 'width: 50%') +
115 115 content_tag('td', h(child.status)) +
116 116 content_tag('td', link_to_user(child.assigned_to)) +
117 117 content_tag('td', progress_bar(child.done_ratio, :width => '80px')),
118 118 :class => css)
119 119 end
120 120 s << '</table></form>'
121 121 s.html_safe
122 122 end
123 123
124 124 def issue_estimated_hours_details(issue)
125 125 if issue.total_estimated_hours.present?
126 126 if issue.total_estimated_hours == issue.estimated_hours
127 127 l_hours_short(issue.estimated_hours)
128 128 else
129 129 s = issue.estimated_hours.present? ? l_hours_short(issue.estimated_hours) : ""
130 130 s << " (#{l(:label_total)}: #{l_hours_short(issue.total_estimated_hours)})"
131 131 s.html_safe
132 132 end
133 133 end
134 134 end
135 135
136 136 def issue_spent_hours_details(issue)
137 137 if issue.total_spent_hours > 0
138 138 if issue.total_spent_hours == issue.spent_hours
139 139 link_to(l_hours_short(issue.spent_hours), issue_time_entries_path(issue))
140 140 else
141 141 s = issue.spent_hours > 0 ? l_hours_short(issue.spent_hours) : ""
142 142 s << " (#{l(:label_total)}: #{link_to l_hours_short(issue.total_spent_hours), issue_time_entries_path(issue)})"
143 143 s.html_safe
144 144 end
145 145 end
146 146 end
147 147
148 148 # Returns an array of error messages for bulk edited issues
149 149 def bulk_edit_error_messages(issues)
150 150 messages = {}
151 151 issues.each do |issue|
152 152 issue.errors.full_messages.each do |message|
153 153 messages[message] ||= []
154 154 messages[message] << issue
155 155 end
156 156 end
157 157 messages.map { |message, issues|
158 158 "#{message}: " + issues.map {|i| "##{i.id}"}.join(', ')
159 159 }
160 160 end
161 161
162 162 # Returns a link for adding a new subtask to the given issue
163 163 def link_to_new_subtask(issue)
164 164 attrs = {
165 165 :tracker_id => issue.tracker,
166 166 :parent_issue_id => issue
167 167 }
168 168 link_to(l(:button_add), new_project_issue_path(issue.project, :issue => attrs))
169 169 end
170 170
171 171 class IssueFieldsRows
172 172 include ActionView::Helpers::TagHelper
173 173
174 174 def initialize
175 175 @left = []
176 176 @right = []
177 177 end
178 178
179 179 def left(*args)
180 180 args.any? ? @left << cells(*args) : @left
181 181 end
182 182
183 183 def right(*args)
184 184 args.any? ? @right << cells(*args) : @right
185 185 end
186 186
187 187 def size
188 188 @left.size > @right.size ? @left.size : @right.size
189 189 end
190 190
191 191 def to_html
192 192 html = ''.html_safe
193 193 blank = content_tag('th', '') + content_tag('td', '')
194 194 size.times do |i|
195 195 left = @left[i] || blank
196 196 right = @right[i] || blank
197 197 html << content_tag('tr', left + right)
198 198 end
199 199 html
200 200 end
201 201
202 202 def cells(label, text, options={})
203 content_tag('th', "#{label}:", options) + content_tag('td', text, options)
203 content_tag('th', label + ":", options) + content_tag('td', text, options)
204 204 end
205 205 end
206 206
207 207 def issue_fields_rows
208 208 r = IssueFieldsRows.new
209 209 yield r
210 210 r.to_html
211 211 end
212 212
213 213 def render_custom_fields_rows(issue)
214 214 values = issue.visible_custom_field_values
215 215 return if values.empty?
216 ordered_values = []
217 216 half = (values.size / 2.0).ceil
218 half.times do |i|
219 ordered_values << values[i]
220 ordered_values << values[i + half]
221 end
222 s = "<tr>\n"
223 n = 0
224 ordered_values.compact.each do |value|
225 css = "cf_#{value.custom_field.id}"
226 s << "</tr>\n<tr>\n" if n > 0 && (n % 2) == 0
227 s << "\t<th class=\"#{css}\">#{ custom_field_name_tag(value.custom_field) }:</th><td class=\"#{css}\">#{ h(show_value(value)) }</td>\n"
228 n += 1
217 issue_fields_rows do |rows|
218 values.each_with_index do |value, i|
219 css = "cf_#{value.custom_field.id}"
220 m = (i < half ? :left : :right)
221 rows.send m, custom_field_name_tag(value.custom_field), show_value(value), :class => css
222 end
229 223 end
230 s << "</tr>\n"
231 s.html_safe
232 224 end
233 225
234 226 # Returns the path for updating the issue form
235 227 # with project as the current project
236 228 def update_issue_form_path(project, issue)
237 229 options = {:format => 'js'}
238 230 if issue.new_record?
239 231 if project
240 232 new_project_issue_path(project, options)
241 233 else
242 234 new_issue_path(options)
243 235 end
244 236 else
245 237 edit_issue_path(issue, options)
246 238 end
247 239 end
248 240
249 241 # Returns the number of descendants for an array of issues
250 242 def issues_descendant_count(issues)
251 243 ids = issues.reject(&:leaf?).map {|issue| issue.descendants.ids}.flatten.uniq
252 244 ids -= issues.map(&:id)
253 245 ids.size
254 246 end
255 247
256 248 def issues_destroy_confirmation_message(issues)
257 249 issues = [issues] unless issues.is_a?(Array)
258 250 message = l(:text_issues_destroy_confirmation)
259 251
260 252 descendant_count = issues_descendant_count(issues)
261 253 if descendant_count > 0
262 254 message << "\n" + l(:text_issues_destroy_descendants_confirmation, :count => descendant_count)
263 255 end
264 256 message
265 257 end
266 258
267 259 # Returns an array of users that are proposed as watchers
268 260 # on the new issue form
269 261 def users_for_new_issue_watchers(issue)
270 262 users = issue.watcher_users
271 263 if issue.project.users.count <= 20
272 264 users = (users + issue.project.users.sort).uniq
273 265 end
274 266 users
275 267 end
276 268
277 269 def sidebar_queries
278 270 unless @sidebar_queries
279 271 @sidebar_queries = IssueQuery.visible.
280 272 order("#{Query.table_name}.name ASC").
281 273 # Project specific queries and global queries
282 274 where(@project.nil? ? ["project_id IS NULL"] : ["project_id IS NULL OR project_id = ?", @project.id]).
283 275 to_a
284 276 end
285 277 @sidebar_queries
286 278 end
287 279
288 280 def query_links(title, queries)
289 281 return '' if queries.empty?
290 282 # links to #index on issues/show
291 283 url_params = controller_name == 'issues' ? {:controller => 'issues', :action => 'index', :project_id => @project} : params
292 284
293 285 content_tag('h3', title) + "\n" +
294 286 content_tag('ul',
295 287 queries.collect {|query|
296 288 css = 'query'
297 289 css << ' selected' if query == @query
298 290 content_tag('li', link_to(query.name, url_params.merge(:query_id => query), :class => css))
299 291 }.join("\n").html_safe,
300 292 :class => 'queries'
301 293 ) + "\n"
302 294 end
303 295
304 296 def render_sidebar_queries
305 297 out = ''.html_safe
306 298 out << query_links(l(:label_my_queries), sidebar_queries.select(&:is_private?))
307 299 out << query_links(l(:label_query_plural), sidebar_queries.reject(&:is_private?))
308 300 out
309 301 end
310 302
311 303 def email_issue_attributes(issue, user)
312 304 items = []
313 305 %w(author status priority assigned_to category fixed_version).each do |attribute|
314 306 unless issue.disabled_core_fields.include?(attribute+"_id")
315 307 items << "#{l("field_#{attribute}")}: #{issue.send attribute}"
316 308 end
317 309 end
318 310 issue.visible_custom_field_values(user).each do |value|
319 311 items << "#{value.custom_field.name}: #{show_value(value, false)}"
320 312 end
321 313 items
322 314 end
323 315
324 316 def render_email_issue_attributes(issue, user, html=false)
325 317 items = email_issue_attributes(issue, user)
326 318 if html
327 319 content_tag('ul', items.map{|s| content_tag('li', s)}.join("\n").html_safe)
328 320 else
329 321 items.map{|s| "* #{s}"}.join("\n")
330 322 end
331 323 end
332 324
333 325 # Returns the textual representation of a journal details
334 326 # as an array of strings
335 327 def details_to_strings(details, no_html=false, options={})
336 328 options[:only_path] = (options[:only_path] == false ? false : true)
337 329 strings = []
338 330 values_by_field = {}
339 331 details.each do |detail|
340 332 if detail.property == 'cf'
341 333 field = detail.custom_field
342 334 if field && field.multiple?
343 335 values_by_field[field] ||= {:added => [], :deleted => []}
344 336 if detail.old_value
345 337 values_by_field[field][:deleted] << detail.old_value
346 338 end
347 339 if detail.value
348 340 values_by_field[field][:added] << detail.value
349 341 end
350 342 next
351 343 end
352 344 end
353 345 strings << show_detail(detail, no_html, options)
354 346 end
355 347 if values_by_field.present?
356 348 multiple_values_detail = Struct.new(:property, :prop_key, :custom_field, :old_value, :value)
357 349 values_by_field.each do |field, changes|
358 350 if changes[:added].any?
359 351 detail = multiple_values_detail.new('cf', field.id.to_s, field)
360 352 detail.value = changes[:added]
361 353 strings << show_detail(detail, no_html, options)
362 354 end
363 355 if changes[:deleted].any?
364 356 detail = multiple_values_detail.new('cf', field.id.to_s, field)
365 357 detail.old_value = changes[:deleted]
366 358 strings << show_detail(detail, no_html, options)
367 359 end
368 360 end
369 361 end
370 362 strings
371 363 end
372 364
373 365 # Returns the textual representation of a single journal detail
374 366 def show_detail(detail, no_html=false, options={})
375 367 multiple = false
376 368 show_diff = false
377 369
378 370 case detail.property
379 371 when 'attr'
380 372 field = detail.prop_key.to_s.gsub(/\_id$/, "")
381 373 label = l(("field_" + field).to_sym)
382 374 case detail.prop_key
383 375 when 'due_date', 'start_date'
384 376 value = format_date(detail.value.to_date) if detail.value
385 377 old_value = format_date(detail.old_value.to_date) if detail.old_value
386 378
387 379 when 'project_id', 'status_id', 'tracker_id', 'assigned_to_id',
388 380 'priority_id', 'category_id', 'fixed_version_id'
389 381 value = find_name_by_reflection(field, detail.value)
390 382 old_value = find_name_by_reflection(field, detail.old_value)
391 383
392 384 when 'estimated_hours'
393 385 value = "%0.02f" % detail.value.to_f unless detail.value.blank?
394 386 old_value = "%0.02f" % detail.old_value.to_f unless detail.old_value.blank?
395 387
396 388 when 'parent_id'
397 389 label = l(:field_parent_issue)
398 390 value = "##{detail.value}" unless detail.value.blank?
399 391 old_value = "##{detail.old_value}" unless detail.old_value.blank?
400 392
401 393 when 'is_private'
402 394 value = l(detail.value == "0" ? :general_text_No : :general_text_Yes) unless detail.value.blank?
403 395 old_value = l(detail.old_value == "0" ? :general_text_No : :general_text_Yes) unless detail.old_value.blank?
404 396
405 397 when 'description'
406 398 show_diff = true
407 399 end
408 400 when 'cf'
409 401 custom_field = detail.custom_field
410 402 if custom_field
411 403 label = custom_field.name
412 404 if custom_field.format.class.change_as_diff
413 405 show_diff = true
414 406 else
415 407 multiple = custom_field.multiple?
416 408 value = format_value(detail.value, custom_field) if detail.value
417 409 old_value = format_value(detail.old_value, custom_field) if detail.old_value
418 410 end
419 411 end
420 412 when 'attachment'
421 413 label = l(:label_attachment)
422 414 when 'relation'
423 415 if detail.value && !detail.old_value
424 416 rel_issue = Issue.visible.find_by_id(detail.value)
425 417 value = rel_issue.nil? ? "#{l(:label_issue)} ##{detail.value}" :
426 418 (no_html ? rel_issue : link_to_issue(rel_issue, :only_path => options[:only_path]))
427 419 elsif detail.old_value && !detail.value
428 420 rel_issue = Issue.visible.find_by_id(detail.old_value)
429 421 old_value = rel_issue.nil? ? "#{l(:label_issue)} ##{detail.old_value}" :
430 422 (no_html ? rel_issue : link_to_issue(rel_issue, :only_path => options[:only_path]))
431 423 end
432 424 relation_type = IssueRelation::TYPES[detail.prop_key]
433 425 label = l(relation_type[:name]) if relation_type
434 426 end
435 427 call_hook(:helper_issues_show_detail_after_setting,
436 428 {:detail => detail, :label => label, :value => value, :old_value => old_value })
437 429
438 430 label ||= detail.prop_key
439 431 value ||= detail.value
440 432 old_value ||= detail.old_value
441 433
442 434 unless no_html
443 435 label = content_tag('strong', label)
444 436 old_value = content_tag("i", h(old_value)) if detail.old_value
445 437 if detail.old_value && detail.value.blank? && detail.property != 'relation'
446 438 old_value = content_tag("del", old_value)
447 439 end
448 440 if detail.property == 'attachment' && value.present? &&
449 441 atta = detail.journal.journalized.attachments.detect {|a| a.id == detail.prop_key.to_i}
450 442 # Link to the attachment if it has not been removed
451 443 value = link_to_attachment(atta, :download => true, :only_path => options[:only_path])
452 444 if options[:only_path] != false && atta.is_text?
453 445 value += link_to(
454 446 image_tag('magnifier.png'),
455 447 :controller => 'attachments', :action => 'show',
456 448 :id => atta, :filename => atta.filename
457 449 )
458 450 end
459 451 else
460 452 value = content_tag("i", h(value)) if value
461 453 end
462 454 end
463 455
464 456 if show_diff
465 457 s = l(:text_journal_changed_no_detail, :label => label)
466 458 unless no_html
467 459 diff_link = link_to 'diff',
468 460 {:controller => 'journals', :action => 'diff', :id => detail.journal_id,
469 461 :detail_id => detail.id, :only_path => options[:only_path]},
470 462 :title => l(:label_view_diff)
471 463 s << " (#{ diff_link })"
472 464 end
473 465 s.html_safe
474 466 elsif detail.value.present?
475 467 case detail.property
476 468 when 'attr', 'cf'
477 469 if detail.old_value.present?
478 470 l(:text_journal_changed, :label => label, :old => old_value, :new => value).html_safe
479 471 elsif multiple
480 472 l(:text_journal_added, :label => label, :value => value).html_safe
481 473 else
482 474 l(:text_journal_set_to, :label => label, :value => value).html_safe
483 475 end
484 476 when 'attachment', 'relation'
485 477 l(:text_journal_added, :label => label, :value => value).html_safe
486 478 end
487 479 else
488 480 l(:text_journal_deleted, :label => label, :old => old_value).html_safe
489 481 end
490 482 end
491 483
492 484 # Find the name of an associated record stored in the field attribute
493 485 def find_name_by_reflection(field, id)
494 486 unless id.present?
495 487 return nil
496 488 end
497 489 @detail_value_name_by_reflection ||= Hash.new do |hash, key|
498 490 association = Issue.reflect_on_association(key.first.to_sym)
499 491 name = nil
500 492 if association
501 493 record = association.klass.find_by_id(key.last)
502 494 if record
503 495 name = record.name.force_encoding('UTF-8')
504 496 end
505 497 end
506 498 hash[key] = name
507 499 end
508 500 @detail_value_name_by_reflection[[field, id]]
509 501 end
510 502
511 503 # Renders issue children recursively
512 504 def render_api_issue_children(issue, api)
513 505 return if issue.leaf?
514 506 api.array :children do
515 507 issue.children.each do |child|
516 508 api.issue(:id => child.id) do
517 509 api.tracker(:id => child.tracker_id, :name => child.tracker.name) unless child.tracker.nil?
518 510 api.subject child.subject
519 511 render_api_issue_children(child, api)
520 512 end
521 513 end
522 514 end
523 515 end
524 516 end
General Comments 0
You need to be logged in to leave comments. Login now