annotate .svn/pristine/4b/4bc7e4b0cb93062cfdf78f8e648d19270494056a.svn-base @ 1327:287f201c2802 redmine-2.2-integration

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