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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
split the PDF attributes tables for report & single export
as-opf83e2f53f288
diff --git a/app/models/work_package/pdf_export/export/attributes_by_columns.rb b/app/models/work_package/pdf_export/export/attributes_by_columns.rbnew file mode 100644index 00000000000..b98bf31f51d--- /dev/null+++ b/app/models/work_package/pdf_export/export/attributes_by_columns.rb@@ -0,0 +1,54 @@+module WorkPackage::PDFExport::Export::AttributesByColumns+ def write_attributes_tables!(work_package)+ rows = attribute_table_rows(work_package)+ return if rows.empty?++ with_margin(styles.wp_attributes_table_margins) do+ pdf.table(+ rows,+ column_widths: attributes_table_column_widths,+ cell_style: styles.wp_attributes_table_cell.merge({ inline_format: true })+ )+ end+ end++ private++ def attributes_table_column_widths+ # calculate fixed work package attribute table columns width+ widths = [1.5, 2.0, 1.5, 2.0] # label | value | label | value+ ratio = pdf.bounds.width / widths.sum+ widths.map { |w| w * ratio }+ end++ def attribute_table_rows(work_package)+ list = attribute_data_list(work_package)+ 0.step(list.length - 1, 2).map do |i|+ build_columns_table_cells(list[i]) ++ build_columns_table_cells(list[i + 1])+ end+ end++ def attribute_data_list(work_package)+ attributes_data_by_columns+ .map { |entry| entry.merge({ value: get_column_value_cell(work_package, entry[:name]) }) }+ end++ def attributes_data_by_columns+ column_objects+ .reject { |column| column.name == :subject }+ .map do |column|+ { label: column.caption || "", name: column.name }+ end+ end++ def build_columns_table_cells(attribute_data)+ return ["", ""] if attribute_data.nil?++ # get work package attribute table cell data: [label, value]+ [+ pdf.make_cell(attribute_data[:label].upcase, styles.wp_attributes_table_label_cell),+ attribute_data[:value]+ ]+ end+enddiff --git a/app/models/work_package/pdf_export/export/attributes_by_form.rb b/app/models/work_package/pdf_export/export/attributes_by_form.rbnew file mode 100644index 00000000000..ecd00b3c36a--- /dev/null+++ b/app/models/work_package/pdf_export/export/attributes_by_form.rb@@ -0,0 +1,91 @@+module WorkPackage::PDFExport::Export::AttributesByForm+ def write_attributes_tables!(work_package)+ rows = attribute_table_rows(work_package)+ return if rows.empty?++ with_margin(styles.wp_attributes_table_margins) do+ pdf.table(+ rows,+ column_widths: attributes_table_column_widths,+ cell_style: styles.wp_attributes_table_cell.merge({ inline_format: true })+ )+ end+ end++ private++ def attributes_table_column_widths+ # calculate fixed work package attribute table columns width+ widths = [1.5, 2.0, 1.5, 2.0] # label | value | label | value+ ratio = pdf.bounds.width / widths.sum+ widths.map { |w| w * ratio }+ end++ def attribute_table_rows(work_package)+ list = attribute_data_list(work_package)+ 0.step(list.length - 1, 2).map do |i|+ build_columns_table_cells(list[i]) ++ build_columns_table_cells(list[i + 1])+ end+ end++ def attribute_data_list(work_package)+ column_entries(%i[id type status])+ .concat(form_configuration_columns(work_package))+ .map { |entry| entry.merge({ value: get_column_value_cell(work_package, entry[:name]) }) }+ end++ def form_configuration_columns(work_package)+ work_package+ .type.attribute_groups+ .filter { |group| group.is_a?(Type::AttributeGroup) }+ .map do |group|+ group.attributes.map do |form_key|+ form_key_to_column_entries(form_key.to_sym, work_package)+ end+ end.flatten+ end++ def form_key_custom_field_to_column_entries(form_key, work_package)+ id = form_key.to_s.sub("custom_field_", "").to_i+ cf = CustomField.find_by(id:)+ return [] if cf.nil? || cf.formattable?++ return [] unless cf.is_for_all? || work_package.project.work_package_custom_field_ids.include?(cf.id)++ [{ label: cf.name || form_key, name: form_key.to_s.sub("custom_field_", "cf_") }]+ end++ def form_key_to_column_entries(form_key, work_package)+ if CustomField.custom_field_attribute? form_key+ return form_key_custom_field_to_column_entries(form_key, work_package)+ end++ if form_key == :date+ column_entries(%i[start_date due_date duration])+ elsif form_key == :bcf_thumbnail+ []+ else+ column_name = ::API::Utilities::PropertyNameConverter.to_ar_name(form_key, context: work_package)+ [column_entry(column_name)]+ end+ end++ def column_entries(column_names)+ column_names.map { |key| column_entry(key) }+ end++ def column_entry(column_name)+ { label: WorkPackage.human_attribute_name(column_name), name: column_name }+ end++ def build_columns_table_cells(attribute_data)+ return ["", ""] if attribute_data.nil?++ # get work package attribute table cell data: [label, value]+ [+ pdf.make_cell(attribute_data[:label].upcase, styles.wp_attributes_table_label_cell),+ attribute_data[:value]+ ]+ end+enddiff --git a/app/models/work_package/pdf_export/export/work_package_detail.rb b/app/models/work_package/pdf_export/export/work_package_detail.rbindex f54b179a08c..306b3dd5312 100644--- a/app/models/work_package/pdf_export/export/work_package_detail.rb+++ b/app/models/work_package/pdf_export/export/work_package_detail.rb@@ -29,8 +29,6 @@#++module WorkPackage::PDFExport::Export::WorkPackageDetail- include WorkPackage::PDFExport::Export::MarkdownField-def write_work_packages_details!(work_packages, id_wp_meta_map)work_packages.each do |work_package|write_work_package_detail!(work_package, id_wp_meta_map[work_package.id])@@ -47,7 +45,7 @@ module WorkPackage::PDFExport::Export::WorkPackageDetailenddef write_work_package_detail_content!(work_package)- write_attributes_table! work_package+ write_attributes_tables! work_packageif options[:long_text_fields].nil?write_description! work_packagewrite_custom_fields! work_package@@ -79,111 +77,6 @@ module WorkPackage::PDFExport::Export::WorkPackageDetaillevel_string_widthend- def write_attributes_table!(work_package)- rows = attribute_table_rows(work_package)- return if rows.empty?-- with_margin(styles.wp_attributes_table_margins) do- pdf.table(- rows,- column_widths: attributes_table_column_widths,- cell_style: styles.wp_attributes_table_cell.merge({ inline_format: true })- )- end- end-- def attributes_table_column_widths- # calculate fixed work package attribute table columns width- widths = [1.5, 2.0, 1.5, 2.0] # label | value | label | value- ratio = pdf.bounds.width / widths.sum- widths.map { |w| w * ratio }- end-- def attribute_table_rows(work_package)- list = attribute_data_list(work_package)- 0.step(list.length - 1, 2).map do |i|- build_columns_table_cells(list[i]) +- build_columns_table_cells(list[i + 1])- end- end-- def attribute_data_list(work_package)- list = if respond_to?(:column_objects)- attributes_data_by_columns- else- attributes_data_by_wp(work_package)- end- list- .map { |entry| entry.merge({ value: get_column_value_cell(work_package, entry[:name]) }) }- end-- def attributes_data_by_columns- column_objects- .reject { |column| column.name == :subject }- .map do |column|- { label: column.caption || "", name: column.name }- end- end-- def attributes_data_by_wp(work_package)- column_entries(%i[id type status])- .concat(form_configuration_columns(work_package))- end-- def form_configuration_columns(work_package)- work_package- .type.attribute_groups- .filter { |group| group.is_a?(Type::AttributeGroup) }- .map do |group|- group.attributes.map do |form_key|- form_key_to_column_entries(form_key.to_sym, work_package)- end- end.flatten- end-- def form_key_custom_field_to_column_entries(form_key, work_package)- id = form_key.to_s.sub("custom_field_", "").to_i- cf = CustomField.find_by(id:)- return [] if cf.nil? || cf.formattable?-- return [] unless cf.is_for_all? || work_package.project.work_package_custom_field_ids.include?(cf.id)-- [{ label: cf.name || form_key, name: form_key.to_s.sub("custom_field_", "cf_") }]- end-- def form_key_to_column_entries(form_key, work_package)- if CustomField.custom_field_attribute? form_key- return form_key_custom_field_to_column_entries(form_key, work_package)- end-- if form_key == :date- column_entries(%i[start_date due_date duration])- elsif form_key == :bcf_thumbnail- []- else- column_name = ::API::Utilities::PropertyNameConverter.to_ar_name(form_key, context: work_package)- [column_entry(column_name)]- end- end-- def column_entries(column_names)- column_names.map { |key| column_entry(key) }- end-- def column_entry(column_name)- { label: WorkPackage.human_attribute_name(column_name), name: column_name }- end-- def build_columns_table_cells(attribute_data)- return ["", ""] if attribute_data.nil?-- # get work package attribute table cell data: [label, value]- [- pdf.make_cell(attribute_data[:label].upcase, styles.wp_attributes_table_label_cell),- attribute_data[:value]- ]- end-def write_long_text_fields!(work_package)selected_long_text_fields.each do |field_id_or_desc|if field_id_or_desc == "description"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.rbindex 676de311df4..c9b55e65270 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@@ -46,16 +46,17 @@ class WorkPackage::PDFExport::WorkPackageListToPdf < WorkPackage::Exports::Queryinclude WorkPackage::PDFExport::Common::Logoinclude WorkPackage::PDFExport::Common::Attachmentsinclude WorkPackage::PDFExport::Export::ExportCommon- include WorkPackage::PDFExport::Export::WorkPackageDetailinclude WorkPackage::PDFExport::Export::Pageinclude WorkPackage::PDFExport::Export::Styleinclude WorkPackage::PDFExport::Export::OverviewTableinclude WorkPackage::PDFExport::Export::SumsTable+ include WorkPackage::PDFExport::Export::MarkdownFieldinclude WorkPackage::PDFExport::Export::WorkPackageDetailinclude WorkPackage::PDFExport::Export::TableOfContentsinclude WorkPackage::PDFExport::Export::Styleinclude WorkPackage::PDFExport::Export::Coverinclude WorkPackage::PDFExport::Export::Gantt+ include WorkPackage::PDFExport::Export::AttributesByColumnsattr_accessor :pdf,:optionsdiff --git a/app/models/work_package/pdf_export/work_package_to_pdf.rb b/app/models/work_package/pdf_export/work_package_to_pdf.rbindex 57947f58631..389e586e011 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@@ -33,9 +33,11 @@ class WorkPackage::PDFExport::WorkPackageToPdf < Exports::Exporterinclude WorkPackage::PDFExport::Common::Logoinclude WorkPackage::PDFExport::Common::Attachmentsinclude WorkPackage::PDFExport::Export::ExportCommon+ include WorkPackage::PDFExport::Export::MarkdownFieldinclude WorkPackage::PDFExport::Export::WorkPackageDetailinclude WorkPackage::PDFExport::Export::Pageinclude WorkPackage::PDFExport::Export::Style+ include WorkPackage::PDFExport::Export::AttributesByFormattr_accessor :pdf, :columns