##// END OF EJS Templates
Use Query.visible scope....
Jean-Philippe Lang -
r7966:b9e02477e902
parent child
Show More
@@ -1,304 +1,301
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>: #{h(issue.status.name)}<br />" +
51 "<strong>#{@cached_label_status}</strong>: #{h(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>: #{h(issue.assigned_to)}<br />" +
54 "<strong>#{@cached_label_assigned_to}</strong>: #{h(issue.assigned_to)}<br />" +
55 "<strong>#{@cached_label_priority}</strong>: #{h(issue.priority.name)}").html_safe
55 "<strong>#{@cached_label_priority}</strong>: #{h(issue.priority.name)}").html_safe
56 end
56 end
57
57
58 def issue_heading(issue)
58 def issue_heading(issue)
59 h("#{issue.tracker} ##{issue.id}")
59 h("#{issue.tracker} ##{issue.id}")
60 end
60 end
61
61
62 def render_issue_subject_with_tree(issue)
62 def render_issue_subject_with_tree(issue)
63 s = ''
63 s = ''
64 ancestors = issue.root? ? [] : issue.ancestors.visible.all
64 ancestors = issue.root? ? [] : issue.ancestors.visible.all
65 ancestors.each do |ancestor|
65 ancestors.each do |ancestor|
66 s << '<div>' + content_tag('p', link_to_issue(ancestor))
66 s << '<div>' + content_tag('p', link_to_issue(ancestor))
67 end
67 end
68 s << '<div>'
68 s << '<div>'
69 subject = h(issue.subject)
69 subject = h(issue.subject)
70 if issue.is_private?
70 if issue.is_private?
71 subject = content_tag('span', l(:field_is_private), :class => 'private') + ' ' + subject
71 subject = content_tag('span', l(:field_is_private), :class => 'private') + ' ' + subject
72 end
72 end
73 s << content_tag('h3', subject)
73 s << content_tag('h3', subject)
74 s << '</div>' * (ancestors.size + 1)
74 s << '</div>' * (ancestors.size + 1)
75 s.html_safe
75 s.html_safe
76 end
76 end
77
77
78 def render_descendants_tree(issue)
78 def render_descendants_tree(issue)
79 s = '<form><table class="list issues">'
79 s = '<form><table class="list issues">'
80 issue_list(issue.descendants.visible.sort_by(&:lft)) do |child, level|
80 issue_list(issue.descendants.visible.sort_by(&:lft)) do |child, level|
81 s << content_tag('tr',
81 s << content_tag('tr',
82 content_tag('td', check_box_tag("ids[]", child.id, false, :id => nil), :class => 'checkbox') +
82 content_tag('td', check_box_tag("ids[]", child.id, false, :id => nil), :class => 'checkbox') +
83 content_tag('td', link_to_issue(child, :truncate => 60), :class => 'subject') +
83 content_tag('td', link_to_issue(child, :truncate => 60), :class => 'subject') +
84 content_tag('td', h(child.status)) +
84 content_tag('td', h(child.status)) +
85 content_tag('td', link_to_user(child.assigned_to)) +
85 content_tag('td', link_to_user(child.assigned_to)) +
86 content_tag('td', progress_bar(child.done_ratio, :width => '80px')),
86 content_tag('td', progress_bar(child.done_ratio, :width => '80px')),
87 :class => "issue issue-#{child.id} hascontextmenu #{level > 0 ? "idnt idnt-#{level}" : nil}")
87 :class => "issue issue-#{child.id} hascontextmenu #{level > 0 ? "idnt idnt-#{level}" : nil}")
88 end
88 end
89 s << '</table></form>'
89 s << '</table></form>'
90 s.html_safe
90 s.html_safe
91 end
91 end
92
92
93 def render_custom_fields_rows(issue)
93 def render_custom_fields_rows(issue)
94 return if issue.custom_field_values.empty?
94 return if issue.custom_field_values.empty?
95 ordered_values = []
95 ordered_values = []
96 half = (issue.custom_field_values.size / 2.0).ceil
96 half = (issue.custom_field_values.size / 2.0).ceil
97 half.times do |i|
97 half.times do |i|
98 ordered_values << issue.custom_field_values[i]
98 ordered_values << issue.custom_field_values[i]
99 ordered_values << issue.custom_field_values[i + half]
99 ordered_values << issue.custom_field_values[i + half]
100 end
100 end
101 s = "<tr>\n"
101 s = "<tr>\n"
102 n = 0
102 n = 0
103 ordered_values.compact.each do |value|
103 ordered_values.compact.each do |value|
104 s << "</tr>\n<tr>\n" if n > 0 && (n % 2) == 0
104 s << "</tr>\n<tr>\n" if n > 0 && (n % 2) == 0
105 s << "\t<th>#{ h(value.custom_field.name) }:</th><td>#{ simple_format_without_paragraph(h(show_value(value))) }</td>\n"
105 s << "\t<th>#{ h(value.custom_field.name) }:</th><td>#{ simple_format_without_paragraph(h(show_value(value))) }</td>\n"
106 n += 1
106 n += 1
107 end
107 end
108 s << "</tr>\n"
108 s << "</tr>\n"
109 s.html_safe
109 s.html_safe
110 end
110 end
111
111
112 def issues_destroy_confirmation_message(issues)
112 def issues_destroy_confirmation_message(issues)
113 issues = [issues] unless issues.is_a?(Array)
113 issues = [issues] unless issues.is_a?(Array)
114 message = l(:text_issues_destroy_confirmation)
114 message = l(:text_issues_destroy_confirmation)
115 descendant_count = issues.inject(0) {|memo, i| memo += (i.right - i.left - 1)/2}
115 descendant_count = issues.inject(0) {|memo, i| memo += (i.right - i.left - 1)/2}
116 if descendant_count > 0
116 if descendant_count > 0
117 issues.each do |issue|
117 issues.each do |issue|
118 next if issue.root?
118 next if issue.root?
119 issues.each do |other_issue|
119 issues.each do |other_issue|
120 descendant_count -= 1 if issue.is_descendant_of?(other_issue)
120 descendant_count -= 1 if issue.is_descendant_of?(other_issue)
121 end
121 end
122 end
122 end
123 if descendant_count > 0
123 if descendant_count > 0
124 message << "\n" + l(:text_issues_destroy_descendants_confirmation, :count => descendant_count)
124 message << "\n" + l(:text_issues_destroy_descendants_confirmation, :count => descendant_count)
125 end
125 end
126 end
126 end
127 message
127 message
128 end
128 end
129
129
130 def sidebar_queries
130 def sidebar_queries
131 unless @sidebar_queries
131 unless @sidebar_queries
132 # User can see public queries and his own queries
132 @sidebar_queries = Query.visible.all(
133 visible = ARCondition.new(["is_public = ? OR user_id = ?", true, (User.current.logged? ? User.current.id : 0)])
133 :order => "#{Query.table_name}.name ASC",
134 # Project specific queries and global queries
134 # Project specific queries and global queries
135 visible << (@project.nil? ? ["project_id IS NULL"] : ["project_id IS NULL OR project_id = ?", @project.id])
135 :conditions => (@project.nil? ? ["project_id IS NULL"] : ["project_id IS NULL OR project_id = ?", @project.id])
136 @sidebar_queries = Query.find(:all,
136 )
137 :select => 'id, name, is_public',
138 :order => "name ASC",
139 :conditions => visible.conditions)
140 end
137 end
141 @sidebar_queries
138 @sidebar_queries
142 end
139 end
143
140
144 def query_links(title, queries)
141 def query_links(title, queries)
145 # links to #index on issues/show
142 # links to #index on issues/show
146 url_params = controller_name == 'issues' ? {:controller => 'issues', :action => 'index', :project_id => @project} : params
143 url_params = controller_name == 'issues' ? {:controller => 'issues', :action => 'index', :project_id => @project} : params
147
144
148 content_tag('h3', h(title)) +
145 content_tag('h3', h(title)) +
149 queries.collect {|query|
146 queries.collect {|query|
150 link_to(h(query.name), url_params.merge(:query_id => query))
147 link_to(h(query.name), url_params.merge(:query_id => query))
151 }.join('<br />')
148 }.join('<br />')
152 end
149 end
153
150
154 def render_sidebar_queries
151 def render_sidebar_queries
155 out = ''
152 out = ''
156 queries = sidebar_queries.select {|q| !q.is_public?}
153 queries = sidebar_queries.select {|q| !q.is_public?}
157 out << query_links(l(:label_my_queries), queries) if queries.any?
154 out << query_links(l(:label_my_queries), queries) if queries.any?
158 queries = sidebar_queries.select {|q| q.is_public?}
155 queries = sidebar_queries.select {|q| q.is_public?}
159 out << query_links(l(:label_query_plural), queries) if queries.any?
156 out << query_links(l(:label_query_plural), queries) if queries.any?
160 out
157 out
161 end
158 end
162
159
163 def show_detail(detail, no_html=false)
160 def show_detail(detail, no_html=false)
164 case detail.property
161 case detail.property
165 when 'attr'
162 when 'attr'
166 field = detail.prop_key.to_s.gsub(/\_id$/, "")
163 field = detail.prop_key.to_s.gsub(/\_id$/, "")
167 label = l(("field_" + field).to_sym)
164 label = l(("field_" + field).to_sym)
168 case
165 case
169 when ['due_date', 'start_date'].include?(detail.prop_key)
166 when ['due_date', 'start_date'].include?(detail.prop_key)
170 value = format_date(detail.value.to_date) if detail.value
167 value = format_date(detail.value.to_date) if detail.value
171 old_value = format_date(detail.old_value.to_date) if detail.old_value
168 old_value = format_date(detail.old_value.to_date) if detail.old_value
172
169
173 when ['project_id', 'status_id', 'tracker_id', 'assigned_to_id',
170 when ['project_id', 'status_id', 'tracker_id', 'assigned_to_id',
174 'priority_id', 'category_id', 'fixed_version_id'].include?(detail.prop_key)
171 'priority_id', 'category_id', 'fixed_version_id'].include?(detail.prop_key)
175 value = find_name_by_reflection(field, detail.value)
172 value = find_name_by_reflection(field, detail.value)
176 old_value = find_name_by_reflection(field, detail.old_value)
173 old_value = find_name_by_reflection(field, detail.old_value)
177
174
178 when detail.prop_key == 'estimated_hours'
175 when detail.prop_key == 'estimated_hours'
179 value = "%0.02f" % detail.value.to_f unless detail.value.blank?
176 value = "%0.02f" % detail.value.to_f unless detail.value.blank?
180 old_value = "%0.02f" % detail.old_value.to_f unless detail.old_value.blank?
177 old_value = "%0.02f" % detail.old_value.to_f unless detail.old_value.blank?
181
178
182 when detail.prop_key == 'parent_id'
179 when detail.prop_key == 'parent_id'
183 label = l(:field_parent_issue)
180 label = l(:field_parent_issue)
184 value = "##{detail.value}" unless detail.value.blank?
181 value = "##{detail.value}" unless detail.value.blank?
185 old_value = "##{detail.old_value}" unless detail.old_value.blank?
182 old_value = "##{detail.old_value}" unless detail.old_value.blank?
186
183
187 when detail.prop_key == 'is_private'
184 when detail.prop_key == 'is_private'
188 value = l(detail.value == "0" ? :general_text_No : :general_text_Yes) unless detail.value.blank?
185 value = l(detail.value == "0" ? :general_text_No : :general_text_Yes) unless detail.value.blank?
189 old_value = l(detail.old_value == "0" ? :general_text_No : :general_text_Yes) unless detail.old_value.blank?
186 old_value = l(detail.old_value == "0" ? :general_text_No : :general_text_Yes) unless detail.old_value.blank?
190 end
187 end
191 when 'cf'
188 when 'cf'
192 custom_field = CustomField.find_by_id(detail.prop_key)
189 custom_field = CustomField.find_by_id(detail.prop_key)
193 if custom_field
190 if custom_field
194 label = custom_field.name
191 label = custom_field.name
195 value = format_value(detail.value, custom_field.field_format) if detail.value
192 value = format_value(detail.value, custom_field.field_format) if detail.value
196 old_value = format_value(detail.old_value, custom_field.field_format) if detail.old_value
193 old_value = format_value(detail.old_value, custom_field.field_format) if detail.old_value
197 end
194 end
198 when 'attachment'
195 when 'attachment'
199 label = l(:label_attachment)
196 label = l(:label_attachment)
200 end
197 end
201 call_hook(:helper_issues_show_detail_after_setting,
198 call_hook(:helper_issues_show_detail_after_setting,
202 {:detail => detail, :label => label, :value => value, :old_value => old_value })
199 {:detail => detail, :label => label, :value => value, :old_value => old_value })
203
200
204 label ||= detail.prop_key
201 label ||= detail.prop_key
205 value ||= detail.value
202 value ||= detail.value
206 old_value ||= detail.old_value
203 old_value ||= detail.old_value
207
204
208 unless no_html
205 unless no_html
209 label = content_tag('strong', label)
206 label = content_tag('strong', label)
210 old_value = content_tag("i", h(old_value)) if detail.old_value
207 old_value = content_tag("i", h(old_value)) if detail.old_value
211 old_value = content_tag("strike", old_value) if detail.old_value and detail.value.blank?
208 old_value = content_tag("strike", old_value) if detail.old_value and detail.value.blank?
212 if detail.property == 'attachment' && !value.blank? && a = Attachment.find_by_id(detail.prop_key)
209 if detail.property == 'attachment' && !value.blank? && a = Attachment.find_by_id(detail.prop_key)
213 # Link to the attachment if it has not been removed
210 # Link to the attachment if it has not been removed
214 value = link_to_attachment(a)
211 value = link_to_attachment(a)
215 else
212 else
216 value = content_tag("i", h(value)) if value
213 value = content_tag("i", h(value)) if value
217 end
214 end
218 end
215 end
219
216
220 if detail.property == 'attr' && detail.prop_key == 'description'
217 if detail.property == 'attr' && detail.prop_key == 'description'
221 s = l(:text_journal_changed_no_detail, :label => label)
218 s = l(:text_journal_changed_no_detail, :label => label)
222 unless no_html
219 unless no_html
223 diff_link = link_to 'diff',
220 diff_link = link_to 'diff',
224 {:controller => 'journals', :action => 'diff', :id => detail.journal_id, :detail_id => detail.id},
221 {:controller => 'journals', :action => 'diff', :id => detail.journal_id, :detail_id => detail.id},
225 :title => l(:label_view_diff)
222 :title => l(:label_view_diff)
226 s << " (#{ diff_link })"
223 s << " (#{ diff_link })"
227 end
224 end
228 s.html_safe
225 s.html_safe
229 elsif !detail.value.blank?
226 elsif !detail.value.blank?
230 case detail.property
227 case detail.property
231 when 'attr', 'cf'
228 when 'attr', 'cf'
232 if !detail.old_value.blank?
229 if !detail.old_value.blank?
233 l(:text_journal_changed, :label => label, :old => old_value, :new => value).html_safe
230 l(:text_journal_changed, :label => label, :old => old_value, :new => value).html_safe
234 else
231 else
235 l(:text_journal_set_to, :label => label, :value => value).html_safe
232 l(:text_journal_set_to, :label => label, :value => value).html_safe
236 end
233 end
237 when 'attachment'
234 when 'attachment'
238 l(:text_journal_added, :label => label, :value => value).html_safe
235 l(:text_journal_added, :label => label, :value => value).html_safe
239 end
236 end
240 else
237 else
241 l(:text_journal_deleted, :label => label, :old => old_value).html_safe
238 l(:text_journal_deleted, :label => label, :old => old_value).html_safe
242 end
239 end
243 end
240 end
244
241
245 # Find the name of an associated record stored in the field attribute
242 # Find the name of an associated record stored in the field attribute
246 def find_name_by_reflection(field, id)
243 def find_name_by_reflection(field, id)
247 association = Issue.reflect_on_association(field.to_sym)
244 association = Issue.reflect_on_association(field.to_sym)
248 if association
245 if association
249 record = association.class_name.constantize.find_by_id(id)
246 record = association.class_name.constantize.find_by_id(id)
250 return record.name if record
247 return record.name if record
251 end
248 end
252 end
249 end
253
250
254 # Renders issue children recursively
251 # Renders issue children recursively
255 def render_api_issue_children(issue, api)
252 def render_api_issue_children(issue, api)
256 return if issue.leaf?
253 return if issue.leaf?
257 api.array :children do
254 api.array :children do
258 issue.children.each do |child|
255 issue.children.each do |child|
259 api.issue(:id => child.id) do
256 api.issue(:id => child.id) do
260 api.tracker(:id => child.tracker_id, :name => child.tracker.name) unless child.tracker.nil?
257 api.tracker(:id => child.tracker_id, :name => child.tracker.name) unless child.tracker.nil?
261 api.subject child.subject
258 api.subject child.subject
262 render_api_issue_children(child, api)
259 render_api_issue_children(child, api)
263 end
260 end
264 end
261 end
265 end
262 end
266 end
263 end
267
264
268 def issues_to_csv(issues, project, query, options={})
265 def issues_to_csv(issues, project, query, options={})
269 decimal_separator = l(:general_csv_decimal_separator)
266 decimal_separator = l(:general_csv_decimal_separator)
270 encoding = l(:general_csv_encoding)
267 encoding = l(:general_csv_encoding)
271 columns = (options[:columns] == 'all' ? query.available_columns : query.columns)
268 columns = (options[:columns] == 'all' ? query.available_columns : query.columns)
272
269
273 export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv|
270 export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv|
274 # csv header fields
271 # csv header fields
275 csv << [ "#" ] + columns.collect {|c| Redmine::CodesetUtil.from_utf8(c.caption.to_s, encoding) } +
272 csv << [ "#" ] + columns.collect {|c| Redmine::CodesetUtil.from_utf8(c.caption.to_s, encoding) } +
276 (options[:description] ? [Redmine::CodesetUtil.from_utf8(l(:field_description), encoding)] : [])
273 (options[:description] ? [Redmine::CodesetUtil.from_utf8(l(:field_description), encoding)] : [])
277
274
278 # csv lines
275 # csv lines
279 issues.each do |issue|
276 issues.each do |issue|
280 col_values = columns.collect do |column|
277 col_values = columns.collect do |column|
281 s = if column.is_a?(QueryCustomFieldColumn)
278 s = if column.is_a?(QueryCustomFieldColumn)
282 cv = issue.custom_values.detect {|v| v.custom_field_id == column.custom_field.id}
279 cv = issue.custom_values.detect {|v| v.custom_field_id == column.custom_field.id}
283 show_value(cv)
280 show_value(cv)
284 else
281 else
285 value = issue.send(column.name)
282 value = issue.send(column.name)
286 if value.is_a?(Date)
283 if value.is_a?(Date)
287 format_date(value)
284 format_date(value)
288 elsif value.is_a?(Time)
285 elsif value.is_a?(Time)
289 format_time(value)
286 format_time(value)
290 elsif value.is_a?(Float)
287 elsif value.is_a?(Float)
291 value.to_s.gsub('.', decimal_separator)
288 value.to_s.gsub('.', decimal_separator)
292 else
289 else
293 value
290 value
294 end
291 end
295 end
292 end
296 s.to_s
293 s.to_s
297 end
294 end
298 csv << [ issue.id.to_s ] + col_values.collect {|c| Redmine::CodesetUtil.from_utf8(c.to_s, encoding) } +
295 csv << [ issue.id.to_s ] + col_values.collect {|c| Redmine::CodesetUtil.from_utf8(c.to_s, encoding) } +
299 (options[:description] ? [Redmine::CodesetUtil.from_utf8(issue.description, encoding)] : [])
296 (options[:description] ? [Redmine::CodesetUtil.from_utf8(issue.description, encoding)] : [])
300 end
297 end
301 end
298 end
302 export
299 export
303 end
300 end
304 end
301 end
General Comments 0
You need to be logged in to leave comments. Login now