mirror of
https://github.com/meineerde/redmine.git
synced 2026-01-06 07:31:31 +00:00
Adds a class for handling CSV generation (#7037).
git-svn-id: http://svn.redmine.org/redmine/trunk@14302 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
parent
ad8cfe1887
commit
49914f010d
@ -1307,6 +1307,11 @@ module ApplicationHelper
|
||||
end
|
||||
end
|
||||
|
||||
def generate_csv(&block)
|
||||
decimal_separator = l(:general_csv_decimal_separator)
|
||||
encoding = l(:general_csv_encoding)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def wiki_helper
|
||||
|
||||
@ -162,7 +162,6 @@ module QueriesHelper
|
||||
end
|
||||
|
||||
def query_to_csv(items, query, options={})
|
||||
encoding = l(:general_csv_encoding)
|
||||
columns = (options[:columns] == 'all' ? query.available_inline_columns : query.inline_columns)
|
||||
query.available_block_columns.each do |column|
|
||||
if options[column.name].present?
|
||||
@ -170,15 +169,14 @@ module QueriesHelper
|
||||
end
|
||||
end
|
||||
|
||||
export = CSV.generate(:col_sep => l(:general_csv_separator)) do |csv|
|
||||
Redmine::Export::CSV.generate do |csv|
|
||||
# csv header fields
|
||||
csv << columns.collect {|c| Redmine::CodesetUtil.from_utf8(c.caption.to_s, encoding) }
|
||||
csv << columns.map {|c| c.caption.to_s}
|
||||
# csv lines
|
||||
items.each do |item|
|
||||
csv << columns.collect {|c| Redmine::CodesetUtil.from_utf8(csv_content(c, item), encoding) }
|
||||
csv << columns.map {|c| csv_content(c, item)}
|
||||
end
|
||||
end
|
||||
export
|
||||
end
|
||||
|
||||
# Retrieve query from session or build a new query
|
||||
|
||||
@ -90,49 +90,42 @@ module TimelogHelper
|
||||
end
|
||||
|
||||
def report_to_csv(report)
|
||||
decimal_separator = l(:general_csv_decimal_separator)
|
||||
export = CSV.generate(:col_sep => l(:general_csv_separator)) do |csv|
|
||||
Redmine::Export::CSV.generate do |csv|
|
||||
# Column headers
|
||||
headers = report.criteria.collect {|criteria| l(report.available_criteria[criteria][:label]) }
|
||||
headers += report.periods
|
||||
headers << l(:label_total_time)
|
||||
csv << headers.collect {|c| Redmine::CodesetUtil.from_utf8(
|
||||
c.to_s,
|
||||
l(:general_csv_encoding) ) }
|
||||
csv << headers
|
||||
# Content
|
||||
report_criteria_to_csv(csv, report.available_criteria, report.columns, report.criteria, report.periods, report.hours)
|
||||
# Total row
|
||||
str_total = Redmine::CodesetUtil.from_utf8(l(:label_total_time), l(:general_csv_encoding))
|
||||
str_total = l(:label_total_time)
|
||||
row = [ str_total ] + [''] * (report.criteria.size - 1)
|
||||
total = 0
|
||||
report.periods.each do |period|
|
||||
sum = sum_hours(select_hours(report.hours, report.columns, period.to_s))
|
||||
total += sum
|
||||
row << (sum > 0 ? ("%.2f" % sum).gsub('.',decimal_separator) : '')
|
||||
row << (sum > 0 ? sum : '')
|
||||
end
|
||||
row << ("%.2f" % total).gsub('.',decimal_separator)
|
||||
row << total
|
||||
csv << row
|
||||
end
|
||||
export
|
||||
end
|
||||
|
||||
def report_criteria_to_csv(csv, available_criteria, columns, criteria, periods, hours, level=0)
|
||||
decimal_separator = l(:general_csv_decimal_separator)
|
||||
hours.collect {|h| h[criteria[level]].to_s}.uniq.each do |value|
|
||||
hours_for_value = select_hours(hours, criteria[level], value)
|
||||
next if hours_for_value.empty?
|
||||
row = [''] * level
|
||||
row << Redmine::CodesetUtil.from_utf8(
|
||||
format_criteria_value(available_criteria[criteria[level]], value).to_s,
|
||||
l(:general_csv_encoding) )
|
||||
row << format_criteria_value(available_criteria[criteria[level]], value).to_s
|
||||
row += [''] * (criteria.length - level - 1)
|
||||
total = 0
|
||||
periods.each do |period|
|
||||
sum = sum_hours(select_hours(hours_for_value, columns, period.to_s))
|
||||
total += sum
|
||||
row << (sum > 0 ? ("%.2f" % sum).gsub('.',decimal_separator) : '')
|
||||
row << (sum > 0 ? sum : '')
|
||||
end
|
||||
row << ("%.2f" % total).gsub('.',decimal_separator)
|
||||
row << total
|
||||
csv << row
|
||||
if criteria.length > level + 1
|
||||
report_criteria_to_csv(csv, available_criteria, columns, criteria, periods, hours_for_value, level + 1)
|
||||
|
||||
@ -63,8 +63,6 @@ require 'redmine/themes'
|
||||
require 'redmine/hook'
|
||||
require 'redmine/plugin'
|
||||
|
||||
require 'csv'
|
||||
|
||||
Redmine::Scm::Base.add "Subversion"
|
||||
Redmine::Scm::Base.add "Darcs"
|
||||
Redmine::Scm::Base.add "Mercurial"
|
||||
|
||||
59
lib/redmine/export/csv.rb
Normal file
59
lib/redmine/export/csv.rb
Normal file
@ -0,0 +1,59 @@
|
||||
# encoding: utf-8
|
||||
#
|
||||
# Redmine - project management software
|
||||
# Copyright (C) 2006-2015 Jean-Philippe Lang
|
||||
#
|
||||
# This program is free software; you can redistribute it and/or
|
||||
# modify it under the terms of the GNU General Public License
|
||||
# as published by the Free Software Foundation; either version 2
|
||||
# of the License, or (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program; if not, write to the Free Software
|
||||
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
|
||||
require 'csv'
|
||||
|
||||
module Redmine
|
||||
module Export
|
||||
module CSV
|
||||
def self.generate(*args, &block)
|
||||
Base.generate(*args, &block)
|
||||
end
|
||||
|
||||
class Base < ::CSV
|
||||
include Redmine::I18n
|
||||
|
||||
class << self
|
||||
|
||||
def generate(&block)
|
||||
col_sep = l(:general_csv_separator)
|
||||
encoding = l(:general_csv_encoding)
|
||||
|
||||
super(:col_sep => col_sep, :encoding => encoding, &block)
|
||||
end
|
||||
end
|
||||
|
||||
def <<(row)
|
||||
row = row.map do |field|
|
||||
case field
|
||||
when String
|
||||
Redmine::CodesetUtil.from_utf8(field, self.encoding.name)
|
||||
when Float
|
||||
@decimal_separator ||= l(:general_csv_decimal_separator)
|
||||
("%.2f" % field).gsub('.', @decimal_separator)
|
||||
else
|
||||
field
|
||||
end
|
||||
end
|
||||
super row
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
x
Reference in New Issue
Block a user