comparison .svn/pristine/e4/e4f048cf029870d9a4f461969a46479b6a5e9977.svn-base @ 1494:e248c7af89ec redmine-2.4

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