annotate app/helpers/issues_helper.rb @ 1477:f2ad2199b49a bibplugin_integration

Close obsolete branch bibplugin_integration
author Chris Cannam
date Fri, 30 Nov 2012 14:41:31 +0000
parents cbb26bc654de
children 433d4f72a19b
rev   line source
Chris@909 1 # encoding: utf-8
Chris@909 2 #
Chris@245 3 # Redmine - project management software
Chris@245 4 # Copyright (C) 2006-2011 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@909 51 (link_to_issue(issue) + "<br /><br />" +
chris@22 52 "<strong>#{@cached_label_project}</strong>: #{link_to_project(issue.project)}<br />" +
Chris@909 53 "<strong>#{@cached_label_status}</strong>: #{h(issue.status.name)}<br />" +
Chris@0 54 "<strong>#{@cached_label_start_date}</strong>: #{format_date(issue.start_date)}<br />" +
Chris@0 55 "<strong>#{@cached_label_due_date}</strong>: #{format_date(issue.due_date)}<br />" +
Chris@909 56 "<strong>#{@cached_label_assigned_to}</strong>: #{h(issue.assigned_to)}<br />" +
Chris@909 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@0 68 s << '<div>' + content_tag('p', link_to_issue(ancestor))
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@0 83 s << content_tag('tr',
Chris@0 84 content_tag('td', check_box_tag("ids[]", child.id, false, :id => nil), :class => 'checkbox') +
Chris@0 85 content_tag('td', link_to_issue(child, :truncate => 60), :class => 'subject') +
Chris@0 86 content_tag('td', h(child.status)) +
Chris@0 87 content_tag('td', link_to_user(child.assigned_to)) +
Chris@0 88 content_tag('td', progress_bar(child.done_ratio, :width => '80px')),
Chris@0 89 :class => "issue issue-#{child.id} hascontextmenu #{level > 0 ? "idnt idnt-#{level}" : nil}")
Chris@0 90 end
Chris@0 91 s << '</form></table>'
Chris@909 92 s.html_safe
Chris@0 93 end
Chris@441 94
Chris@0 95 def render_custom_fields_rows(issue)
Chris@0 96 return if issue.custom_field_values.empty?
Chris@0 97 ordered_values = []
Chris@0 98 half = (issue.custom_field_values.size / 2.0).ceil
Chris@0 99 half.times do |i|
Chris@0 100 ordered_values << issue.custom_field_values[i]
Chris@0 101 ordered_values << issue.custom_field_values[i + half]
Chris@0 102 end
Chris@0 103 s = "<tr>\n"
Chris@0 104 n = 0
Chris@0 105 ordered_values.compact.each do |value|
Chris@0 106 s << "</tr>\n<tr>\n" if n > 0 && (n % 2) == 0
Chris@0 107 s << "\t<th>#{ h(value.custom_field.name) }:</th><td>#{ simple_format_without_paragraph(h(show_value(value))) }</td>\n"
Chris@0 108 n += 1
Chris@0 109 end
Chris@0 110 s << "</tr>\n"
Chris@909 111 s.html_safe
Chris@0 112 end
Chris@441 113
Chris@441 114 def issues_destroy_confirmation_message(issues)
Chris@441 115 issues = [issues] unless issues.is_a?(Array)
Chris@441 116 message = l(:text_issues_destroy_confirmation)
Chris@441 117 descendant_count = issues.inject(0) {|memo, i| memo += (i.right - i.left - 1)/2}
Chris@441 118 if descendant_count > 0
Chris@441 119 issues.each do |issue|
Chris@441 120 next if issue.root?
Chris@441 121 issues.each do |other_issue|
Chris@441 122 descendant_count -= 1 if issue.is_descendant_of?(other_issue)
Chris@441 123 end
Chris@441 124 end
Chris@441 125 if descendant_count > 0
Chris@441 126 message << "\n" + l(:text_issues_destroy_descendants_confirmation, :count => descendant_count)
Chris@441 127 end
Chris@441 128 end
Chris@441 129 message
Chris@441 130 end
Chris@441 131
Chris@0 132 def sidebar_queries
Chris@0 133 unless @sidebar_queries
Chris@0 134 # User can see public queries and his own queries
Chris@0 135 visible = ARCondition.new(["is_public = ? OR user_id = ?", true, (User.current.logged? ? User.current.id : 0)])
Chris@0 136 # Project specific queries and global queries
Chris@0 137 visible << (@project.nil? ? ["project_id IS NULL"] : ["project_id IS NULL OR project_id = ?", @project.id])
Chris@441 138 @sidebar_queries = Query.find(:all,
Chris@245 139 :select => 'id, name, is_public',
Chris@0 140 :order => "name ASC",
Chris@0 141 :conditions => visible.conditions)
Chris@0 142 end
Chris@0 143 @sidebar_queries
Chris@0 144 end
Chris@0 145
Chris@245 146 def query_links(title, queries)
Chris@245 147 # links to #index on issues/show
Chris@245 148 url_params = controller_name == 'issues' ? {:controller => 'issues', :action => 'index', :project_id => @project} : params
Chris@441 149
Chris@909 150 content_tag('h3', h(title)) +
Chris@245 151 queries.collect {|query|
Chris@245 152 link_to(h(query.name), url_params.merge(:query_id => query))
Chris@245 153 }.join('<br />')
Chris@245 154 end
Chris@441 155
Chris@245 156 def render_sidebar_queries
Chris@245 157 out = ''
Chris@245 158 queries = sidebar_queries.select {|q| !q.is_public?}
Chris@245 159 out << query_links(l(:label_my_queries), queries) if queries.any?
Chris@245 160 queries = sidebar_queries.select {|q| q.is_public?}
Chris@245 161 out << query_links(l(:label_query_plural), queries) if queries.any?
Chris@245 162 out
Chris@245 163 end
Chris@245 164
Chris@0 165 def show_detail(detail, no_html=false)
Chris@0 166 case detail.property
Chris@0 167 when 'attr'
Chris@0 168 field = detail.prop_key.to_s.gsub(/\_id$/, "")
Chris@0 169 label = l(("field_" + field).to_sym)
Chris@0 170 case
Chris@0 171 when ['due_date', 'start_date'].include?(detail.prop_key)
Chris@0 172 value = format_date(detail.value.to_date) if detail.value
Chris@0 173 old_value = format_date(detail.old_value.to_date) if detail.old_value
Chris@0 174
Chris@0 175 when ['project_id', 'status_id', 'tracker_id', 'assigned_to_id', 'priority_id', 'category_id', 'fixed_version_id'].include?(detail.prop_key)
Chris@0 176 value = find_name_by_reflection(field, detail.value)
Chris@0 177 old_value = find_name_by_reflection(field, detail.old_value)
Chris@0 178
Chris@0 179 when detail.prop_key == 'estimated_hours'
Chris@0 180 value = "%0.02f" % detail.value.to_f unless detail.value.blank?
Chris@0 181 old_value = "%0.02f" % detail.old_value.to_f unless detail.old_value.blank?
Chris@0 182
Chris@0 183 when detail.prop_key == 'parent_id'
Chris@0 184 label = l(:field_parent_issue)
Chris@0 185 value = "##{detail.value}" unless detail.value.blank?
Chris@0 186 old_value = "##{detail.old_value}" unless detail.old_value.blank?
Chris@441 187
Chris@441 188 when detail.prop_key == 'is_private'
Chris@441 189 value = l(detail.value == "0" ? :general_text_No : :general_text_Yes) unless detail.value.blank?
Chris@441 190 old_value = l(detail.old_value == "0" ? :general_text_No : :general_text_Yes) unless detail.old_value.blank?
Chris@0 191 end
Chris@0 192 when 'cf'
Chris@0 193 custom_field = CustomField.find_by_id(detail.prop_key)
Chris@0 194 if custom_field
Chris@0 195 label = custom_field.name
Chris@0 196 value = format_value(detail.value, custom_field.field_format) if detail.value
Chris@0 197 old_value = format_value(detail.old_value, custom_field.field_format) if detail.old_value
Chris@0 198 end
Chris@0 199 when 'attachment'
Chris@0 200 label = l(:label_attachment)
Chris@0 201 end
Chris@0 202 call_hook(:helper_issues_show_detail_after_setting, {:detail => detail, :label => label, :value => value, :old_value => old_value })
Chris@0 203
Chris@0 204 label ||= detail.prop_key
Chris@0 205 value ||= detail.value
Chris@0 206 old_value ||= detail.old_value
Chris@441 207
Chris@0 208 unless no_html
Chris@0 209 label = content_tag('strong', label)
Chris@0 210 old_value = content_tag("i", h(old_value)) if detail.old_value
Chris@909 211 old_value = content_tag("strike", old_value) if detail.old_value and detail.value.blank?
Chris@0 212 if detail.property == 'attachment' && !value.blank? && a = Attachment.find_by_id(detail.prop_key)
Chris@0 213 # Link to the attachment if it has not been removed
Chris@0 214 value = link_to_attachment(a)
Chris@0 215 else
Chris@0 216 value = content_tag("i", h(value)) if value
Chris@0 217 end
Chris@0 218 end
Chris@441 219
Chris@245 220 if detail.property == 'attr' && detail.prop_key == 'description'
Chris@245 221 s = l(:text_journal_changed_no_detail, :label => label)
Chris@245 222 unless no_html
Chris@441 223 diff_link = link_to 'diff',
Chris@245 224 {:controller => 'journals', :action => 'diff', :id => detail.journal_id, :detail_id => detail.id},
Chris@245 225 :title => l(:label_view_diff)
Chris@245 226 s << " (#{ diff_link })"
Chris@245 227 end
Chris@245 228 s
Chris@245 229 elsif !detail.value.blank?
Chris@0 230 case detail.property
Chris@0 231 when 'attr', 'cf'
Chris@0 232 if !detail.old_value.blank?
Chris@0 233 l(:text_journal_changed, :label => label, :old => old_value, :new => value)
Chris@0 234 else
Chris@0 235 l(:text_journal_set_to, :label => label, :value => value)
Chris@0 236 end
Chris@0 237 when 'attachment'
Chris@0 238 l(:text_journal_added, :label => label, :value => value)
Chris@0 239 end
Chris@0 240 else
Chris@0 241 l(:text_journal_deleted, :label => label, :old => old_value)
Chris@0 242 end
Chris@0 243 end
Chris@0 244
Chris@0 245 # Find the name of an associated record stored in the field attribute
Chris@0 246 def find_name_by_reflection(field, id)
Chris@0 247 association = Issue.reflect_on_association(field.to_sym)
Chris@0 248 if association
Chris@0 249 record = association.class_name.constantize.find_by_id(id)
Chris@0 250 return record.name if record
Chris@0 251 end
Chris@0 252 end
Chris@441 253
Chris@119 254 # Renders issue children recursively
Chris@119 255 def render_api_issue_children(issue, api)
Chris@119 256 return if issue.leaf?
Chris@119 257 api.array :children do
Chris@119 258 issue.children.each do |child|
Chris@119 259 api.issue(:id => child.id) do
Chris@119 260 api.tracker(:id => child.tracker_id, :name => child.tracker.name) unless child.tracker.nil?
Chris@119 261 api.subject child.subject
Chris@119 262 render_api_issue_children(child, api)
Chris@119 263 end
Chris@119 264 end
Chris@119 265 end
Chris@119 266 end
Chris@441 267
Chris@909 268 def issues_to_csv(issues, project, query, options={})
Chris@0 269 decimal_separator = l(:general_csv_decimal_separator)
Chris@909 270 encoding = l(:general_csv_encoding)
Chris@909 271 columns = (options[:columns] == 'all' ? query.available_columns : query.columns)
Chris@909 272
Chris@0 273 export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv|
Chris@0 274 # csv header fields
Chris@909 275 csv << [ "#" ] + columns.collect {|c| Redmine::CodesetUtil.from_utf8(c.caption.to_s, encoding) } +
Chris@909 276 (options[:description] ? [Redmine::CodesetUtil.from_utf8(l(:field_description), encoding)] : [])
Chris@909 277
Chris@0 278 # csv lines
Chris@0 279 issues.each do |issue|
Chris@909 280 col_values = columns.collect do |column|
Chris@909 281 s = if column.is_a?(QueryCustomFieldColumn)
Chris@909 282 cv = issue.custom_values.detect {|v| v.custom_field_id == column.custom_field.id}
Chris@909 283 show_value(cv)
Chris@909 284 else
Chris@909 285 value = issue.send(column.name)
Chris@909 286 if value.is_a?(Date)
Chris@909 287 format_date(value)
Chris@909 288 elsif value.is_a?(Time)
Chris@909 289 format_time(value)
Chris@909 290 elsif value.is_a?(Float)
Chris@909 291 value.to_s.gsub('.', decimal_separator)
Chris@909 292 else
Chris@909 293 value
Chris@909 294 end
Chris@909 295 end
Chris@909 296 s.to_s
Chris@909 297 end
Chris@909 298 csv << [ issue.id.to_s ] + col_values.collect {|c| Redmine::CodesetUtil.from_utf8(c.to_s, encoding) } +
Chris@909 299 (options[:description] ? [Redmine::CodesetUtil.from_utf8(issue.description, encoding)] : [])
Chris@0 300 end
Chris@0 301 end
Chris@0 302 export
Chris@0 303 end
Chris@0 304 end