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