Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

[35853] Extract formatting in PDF export for custom fields (#9095)

Oliver Günther134552e95c39
diff --git a/app/models/custom_field.rb b/app/models/custom_field.rb
index db0430ead30..067ec3b75c0 100644
--- a/app/models/custom_field.rb
+++ b/app/models/custom_field.rb
@@ -250,6 +250,10 @@ class CustomField < ApplicationRecord
field_format == "list"
end
+ def formattable?
+ field_format == "text"
+ end
+
def multi_value?
multi_value
end
diff --git a/app/models/work_package/pdf_export/common.rb b/app/models/work_package/pdf_export/common.rb
index 914734ebb25..d81c3c36b8d 100644
--- a/app/models/work_package/pdf_export/common.rb
+++ b/app/models/work_package/pdf_export/common.rb
@@ -79,109 +79,6 @@ module WorkPackage::PDFExport::Common
}
end
- ##
- # Writes the formatted work package description into the document.
- #
- # A border (without one on the top) is painted around the area painted by the description.
- #
- # @param work_package [WorkPackage] The work package for which the description is to be printed.
- # @param label [boolean] Whether a label is to be printed in a column preceding the description.
- def write_description!(work_package, label = true)
- height = write_description_html!(work_package, label)
-
- data = make_description_label_row(label) +
- make_description_border_rows(height, label)
-
- pdf.table(data, column_widths: column_widths)
- end
-
- def write_description_html!(work_package, label)
- float_with_height_indicator do
- pdf.move_down(cell_padding[1])
-
- pdf.indent(description_padding_left(label), cell_padding[3]) do
- pdf.markup(formatted_description_text(work_package))
- end
- end
- end
-
- def float_with_height_indicator
- former_position = new_position = current_y_position
-
- pdf.float do
- yield
-
- new_position = current_y_position
- end
-
- position_diff(former_position, new_position)
- end
-
- def make_description_label_row(label)
- if label
- [[make_description_label, pdf.make_cell('', borders: [:right], colspan: description_colspan)].compact]
- else
- [[pdf.make_cell('', borders: %i[right left], colspan: description_colspan)]]
- end
- end
-
- def description_padding_left(label)
- if label
- column_widths.first + cell_padding[1]
- else
- cell_padding[1]
- end
- end
-
- def description_padding_right
- cell_padding[3]
- end
-
- def formatted_description_text(work_package)
- format_text(work_package.description.to_s, object: work_package, format: :html)
- end
-
- def make_description_border_rows(height, label)
- border_row_height = 10
-
- (height / border_row_height).ceil.times.map do |index|
- make_description_border_row(border_row_height,
- label,
- index == (height / border_row_height).ceil - 1)
- end
- end
-
- def make_description_border_row(border_row_height, label, last)
- border_left = if label
- [:left]
- else
- %i[left right]
- end
-
- border_right = [:right]
-
- if last
- border_left << :bottom
- border_right << :bottom
- end
-
- if label
- [pdf.make_cell('', height: border_row_height, borders: border_left, colspan: 1),
- pdf.make_cell('', height: border_row_height, borders: border_right, colspan: description_colspan)]
- else
- [pdf.make_cell('', height: border_row_height, borders: border_left, colspan: description_colspan)]
- end
- end
-
- def make_description_label
- text = WorkPackage.human_attribute_name(:description) + ':'
- pdf.make_cell(text, borders: [:left], font_style: :bold, padding: cell_padding)
- end
-
- def description_colspan
- raise NotImplementedError, 'to be implemented where included'
- end
-
def current_y_position
OpenStruct.new y: pdf.y, page: pdf.page_number
end
diff --git a/app/models/work_package/pdf_export/formattable.rb b/app/models/work_package/pdf_export/formattable.rb
new file mode 100644
index 00000000000..e8a7e2c7ab6
--- /dev/null
+++ b/app/models/work_package/pdf_export/formattable.rb
@@ -0,0 +1,130 @@
+#-- encoding: UTF-8
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# 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.
+#
+# See docs/COPYRIGHT.rdoc for more details.
+#++
+
+module WorkPackage::PDFExport::Formattable
+ ##
+ # Writes the formatted work package description into the document.
+ #
+ # A border (without one on the top) is painted around the area painted by the description.
+ #
+ # @param work_package [WorkPackage] The work package for which the description is to be printed.
+ # @param markdown [string] The formattable text
+ # @param label [string|null] The label to print, if any
+ def write_formattable!(work_package, markdown:, label: WorkPackage.human_attribute_name(attribute))
+ height = write_formattable_content(work_package, markdown: markdown, label: label)
+
+ data = make_formattable_label_row(label) +
+ make_formattable_border_rows(height, label)
+
+ pdf.table(data, column_widths: column_widths)
+ end
+
+ def write_formattable_content(work_package, markdown:, label: true)
+ float_with_height_indicator do
+ pdf.move_down(cell_padding[1])
+
+ pdf.indent(formattable_padding_left(label), cell_padding[3]) do
+ pdf.markup format_text(markdown, object: work_package, format: :html)
+ end
+ end
+ end
+
+ def float_with_height_indicator
+ former_position = new_position = current_y_position
+
+ pdf.float do
+ yield
+
+ new_position = current_y_position
+ end
+
+ position_diff(former_position, new_position)
+ end
+
+ def make_formattable_label_row(label)
+ if label.present?
+ [[make_formattable_label(label), pdf.make_cell('', borders: [:right], colspan: formattable_colspan)].compact]
+ else
+ [[pdf.make_cell('', borders: %i[right left], colspan: formattable_colspan)]]
+ end
+ end
+
+ def formattable_padding_left(label)
+ if label.present?
+ column_widths.first + cell_padding[1]
+ else
+ cell_padding[1]
+ end
+ end
+
+ def formattable_padding_right
+ cell_padding[3]
+ end
+
+ def make_formattable_border_rows(height, label)
+ border_row_height = 10
+
+ (height / border_row_height).ceil.times.map do |index|
+ make_formattable_border_row(border_row_height,
+ label,
+ index == (height / border_row_height).ceil - 1)
+ end
+ end
+
+ def make_formattable_border_row(border_row_height, label, last)
+ border_left = if label
+ [:left]
+ else
+ %i[left right]
+ end
+
+ border_right = [:right]
+
+ if last
+ border_left << :bottom
+ border_right << :bottom
+ end
+
+ if label
+ [pdf.make_cell('', height: border_row_height, borders: border_left, colspan: 1),
+ pdf.make_cell('', height: border_row_height, borders: border_right, colspan: formattable_colspan)]
+ else
+ [pdf.make_cell('', height: border_row_height, borders: border_left, colspan: formattable_colspan)]
+ end
+ end
+
+ def make_formattable_label(label)
+ pdf.make_cell("#{label}:", borders: [:left], font_style: :bold, padding: cell_padding)
+ end
+
+ def formattable_colspan
+ raise NotImplementedError, 'to be implemented where included'
+ end
+end
diff --git a/app/models/work_package/pdf_export/work_package_list_to_pdf.rb b/app/models/work_package/pdf_export/work_package_list_to_pdf.rb
index 807be78ec14..caf5f7b4a9b 100644
--- a/app/models/work_package/pdf_export/work_package_list_to_pdf.rb
+++ b/app/models/work_package/pdf_export/work_package_list_to_pdf.rb
@@ -44,6 +44,7 @@ require 'open3'
class WorkPackage::PDFExport::WorkPackageListToPdf < WorkPackage::Exporter::Base
include WorkPackage::PDFExport::Common
+ include WorkPackage::PDFExport::Formattable
include WorkPackage::PDFExport::Attachments
attr_accessor :pdf,
@@ -182,7 +183,7 @@ class WorkPackage::PDFExport::WorkPackageListToPdf < WorkPackage::Exporter::Base
widths.map { |w| w * ratio }
end
- def description_colspan
+ def formattable_colspan
valid_export_columns.size
end
@@ -212,7 +213,9 @@ class WorkPackage::PDFExport::WorkPackageListToPdf < WorkPackage::Exporter::Base
write_attributes!(work_package)
if options[:show_descriptions]
- write_description!(work_package, false)
+ write_formattable! work_package,
+ markdown: work_package.description,
+ label: WorkPackage.human_attribute_name(:description)
end
if options[:show_attachments] && work_package.attachments.exists?
diff --git a/app/models/work_package/pdf_export/work_package_to_pdf.rb b/app/models/work_package/pdf_export/work_package_to_pdf.rb
index d56d0157a11..d438beacc4f 100644
--- a/app/models/work_package/pdf_export/work_package_to_pdf.rb
+++ b/app/models/work_package/pdf_export/work_package_to_pdf.rb
@@ -30,6 +30,7 @@
class WorkPackage::PDFExport::WorkPackageToPdf < WorkPackage::Exporter::Base
include WorkPackage::PDFExport::Common
+ include WorkPackage::PDFExport::Formattable
include WorkPackage::PDFExport::Attachments
attr_accessor :pdf
@@ -101,8 +102,10 @@ class WorkPackage::PDFExport::WorkPackageToPdf < WorkPackage::Exporter::Base
end
end
- def make_custom_fields
- work_package.custom_field_values.map do |custom_value|
+ def make_plain_custom_fields
+ work_package.custom_field_values.each do |custom_value|
+ next if custom_value.custom_field.formattable?
+
cf = custom_value.custom_field
name = cf.name || Array(cf.name_translations.first).last || '?'
@@ -113,7 +116,7 @@ class WorkPackage::PDFExport::WorkPackageToPdf < WorkPackage::Exporter::Base
colspan: 3,
borders: [:right],
padding: cell_padding
- [label, value]
+ yield [label, value]
end
end
@@ -136,7 +139,7 @@ class WorkPackage::PDFExport::WorkPackageToPdf < WorkPackage::Exporter::Base
end
end
- def description_colspan
+ def formattable_colspan
3
end
@@ -173,12 +176,25 @@ class WorkPackage::PDFExport::WorkPackageToPdf < WorkPackage::Exporter::Base
data.first.each { |cell| cell.borders << :top } # top horizontal line
data.last.each { |cell| cell.borders << :bottom } # horizontal line after main attrs
- make_custom_fields.each { |row| data << row }
+ # Render plain custom values
+ make_plain_custom_fields { |row| data << row }
pdf.font style: :normal, size: 9
pdf.table(data, column_widths: column_widths)
- write_description!(work_package)
+ # Render formattable custom values
+ work_package.custom_field_values
+ .select { |cv| cv.custom_field.formattable? }
+ .each do |custom_value|
+
+ write_formattable! work_package,
+ markdown: custom_value.value,
+ label: custom_value.custom_field.name
+ end
+
+ write_formattable! work_package,
+ markdown: work_package.description,
+ label: WorkPackage.human_attribute_name(:description)
end
def write_changesets!