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