Mercurial > hg > soundsoftware-site
comparison app/helpers/queries_helper.rb @ 1295:622f24f53b42 redmine-2.3
Update to Redmine SVN revision 11972 on 2.3-stable branch
author | Chris Cannam |
---|---|
date | Fri, 14 Jun 2013 09:02:21 +0100 |
parents | 3e4c3460b6ca |
children |
comparison
equal
deleted
inserted
replaced
1294:3e4c3460b6ca | 1295:622f24f53b42 |
---|---|
1 # encoding: utf-8 | 1 # encoding: utf-8 |
2 # | 2 # |
3 # Redmine - project management software | 3 # Redmine - project management software |
4 # Copyright (C) 2006-2012 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. |
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 sorted_options = query.available_filters.sort do |a, b| | 27 options += query.available_filters.map do |field, field_options| |
28 ord = 0 | |
29 if !(a[1][:order] == 20 && b[1][:order] == 20) | |
30 ord = a[1][:order] <=> b[1][:order] | |
31 else | |
32 cn = (CustomField::CUSTOM_FIELDS_NAMES.index(a[1][:field].class.name) <=> | |
33 CustomField::CUSTOM_FIELDS_NAMES.index(b[1][:field].class.name)) | |
34 if cn != 0 | |
35 ord = cn | |
36 else | |
37 f = (a[1][:field] <=> b[1][:field]) | |
38 if f != 0 | |
39 ord = f | |
40 else | |
41 # assigned_to or author | |
42 ord = (a[0] <=> b[0]) | |
43 end | |
44 end | |
45 end | |
46 ord | |
47 end | |
48 options += sorted_options.map do |field, field_options| | |
49 [field_options[:name], field] | 28 [field_options[:name], field] |
50 end | 29 end |
30 end | |
31 | |
32 def query_filters_hidden_tags(query) | |
33 tags = ''.html_safe | |
34 query.filters.each do |field, options| | |
35 tags << hidden_field_tag("f[]", field, :id => nil) | |
36 tags << hidden_field_tag("op[#{field}]", options[:operator], :id => nil) | |
37 options[:values].each do |value| | |
38 tags << hidden_field_tag("v[#{field}][]", value, :id => nil) | |
39 end | |
40 end | |
41 tags | |
42 end | |
43 | |
44 def query_columns_hidden_tags(query) | |
45 tags = ''.html_safe | |
46 query.columns.each do |column| | |
47 tags << hidden_field_tag("c[]", column.name, :id => nil) | |
48 end | |
49 tags | |
50 end | |
51 | |
52 def query_hidden_tags(query) | |
53 query_filters_hidden_tags(query) + query_columns_hidden_tags(query) | |
51 end | 54 end |
52 | 55 |
53 def available_block_columns_tags(query) | 56 def available_block_columns_tags(query) |
54 tags = ''.html_safe | 57 tags = ''.html_safe |
55 query.available_block_columns.each do |column| | 58 query.available_block_columns.each do |column| |
56 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)) + " #{column.caption}", :class => 'inline') |
57 end | 60 end |
58 tags | 61 tags |
62 end | |
63 | |
64 def query_available_inline_columns_options(query) | |
65 (query.available_inline_columns - query.columns).reject(&:frozen?).collect {|column| [column.caption, column.name]} | |
66 end | |
67 | |
68 def query_selected_inline_columns_options(query) | |
69 (query.inline_columns & query.available_inline_columns).reject(&:frozen?).collect {|column| [column.caption, column.name]} | |
70 end | |
71 | |
72 def render_query_columns_selection(query, options={}) | |
73 tag_name = (options[:name] || 'c') + '[]' | |
74 render :partial => 'queries/columns', :locals => {:query => query, :tag_name => tag_name} | |
59 end | 75 end |
60 | 76 |
61 def column_header(column) | 77 def column_header(column) |
62 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, |
63 :default_order => column.default_order) : | 79 :default_order => column.default_order) : |
86 when 'Time' | 102 when 'Time' |
87 format_time(value) | 103 format_time(value) |
88 when 'Date' | 104 when 'Date' |
89 format_date(value) | 105 format_date(value) |
90 when 'Fixnum' | 106 when 'Fixnum' |
91 if column.name == :done_ratio | 107 if column.name == :id |
108 link_to value, issue_path(issue) | |
109 elsif column.name == :done_ratio | |
92 progress_bar(value, :width => '80px') | 110 progress_bar(value, :width => '80px') |
93 else | 111 else |
94 value.to_s | 112 value.to_s |
95 end | 113 end |
96 when 'Float' | 114 when 'Float' |
104 when 'TrueClass' | 122 when 'TrueClass' |
105 l(:general_text_Yes) | 123 l(:general_text_Yes) |
106 when 'FalseClass' | 124 when 'FalseClass' |
107 l(:general_text_No) | 125 l(:general_text_No) |
108 when 'Issue' | 126 when 'Issue' |
109 link_to_issue(value, :subject => false) | 127 value.visible? ? link_to_issue(value) : "##{value.id}" |
110 when 'IssueRelation' | 128 when 'IssueRelation' |
111 other = value.other_issue(issue) | 129 other = value.other_issue(issue) |
112 content_tag('span', | 130 content_tag('span', |
113 (l(value.label_for(issue)) + " " + link_to_issue(other, :subject => false, :tracker => false)).html_safe, | 131 (l(value.label_for(issue)) + " " + link_to_issue(other, :subject => false, :tracker => false)).html_safe, |
114 :class => value.css_classes_for(issue)) | 132 :class => value.css_classes_for(issue)) |
115 else | 133 else |
116 h(value) | 134 h(value) |
117 end | 135 end |
118 end | 136 end |
119 | 137 |
138 def csv_content(column, issue) | |
139 value = column.value(issue) | |
140 if value.is_a?(Array) | |
141 value.collect {|v| csv_value(column, issue, v)}.compact.join(', ') | |
142 else | |
143 csv_value(column, issue, value) | |
144 end | |
145 end | |
146 | |
147 def csv_value(column, issue, value) | |
148 case value.class.name | |
149 when 'Time' | |
150 format_time(value) | |
151 when 'Date' | |
152 format_date(value) | |
153 when 'Float' | |
154 sprintf("%.2f", value).gsub('.', l(:general_csv_decimal_separator)) | |
155 when 'IssueRelation' | |
156 other = value.other_issue(issue) | |
157 l(value.label_for(issue)) + " ##{other.id}" | |
158 else | |
159 value.to_s | |
160 end | |
161 end | |
162 | |
163 def query_to_csv(items, query, options={}) | |
164 encoding = l(:general_csv_encoding) | |
165 columns = (options[:columns] == 'all' ? query.available_inline_columns : query.inline_columns) | |
166 query.available_block_columns.each do |column| | |
167 if options[column.name].present? | |
168 columns << column | |
169 end | |
170 end | |
171 | |
172 export = FCSV.generate(:col_sep => l(:general_csv_separator)) do |csv| | |
173 # csv header fields | |
174 csv << columns.collect {|c| Redmine::CodesetUtil.from_utf8(c.caption.to_s, encoding) } | |
175 # csv lines | |
176 items.each do |item| | |
177 csv << columns.collect {|c| Redmine::CodesetUtil.from_utf8(csv_content(c, item), encoding) } | |
178 end | |
179 end | |
180 export | |
181 end | |
182 | |
120 # Retrieve query from session or build a new query | 183 # Retrieve query from session or build a new query |
121 def retrieve_query | 184 def retrieve_query |
122 if !params[:query_id].blank? | 185 if !params[:query_id].blank? |
123 cond = "project_id IS NULL" | 186 cond = "project_id IS NULL" |
124 cond << " OR project_id = #{@project.id}" if @project | 187 cond << " OR project_id = #{@project.id}" if @project |
125 @query = Query.find(params[:query_id], :conditions => cond) | 188 @query = IssueQuery.find(params[:query_id], :conditions => cond) |
126 raise ::Unauthorized unless @query.visible? | 189 raise ::Unauthorized unless @query.visible? |
127 @query.project = @project | 190 @query.project = @project |
128 session[:query] = {:id => @query.id, :project_id => @query.project_id} | 191 session[:query] = {:id => @query.id, :project_id => @query.project_id} |
129 sort_clear | 192 sort_clear |
130 elsif api_request? || params[:set_filter] || session[:query].nil? || session[:query][:project_id] != (@project ? @project.id : nil) | 193 elsif api_request? || params[:set_filter] || session[:query].nil? || session[:query][:project_id] != (@project ? @project.id : nil) |
131 # Give it a name, required to be valid | 194 # Give it a name, required to be valid |
132 @query = Query.new(:name => "_") | 195 @query = IssueQuery.new(:name => "_") |
133 @query.project = @project | 196 @query.project = @project |
134 build_query_from_params | 197 @query.build_from_params(params) |
135 session[:query] = {:project_id => @query.project_id, :filters => @query.filters, :group_by => @query.group_by, :column_names => @query.column_names} | 198 session[:query] = {:project_id => @query.project_id, :filters => @query.filters, :group_by => @query.group_by, :column_names => @query.column_names} |
136 else | 199 else |
137 # retrieve from session | 200 # retrieve from session |
138 @query = Query.find_by_id(session[:query][:id]) if session[:query][:id] | 201 @query = IssueQuery.find_by_id(session[:query][:id]) if session[:query][:id] |
139 @query ||= Query.new(:name => "_", :filters => session[:query][:filters], :group_by => session[:query][:group_by], :column_names => session[:query][:column_names]) | 202 @query ||= IssueQuery.new(:name => "_", :filters => session[:query][:filters], :group_by => session[:query][:group_by], :column_names => session[:query][:column_names]) |
140 @query.project = @project | 203 @query.project = @project |
141 end | 204 end |
142 end | 205 end |
143 | 206 |
144 def retrieve_query_from_session | 207 def retrieve_query_from_session |
145 if session[:query] | 208 if session[:query] |
146 if session[:query][:id] | 209 if session[:query][:id] |
147 @query = Query.find_by_id(session[:query][:id]) | 210 @query = IssueQuery.find_by_id(session[:query][:id]) |
148 return unless @query | 211 return unless @query |
149 else | 212 else |
150 @query = Query.new(:name => "_", :filters => session[:query][:filters], :group_by => session[:query][:group_by], :column_names => session[:query][:column_names]) | 213 @query = IssueQuery.new(:name => "_", :filters => session[:query][:filters], :group_by => session[:query][:group_by], :column_names => session[:query][:column_names]) |
151 end | 214 end |
152 if session[:query].has_key?(:project_id) | 215 if session[:query].has_key?(:project_id) |
153 @query.project_id = session[:query][:project_id] | 216 @query.project_id = session[:query][:project_id] |
154 else | 217 else |
155 @query.project = @project | 218 @query.project = @project |
156 end | 219 end |
157 @query | 220 @query |
158 end | 221 end |
159 end | 222 end |
160 | |
161 def build_query_from_params | |
162 if params[:fields] || params[:f] | |
163 @query.filters = {} | |
164 @query.add_filters(params[:fields] || params[:f], params[:operators] || params[:op], params[:values] || params[:v]) | |
165 else | |
166 @query.available_filters.keys.each do |field| | |
167 @query.add_short_filter(field, params[field]) if params[field] | |
168 end | |
169 end | |
170 @query.group_by = params[:group_by] || (params[:query] && params[:query][:group_by]) | |
171 @query.column_names = params[:c] || (params[:query] && params[:query][:column_names]) | |
172 end | |
173 end | 223 end |