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