@@ -0,0 +1,59 | |||
|
1 | # encoding: utf-8 | |
|
2 | # | |
|
3 | # Redmine - project management software | |
|
4 | # Copyright (C) 2006-2015 Jean-Philippe Lang | |
|
5 | # | |
|
6 | # This program is free software; you can redistribute it and/or | |
|
7 | # modify it under the terms of the GNU General Public License | |
|
8 | # as published by the Free Software Foundation; either version 2 | |
|
9 | # of the License, or (at your option) any later version. | |
|
10 | # | |
|
11 | # This program is distributed in the hope that it will be useful, | |
|
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
|
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
|
14 | # GNU General Public License for more details. | |
|
15 | # | |
|
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 | |
|
18 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
|
19 | ||
|
20 | require 'csv' | |
|
21 | ||
|
22 | module Redmine | |
|
23 | module Export | |
|
24 | module CSV | |
|
25 | def self.generate(*args, &block) | |
|
26 | Base.generate(*args, &block) | |
|
27 | end | |
|
28 | ||
|
29 | class Base < ::CSV | |
|
30 | include Redmine::I18n | |
|
31 | ||
|
32 | class << self | |
|
33 | ||
|
34 | def generate(&block) | |
|
35 | col_sep = l(:general_csv_separator) | |
|
36 | encoding = l(:general_csv_encoding) | |
|
37 | ||
|
38 | super(:col_sep => col_sep, :encoding => encoding, &block) | |
|
39 | end | |
|
40 | end | |
|
41 | ||
|
42 | def <<(row) | |
|
43 | row = row.map do |field| | |
|
44 | case field | |
|
45 | when String | |
|
46 | Redmine::CodesetUtil.from_utf8(field, self.encoding.name) | |
|
47 | when Float | |
|
48 | @decimal_separator ||= l(:general_csv_decimal_separator) | |
|
49 | ("%.2f" % field).gsub('.', @decimal_separator) | |
|
50 | else | |
|
51 | field | |
|
52 | end | |
|
53 | end | |
|
54 | super row | |
|
55 | end | |
|
56 | end | |
|
57 | end | |
|
58 | end | |
|
59 | end |
@@ -1307,6 +1307,11 module ApplicationHelper | |||
|
1307 | 1307 | end |
|
1308 | 1308 | end |
|
1309 | 1309 | |
|
1310 | def generate_csv(&block) | |
|
1311 | decimal_separator = l(:general_csv_decimal_separator) | |
|
1312 | encoding = l(:general_csv_encoding) | |
|
1313 | end | |
|
1314 | ||
|
1310 | 1315 | private |
|
1311 | 1316 | |
|
1312 | 1317 | def wiki_helper |
@@ -162,7 +162,6 module QueriesHelper | |||
|
162 | 162 | end |
|
163 | 163 | |
|
164 | 164 | def query_to_csv(items, query, options={}) |
|
165 | encoding = l(:general_csv_encoding) | |
|
166 | 165 | columns = (options[:columns] == 'all' ? query.available_inline_columns : query.inline_columns) |
|
167 | 166 | query.available_block_columns.each do |column| |
|
168 | 167 | if options[column.name].present? |
@@ -170,15 +169,14 module QueriesHelper | |||
|
170 | 169 | end |
|
171 | 170 | end |
|
172 | 171 | |
|
173 | export = CSV.generate(:col_sep => l(:general_csv_separator)) do |csv| | |
|
172 | Redmine::Export::CSV.generate do |csv| | |
|
174 | 173 | # csv header fields |
|
175 |
csv << columns. |
|
|
174 | csv << columns.map {|c| c.caption.to_s} | |
|
176 | 175 | # csv lines |
|
177 | 176 | items.each do |item| |
|
178 |
csv << columns. |
|
|
177 | csv << columns.map {|c| csv_content(c, item)} | |
|
179 | 178 | end |
|
180 | 179 | end |
|
181 | export | |
|
182 | 180 | end |
|
183 | 181 | |
|
184 | 182 | # Retrieve query from session or build a new query |
@@ -90,49 +90,42 module TimelogHelper | |||
|
90 | 90 | end |
|
91 | 91 | |
|
92 | 92 | def report_to_csv(report) |
|
93 | decimal_separator = l(:general_csv_decimal_separator) | |
|
94 | export = CSV.generate(:col_sep => l(:general_csv_separator)) do |csv| | |
|
93 | Redmine::Export::CSV.generate do |csv| | |
|
95 | 94 | # Column headers |
|
96 | 95 | headers = report.criteria.collect {|criteria| l(report.available_criteria[criteria][:label]) } |
|
97 | 96 | headers += report.periods |
|
98 | 97 | headers << l(:label_total_time) |
|
99 | csv << headers.collect {|c| Redmine::CodesetUtil.from_utf8( | |
|
100 | c.to_s, | |
|
101 | l(:general_csv_encoding) ) } | |
|
98 | csv << headers | |
|
102 | 99 | # Content |
|
103 | 100 | report_criteria_to_csv(csv, report.available_criteria, report.columns, report.criteria, report.periods, report.hours) |
|
104 | 101 | # Total row |
|
105 | str_total = Redmine::CodesetUtil.from_utf8(l(:label_total_time), l(:general_csv_encoding)) | |
|
102 | str_total = l(:label_total_time) | |
|
106 | 103 | row = [ str_total ] + [''] * (report.criteria.size - 1) |
|
107 | 104 | total = 0 |
|
108 | 105 | report.periods.each do |period| |
|
109 | 106 | sum = sum_hours(select_hours(report.hours, report.columns, period.to_s)) |
|
110 | 107 | total += sum |
|
111 |
row << (sum > 0 ? |
|
|
108 | row << (sum > 0 ? sum : '') | |
|
112 | 109 | end |
|
113 | row << ("%.2f" % total).gsub('.',decimal_separator) | |
|
110 | row << total | |
|
114 | 111 | csv << row |
|
115 | 112 | end |
|
116 | export | |
|
117 | 113 | end |
|
118 | 114 | |
|
119 | 115 | def report_criteria_to_csv(csv, available_criteria, columns, criteria, periods, hours, level=0) |
|
120 | decimal_separator = l(:general_csv_decimal_separator) | |
|
121 | 116 | hours.collect {|h| h[criteria[level]].to_s}.uniq.each do |value| |
|
122 | 117 | hours_for_value = select_hours(hours, criteria[level], value) |
|
123 | 118 | next if hours_for_value.empty? |
|
124 | 119 | row = [''] * level |
|
125 | row << Redmine::CodesetUtil.from_utf8( | |
|
126 | format_criteria_value(available_criteria[criteria[level]], value).to_s, | |
|
127 | l(:general_csv_encoding) ) | |
|
120 | row << format_criteria_value(available_criteria[criteria[level]], value).to_s | |
|
128 | 121 | row += [''] * (criteria.length - level - 1) |
|
129 | 122 | total = 0 |
|
130 | 123 | periods.each do |period| |
|
131 | 124 | sum = sum_hours(select_hours(hours_for_value, columns, period.to_s)) |
|
132 | 125 | total += sum |
|
133 |
row << (sum > 0 ? |
|
|
126 | row << (sum > 0 ? sum : '') | |
|
134 | 127 | end |
|
135 | row << ("%.2f" % total).gsub('.',decimal_separator) | |
|
128 | row << total | |
|
136 | 129 | csv << row |
|
137 | 130 | if criteria.length > level + 1 |
|
138 | 131 | report_criteria_to_csv(csv, available_criteria, columns, criteria, periods, hours_for_value, level + 1) |
General Comments 0
You need to be logged in to leave comments.
Login now