annotate app/helpers/issues_helper.rb @ 1516:b450a9d58aed redmine-2.4

Update to Redmine SVN revision 13356 on 2.4-stable branch
author Chris Cannam
date Tue, 09 Sep 2014 09:28:31 +0100
parents e248c7af89ec
children dffacf8a6908
rev   line source
Chris@909 1 # encoding: utf-8
Chris@909 2 #
Chris@245 3 # Redmine - project management software
Chris@1494 4 # Copyright (C) 2006-2014 Jean-Philippe Lang
Chris@0 5 #
Chris@0 6 # This program is free software; you can redistribute it and/or
Chris@0 7 # modify it under the terms of the GNU General Public License
Chris@0 8 # as published by the Free Software Foundation; either version 2
Chris@0 9 # of the License, or (at your option) any later version.
Chris@441 10 #
Chris@0 11 # This program is distributed in the hope that it will be useful,
Chris@0 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 14 # GNU General Public License for more details.
Chris@441 15 #
Chris@0 16 # You should have received a copy of the GNU General Public License
Chris@0 17 # along with this program; if not, write to the Free Software
Chris@0 18 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 19
Chris@0 20 module IssuesHelper
Chris@0 21 include ApplicationHelper
Chris@0 22
Chris@0 23 def issue_list(issues, &block)
Chris@0 24 ancestors = []
Chris@0 25 issues.each do |issue|
Chris@0 26 while (ancestors.any? && !issue.is_descendant_of?(ancestors.last))
Chris@0 27 ancestors.pop
Chris@0 28 end
Chris@0 29 yield issue, ancestors.size
Chris@0 30 ancestors << issue unless issue.leaf?
Chris@0 31 end
Chris@0 32 end
chris@22 33
chris@22 34 # Renders a HTML/CSS tooltip
chris@22 35 #
chris@22 36 # To use, a trigger div is needed. This is a div with the class of "tooltip"
chris@22 37 # that contains this method wrapped in a span with the class of "tip"
chris@22 38 #
chris@22 39 # <div class="tooltip"><%= link_to_issue(issue) %>
chris@22 40 # <span class="tip"><%= render_issue_tooltip(issue) %></span>
chris@22 41 # </div>
chris@22 42 #
Chris@0 43 def render_issue_tooltip(issue)
Chris@14 44 @cached_label_status ||= l(:field_status)
Chris@0 45 @cached_label_start_date ||= l(:field_start_date)
Chris@0 46 @cached_label_due_date ||= l(:field_due_date)
Chris@0 47 @cached_label_assigned_to ||= l(:field_assigned_to)
Chris@0 48 @cached_label_priority ||= l(:field_priority)
chris@22 49 @cached_label_project ||= l(:field_project)
chris@22 50
Chris@1115 51 link_to_issue(issue) + "<br /><br />".html_safe +
Chris@1115 52 "<strong>#{@cached_label_project}</strong>: #{link_to_project(issue.project)}<br />".html_safe +
Chris@1115 53 "<strong>#{@cached_label_status}</strong>: #{h(issue.status.name)}<br />".html_safe +
Chris@1115 54 "<strong>#{@cached_label_start_date}</strong>: #{format_date(issue.start_date)}<br />".html_safe +
Chris@1115 55 "<strong>#{@cached_label_due_date}</strong>: #{format_date(issue.due_date)}<br />".html_safe +
Chris@1115 56 "<strong>#{@cached_label_assigned_to}</strong>: #{h(issue.assigned_to)}<br />".html_safe +
Chris@1115 57 "<strong>#{@cached_label_priority}</strong>: #{h(issue.priority.name)}".html_safe
Chris@0 58 end
Chris@441 59
Chris@441 60 def issue_heading(issue)
Chris@441 61 h("#{issue.tracker} ##{issue.id}")
Chris@441 62 end
Chris@441 63
Chris@0 64 def render_issue_subject_with_tree(issue)
Chris@0 65 s = ''
Chris@441 66 ancestors = issue.root? ? [] : issue.ancestors.visible.all
Chris@441 67 ancestors.each do |ancestor|
Chris@1115 68 s << '<div>' + content_tag('p', link_to_issue(ancestor, :project => (issue.project_id != ancestor.project_id)))
Chris@0 69 end
Chris@441 70 s << '<div>'
Chris@441 71 subject = h(issue.subject)
Chris@441 72 if issue.is_private?
Chris@441 73 subject = content_tag('span', l(:field_is_private), :class => 'private') + ' ' + subject
Chris@441 74 end
Chris@441 75 s << content_tag('h3', subject)
Chris@441 76 s << '</div>' * (ancestors.size + 1)
Chris@909 77 s.html_safe
Chris@0 78 end
Chris@441 79
Chris@0 80 def render_descendants_tree(issue)
Chris@0 81 s = '<form><table class="list issues">'
Chris@441 82 issue_list(issue.descendants.visible.sort_by(&:lft)) do |child, level|
Chris@1115 83 css = "issue issue-#{child.id} hascontextmenu"
Chris@1115 84 css << " idnt idnt-#{level}" if level > 0
Chris@0 85 s << content_tag('tr',
Chris@0 86 content_tag('td', check_box_tag("ids[]", child.id, false, :id => nil), :class => 'checkbox') +
Chris@1115 87 content_tag('td', link_to_issue(child, :truncate => 60, :project => (issue.project_id != child.project_id)), :class => 'subject') +
Chris@0 88 content_tag('td', h(child.status)) +
Chris@0 89 content_tag('td', link_to_user(child.assigned_to)) +
Chris@0 90 content_tag('td', progress_bar(child.done_ratio, :width => '80px')),
Chris@1115 91 :class => css)
Chris@0 92 end
Chris@1115 93 s << '</table></form>'
Chris@909 94 s.html_safe
Chris@0 95 end
Chris@441 96
Chris@1464 97 # Returns an array of error messages for bulk edited issues
Chris@1464 98 def bulk_edit_error_messages(issues)
Chris@1464 99 messages = {}
Chris@1464 100 issues.each do |issue|
Chris@1464 101 issue.errors.full_messages.each do |message|
Chris@1464 102 messages[message] ||= []
Chris@1464 103 messages[message] << issue
Chris@1464 104 end
Chris@1464 105 end
Chris@1464 106 messages.map { |message, issues|
Chris@1464 107 "#{message}: " + issues.map {|i| "##{i.id}"}.join(', ')
Chris@1464 108 }
Chris@1464 109 end
Chris@1464 110
Chris@1115 111 # Returns a link for adding a new subtask to the given issue
Chris@1115 112 def link_to_new_subtask(issue)
Chris@1115 113 attrs = {
Chris@1115 114 :tracker_id => issue.tracker,
Chris@1115 115 :parent_issue_id => issue
Chris@1115 116 }
Chris@1115 117 link_to(l(:button_add), new_project_issue_path(issue.project, :issue => attrs))
Chris@1115 118 end
Chris@1115 119
Chris@1115 120 class IssueFieldsRows
Chris@1115 121 include ActionView::Helpers::TagHelper
Chris@1115 122
Chris@1115 123 def initialize
Chris@1115 124 @left = []
Chris@1115 125 @right = []
Chris@1115 126 end
Chris@1115 127
Chris@1115 128 def left(*args)
Chris@1115 129 args.any? ? @left << cells(*args) : @left
Chris@1115 130 end
Chris@1115 131
Chris@1115 132 def right(*args)
Chris@1115 133 args.any? ? @right << cells(*args) : @right
Chris@1115 134 end
Chris@1115 135
Chris@1115 136 def size
Chris@1115 137 @left.size > @right.size ? @left.size : @right.size
Chris@1115 138 end
Chris@1115 139
Chris@1115 140 def to_html
Chris@1115 141 html = ''.html_safe
Chris@1115 142 blank = content_tag('th', '') + content_tag('td', '')
Chris@1115 143 size.times do |i|
Chris@1115 144 left = @left[i] || blank
Chris@1115 145 right = @right[i] || blank
Chris@1115 146 html << content_tag('tr', left + right)
Chris@1115 147 end
Chris@1115 148 html
Chris@1115 149 end
Chris@1115 150
Chris@1115 151 def cells(label, text, options={})
Chris@1115 152 content_tag('th', "#{label}:", options) + content_tag('td', text, options)
Chris@1115 153 end
Chris@1115 154 end
Chris@1115 155
Chris@1115 156 def issue_fields_rows
Chris@1115 157 r = IssueFieldsRows.new
Chris@1115 158 yield r
Chris@1115 159 r.to_html
Chris@1115 160 end
Chris@1115 161
Chris@0 162 def render_custom_fields_rows(issue)
Chris@1464 163 values = issue.visible_custom_field_values
Chris@1464 164 return if values.empty?
Chris@0 165 ordered_values = []
Chris@1464 166 half = (values.size / 2.0).ceil
Chris@0 167 half.times do |i|
Chris@1464 168 ordered_values << values[i]
Chris@1464 169 ordered_values << values[i + half]
Chris@0 170 end
Chris@0 171 s = "<tr>\n"
Chris@0 172 n = 0
Chris@0 173 ordered_values.compact.each do |value|
Chris@0 174 s << "</tr>\n<tr>\n" if n > 0 && (n % 2) == 0
Chris@0 175 s << "\t<th>#{ h(value.custom_field.name) }:</th><td>#{ simple_format_without_paragraph(h(show_value(value))) }</td>\n"
Chris@0 176 n += 1
Chris@0 177 end
Chris@0 178 s << "</tr>\n"
Chris@909 179 s.html_safe
Chris@0 180 end
Chris@441 181
Chris@441 182 def issues_destroy_confirmation_message(issues)
Chris@441 183 issues = [issues] unless issues.is_a?(Array)
Chris@441 184 message = l(:text_issues_destroy_confirmation)
Chris@441 185 descendant_count = issues.inject(0) {|memo, i| memo += (i.right - i.left - 1)/2}
Chris@441 186 if descendant_count > 0
Chris@441 187 issues.each do |issue|
Chris@441 188 next if issue.root?
Chris@441 189 issues.each do |other_issue|
Chris@441 190 descendant_count -= 1 if issue.is_descendant_of?(other_issue)
Chris@441 191 end
Chris@441 192 end
Chris@441 193 if descendant_count > 0
Chris@441 194 message << "\n" + l(:text_issues_destroy_descendants_confirmation, :count => descendant_count)
Chris@441 195 end
Chris@441 196 end
Chris@441 197 message
Chris@441 198 end
Chris@441 199
Chris@0 200 def sidebar_queries
Chris@0 201 unless @sidebar_queries
Chris@1464 202 @sidebar_queries = IssueQuery.visible.
Chris@1464 203 order("#{Query.table_name}.name ASC").
Chris@1115 204 # Project specific queries and global queries
Chris@1464 205 where(@project.nil? ? ["project_id IS NULL"] : ["project_id IS NULL OR project_id = ?", @project.id]).
Chris@1464 206 all
Chris@0 207 end
Chris@0 208 @sidebar_queries
Chris@0 209 end
Chris@0 210
Chris@245 211 def query_links(title, queries)
Chris@1464 212 return '' if queries.empty?
Chris@245 213 # links to #index on issues/show
Chris@245 214 url_params = controller_name == 'issues' ? {:controller => 'issues', :action => 'index', :project_id => @project} : params
Chris@441 215
Chris@1464 216 content_tag('h3', title) + "\n" +
Chris@1464 217 content_tag('ul',
Chris@1464 218 queries.collect {|query|
Chris@1464 219 css = 'query'
Chris@1464 220 css << ' selected' if query == @query
Chris@1464 221 content_tag('li', link_to(query.name, url_params.merge(:query_id => query), :class => css))
Chris@1464 222 }.join("\n").html_safe,
Chris@1464 223 :class => 'queries'
Chris@1464 224 ) + "\n"
Chris@245 225 end
Chris@441 226
Chris@245 227 def render_sidebar_queries
Chris@1115 228 out = ''.html_safe
Chris@1464 229 out << query_links(l(:label_my_queries), sidebar_queries.select(&:is_private?))
Chris@1464 230 out << query_links(l(:label_query_plural), sidebar_queries.reject(&:is_private?))
Chris@245 231 out
Chris@245 232 end
Chris@245 233
Chris@1464 234 def email_issue_attributes(issue, user)
Chris@1464 235 items = []
Chris@1464 236 %w(author status priority assigned_to category fixed_version).each do |attribute|
Chris@1464 237 unless issue.disabled_core_fields.include?(attribute+"_id")
Chris@1464 238 items << "#{l("field_#{attribute}")}: #{issue.send attribute}"
Chris@1464 239 end
Chris@1464 240 end
Chris@1464 241 issue.visible_custom_field_values(user).each do |value|
Chris@1464 242 items << "#{value.custom_field.name}: #{show_value(value)}"
Chris@1464 243 end
Chris@1464 244 items
Chris@1464 245 end
Chris@1464 246
Chris@1464 247 def render_email_issue_attributes(issue, user, html=false)
Chris@1464 248 items = email_issue_attributes(issue, user)
Chris@1464 249 if html
Chris@1464 250 content_tag('ul', items.map{|s| content_tag('li', s)}.join("\n").html_safe)
Chris@1464 251 else
Chris@1464 252 items.map{|s| "* #{s}"}.join("\n")
Chris@1464 253 end
Chris@1464 254 end
Chris@1464 255
Chris@1115 256 # Returns the textual representation of a journal details
Chris@1115 257 # as an array of strings
Chris@1115 258 def details_to_strings(details, no_html=false, options={})
Chris@1115 259 options[:only_path] = (options[:only_path] == false ? false : true)
Chris@1115 260 strings = []
Chris@1115 261 values_by_field = {}
Chris@1115 262 details.each do |detail|
Chris@1115 263 if detail.property == 'cf'
Chris@1464 264 field = detail.custom_field
Chris@1115 265 if field && field.multiple?
Chris@1464 266 values_by_field[field] ||= {:added => [], :deleted => []}
Chris@1115 267 if detail.old_value
Chris@1464 268 values_by_field[field][:deleted] << detail.old_value
Chris@1115 269 end
Chris@1115 270 if detail.value
Chris@1464 271 values_by_field[field][:added] << detail.value
Chris@1115 272 end
Chris@1115 273 next
Chris@1115 274 end
Chris@1115 275 end
Chris@1115 276 strings << show_detail(detail, no_html, options)
Chris@1115 277 end
Chris@1464 278 values_by_field.each do |field, changes|
Chris@1464 279 detail = JournalDetail.new(:property => 'cf', :prop_key => field.id.to_s)
Chris@1464 280 detail.instance_variable_set "@custom_field", field
Chris@1115 281 if changes[:added].any?
Chris@1115 282 detail.value = changes[:added]
Chris@1115 283 strings << show_detail(detail, no_html, options)
Chris@1115 284 elsif changes[:deleted].any?
Chris@1115 285 detail.old_value = changes[:deleted]
Chris@1115 286 strings << show_detail(detail, no_html, options)
Chris@1115 287 end
Chris@1115 288 end
Chris@1115 289 strings
Chris@1115 290 end
Chris@1115 291
Chris@1115 292 # Returns the textual representation of a single journal detail
Chris@1115 293 def show_detail(detail, no_html=false, options={})
Chris@1115 294 multiple = false
Chris@0 295 case detail.property
Chris@0 296 when 'attr'
Chris@0 297 field = detail.prop_key.to_s.gsub(/\_id$/, "")
Chris@0 298 label = l(("field_" + field).to_sym)
Chris@1115 299 case detail.prop_key
Chris@1115 300 when 'due_date', 'start_date'
Chris@0 301 value = format_date(detail.value.to_date) if detail.value
Chris@0 302 old_value = format_date(detail.old_value.to_date) if detail.old_value
Chris@0 303
Chris@1115 304 when 'project_id', 'status_id', 'tracker_id', 'assigned_to_id',
Chris@1115 305 'priority_id', 'category_id', 'fixed_version_id'
Chris@0 306 value = find_name_by_reflection(field, detail.value)
Chris@0 307 old_value = find_name_by_reflection(field, detail.old_value)
Chris@0 308
Chris@1115 309 when 'estimated_hours'
Chris@0 310 value = "%0.02f" % detail.value.to_f unless detail.value.blank?
Chris@0 311 old_value = "%0.02f" % detail.old_value.to_f unless detail.old_value.blank?
Chris@0 312
Chris@1115 313 when 'parent_id'
Chris@0 314 label = l(:field_parent_issue)
Chris@0 315 value = "##{detail.value}" unless detail.value.blank?
Chris@0 316 old_value = "##{detail.old_value}" unless detail.old_value.blank?
Chris@441 317
Chris@1115 318 when 'is_private'
Chris@441 319 value = l(detail.value == "0" ? :general_text_No : :general_text_Yes) unless detail.value.blank?
Chris@441 320 old_value = l(detail.old_value == "0" ? :general_text_No : :general_text_Yes) unless detail.old_value.blank?
Chris@0 321 end
Chris@0 322 when 'cf'
Chris@1464 323 custom_field = detail.custom_field
Chris@0 324 if custom_field
Chris@1115 325 multiple = custom_field.multiple?
Chris@0 326 label = custom_field.name
Chris@0 327 value = format_value(detail.value, custom_field.field_format) if detail.value
Chris@0 328 old_value = format_value(detail.old_value, custom_field.field_format) if detail.old_value
Chris@0 329 end
Chris@0 330 when 'attachment'
Chris@0 331 label = l(:label_attachment)
Chris@1464 332 when 'relation'
Chris@1464 333 if detail.value && !detail.old_value
Chris@1464 334 rel_issue = Issue.visible.find_by_id(detail.value)
Chris@1464 335 value = rel_issue.nil? ? "#{l(:label_issue)} ##{detail.value}" :
Chris@1464 336 (no_html ? rel_issue : link_to_issue(rel_issue, :only_path => options[:only_path]))
Chris@1464 337 elsif detail.old_value && !detail.value
Chris@1464 338 rel_issue = Issue.visible.find_by_id(detail.old_value)
Chris@1464 339 old_value = rel_issue.nil? ? "#{l(:label_issue)} ##{detail.old_value}" :
Chris@1464 340 (no_html ? rel_issue : link_to_issue(rel_issue, :only_path => options[:only_path]))
Chris@1464 341 end
Chris@1464 342 label = l(detail.prop_key.to_sym)
Chris@0 343 end
Chris@1115 344 call_hook(:helper_issues_show_detail_after_setting,
Chris@1115 345 {:detail => detail, :label => label, :value => value, :old_value => old_value })
Chris@0 346
Chris@0 347 label ||= detail.prop_key
Chris@0 348 value ||= detail.value
Chris@0 349 old_value ||= detail.old_value
Chris@441 350
Chris@0 351 unless no_html
Chris@0 352 label = content_tag('strong', label)
Chris@0 353 old_value = content_tag("i", h(old_value)) if detail.old_value
Chris@1464 354 if detail.old_value && detail.value.blank? && detail.property != 'relation'
Chris@1464 355 old_value = content_tag("del", old_value)
Chris@1464 356 end
Chris@1115 357 if detail.property == 'attachment' && !value.blank? && atta = Attachment.find_by_id(detail.prop_key)
Chris@0 358 # Link to the attachment if it has not been removed
Chris@1115 359 value = link_to_attachment(atta, :download => true, :only_path => options[:only_path])
Chris@1115 360 if options[:only_path] != false && atta.is_text?
Chris@1115 361 value += link_to(
Chris@1115 362 image_tag('magnifier.png'),
Chris@1115 363 :controller => 'attachments', :action => 'show',
Chris@1115 364 :id => atta, :filename => atta.filename
Chris@1115 365 )
Chris@1115 366 end
Chris@0 367 else
Chris@0 368 value = content_tag("i", h(value)) if value
Chris@0 369 end
Chris@0 370 end
Chris@441 371
Chris@245 372 if detail.property == 'attr' && detail.prop_key == 'description'
Chris@245 373 s = l(:text_journal_changed_no_detail, :label => label)
Chris@245 374 unless no_html
Chris@441 375 diff_link = link_to 'diff',
Chris@1115 376 {:controller => 'journals', :action => 'diff', :id => detail.journal_id,
Chris@1115 377 :detail_id => detail.id, :only_path => options[:only_path]},
Chris@245 378 :title => l(:label_view_diff)
Chris@245 379 s << " (#{ diff_link })"
Chris@245 380 end
Chris@1115 381 s.html_safe
Chris@1115 382 elsif detail.value.present?
Chris@0 383 case detail.property
Chris@0 384 when 'attr', 'cf'
Chris@1115 385 if detail.old_value.present?
Chris@1115 386 l(:text_journal_changed, :label => label, :old => old_value, :new => value).html_safe
Chris@1115 387 elsif multiple
Chris@1115 388 l(:text_journal_added, :label => label, :value => value).html_safe
Chris@0 389 else
Chris@1115 390 l(:text_journal_set_to, :label => label, :value => value).html_safe
Chris@0 391 end
Chris@1464 392 when 'attachment', 'relation'
Chris@1115 393 l(:text_journal_added, :label => label, :value => value).html_safe
Chris@0 394 end
Chris@0 395 else
Chris@1115 396 l(:text_journal_deleted, :label => label, :old => old_value).html_safe
Chris@0 397 end
Chris@0 398 end
Chris@0 399
Chris@0 400 # Find the name of an associated record stored in the field attribute
Chris@0 401 def find_name_by_reflection(field, id)
Chris@1464 402 unless id.present?
Chris@1464 403 return nil
Chris@1464 404 end
Chris@0 405 association = Issue.reflect_on_association(field.to_sym)
Chris@0 406 if association
Chris@0 407 record = association.class_name.constantize.find_by_id(id)
Chris@1294 408 if record
Chris@1294 409 record.name.force_encoding('UTF-8') if record.name.respond_to?(:force_encoding)
Chris@1294 410 return record.name
Chris@1294 411 end
Chris@0 412 end
Chris@0 413 end
Chris@441 414
Chris@119 415 # Renders issue children recursively
Chris@119 416 def render_api_issue_children(issue, api)
Chris@119 417 return if issue.leaf?
Chris@119 418 api.array :children do
Chris@119 419 issue.children.each do |child|
Chris@119 420 api.issue(:id => child.id) do
Chris@119 421 api.tracker(:id => child.tracker_id, :name => child.tracker.name) unless child.tracker.nil?
Chris@119 422 api.subject child.subject
Chris@119 423 render_api_issue_children(child, api)
Chris@119 424 end
Chris@119 425 end
Chris@119 426 end
Chris@119 427 end
Chris@0 428 end