##// END OF EJS Templates
Code cleanup....
Jean-Philippe Lang -
r8604:cd4a19241ad7
parent child
Show More
@@ -1,345 +1,345
1 1 # encoding: utf-8
2 2 #
3 3 # Redmine - project management software
4 4 # Copyright (C) 2006-2011 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))
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 s << content_tag('tr',
84 84 content_tag('td', check_box_tag("ids[]", child.id, false, :id => nil), :class => 'checkbox') +
85 85 content_tag('td', link_to_issue(child, :truncate => 60), :class => 'subject') +
86 86 content_tag('td', h(child.status)) +
87 87 content_tag('td', link_to_user(child.assigned_to)) +
88 88 content_tag('td', progress_bar(child.done_ratio, :width => '80px')),
89 89 :class => "issue issue-#{child.id} hascontextmenu #{level > 0 ? "idnt idnt-#{level}" : nil}")
90 90 end
91 91 s << '</table></form>'
92 92 s.html_safe
93 93 end
94 94
95 95 def render_custom_fields_rows(issue)
96 96 return if issue.custom_field_values.empty?
97 97 ordered_values = []
98 98 half = (issue.custom_field_values.size / 2.0).ceil
99 99 half.times do |i|
100 100 ordered_values << issue.custom_field_values[i]
101 101 ordered_values << issue.custom_field_values[i + half]
102 102 end
103 103 s = "<tr>\n"
104 104 n = 0
105 105 ordered_values.compact.each do |value|
106 106 s << "</tr>\n<tr>\n" if n > 0 && (n % 2) == 0
107 107 s << "\t<th>#{ h(value.custom_field.name) }:</th><td>#{ simple_format_without_paragraph(h(show_value(value))) }</td>\n"
108 108 n += 1
109 109 end
110 110 s << "</tr>\n"
111 111 s.html_safe
112 112 end
113 113
114 114 def issues_destroy_confirmation_message(issues)
115 115 issues = [issues] unless issues.is_a?(Array)
116 116 message = l(:text_issues_destroy_confirmation)
117 117 descendant_count = issues.inject(0) {|memo, i| memo += (i.right - i.left - 1)/2}
118 118 if descendant_count > 0
119 119 issues.each do |issue|
120 120 next if issue.root?
121 121 issues.each do |other_issue|
122 122 descendant_count -= 1 if issue.is_descendant_of?(other_issue)
123 123 end
124 124 end
125 125 if descendant_count > 0
126 126 message << "\n" + l(:text_issues_destroy_descendants_confirmation, :count => descendant_count)
127 127 end
128 128 end
129 129 message
130 130 end
131 131
132 132 def sidebar_queries
133 133 unless @sidebar_queries
134 134 @sidebar_queries = Query.visible.all(
135 135 :order => "#{Query.table_name}.name ASC",
136 136 # Project specific queries and global queries
137 137 :conditions => (@project.nil? ? ["project_id IS NULL"] : ["project_id IS NULL OR project_id = ?", @project.id])
138 138 )
139 139 end
140 140 @sidebar_queries
141 141 end
142 142
143 143 def query_links(title, queries)
144 144 # links to #index on issues/show
145 145 url_params = controller_name == 'issues' ? {:controller => 'issues', :action => 'index', :project_id => @project} : params
146 146
147 147 content_tag('h3', h(title)) +
148 148 queries.collect {|query|
149 149 css = 'query'
150 150 css << ' selected' if query == @query
151 151 link_to(h(query.name), url_params.merge(:query_id => query), :class => css)
152 152 }.join('<br />').html_safe
153 153 end
154 154
155 155 def render_sidebar_queries
156 156 out = ''.html_safe
157 157 queries = sidebar_queries.select {|q| !q.is_public?}
158 158 out << query_links(l(:label_my_queries), queries) if queries.any?
159 159 queries = sidebar_queries.select {|q| q.is_public?}
160 160 out << query_links(l(:label_query_plural), queries) if queries.any?
161 161 out
162 162 end
163 163
164 164 # Returns the textual representation of a journal details
165 165 # as an array of strings
166 166 def details_to_strings(details, no_html=false)
167 167 strings = []
168 168 values_by_field = {}
169 169 details.each do |detail|
170 170 if detail.property == 'cf'
171 171 field_id = detail.prop_key
172 172 field = CustomField.find_by_id(field_id)
173 173 if field && field.multiple?
174 174 values_by_field[field_id] ||= {:added => [], :deleted => []}
175 175 if detail.old_value
176 176 values_by_field[field_id][:deleted] << detail.old_value
177 177 end
178 178 if detail.value
179 179 values_by_field[field_id][:added] << detail.value
180 180 end
181 181 next
182 182 end
183 183 end
184 184 strings << show_detail(detail, no_html)
185 185 end
186 186 values_by_field.each do |field_id, changes|
187 187 detail = JournalDetail.new(:property => 'cf', :prop_key => field_id)
188 188 if changes[:added].any?
189 189 detail.value = changes[:added]
190 190 strings << show_detail(detail, no_html)
191 191 elsif changes[:deleted].any?
192 192 detail.old_value = changes[:deleted]
193 193 strings << show_detail(detail, no_html)
194 194 end
195 195 end
196 196 strings
197 197 end
198 198
199 199 # Returns the textual representation of a single journal detail
200 200 def show_detail(detail, no_html=false)
201 201 multiple = false
202 202 case detail.property
203 203 when 'attr'
204 204 field = detail.prop_key.to_s.gsub(/\_id$/, "")
205 205 label = l(("field_" + field).to_sym)
206 case
207 when ['due_date', 'start_date'].include?(detail.prop_key)
206 case detail.prop_key
207 when 'due_date', 'start_date'
208 208 value = format_date(detail.value.to_date) if detail.value
209 209 old_value = format_date(detail.old_value.to_date) if detail.old_value
210 210
211 when ['project_id', 'status_id', 'tracker_id', 'assigned_to_id',
212 'priority_id', 'category_id', 'fixed_version_id'].include?(detail.prop_key)
211 when 'project_id', 'status_id', 'tracker_id', 'assigned_to_id',
212 'priority_id', 'category_id', 'fixed_version_id'
213 213 value = find_name_by_reflection(field, detail.value)
214 214 old_value = find_name_by_reflection(field, detail.old_value)
215 215
216 when detail.prop_key == 'estimated_hours'
216 when 'estimated_hours'
217 217 value = "%0.02f" % detail.value.to_f unless detail.value.blank?
218 218 old_value = "%0.02f" % detail.old_value.to_f unless detail.old_value.blank?
219 219
220 when detail.prop_key == 'parent_id'
220 when 'parent_id'
221 221 label = l(:field_parent_issue)
222 222 value = "##{detail.value}" unless detail.value.blank?
223 223 old_value = "##{detail.old_value}" unless detail.old_value.blank?
224 224
225 when detail.prop_key == 'is_private'
225 when 'is_private'
226 226 value = l(detail.value == "0" ? :general_text_No : :general_text_Yes) unless detail.value.blank?
227 227 old_value = l(detail.old_value == "0" ? :general_text_No : :general_text_Yes) unless detail.old_value.blank?
228 228 end
229 229 when 'cf'
230 230 custom_field = CustomField.find_by_id(detail.prop_key)
231 231 if custom_field
232 232 multiple = custom_field.multiple?
233 233 label = custom_field.name
234 234 value = format_value(detail.value, custom_field.field_format) if detail.value
235 235 old_value = format_value(detail.old_value, custom_field.field_format) if detail.old_value
236 236 end
237 237 when 'attachment'
238 238 label = l(:label_attachment)
239 239 end
240 240 call_hook(:helper_issues_show_detail_after_setting,
241 241 {:detail => detail, :label => label, :value => value, :old_value => old_value })
242 242
243 243 label ||= detail.prop_key
244 244 value ||= detail.value
245 245 old_value ||= detail.old_value
246 246
247 247 unless no_html
248 248 label = content_tag('strong', label)
249 249 old_value = content_tag("i", h(old_value)) if detail.old_value
250 250 old_value = content_tag("strike", old_value) if detail.old_value and detail.value.blank?
251 251 if detail.property == 'attachment' && !value.blank? && a = Attachment.find_by_id(detail.prop_key)
252 252 # Link to the attachment if it has not been removed
253 253 value = link_to_attachment(a, :download => true)
254 254 else
255 255 value = content_tag("i", h(value)) if value
256 256 end
257 257 end
258 258
259 259 if detail.property == 'attr' && detail.prop_key == 'description'
260 260 s = l(:text_journal_changed_no_detail, :label => label)
261 261 unless no_html
262 262 diff_link = link_to 'diff',
263 263 {:controller => 'journals', :action => 'diff', :id => detail.journal_id, :detail_id => detail.id},
264 264 :title => l(:label_view_diff)
265 265 s << " (#{ diff_link })"
266 266 end
267 267 s.html_safe
268 elsif !detail.value.blank?
268 elsif detail.value.present?
269 269 case detail.property
270 270 when 'attr', 'cf'
271 if !detail.old_value.blank?
271 if detail.old_value.present?
272 272 l(:text_journal_changed, :label => label, :old => old_value, :new => value).html_safe
273 273 elsif multiple
274 274 l(:text_journal_added, :label => label, :value => value).html_safe
275 275 else
276 276 l(:text_journal_set_to, :label => label, :value => value).html_safe
277 277 end
278 278 when 'attachment'
279 279 l(:text_journal_added, :label => label, :value => value).html_safe
280 280 end
281 281 else
282 282 l(:text_journal_deleted, :label => label, :old => old_value).html_safe
283 283 end
284 284 end
285 285
286 286 # Find the name of an associated record stored in the field attribute
287 287 def find_name_by_reflection(field, id)
288 288 association = Issue.reflect_on_association(field.to_sym)
289 289 if association
290 290 record = association.class_name.constantize.find_by_id(id)
291 291 return record.name if record
292 292 end
293 293 end
294 294
295 295 # Renders issue children recursively
296 296 def render_api_issue_children(issue, api)
297 297 return if issue.leaf?
298 298 api.array :children do
299 299 issue.children.each do |child|
300 300 api.issue(:id => child.id) do
301 301 api.tracker(:id => child.tracker_id, :name => child.tracker.name) unless child.tracker.nil?
302 302 api.subject child.subject
303 303 render_api_issue_children(child, api)
304 304 end
305 305 end
306 306 end
307 307 end
308 308
309 309 def issues_to_csv(issues, project, query, options={})
310 310 decimal_separator = l(:general_csv_decimal_separator)
311 311 encoding = l(:general_csv_encoding)
312 312 columns = (options[:columns] == 'all' ? query.available_columns : query.columns)
313 313
314 314 export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv|
315 315 # csv header fields
316 316 csv << [ "#" ] + columns.collect {|c| Redmine::CodesetUtil.from_utf8(c.caption.to_s, encoding) } +
317 317 (options[:description] ? [Redmine::CodesetUtil.from_utf8(l(:field_description), encoding)] : [])
318 318
319 319 # csv lines
320 320 issues.each do |issue|
321 321 col_values = columns.collect do |column|
322 322 s = if column.is_a?(QueryCustomFieldColumn)
323 323 cv = issue.custom_values.detect {|v| v.custom_field_id == column.custom_field.id}
324 324 show_value(cv)
325 325 else
326 326 value = issue.send(column.name)
327 327 if value.is_a?(Date)
328 328 format_date(value)
329 329 elsif value.is_a?(Time)
330 330 format_time(value)
331 331 elsif value.is_a?(Float)
332 332 value.to_s.gsub('.', decimal_separator)
333 333 else
334 334 value
335 335 end
336 336 end
337 337 s.to_s
338 338 end
339 339 csv << [ issue.id.to_s ] + col_values.collect {|c| Redmine::CodesetUtil.from_utf8(c.to_s, encoding) } +
340 340 (options[:description] ? [Redmine::CodesetUtil.from_utf8(issue.description, encoding)] : [])
341 341 end
342 342 end
343 343 export
344 344 end
345 345 end
General Comments 0
You need to be logged in to leave comments. Login now