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