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