annotate .svn/pristine/b4/b4fc2c9a97b87455aef29af5d4be937c21ee3844.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

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