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