@@ -1,198 +1,198 | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 | # |
|
2 | # | |
3 | # Redmine - project management software |
|
3 | # Redmine - project management software | |
4 | # Copyright (C) 2006-2013 Jean-Philippe Lang |
|
4 | # Copyright (C) 2006-2013 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 | def filters_options_for_select(query) |
|
21 | def filters_options_for_select(query) | |
22 | options_for_select(filters_options(query)) |
|
22 | options_for_select(filters_options(query)) | |
23 | end |
|
23 | end | |
24 |
|
24 | |||
25 | def filters_options(query) |
|
25 | def filters_options(query) | |
26 | options = [[]] |
|
26 | options = [[]] | |
27 | options += query.available_filters.map do |field, field_options| |
|
27 | options += query.available_filters.map do |field, field_options| | |
28 | [field_options[:name], field] |
|
28 | [field_options[:name], field] | |
29 | end |
|
29 | end | |
30 | end |
|
30 | end | |
31 |
|
31 | |||
32 | def query_filters_hidden_tags(query) |
|
32 | def query_filters_hidden_tags(query) | |
33 | tags = ''.html_safe |
|
33 | tags = ''.html_safe | |
34 | query.filters.each do |field, options| |
|
34 | query.filters.each do |field, options| | |
35 | tags << hidden_field_tag("f[]", field, :id => nil) |
|
35 | tags << hidden_field_tag("f[]", field, :id => nil) | |
36 | tags << hidden_field_tag("op[#{field}]", options[:operator], :id => nil) |
|
36 | tags << hidden_field_tag("op[#{field}]", options[:operator], :id => nil) | |
37 | options[:values].each do |value| |
|
37 | options[:values].each do |value| | |
38 | tags << hidden_field_tag("v[#{field}][]", value, :id => nil) |
|
38 | tags << hidden_field_tag("v[#{field}][]", value, :id => nil) | |
39 | end |
|
39 | end | |
40 | end |
|
40 | end | |
41 | tags |
|
41 | tags | |
42 | end |
|
42 | end | |
43 |
|
43 | |||
44 | def query_columns_hidden_tags(query) |
|
44 | def query_columns_hidden_tags(query) | |
45 | tags = ''.html_safe |
|
45 | tags = ''.html_safe | |
46 | query.columns.each do |column| |
|
46 | query.columns.each do |column| | |
47 | tags << hidden_field_tag("c[]", column.name, :id => nil) |
|
47 | tags << hidden_field_tag("c[]", column.name, :id => nil) | |
48 | end |
|
48 | end | |
49 | tags |
|
49 | tags | |
50 | end |
|
50 | end | |
51 |
|
51 | |||
52 | def query_hidden_tags(query) |
|
52 | def query_hidden_tags(query) | |
53 | query_filters_hidden_tags(query) + query_columns_hidden_tags(query) |
|
53 | query_filters_hidden_tags(query) + query_columns_hidden_tags(query) | |
54 | end |
|
54 | end | |
55 |
|
55 | |||
56 | def available_block_columns_tags(query) |
|
56 | def available_block_columns_tags(query) | |
57 | tags = ''.html_safe |
|
57 | tags = ''.html_safe | |
58 | query.available_block_columns.each do |column| |
|
58 | query.available_block_columns.each do |column| | |
59 | tags << content_tag('label', check_box_tag('c[]', column.name.to_s, query.has_column?(column)) + " #{column.caption}", :class => 'inline') |
|
59 | tags << content_tag('label', check_box_tag('c[]', column.name.to_s, query.has_column?(column), :id => nil) + " #{column.caption}", :class => 'inline') | |
60 | end |
|
60 | end | |
61 | tags |
|
61 | tags | |
62 | end |
|
62 | end | |
63 |
|
63 | |||
64 | def query_available_inline_columns_options(query) |
|
64 | def query_available_inline_columns_options(query) | |
65 | (query.available_inline_columns - query.columns).reject(&:frozen?).collect {|column| [column.caption, column.name]} |
|
65 | (query.available_inline_columns - query.columns).reject(&:frozen?).collect {|column| [column.caption, column.name]} | |
66 | end |
|
66 | end | |
67 |
|
67 | |||
68 | def query_selected_inline_columns_options(query) |
|
68 | def query_selected_inline_columns_options(query) | |
69 | (query.inline_columns & query.available_inline_columns).reject(&:frozen?).collect {|column| [column.caption, column.name]} |
|
69 | (query.inline_columns & query.available_inline_columns).reject(&:frozen?).collect {|column| [column.caption, column.name]} | |
70 | end |
|
70 | end | |
71 |
|
71 | |||
72 | def render_query_columns_selection(query, options={}) |
|
72 | def render_query_columns_selection(query, options={}) | |
73 | tag_name = (options[:name] || 'c') + '[]' |
|
73 | tag_name = (options[:name] || 'c') + '[]' | |
74 | render :partial => 'queries/columns', :locals => {:query => query, :tag_name => tag_name} |
|
74 | render :partial => 'queries/columns', :locals => {:query => query, :tag_name => tag_name} | |
75 | end |
|
75 | end | |
76 |
|
76 | |||
77 | def column_header(column) |
|
77 | def column_header(column) | |
78 | column.sortable ? sort_header_tag(column.name.to_s, :caption => column.caption, |
|
78 | column.sortable ? sort_header_tag(column.name.to_s, :caption => column.caption, | |
79 | :default_order => column.default_order) : |
|
79 | :default_order => column.default_order) : | |
80 | content_tag('th', h(column.caption)) |
|
80 | content_tag('th', h(column.caption)) | |
81 | end |
|
81 | end | |
82 |
|
82 | |||
83 | def column_content(column, issue) |
|
83 | def column_content(column, issue) | |
84 | value = column.value(issue) |
|
84 | value = column.value(issue) | |
85 | if value.is_a?(Array) |
|
85 | if value.is_a?(Array) | |
86 | value.collect {|v| column_value(column, issue, v)}.compact.join(', ').html_safe |
|
86 | value.collect {|v| column_value(column, issue, v)}.compact.join(', ').html_safe | |
87 | else |
|
87 | else | |
88 | column_value(column, issue, value) |
|
88 | column_value(column, issue, value) | |
89 | end |
|
89 | end | |
90 | end |
|
90 | end | |
91 |
|
91 | |||
92 | def column_value(column, issue, value) |
|
92 | def column_value(column, issue, value) | |
93 | case column.name |
|
93 | case column.name | |
94 | when :id |
|
94 | when :id | |
95 | link_to value, issue_path(issue) |
|
95 | link_to value, issue_path(issue) | |
96 | when :subject |
|
96 | when :subject | |
97 | link_to value, issue_path(issue) |
|
97 | link_to value, issue_path(issue) | |
98 | when :description |
|
98 | when :description | |
99 | issue.description? ? content_tag('div', textilizable(issue, :description), :class => "wiki") : '' |
|
99 | issue.description? ? content_tag('div', textilizable(issue, :description), :class => "wiki") : '' | |
100 | when :done_ratio |
|
100 | when :done_ratio | |
101 | progress_bar(value, :width => '80px') |
|
101 | progress_bar(value, :width => '80px') | |
102 | when :relations |
|
102 | when :relations | |
103 | other = value.other_issue(issue) |
|
103 | other = value.other_issue(issue) | |
104 | content_tag('span', |
|
104 | content_tag('span', | |
105 | (l(value.label_for(issue)) + " " + link_to_issue(other, :subject => false, :tracker => false)).html_safe, |
|
105 | (l(value.label_for(issue)) + " " + link_to_issue(other, :subject => false, :tracker => false)).html_safe, | |
106 | :class => value.css_classes_for(issue)) |
|
106 | :class => value.css_classes_for(issue)) | |
107 | else |
|
107 | else | |
108 | format_object(value) |
|
108 | format_object(value) | |
109 | end |
|
109 | end | |
110 | end |
|
110 | end | |
111 |
|
111 | |||
112 | def csv_content(column, issue) |
|
112 | def csv_content(column, issue) | |
113 | value = column.value(issue) |
|
113 | value = column.value(issue) | |
114 | if value.is_a?(Array) |
|
114 | if value.is_a?(Array) | |
115 | value.collect {|v| csv_value(column, issue, v)}.compact.join(', ') |
|
115 | value.collect {|v| csv_value(column, issue, v)}.compact.join(', ') | |
116 | else |
|
116 | else | |
117 | csv_value(column, issue, value) |
|
117 | csv_value(column, issue, value) | |
118 | end |
|
118 | end | |
119 | end |
|
119 | end | |
120 |
|
120 | |||
121 | def csv_value(column, issue, value) |
|
121 | def csv_value(column, issue, value) | |
122 | case value.class.name |
|
122 | case value.class.name | |
123 | when 'Time' |
|
123 | when 'Time' | |
124 | format_time(value) |
|
124 | format_time(value) | |
125 | when 'Date' |
|
125 | when 'Date' | |
126 | format_date(value) |
|
126 | format_date(value) | |
127 | when 'Float' |
|
127 | when 'Float' | |
128 | sprintf("%.2f", value).gsub('.', l(:general_csv_decimal_separator)) |
|
128 | sprintf("%.2f", value).gsub('.', l(:general_csv_decimal_separator)) | |
129 | when 'IssueRelation' |
|
129 | when 'IssueRelation' | |
130 | other = value.other_issue(issue) |
|
130 | other = value.other_issue(issue) | |
131 | l(value.label_for(issue)) + " ##{other.id}" |
|
131 | l(value.label_for(issue)) + " ##{other.id}" | |
132 | else |
|
132 | else | |
133 | value.to_s |
|
133 | value.to_s | |
134 | end |
|
134 | end | |
135 | end |
|
135 | end | |
136 |
|
136 | |||
137 | def query_to_csv(items, query, options={}) |
|
137 | def query_to_csv(items, query, options={}) | |
138 | encoding = l(:general_csv_encoding) |
|
138 | encoding = l(:general_csv_encoding) | |
139 | columns = (options[:columns] == 'all' ? query.available_inline_columns : query.inline_columns) |
|
139 | columns = (options[:columns] == 'all' ? query.available_inline_columns : query.inline_columns) | |
140 | query.available_block_columns.each do |column| |
|
140 | query.available_block_columns.each do |column| | |
141 | if options[column.name].present? |
|
141 | if options[column.name].present? | |
142 | columns << column |
|
142 | columns << column | |
143 | end |
|
143 | end | |
144 | end |
|
144 | end | |
145 |
|
145 | |||
146 | export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv| |
|
146 | export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv| | |
147 | # csv header fields |
|
147 | # csv header fields | |
148 | csv << columns.collect {|c| Redmine::CodesetUtil.from_utf8(c.caption.to_s, encoding) } |
|
148 | csv << columns.collect {|c| Redmine::CodesetUtil.from_utf8(c.caption.to_s, encoding) } | |
149 | # csv lines |
|
149 | # csv lines | |
150 | items.each do |item| |
|
150 | items.each do |item| | |
151 | csv << columns.collect {|c| Redmine::CodesetUtil.from_utf8(csv_content(c, item), encoding) } |
|
151 | csv << columns.collect {|c| Redmine::CodesetUtil.from_utf8(csv_content(c, item), encoding) } | |
152 | end |
|
152 | end | |
153 | end |
|
153 | end | |
154 | export |
|
154 | export | |
155 | end |
|
155 | end | |
156 |
|
156 | |||
157 | # Retrieve query from session or build a new query |
|
157 | # Retrieve query from session or build a new query | |
158 | def retrieve_query |
|
158 | def retrieve_query | |
159 | if !params[:query_id].blank? |
|
159 | if !params[:query_id].blank? | |
160 | cond = "project_id IS NULL" |
|
160 | cond = "project_id IS NULL" | |
161 | cond << " OR project_id = #{@project.id}" if @project |
|
161 | cond << " OR project_id = #{@project.id}" if @project | |
162 | @query = IssueQuery.where(cond).find(params[:query_id]) |
|
162 | @query = IssueQuery.where(cond).find(params[:query_id]) | |
163 | raise ::Unauthorized unless @query.visible? |
|
163 | raise ::Unauthorized unless @query.visible? | |
164 | @query.project = @project |
|
164 | @query.project = @project | |
165 | session[:query] = {:id => @query.id, :project_id => @query.project_id} |
|
165 | session[:query] = {:id => @query.id, :project_id => @query.project_id} | |
166 | sort_clear |
|
166 | sort_clear | |
167 | elsif api_request? || params[:set_filter] || session[:query].nil? || session[:query][:project_id] != (@project ? @project.id : nil) |
|
167 | elsif api_request? || params[:set_filter] || session[:query].nil? || session[:query][:project_id] != (@project ? @project.id : nil) | |
168 | # Give it a name, required to be valid |
|
168 | # Give it a name, required to be valid | |
169 | @query = IssueQuery.new(:name => "_") |
|
169 | @query = IssueQuery.new(:name => "_") | |
170 | @query.project = @project |
|
170 | @query.project = @project | |
171 | @query.build_from_params(params) |
|
171 | @query.build_from_params(params) | |
172 | session[:query] = {:project_id => @query.project_id, :filters => @query.filters, :group_by => @query.group_by, :column_names => @query.column_names} |
|
172 | session[:query] = {:project_id => @query.project_id, :filters => @query.filters, :group_by => @query.group_by, :column_names => @query.column_names} | |
173 | else |
|
173 | else | |
174 | # retrieve from session |
|
174 | # retrieve from session | |
175 | @query = nil |
|
175 | @query = nil | |
176 | @query = IssueQuery.find_by_id(session[:query][:id]) if session[:query][:id] |
|
176 | @query = IssueQuery.find_by_id(session[:query][:id]) if session[:query][:id] | |
177 | @query ||= IssueQuery.new(:name => "_", :filters => session[:query][:filters], :group_by => session[:query][:group_by], :column_names => session[:query][:column_names]) |
|
177 | @query ||= IssueQuery.new(:name => "_", :filters => session[:query][:filters], :group_by => session[:query][:group_by], :column_names => session[:query][:column_names]) | |
178 | @query.project = @project |
|
178 | @query.project = @project | |
179 | end |
|
179 | end | |
180 | end |
|
180 | end | |
181 |
|
181 | |||
182 | def retrieve_query_from_session |
|
182 | def retrieve_query_from_session | |
183 | if session[:query] |
|
183 | if session[:query] | |
184 | if session[:query][:id] |
|
184 | if session[:query][:id] | |
185 | @query = IssueQuery.find_by_id(session[:query][:id]) |
|
185 | @query = IssueQuery.find_by_id(session[:query][:id]) | |
186 | return unless @query |
|
186 | return unless @query | |
187 | else |
|
187 | else | |
188 | @query = IssueQuery.new(:name => "_", :filters => session[:query][:filters], :group_by => session[:query][:group_by], :column_names => session[:query][:column_names]) |
|
188 | @query = IssueQuery.new(:name => "_", :filters => session[:query][:filters], :group_by => session[:query][:group_by], :column_names => session[:query][:column_names]) | |
189 | end |
|
189 | end | |
190 | if session[:query].has_key?(:project_id) |
|
190 | if session[:query].has_key?(:project_id) | |
191 | @query.project_id = session[:query][:project_id] |
|
191 | @query.project_id = session[:query][:project_id] | |
192 | else |
|
192 | else | |
193 | @query.project = @project |
|
193 | @query.project = @project | |
194 | end |
|
194 | end | |
195 | @query |
|
195 | @query | |
196 | end |
|
196 | end | |
197 | end |
|
197 | end | |
198 | end |
|
198 | end |
General Comments 0
You need to be logged in to leave comments.
Login now