##// END OF EJS Templates
Save 2 queries when displaying a root issue....
Jean-Philippe Lang -
r5124:ba0c9069ed64
parent child
Show More
@@ -1,291 +1,292
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 module IssuesHelper
19 19 include ApplicationHelper
20 20
21 21 def issue_list(issues, &block)
22 22 ancestors = []
23 23 issues.each do |issue|
24 24 while (ancestors.any? && !issue.is_descendant_of?(ancestors.last))
25 25 ancestors.pop
26 26 end
27 27 yield issue, ancestors.size
28 28 ancestors << issue unless issue.leaf?
29 29 end
30 30 end
31 31
32 32 # Renders a HTML/CSS tooltip
33 33 #
34 34 # To use, a trigger div is needed. This is a div with the class of "tooltip"
35 35 # that contains this method wrapped in a span with the class of "tip"
36 36 #
37 37 # <div class="tooltip"><%= link_to_issue(issue) %>
38 38 # <span class="tip"><%= render_issue_tooltip(issue) %></span>
39 39 # </div>
40 40 #
41 41 def render_issue_tooltip(issue)
42 42 @cached_label_status ||= l(:field_status)
43 43 @cached_label_start_date ||= l(:field_start_date)
44 44 @cached_label_due_date ||= l(:field_due_date)
45 45 @cached_label_assigned_to ||= l(:field_assigned_to)
46 46 @cached_label_priority ||= l(:field_priority)
47 47 @cached_label_project ||= l(:field_project)
48 48
49 49 link_to_issue(issue) + "<br /><br />" +
50 50 "<strong>#{@cached_label_project}</strong>: #{link_to_project(issue.project)}<br />" +
51 51 "<strong>#{@cached_label_status}</strong>: #{issue.status.name}<br />" +
52 52 "<strong>#{@cached_label_start_date}</strong>: #{format_date(issue.start_date)}<br />" +
53 53 "<strong>#{@cached_label_due_date}</strong>: #{format_date(issue.due_date)}<br />" +
54 54 "<strong>#{@cached_label_assigned_to}</strong>: #{issue.assigned_to}<br />" +
55 55 "<strong>#{@cached_label_priority}</strong>: #{issue.priority.name}"
56 56 end
57 57
58 58 def render_issue_subject_with_tree(issue)
59 59 s = ''
60 issue.ancestors.each do |ancestor|
60 ancestors = issue.root? ? [] : issue.ancestors.all
61 ancestors.each do |ancestor|
61 62 s << '<div>' + content_tag('p', link_to_issue(ancestor))
62 63 end
63 64 s << '<div>' + content_tag('h3', h(issue.subject))
64 s << '</div>' * (issue.ancestors.size + 1)
65 s << '</div>' * (ancestors.size + 1)
65 66 s
66 67 end
67 68
68 69 def render_descendants_tree(issue)
69 70 s = '<form><table class="list issues">'
70 71 issue_list(issue.descendants.sort_by(&:lft)) do |child, level|
71 72 s << content_tag('tr',
72 73 content_tag('td', check_box_tag("ids[]", child.id, false, :id => nil), :class => 'checkbox') +
73 74 content_tag('td', link_to_issue(child, :truncate => 60), :class => 'subject') +
74 75 content_tag('td', h(child.status)) +
75 76 content_tag('td', link_to_user(child.assigned_to)) +
76 77 content_tag('td', progress_bar(child.done_ratio, :width => '80px')),
77 78 :class => "issue issue-#{child.id} hascontextmenu #{level > 0 ? "idnt idnt-#{level}" : nil}")
78 79 end
79 80 s << '</form></table>'
80 81 s
81 82 end
82 83
83 84 def render_custom_fields_rows(issue)
84 85 return if issue.custom_field_values.empty?
85 86 ordered_values = []
86 87 half = (issue.custom_field_values.size / 2.0).ceil
87 88 half.times do |i|
88 89 ordered_values << issue.custom_field_values[i]
89 90 ordered_values << issue.custom_field_values[i + half]
90 91 end
91 92 s = "<tr>\n"
92 93 n = 0
93 94 ordered_values.compact.each do |value|
94 95 s << "</tr>\n<tr>\n" if n > 0 && (n % 2) == 0
95 96 s << "\t<th>#{ h(value.custom_field.name) }:</th><td>#{ simple_format_without_paragraph(h(show_value(value))) }</td>\n"
96 97 n += 1
97 98 end
98 99 s << "</tr>\n"
99 100 s
100 101 end
101 102
102 103 def sidebar_queries
103 104 unless @sidebar_queries
104 105 # User can see public queries and his own queries
105 106 visible = ARCondition.new(["is_public = ? OR user_id = ?", true, (User.current.logged? ? User.current.id : 0)])
106 107 # Project specific queries and global queries
107 108 visible << (@project.nil? ? ["project_id IS NULL"] : ["project_id IS NULL OR project_id = ?", @project.id])
108 109 @sidebar_queries = Query.find(:all,
109 110 :select => 'id, name, is_public',
110 111 :order => "name ASC",
111 112 :conditions => visible.conditions)
112 113 end
113 114 @sidebar_queries
114 115 end
115 116
116 117 def query_links(title, queries)
117 118 # links to #index on issues/show
118 119 url_params = controller_name == 'issues' ? {:controller => 'issues', :action => 'index', :project_id => @project} : params
119 120
120 121 content_tag('h3', title) +
121 122 queries.collect {|query|
122 123 link_to(h(query.name), url_params.merge(:query_id => query))
123 124 }.join('<br />')
124 125 end
125 126
126 127 def render_sidebar_queries
127 128 out = ''
128 129 queries = sidebar_queries.select {|q| !q.is_public?}
129 130 out << query_links(l(:label_my_queries), queries) if queries.any?
130 131 queries = sidebar_queries.select {|q| q.is_public?}
131 132 out << query_links(l(:label_query_plural), queries) if queries.any?
132 133 out
133 134 end
134 135
135 136 def show_detail(detail, no_html=false)
136 137 case detail.property
137 138 when 'attr'
138 139 field = detail.prop_key.to_s.gsub(/\_id$/, "")
139 140 label = l(("field_" + field).to_sym)
140 141 case
141 142 when ['due_date', 'start_date'].include?(detail.prop_key)
142 143 value = format_date(detail.value.to_date) if detail.value
143 144 old_value = format_date(detail.old_value.to_date) if detail.old_value
144 145
145 146 when ['project_id', 'status_id', 'tracker_id', 'assigned_to_id', 'priority_id', 'category_id', 'fixed_version_id'].include?(detail.prop_key)
146 147 value = find_name_by_reflection(field, detail.value)
147 148 old_value = find_name_by_reflection(field, detail.old_value)
148 149
149 150 when detail.prop_key == 'estimated_hours'
150 151 value = "%0.02f" % detail.value.to_f unless detail.value.blank?
151 152 old_value = "%0.02f" % detail.old_value.to_f unless detail.old_value.blank?
152 153
153 154 when detail.prop_key == 'parent_id'
154 155 label = l(:field_parent_issue)
155 156 value = "##{detail.value}" unless detail.value.blank?
156 157 old_value = "##{detail.old_value}" unless detail.old_value.blank?
157 158 end
158 159 when 'cf'
159 160 custom_field = CustomField.find_by_id(detail.prop_key)
160 161 if custom_field
161 162 label = custom_field.name
162 163 value = format_value(detail.value, custom_field.field_format) if detail.value
163 164 old_value = format_value(detail.old_value, custom_field.field_format) if detail.old_value
164 165 end
165 166 when 'attachment'
166 167 label = l(:label_attachment)
167 168 end
168 169 call_hook(:helper_issues_show_detail_after_setting, {:detail => detail, :label => label, :value => value, :old_value => old_value })
169 170
170 171 label ||= detail.prop_key
171 172 value ||= detail.value
172 173 old_value ||= detail.old_value
173 174
174 175 unless no_html
175 176 label = content_tag('strong', label)
176 177 old_value = content_tag("i", h(old_value)) if detail.old_value
177 178 old_value = content_tag("strike", old_value) if detail.old_value and (!detail.value or detail.value.empty?)
178 179 if detail.property == 'attachment' && !value.blank? && a = Attachment.find_by_id(detail.prop_key)
179 180 # Link to the attachment if it has not been removed
180 181 value = link_to_attachment(a)
181 182 else
182 183 value = content_tag("i", h(value)) if value
183 184 end
184 185 end
185 186
186 187 if detail.property == 'attr' && detail.prop_key == 'description'
187 188 s = l(:text_journal_changed_no_detail, :label => label)
188 189 unless no_html
189 190 diff_link = link_to 'diff',
190 191 {:controller => 'journals', :action => 'diff', :id => detail.journal_id, :detail_id => detail.id},
191 192 :title => l(:label_view_diff)
192 193 s << " (#{ diff_link })"
193 194 end
194 195 s
195 196 elsif !detail.value.blank?
196 197 case detail.property
197 198 when 'attr', 'cf'
198 199 if !detail.old_value.blank?
199 200 l(:text_journal_changed, :label => label, :old => old_value, :new => value)
200 201 else
201 202 l(:text_journal_set_to, :label => label, :value => value)
202 203 end
203 204 when 'attachment'
204 205 l(:text_journal_added, :label => label, :value => value)
205 206 end
206 207 else
207 208 l(:text_journal_deleted, :label => label, :old => old_value)
208 209 end
209 210 end
210 211
211 212 # Find the name of an associated record stored in the field attribute
212 213 def find_name_by_reflection(field, id)
213 214 association = Issue.reflect_on_association(field.to_sym)
214 215 if association
215 216 record = association.class_name.constantize.find_by_id(id)
216 217 return record.name if record
217 218 end
218 219 end
219 220
220 221 # Renders issue children recursively
221 222 def render_api_issue_children(issue, api)
222 223 return if issue.leaf?
223 224 api.array :children do
224 225 issue.children.each do |child|
225 226 api.issue(:id => child.id) do
226 227 api.tracker(:id => child.tracker_id, :name => child.tracker.name) unless child.tracker.nil?
227 228 api.subject child.subject
228 229 render_api_issue_children(child, api)
229 230 end
230 231 end
231 232 end
232 233 end
233 234
234 235 def issues_to_csv(issues, project = nil)
235 236 ic = Iconv.new(l(:general_csv_encoding), 'UTF-8')
236 237 decimal_separator = l(:general_csv_decimal_separator)
237 238 export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv|
238 239 # csv header fields
239 240 headers = [ "#",
240 241 l(:field_status),
241 242 l(:field_project),
242 243 l(:field_tracker),
243 244 l(:field_priority),
244 245 l(:field_subject),
245 246 l(:field_assigned_to),
246 247 l(:field_category),
247 248 l(:field_fixed_version),
248 249 l(:field_author),
249 250 l(:field_start_date),
250 251 l(:field_due_date),
251 252 l(:field_done_ratio),
252 253 l(:field_estimated_hours),
253 254 l(:field_parent_issue),
254 255 l(:field_created_on),
255 256 l(:field_updated_on)
256 257 ]
257 258 # Export project custom fields if project is given
258 259 # otherwise export custom fields marked as "For all projects"
259 260 custom_fields = project.nil? ? IssueCustomField.for_all : project.all_issue_custom_fields
260 261 custom_fields.each {|f| headers << f.name}
261 262 # Description in the last column
262 263 headers << l(:field_description)
263 264 csv << headers.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end }
264 265 # csv lines
265 266 issues.each do |issue|
266 267 fields = [issue.id,
267 268 issue.status.name,
268 269 issue.project.name,
269 270 issue.tracker.name,
270 271 issue.priority.name,
271 272 issue.subject,
272 273 issue.assigned_to,
273 274 issue.category,
274 275 issue.fixed_version,
275 276 issue.author.name,
276 277 format_date(issue.start_date),
277 278 format_date(issue.due_date),
278 279 issue.done_ratio,
279 280 issue.estimated_hours.to_s.gsub('.', decimal_separator),
280 281 issue.parent_id,
281 282 format_time(issue.created_on),
282 283 format_time(issue.updated_on)
283 284 ]
284 285 custom_fields.each {|f| fields << show_value(issue.custom_value_for(f)) }
285 286 fields << issue.description
286 287 csv << fields.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end }
287 288 end
288 289 end
289 290 export
290 291 end
291 292 end
General Comments 0
You need to be logged in to leave comments. Login now