@@ -1,355 +1,355 | |||
|
1 | 1 | # encoding: utf-8 |
|
2 | 2 | # |
|
3 | 3 | # Redmine - project management software |
|
4 | 4 | # Copyright (C) 2006-2016 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 QueriesHelper |
|
21 | 21 | include ApplicationHelper |
|
22 | 22 | |
|
23 | 23 | def filters_options_for_select(query) |
|
24 | 24 | ungrouped = [] |
|
25 | 25 | grouped = {} |
|
26 | 26 | query.available_filters.map do |field, field_options| |
|
27 | 27 | if field_options[:type] == :relation |
|
28 | 28 | group = :label_relations |
|
29 | 29 | elsif field_options[:type] == :tree |
|
30 | 30 | group = query.is_a?(IssueQuery) ? :label_relations : nil |
|
31 | 31 | elsif field =~ /^(.+)\./ |
|
32 | 32 | # association filters |
|
33 | group = "field_#{$1}" | |
|
33 | group = "field_#{$1}".to_sym | |
|
34 | 34 | elsif %w(member_of_group assigned_to_role).include?(field) |
|
35 | 35 | group = :field_assigned_to |
|
36 | 36 | elsif field_options[:type] == :date_past || field_options[:type] == :date |
|
37 | 37 | group = :label_date |
|
38 | 38 | end |
|
39 | 39 | if group |
|
40 | 40 | (grouped[group] ||= []) << [field_options[:name], field] |
|
41 | 41 | else |
|
42 | 42 | ungrouped << [field_options[:name], field] |
|
43 | 43 | end |
|
44 | 44 | end |
|
45 | 45 | # Don't group dates if there's only one (eg. time entries filters) |
|
46 | 46 | if grouped[:label_date].try(:size) == 1 |
|
47 | 47 | ungrouped << grouped.delete(:label_date).first |
|
48 | 48 | end |
|
49 | 49 | s = options_for_select([[]] + ungrouped) |
|
50 | 50 | if grouped.present? |
|
51 | 51 | localized_grouped = grouped.map {|k,v| [l(k), v]} |
|
52 | 52 | s << grouped_options_for_select(localized_grouped) |
|
53 | 53 | end |
|
54 | 54 | s |
|
55 | 55 | end |
|
56 | 56 | |
|
57 | 57 | def query_filters_hidden_tags(query) |
|
58 | 58 | tags = ''.html_safe |
|
59 | 59 | query.filters.each do |field, options| |
|
60 | 60 | tags << hidden_field_tag("f[]", field, :id => nil) |
|
61 | 61 | tags << hidden_field_tag("op[#{field}]", options[:operator], :id => nil) |
|
62 | 62 | options[:values].each do |value| |
|
63 | 63 | tags << hidden_field_tag("v[#{field}][]", value, :id => nil) |
|
64 | 64 | end |
|
65 | 65 | end |
|
66 | 66 | tags |
|
67 | 67 | end |
|
68 | 68 | |
|
69 | 69 | def query_columns_hidden_tags(query) |
|
70 | 70 | tags = ''.html_safe |
|
71 | 71 | query.columns.each do |column| |
|
72 | 72 | tags << hidden_field_tag("c[]", column.name, :id => nil) |
|
73 | 73 | end |
|
74 | 74 | tags |
|
75 | 75 | end |
|
76 | 76 | |
|
77 | 77 | def query_hidden_tags(query) |
|
78 | 78 | query_filters_hidden_tags(query) + query_columns_hidden_tags(query) |
|
79 | 79 | end |
|
80 | 80 | |
|
81 | 81 | def group_by_column_select_tag(query) |
|
82 | 82 | options = [[]] + query.groupable_columns.collect {|c| [c.caption, c.name.to_s]} |
|
83 | 83 | select_tag('group_by', options_for_select(options, @query.group_by)) |
|
84 | 84 | end |
|
85 | 85 | |
|
86 | 86 | def available_block_columns_tags(query) |
|
87 | 87 | tags = ''.html_safe |
|
88 | 88 | query.available_block_columns.each do |column| |
|
89 | 89 | tags << content_tag('label', check_box_tag('c[]', column.name.to_s, query.has_column?(column), :id => nil) + " #{column.caption}", :class => 'inline') |
|
90 | 90 | end |
|
91 | 91 | tags |
|
92 | 92 | end |
|
93 | 93 | |
|
94 | 94 | def available_totalable_columns_tags(query) |
|
95 | 95 | tags = ''.html_safe |
|
96 | 96 | query.available_totalable_columns.each do |column| |
|
97 | 97 | tags << content_tag('label', check_box_tag('t[]', column.name.to_s, query.totalable_columns.include?(column), :id => nil) + " #{column.caption}", :class => 'inline') |
|
98 | 98 | end |
|
99 | 99 | tags << hidden_field_tag('t[]', '') |
|
100 | 100 | tags |
|
101 | 101 | end |
|
102 | 102 | |
|
103 | 103 | def query_available_inline_columns_options(query) |
|
104 | 104 | (query.available_inline_columns - query.columns).reject(&:frozen?).collect {|column| [column.caption, column.name]} |
|
105 | 105 | end |
|
106 | 106 | |
|
107 | 107 | def query_selected_inline_columns_options(query) |
|
108 | 108 | (query.inline_columns & query.available_inline_columns).reject(&:frozen?).collect {|column| [column.caption, column.name]} |
|
109 | 109 | end |
|
110 | 110 | |
|
111 | 111 | def render_query_columns_selection(query, options={}) |
|
112 | 112 | tag_name = (options[:name] || 'c') + '[]' |
|
113 | 113 | render :partial => 'queries/columns', :locals => {:query => query, :tag_name => tag_name} |
|
114 | 114 | end |
|
115 | 115 | |
|
116 | 116 | def grouped_query_results(items, query, item_count_by_group, &block) |
|
117 | 117 | previous_group, first = false, true |
|
118 | 118 | totals_by_group = query.totalable_columns.inject({}) do |h, column| |
|
119 | 119 | h[column] = query.total_by_group_for(column) |
|
120 | 120 | h |
|
121 | 121 | end |
|
122 | 122 | items.each do |item| |
|
123 | 123 | group_name = group_count = nil |
|
124 | 124 | if query.grouped? |
|
125 | 125 | group = query.group_by_column.value(item) |
|
126 | 126 | if first || group != previous_group |
|
127 | 127 | if group.blank? && group != false |
|
128 | 128 | group_name = "(#{l(:label_blank_value)})" |
|
129 | 129 | else |
|
130 | 130 | group_name = format_object(group) |
|
131 | 131 | end |
|
132 | 132 | group_name ||= "" |
|
133 | 133 | group_count = item_count_by_group ? item_count_by_group[group] : nil |
|
134 | 134 | group_totals = totals_by_group.map {|column, t| total_tag(column, t[group] || 0)}.join(" ").html_safe |
|
135 | 135 | end |
|
136 | 136 | end |
|
137 | 137 | yield item, group_name, group_count, group_totals |
|
138 | 138 | previous_group, first = group, false |
|
139 | 139 | end |
|
140 | 140 | end |
|
141 | 141 | |
|
142 | 142 | def render_query_totals(query) |
|
143 | 143 | return unless query.totalable_columns.present? |
|
144 | 144 | totals = query.totalable_columns.map do |column| |
|
145 | 145 | total_tag(column, query.total_for(column)) |
|
146 | 146 | end |
|
147 | 147 | content_tag('p', totals.join(" ").html_safe, :class => "query-totals") |
|
148 | 148 | end |
|
149 | 149 | |
|
150 | 150 | def total_tag(column, value) |
|
151 | 151 | label = content_tag('span', "#{column.caption}:") |
|
152 | 152 | value = if [:hours, :spent_hours, :total_spent_hours, :estimated_hours].include? column.name |
|
153 | 153 | format_hours(value) |
|
154 | 154 | else |
|
155 | 155 | format_object(value) |
|
156 | 156 | end |
|
157 | 157 | value = content_tag('span', value, :class => 'value') |
|
158 | 158 | content_tag('span', label + " " + value, :class => "total-for-#{column.name.to_s.dasherize}") |
|
159 | 159 | end |
|
160 | 160 | |
|
161 | 161 | def column_header(column) |
|
162 | 162 | column.sortable ? sort_header_tag(column.name.to_s, :caption => column.caption, |
|
163 | 163 | :default_order => column.default_order) : |
|
164 | 164 | content_tag('th', h(column.caption)) |
|
165 | 165 | end |
|
166 | 166 | |
|
167 | 167 | def column_content(column, item) |
|
168 | 168 | value = column.value_object(item) |
|
169 | 169 | if value.is_a?(Array) |
|
170 | 170 | value.collect {|v| column_value(column, item, v)}.compact.join(', ').html_safe |
|
171 | 171 | else |
|
172 | 172 | column_value(column, item, value) |
|
173 | 173 | end |
|
174 | 174 | end |
|
175 | 175 | |
|
176 | 176 | def column_value(column, item, value) |
|
177 | 177 | case column.name |
|
178 | 178 | when :id |
|
179 | 179 | link_to value, issue_path(item) |
|
180 | 180 | when :subject |
|
181 | 181 | link_to value, issue_path(item) |
|
182 | 182 | when :parent |
|
183 | 183 | value ? (value.visible? ? link_to_issue(value, :subject => false) : "##{value.id}") : '' |
|
184 | 184 | when :description |
|
185 | 185 | item.description? ? content_tag('div', textilizable(item, :description), :class => "wiki") : '' |
|
186 | 186 | when :done_ratio |
|
187 | 187 | progress_bar(value) |
|
188 | 188 | when :relations |
|
189 | 189 | content_tag('span', |
|
190 | 190 | value.to_s(item) {|other| link_to_issue(other, :subject => false, :tracker => false)}.html_safe, |
|
191 | 191 | :class => value.css_classes_for(item)) |
|
192 | 192 | when :hours, :spent_hours, :total_spent_hours, :estimated_hours |
|
193 | 193 | format_hours(value) |
|
194 | 194 | else |
|
195 | 195 | format_object(value) |
|
196 | 196 | end |
|
197 | 197 | end |
|
198 | 198 | |
|
199 | 199 | def csv_content(column, item) |
|
200 | 200 | value = column.value_object(item) |
|
201 | 201 | if value.is_a?(Array) |
|
202 | 202 | value.collect {|v| csv_value(column, item, v)}.compact.join(', ') |
|
203 | 203 | else |
|
204 | 204 | csv_value(column, item, value) |
|
205 | 205 | end |
|
206 | 206 | end |
|
207 | 207 | |
|
208 | 208 | def csv_value(column, object, value) |
|
209 | 209 | format_object(value, false) do |value| |
|
210 | 210 | case value.class.name |
|
211 | 211 | when 'Float' |
|
212 | 212 | sprintf("%.2f", value).gsub('.', l(:general_csv_decimal_separator)) |
|
213 | 213 | when 'IssueRelation' |
|
214 | 214 | value.to_s(object) |
|
215 | 215 | when 'Issue' |
|
216 | 216 | if object.is_a?(TimeEntry) |
|
217 | 217 | "#{value.tracker} ##{value.id}: #{value.subject}" |
|
218 | 218 | else |
|
219 | 219 | value.id |
|
220 | 220 | end |
|
221 | 221 | else |
|
222 | 222 | value |
|
223 | 223 | end |
|
224 | 224 | end |
|
225 | 225 | end |
|
226 | 226 | |
|
227 | 227 | def query_to_csv(items, query, options={}) |
|
228 | 228 | options ||= {} |
|
229 | 229 | columns = (options[:columns] == 'all' ? query.available_inline_columns : query.inline_columns) |
|
230 | 230 | query.available_block_columns.each do |column| |
|
231 | 231 | if options[column.name].present? |
|
232 | 232 | columns << column |
|
233 | 233 | end |
|
234 | 234 | end |
|
235 | 235 | |
|
236 | 236 | Redmine::Export::CSV.generate do |csv| |
|
237 | 237 | # csv header fields |
|
238 | 238 | csv << columns.map {|c| c.caption.to_s} |
|
239 | 239 | # csv lines |
|
240 | 240 | items.each do |item| |
|
241 | 241 | csv << columns.map {|c| csv_content(c, item)} |
|
242 | 242 | end |
|
243 | 243 | end |
|
244 | 244 | end |
|
245 | 245 | |
|
246 | 246 | # Retrieve query from session or build a new query |
|
247 | 247 | def retrieve_query(klass=IssueQuery, use_session=true) |
|
248 | 248 | session_key = klass.name.underscore.to_sym |
|
249 | 249 | |
|
250 | 250 | if params[:query_id].present? |
|
251 | 251 | cond = "project_id IS NULL" |
|
252 | 252 | cond << " OR project_id = #{@project.id}" if @project |
|
253 | 253 | @query = klass.where(cond).find(params[:query_id]) |
|
254 | 254 | raise ::Unauthorized unless @query.visible? |
|
255 | 255 | @query.project = @project |
|
256 | 256 | session[session_key] = {:id => @query.id, :project_id => @query.project_id} if use_session |
|
257 | 257 | sort_clear |
|
258 | 258 | elsif api_request? || params[:set_filter] || !use_session || session[session_key].nil? || session[session_key][:project_id] != (@project ? @project.id : nil) |
|
259 | 259 | # Give it a name, required to be valid |
|
260 | 260 | @query = klass.new(:name => "_", :project => @project) |
|
261 | 261 | @query.build_from_params(params) |
|
262 | 262 | session[session_key] = {:project_id => @query.project_id, :filters => @query.filters, :group_by => @query.group_by, :column_names => @query.column_names, :totalable_names => @query.totalable_names} if use_session |
|
263 | 263 | else |
|
264 | 264 | # retrieve from session |
|
265 | 265 | @query = nil |
|
266 | 266 | @query = klass.find_by_id(session[session_key][:id]) if session[session_key][:id] |
|
267 | 267 | @query ||= klass.new(:name => "_", :filters => session[session_key][:filters], :group_by => session[session_key][:group_by], :column_names => session[session_key][:column_names], :totalable_names => session[session_key][:totalable_names]) |
|
268 | 268 | @query.project = @project |
|
269 | 269 | end |
|
270 | 270 | end |
|
271 | 271 | |
|
272 | 272 | def retrieve_query_from_session(klass=IssueQuery) |
|
273 | 273 | session_key = klass.name.underscore.to_sym |
|
274 | 274 | session_data = session[session_key] |
|
275 | 275 | |
|
276 | 276 | if session_data |
|
277 | 277 | if session_data[:id] |
|
278 | 278 | @query = IssueQuery.find_by_id(session_data[:id]) |
|
279 | 279 | return unless @query |
|
280 | 280 | else |
|
281 | 281 | @query = IssueQuery.new(:name => "_", :filters => session_data[:filters], :group_by => session_data[:group_by], :column_names => session_data[:column_names], :totalable_names => session_data[:totalable_names]) |
|
282 | 282 | end |
|
283 | 283 | if session_data.has_key?(:project_id) |
|
284 | 284 | @query.project_id = session_data[:project_id] |
|
285 | 285 | else |
|
286 | 286 | @query.project = @project |
|
287 | 287 | end |
|
288 | 288 | @query |
|
289 | 289 | end |
|
290 | 290 | end |
|
291 | 291 | |
|
292 | 292 | # Returns the query definition as hidden field tags |
|
293 | 293 | def query_as_hidden_field_tags(query) |
|
294 | 294 | tags = hidden_field_tag("set_filter", "1", :id => nil) |
|
295 | 295 | |
|
296 | 296 | if query.filters.present? |
|
297 | 297 | query.filters.each do |field, filter| |
|
298 | 298 | tags << hidden_field_tag("f[]", field, :id => nil) |
|
299 | 299 | tags << hidden_field_tag("op[#{field}]", filter[:operator], :id => nil) |
|
300 | 300 | filter[:values].each do |value| |
|
301 | 301 | tags << hidden_field_tag("v[#{field}][]", value, :id => nil) |
|
302 | 302 | end |
|
303 | 303 | end |
|
304 | 304 | else |
|
305 | 305 | tags << hidden_field_tag("f[]", "", :id => nil) |
|
306 | 306 | end |
|
307 | 307 | if query.column_names.present? |
|
308 | 308 | query.column_names.each do |name| |
|
309 | 309 | tags << hidden_field_tag("c[]", name, :id => nil) |
|
310 | 310 | end |
|
311 | 311 | end |
|
312 | 312 | if query.totalable_names.present? |
|
313 | 313 | query.totalable_names.each do |name| |
|
314 | 314 | tags << hidden_field_tag("t[]", name, :id => nil) |
|
315 | 315 | end |
|
316 | 316 | end |
|
317 | 317 | if query.group_by.present? |
|
318 | 318 | tags << hidden_field_tag("group_by", query.group_by, :id => nil) |
|
319 | 319 | end |
|
320 | 320 | |
|
321 | 321 | tags |
|
322 | 322 | end |
|
323 | 323 | |
|
324 | 324 | # Returns the queries that are rendered in the sidebar |
|
325 | 325 | def sidebar_queries(klass, project) |
|
326 | 326 | klass.visible.global_or_on_project(@project).sorted.to_a |
|
327 | 327 | end |
|
328 | 328 | |
|
329 | 329 | # Renders a group of queries |
|
330 | 330 | def query_links(title, queries) |
|
331 | 331 | return '' if queries.empty? |
|
332 | 332 | # links to #index on issues/show |
|
333 | 333 | url_params = controller_name == 'issues' ? {:controller => 'issues', :action => 'index', :project_id => @project} : {} |
|
334 | 334 | |
|
335 | 335 | content_tag('h3', title) + "\n" + |
|
336 | 336 | content_tag('ul', |
|
337 | 337 | queries.collect {|query| |
|
338 | 338 | css = 'query' |
|
339 | 339 | css << ' selected' if query == @query |
|
340 | 340 | content_tag('li', link_to(query.name, url_params.merge(:query_id => query), :class => css)) |
|
341 | 341 | }.join("\n").html_safe, |
|
342 | 342 | :class => 'queries' |
|
343 | 343 | ) + "\n" |
|
344 | 344 | end |
|
345 | 345 | |
|
346 | 346 | # Renders the list of queries for the sidebar |
|
347 | 347 | def render_sidebar_queries(klass, project) |
|
348 | 348 | queries = sidebar_queries(klass, project) |
|
349 | 349 | |
|
350 | 350 | out = ''.html_safe |
|
351 | 351 | out << query_links(l(:label_my_queries), queries.select(&:is_private?)) |
|
352 | 352 | out << query_links(l(:label_query_plural), queries.reject(&:is_private?)) |
|
353 | 353 | out |
|
354 | 354 | end |
|
355 | 355 | end |
General Comments 0
You need to be logged in to leave comments.
Login now