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