annotate app/helpers/issues_helper.rb @ 1295:622f24f53b42 redmine-2.3

Update to Redmine SVN revision 11972 on 2.3-stable branch
author Chris Cannam
date Fri, 14 Jun 2013 09:02:21 +0100
parents 3e4c3460b6ca
children
rev   line source
Chris@909 1 # encoding: utf-8
Chris@909 2 #
Chris@245 3 # Redmine - project management software
Chris@1295 4 # Copyright (C) 2006-2013 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@1115 97 # Returns a link for adding a new subtask to the given issue
Chris@1115 98 def link_to_new_subtask(issue)
Chris@1115 99 attrs = {
Chris@1115 100 :tracker_id => issue.tracker,
Chris@1115 101 :parent_issue_id => issue
Chris@1115 102 }
Chris@1115 103 link_to(l(:button_add), new_project_issue_path(issue.project, :issue => attrs))
Chris@1115 104 end
Chris@1115 105
Chris@1115 106 class IssueFieldsRows
Chris@1115 107 include ActionView::Helpers::TagHelper
Chris@1115 108
Chris@1115 109 def initialize
Chris@1115 110 @left = []
Chris@1115 111 @right = []
Chris@1115 112 end
Chris@1115 113
Chris@1115 114 def left(*args)
Chris@1115 115 args.any? ? @left << cells(*args) : @left
Chris@1115 116 end
Chris@1115 117
Chris@1115 118 def right(*args)
Chris@1115 119 args.any? ? @right << cells(*args) : @right
Chris@1115 120 end
Chris@1115 121
Chris@1115 122 def size
Chris@1115 123 @left.size > @right.size ? @left.size : @right.size
Chris@1115 124 end
Chris@1115 125
Chris@1115 126 def to_html
Chris@1115 127 html = ''.html_safe
Chris@1115 128 blank = content_tag('th', '') + content_tag('td', '')
Chris@1115 129 size.times do |i|
Chris@1115 130 left = @left[i] || blank
Chris@1115 131 right = @right[i] || blank
Chris@1115 132 html << content_tag('tr', left + right)
Chris@1115 133 end
Chris@1115 134 html
Chris@1115 135 end
Chris@1115 136
Chris@1115 137 def cells(label, text, options={})
Chris@1115 138 content_tag('th', "#{label}:", options) + content_tag('td', text, options)
Chris@1115 139 end
Chris@1115 140 end
Chris@1115 141
Chris@1115 142 def issue_fields_rows
Chris@1115 143 r = IssueFieldsRows.new
Chris@1115 144 yield r
Chris@1115 145 r.to_html
Chris@1115 146 end
Chris@1115 147
Chris@0 148 def render_custom_fields_rows(issue)
Chris@0 149 return if issue.custom_field_values.empty?
Chris@0 150 ordered_values = []
Chris@0 151 half = (issue.custom_field_values.size / 2.0).ceil
Chris@0 152 half.times do |i|
Chris@0 153 ordered_values << issue.custom_field_values[i]
Chris@0 154 ordered_values << issue.custom_field_values[i + half]
Chris@0 155 end
Chris@0 156 s = "<tr>\n"
Chris@0 157 n = 0
Chris@0 158 ordered_values.compact.each do |value|
Chris@0 159 s << "</tr>\n<tr>\n" if n > 0 && (n % 2) == 0
Chris@0 160 s << "\t<th>#{ h(value.custom_field.name) }:</th><td>#{ simple_format_without_paragraph(h(show_value(value))) }</td>\n"
Chris@0 161 n += 1
Chris@0 162 end
Chris@0 163 s << "</tr>\n"
Chris@909 164 s.html_safe
Chris@0 165 end
Chris@441 166
Chris@441 167 def issues_destroy_confirmation_message(issues)
Chris@441 168 issues = [issues] unless issues.is_a?(Array)
Chris@441 169 message = l(:text_issues_destroy_confirmation)
Chris@441 170 descendant_count = issues.inject(0) {|memo, i| memo += (i.right - i.left - 1)/2}
Chris@441 171 if descendant_count > 0
Chris@441 172 issues.each do |issue|
Chris@441 173 next if issue.root?
Chris@441 174 issues.each do |other_issue|
Chris@441 175 descendant_count -= 1 if issue.is_descendant_of?(other_issue)
Chris@441 176 end
Chris@441 177 end
Chris@441 178 if descendant_count > 0
Chris@441 179 message << "\n" + l(:text_issues_destroy_descendants_confirmation, :count => descendant_count)
Chris@441 180 end
Chris@441 181 end
Chris@441 182 message
Chris@441 183 end
Chris@441 184
Chris@0 185 def sidebar_queries
Chris@0 186 unless @sidebar_queries
Chris@1295 187 @sidebar_queries = IssueQuery.visible.all(
Chris@1115 188 :order => "#{Query.table_name}.name ASC",
Chris@1115 189 # Project specific queries and global queries
Chris@1115 190 :conditions => (@project.nil? ? ["project_id IS NULL"] : ["project_id IS NULL OR project_id = ?", @project.id])
Chris@1115 191 )
Chris@0 192 end
Chris@0 193 @sidebar_queries
Chris@0 194 end
Chris@0 195
Chris@245 196 def query_links(title, queries)
Chris@245 197 # links to #index on issues/show
Chris@245 198 url_params = controller_name == 'issues' ? {:controller => 'issues', :action => 'index', :project_id => @project} : params
Chris@441 199
Chris@909 200 content_tag('h3', h(title)) +
Chris@245 201 queries.collect {|query|
Chris@1115 202 css = 'query'
Chris@1115 203 css << ' selected' if query == @query
Chris@1115 204 link_to(h(query.name), url_params.merge(:query_id => query), :class => css)
Chris@1115 205 }.join('<br />').html_safe
Chris@245 206 end
Chris@441 207
Chris@245 208 def render_sidebar_queries
Chris@1115 209 out = ''.html_safe
Chris@245 210 queries = sidebar_queries.select {|q| !q.is_public?}
Chris@245 211 out << query_links(l(:label_my_queries), queries) if queries.any?
Chris@245 212 queries = sidebar_queries.select {|q| q.is_public?}
Chris@245 213 out << query_links(l(:label_query_plural), queries) if queries.any?
Chris@245 214 out
Chris@245 215 end
Chris@245 216
Chris@1115 217 # Returns the textual representation of a journal details
Chris@1115 218 # as an array of strings
Chris@1115 219 def details_to_strings(details, no_html=false, options={})
Chris@1115 220 options[:only_path] = (options[:only_path] == false ? false : true)
Chris@1115 221 strings = []
Chris@1115 222 values_by_field = {}
Chris@1115 223 details.each do |detail|
Chris@1115 224 if detail.property == 'cf'
Chris@1115 225 field_id = detail.prop_key
Chris@1115 226 field = CustomField.find_by_id(field_id)
Chris@1115 227 if field && field.multiple?
Chris@1115 228 values_by_field[field_id] ||= {:added => [], :deleted => []}
Chris@1115 229 if detail.old_value
Chris@1115 230 values_by_field[field_id][:deleted] << detail.old_value
Chris@1115 231 end
Chris@1115 232 if detail.value
Chris@1115 233 values_by_field[field_id][:added] << detail.value
Chris@1115 234 end
Chris@1115 235 next
Chris@1115 236 end
Chris@1115 237 end
Chris@1115 238 strings << show_detail(detail, no_html, options)
Chris@1115 239 end
Chris@1115 240 values_by_field.each do |field_id, changes|
Chris@1115 241 detail = JournalDetail.new(:property => 'cf', :prop_key => field_id)
Chris@1115 242 if changes[:added].any?
Chris@1115 243 detail.value = changes[:added]
Chris@1115 244 strings << show_detail(detail, no_html, options)
Chris@1115 245 elsif changes[:deleted].any?
Chris@1115 246 detail.old_value = changes[:deleted]
Chris@1115 247 strings << show_detail(detail, no_html, options)
Chris@1115 248 end
Chris@1115 249 end
Chris@1115 250 strings
Chris@1115 251 end
Chris@1115 252
Chris@1115 253 # Returns the textual representation of a single journal detail
Chris@1115 254 def show_detail(detail, no_html=false, options={})
Chris@1115 255 multiple = false
Chris@0 256 case detail.property
Chris@0 257 when 'attr'
Chris@0 258 field = detail.prop_key.to_s.gsub(/\_id$/, "")
Chris@0 259 label = l(("field_" + field).to_sym)
Chris@1115 260 case detail.prop_key
Chris@1115 261 when 'due_date', 'start_date'
Chris@0 262 value = format_date(detail.value.to_date) if detail.value
Chris@0 263 old_value = format_date(detail.old_value.to_date) if detail.old_value
Chris@0 264
Chris@1115 265 when 'project_id', 'status_id', 'tracker_id', 'assigned_to_id',
Chris@1115 266 'priority_id', 'category_id', 'fixed_version_id'
Chris@0 267 value = find_name_by_reflection(field, detail.value)
Chris@0 268 old_value = find_name_by_reflection(field, detail.old_value)
Chris@0 269
Chris@1115 270 when 'estimated_hours'
Chris@0 271 value = "%0.02f" % detail.value.to_f unless detail.value.blank?
Chris@0 272 old_value = "%0.02f" % detail.old_value.to_f unless detail.old_value.blank?
Chris@0 273
Chris@1115 274 when 'parent_id'
Chris@0 275 label = l(:field_parent_issue)
Chris@0 276 value = "##{detail.value}" unless detail.value.blank?
Chris@0 277 old_value = "##{detail.old_value}" unless detail.old_value.blank?
Chris@441 278
Chris@1115 279 when 'is_private'
Chris@441 280 value = l(detail.value == "0" ? :general_text_No : :general_text_Yes) unless detail.value.blank?
Chris@441 281 old_value = l(detail.old_value == "0" ? :general_text_No : :general_text_Yes) unless detail.old_value.blank?
Chris@0 282 end
Chris@0 283 when 'cf'
Chris@0 284 custom_field = CustomField.find_by_id(detail.prop_key)
Chris@0 285 if custom_field
Chris@1115 286 multiple = custom_field.multiple?
Chris@0 287 label = custom_field.name
Chris@0 288 value = format_value(detail.value, custom_field.field_format) if detail.value
Chris@0 289 old_value = format_value(detail.old_value, custom_field.field_format) if detail.old_value
Chris@0 290 end
Chris@0 291 when 'attachment'
Chris@0 292 label = l(:label_attachment)
Chris@0 293 end
Chris@1115 294 call_hook(:helper_issues_show_detail_after_setting,
Chris@1115 295 {:detail => detail, :label => label, :value => value, :old_value => old_value })
Chris@0 296
Chris@0 297 label ||= detail.prop_key
Chris@0 298 value ||= detail.value
Chris@0 299 old_value ||= detail.old_value
Chris@441 300
Chris@0 301 unless no_html
Chris@0 302 label = content_tag('strong', label)
Chris@0 303 old_value = content_tag("i", h(old_value)) if detail.old_value
Chris@1115 304 old_value = content_tag("del", old_value) if detail.old_value and detail.value.blank?
Chris@1115 305 if detail.property == 'attachment' && !value.blank? && atta = Attachment.find_by_id(detail.prop_key)
Chris@0 306 # Link to the attachment if it has not been removed
Chris@1115 307 value = link_to_attachment(atta, :download => true, :only_path => options[:only_path])
Chris@1115 308 if options[:only_path] != false && atta.is_text?
Chris@1115 309 value += link_to(
Chris@1115 310 image_tag('magnifier.png'),
Chris@1115 311 :controller => 'attachments', :action => 'show',
Chris@1115 312 :id => atta, :filename => atta.filename
Chris@1115 313 )
Chris@1115 314 end
Chris@0 315 else
Chris@0 316 value = content_tag("i", h(value)) if value
Chris@0 317 end
Chris@0 318 end
Chris@441 319
Chris@245 320 if detail.property == 'attr' && detail.prop_key == 'description'
Chris@245 321 s = l(:text_journal_changed_no_detail, :label => label)
Chris@245 322 unless no_html
Chris@441 323 diff_link = link_to 'diff',
Chris@1115 324 {:controller => 'journals', :action => 'diff', :id => detail.journal_id,
Chris@1115 325 :detail_id => detail.id, :only_path => options[:only_path]},
Chris@245 326 :title => l(:label_view_diff)
Chris@245 327 s << " (#{ diff_link })"
Chris@245 328 end
Chris@1115 329 s.html_safe
Chris@1115 330 elsif detail.value.present?
Chris@0 331 case detail.property
Chris@0 332 when 'attr', 'cf'
Chris@1115 333 if detail.old_value.present?
Chris@1115 334 l(:text_journal_changed, :label => label, :old => old_value, :new => value).html_safe
Chris@1115 335 elsif multiple
Chris@1115 336 l(:text_journal_added, :label => label, :value => value).html_safe
Chris@0 337 else
Chris@1115 338 l(:text_journal_set_to, :label => label, :value => value).html_safe
Chris@0 339 end
Chris@0 340 when 'attachment'
Chris@1115 341 l(:text_journal_added, :label => label, :value => value).html_safe
Chris@0 342 end
Chris@0 343 else
Chris@1115 344 l(:text_journal_deleted, :label => label, :old => old_value).html_safe
Chris@0 345 end
Chris@0 346 end
Chris@0 347
Chris@0 348 # Find the name of an associated record stored in the field attribute
Chris@0 349 def find_name_by_reflection(field, id)
Chris@1295 350 unless id.present?
Chris@1295 351 return nil
Chris@1295 352 end
Chris@0 353 association = Issue.reflect_on_association(field.to_sym)
Chris@0 354 if association
Chris@0 355 record = association.class_name.constantize.find_by_id(id)
Chris@1294 356 if record
Chris@1294 357 record.name.force_encoding('UTF-8') if record.name.respond_to?(:force_encoding)
Chris@1294 358 return record.name
Chris@1294 359 end
Chris@0 360 end
Chris@0 361 end
Chris@441 362
Chris@119 363 # Renders issue children recursively
Chris@119 364 def render_api_issue_children(issue, api)
Chris@119 365 return if issue.leaf?
Chris@119 366 api.array :children do
Chris@119 367 issue.children.each do |child|
Chris@119 368 api.issue(:id => child.id) do
Chris@119 369 api.tracker(:id => child.tracker_id, :name => child.tracker.name) unless child.tracker.nil?
Chris@119 370 api.subject child.subject
Chris@119 371 render_api_issue_children(child, api)
Chris@119 372 end
Chris@119 373 end
Chris@119 374 end
Chris@119 375 end
Chris@0 376 end