|
|
@ -30,60 +30,17 @@ |
|
|
|
module WorkPackage::CsvExporter |
|
|
|
module WorkPackage::CsvExporter |
|
|
|
include Redmine::I18n |
|
|
|
include Redmine::I18n |
|
|
|
include CustomFieldsHelper |
|
|
|
include CustomFieldsHelper |
|
|
|
|
|
|
|
include ActionView::Helpers::TextHelper |
|
|
|
|
|
|
|
include ActionView::Helpers::NumberHelper |
|
|
|
|
|
|
|
|
|
|
|
def csv(work_packages, project = nil) |
|
|
|
def csv(work_packages, query) |
|
|
|
decimal_separator = l(:general_csv_decimal_separator) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export = CSV.generate(col_sep: l(:general_csv_separator)) do |csv| |
|
|
|
export = CSV.generate(col_sep: l(:general_csv_separator)) do |csv| |
|
|
|
# csv header fields |
|
|
|
headers = csv_headers(query) |
|
|
|
headers = ['#', |
|
|
|
|
|
|
|
WorkPackage.human_attribute_name(:status), |
|
|
|
|
|
|
|
WorkPackage.human_attribute_name(:project), |
|
|
|
|
|
|
|
WorkPackage.human_attribute_name(:type), |
|
|
|
|
|
|
|
WorkPackage.human_attribute_name(:priority), |
|
|
|
|
|
|
|
WorkPackage.human_attribute_name(:subject), |
|
|
|
|
|
|
|
WorkPackage.human_attribute_name(:assigned_to), |
|
|
|
|
|
|
|
WorkPackage.human_attribute_name(:category), |
|
|
|
|
|
|
|
WorkPackage.human_attribute_name(:fixed_version), |
|
|
|
|
|
|
|
WorkPackage.human_attribute_name(:author), |
|
|
|
|
|
|
|
WorkPackage.human_attribute_name(:start_date), |
|
|
|
|
|
|
|
WorkPackage.human_attribute_name(:due_date), |
|
|
|
|
|
|
|
WorkPackage.human_attribute_name(:done_ratio), |
|
|
|
|
|
|
|
WorkPackage.human_attribute_name(:estimated_hours), |
|
|
|
|
|
|
|
WorkPackage.human_attribute_name(:parent_work_package), |
|
|
|
|
|
|
|
WorkPackage.human_attribute_name(:created_at), |
|
|
|
|
|
|
|
WorkPackage.human_attribute_name(:updated_at) |
|
|
|
|
|
|
|
] |
|
|
|
|
|
|
|
# Export project custom fields if project is given |
|
|
|
|
|
|
|
# otherwise export custom fields marked as "For all projects" |
|
|
|
|
|
|
|
custom_fields = project.nil? ? WorkPackageCustomField.for_all : project.all_work_package_custom_fields |
|
|
|
|
|
|
|
custom_fields.each { |f| headers << f.name } |
|
|
|
|
|
|
|
# Description in the last column |
|
|
|
|
|
|
|
headers << CustomField.human_attribute_name(:description) |
|
|
|
|
|
|
|
csv << encode_csv_columns(headers) |
|
|
|
csv << encode_csv_columns(headers) |
|
|
|
# csv lines |
|
|
|
|
|
|
|
work_packages.each do |work_package| |
|
|
|
work_packages.each do |work_package| |
|
|
|
fields = [work_package.id, |
|
|
|
row = csv_row(work_package, query) |
|
|
|
work_package.status.name, |
|
|
|
csv << encode_csv_columns(row) |
|
|
|
work_package.project.name, |
|
|
|
|
|
|
|
work_package.type.name, |
|
|
|
|
|
|
|
work_package.priority.name, |
|
|
|
|
|
|
|
work_package.subject, |
|
|
|
|
|
|
|
work_package.assigned_to, |
|
|
|
|
|
|
|
work_package.category, |
|
|
|
|
|
|
|
work_package.fixed_version, |
|
|
|
|
|
|
|
work_package.author.name, |
|
|
|
|
|
|
|
format_date(work_package.start_date), |
|
|
|
|
|
|
|
format_date(work_package.due_date), |
|
|
|
|
|
|
|
(Setting.work_package_done_ratio != 'disabled' ? work_package.done_ratio : ''), |
|
|
|
|
|
|
|
work_package.estimated_hours.to_s.gsub('.', decimal_separator), |
|
|
|
|
|
|
|
work_package.parent_id, |
|
|
|
|
|
|
|
format_time(work_package.created_at), |
|
|
|
|
|
|
|
format_time(work_package.updated_at) |
|
|
|
|
|
|
|
] |
|
|
|
|
|
|
|
custom_fields.each { |f| fields << show_value(work_package.custom_value_for(f)) } |
|
|
|
|
|
|
|
fields << work_package.description |
|
|
|
|
|
|
|
csv << encode_csv_columns(fields) |
|
|
|
|
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
@ -95,4 +52,52 @@ module WorkPackage::CsvExporter |
|
|
|
Redmine::CodesetUtil.from_utf8(cell.to_s, encoding) |
|
|
|
Redmine::CodesetUtil.from_utf8(cell.to_s, encoding) |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# fetch all headers |
|
|
|
|
|
|
|
def csv_headers(query) |
|
|
|
|
|
|
|
headers = [] |
|
|
|
|
|
|
|
headers << '#' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
query.columns.each_with_index do |column, _| |
|
|
|
|
|
|
|
headers << column.caption |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
headers << CustomField.human_attribute_name(:description) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
headers |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# fetch all row values |
|
|
|
|
|
|
|
def csv_row(work_package, query) |
|
|
|
|
|
|
|
row = query.columns.collect do |column| |
|
|
|
|
|
|
|
csv_format_value(work_package, column) |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if row.size > 0 |
|
|
|
|
|
|
|
row.unshift(work_package.id.to_s) |
|
|
|
|
|
|
|
row << work_package.description.gsub(/\r/, '').gsub(/\n/, ' ') |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
row |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def csv_format_value(work_package, column) |
|
|
|
|
|
|
|
if column.is_a?(QueryCustomFieldColumn) |
|
|
|
|
|
|
|
cv = work_package.custom_values.detect { |v| v.custom_field_id == column.custom_field.id } |
|
|
|
|
|
|
|
show_value(cv) |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
value = work_package.send(column.name) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case value |
|
|
|
|
|
|
|
when Date |
|
|
|
|
|
|
|
format_date(value) |
|
|
|
|
|
|
|
when Time |
|
|
|
|
|
|
|
format_time(value) |
|
|
|
|
|
|
|
else |
|
|
|
|
|
|
|
value |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
end.to_s |
|
|
|
|
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|