@@ -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 | end |
|
1307 | end | |
1308 | end |
|
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 | private |
|
1315 | private | |
1311 |
|
1316 | |||
1312 | def wiki_helper |
|
1317 | def wiki_helper |
@@ -162,7 +162,6 module QueriesHelper | |||||
162 | end |
|
162 | end | |
163 |
|
163 | |||
164 | def query_to_csv(items, query, options={}) |
|
164 | def query_to_csv(items, query, options={}) | |
165 | encoding = l(:general_csv_encoding) |
|
|||
166 | columns = (options[:columns] == 'all' ? query.available_inline_columns : query.inline_columns) |
|
165 | columns = (options[:columns] == 'all' ? query.available_inline_columns : query.inline_columns) | |
167 | query.available_block_columns.each do |column| |
|
166 | query.available_block_columns.each do |column| | |
168 | if options[column.name].present? |
|
167 | if options[column.name].present? | |
@@ -170,15 +169,14 module QueriesHelper | |||||
170 | end |
|
169 | end | |
171 | end |
|
170 | end | |
172 |
|
171 | |||
173 | export = CSV.generate(:col_sep => l(:general_csv_separator)) do |csv| |
|
172 | Redmine::Export::CSV.generate do |csv| | |
174 | # csv header fields |
|
173 | # csv header fields | |
175 |
csv << columns. |
|
174 | csv << columns.map {|c| c.caption.to_s} | |
176 | # csv lines |
|
175 | # csv lines | |
177 | items.each do |item| |
|
176 | items.each do |item| | |
178 |
csv << columns. |
|
177 | csv << columns.map {|c| csv_content(c, item)} | |
179 | end |
|
178 | end | |
180 | end |
|
179 | end | |
181 | export |
|
|||
182 | end |
|
180 | end | |
183 |
|
181 | |||
184 | # Retrieve query from session or build a new query |
|
182 | # Retrieve query from session or build a new query |
@@ -90,49 +90,42 module TimelogHelper | |||||
90 | end |
|
90 | end | |
91 |
|
91 | |||
92 | def report_to_csv(report) |
|
92 | def report_to_csv(report) | |
93 | decimal_separator = l(:general_csv_decimal_separator) |
|
93 | Redmine::Export::CSV.generate do |csv| | |
94 | export = CSV.generate(:col_sep => l(:general_csv_separator)) do |csv| |
|
|||
95 | # Column headers |
|
94 | # Column headers | |
96 | headers = report.criteria.collect {|criteria| l(report.available_criteria[criteria][:label]) } |
|
95 | headers = report.criteria.collect {|criteria| l(report.available_criteria[criteria][:label]) } | |
97 | headers += report.periods |
|
96 | headers += report.periods | |
98 | headers << l(:label_total_time) |
|
97 | headers << l(:label_total_time) | |
99 | csv << headers.collect {|c| Redmine::CodesetUtil.from_utf8( |
|
98 | csv << headers | |
100 | c.to_s, |
|
|||
101 | l(:general_csv_encoding) ) } |
|
|||
102 | # Content |
|
99 | # Content | |
103 | report_criteria_to_csv(csv, report.available_criteria, report.columns, report.criteria, report.periods, report.hours) |
|
100 | report_criteria_to_csv(csv, report.available_criteria, report.columns, report.criteria, report.periods, report.hours) | |
104 | # Total row |
|
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 | row = [ str_total ] + [''] * (report.criteria.size - 1) |
|
103 | row = [ str_total ] + [''] * (report.criteria.size - 1) | |
107 | total = 0 |
|
104 | total = 0 | |
108 | report.periods.each do |period| |
|
105 | report.periods.each do |period| | |
109 | sum = sum_hours(select_hours(report.hours, report.columns, period.to_s)) |
|
106 | sum = sum_hours(select_hours(report.hours, report.columns, period.to_s)) | |
110 | total += sum |
|
107 | total += sum | |
111 |
row << (sum > 0 ? |
|
108 | row << (sum > 0 ? sum : '') | |
112 | end |
|
109 | end | |
113 | row << ("%.2f" % total).gsub('.',decimal_separator) |
|
110 | row << total | |
114 | csv << row |
|
111 | csv << row | |
115 | end |
|
112 | end | |
116 | export |
|
|||
117 | end |
|
113 | end | |
118 |
|
114 | |||
119 | def report_criteria_to_csv(csv, available_criteria, columns, criteria, periods, hours, level=0) |
|
115 | def report_criteria_to_csv(csv, available_criteria, columns, criteria, periods, hours, level=0) | |
120 | decimal_separator = l(:general_csv_decimal_separator) |
|
|||
121 | hours.collect {|h| h[criteria[level]].to_s}.uniq.each do |value| |
|
116 | hours.collect {|h| h[criteria[level]].to_s}.uniq.each do |value| | |
122 | hours_for_value = select_hours(hours, criteria[level], value) |
|
117 | hours_for_value = select_hours(hours, criteria[level], value) | |
123 | next if hours_for_value.empty? |
|
118 | next if hours_for_value.empty? | |
124 | row = [''] * level |
|
119 | row = [''] * level | |
125 | row << Redmine::CodesetUtil.from_utf8( |
|
120 | row << format_criteria_value(available_criteria[criteria[level]], value).to_s | |
126 | format_criteria_value(available_criteria[criteria[level]], value).to_s, |
|
|||
127 | l(:general_csv_encoding) ) |
|
|||
128 | row += [''] * (criteria.length - level - 1) |
|
121 | row += [''] * (criteria.length - level - 1) | |
129 | total = 0 |
|
122 | total = 0 | |
130 | periods.each do |period| |
|
123 | periods.each do |period| | |
131 | sum = sum_hours(select_hours(hours_for_value, columns, period.to_s)) |
|
124 | sum = sum_hours(select_hours(hours_for_value, columns, period.to_s)) | |
132 | total += sum |
|
125 | total += sum | |
133 |
row << (sum > 0 ? |
|
126 | row << (sum > 0 ? sum : '') | |
134 | end |
|
127 | end | |
135 | row << ("%.2f" % total).gsub('.',decimal_separator) |
|
128 | row << total | |
136 | csv << row |
|
129 | csv << row | |
137 | if criteria.length > level + 1 |
|
130 | if criteria.length > level + 1 | |
138 | report_criteria_to_csv(csv, available_criteria, columns, criteria, periods, hours_for_value, level + 1) |
|
131 | report_criteria_to_csv(csv, available_criteria, columns, criteria, periods, hours_for_value, level + 1) |
@@ -63,8 +63,6 require 'redmine/themes' | |||||
63 | require 'redmine/hook' |
|
63 | require 'redmine/hook' | |
64 | require 'redmine/plugin' |
|
64 | require 'redmine/plugin' | |
65 |
|
65 | |||
66 | require 'csv' |
|
|||
67 |
|
||||
68 | Redmine::Scm::Base.add "Subversion" |
|
66 | Redmine::Scm::Base.add "Subversion" | |
69 | Redmine::Scm::Base.add "Darcs" |
|
67 | Redmine::Scm::Base.add "Darcs" | |
70 | Redmine::Scm::Base.add "Mercurial" |
|
68 | Redmine::Scm::Base.add "Mercurial" |
General Comments 0
You need to be logged in to leave comments.
Login now