diff --git a/app/controllers/plan_exports_controller.rb b/app/controllers/plan_exports_controller.rb new file mode 100644 index 0000000..e475c9a --- /dev/null +++ b/app/controllers/plan_exports_controller.rb @@ -0,0 +1,67 @@ +class PlanExportsController < ApplicationController + + def show + @show_coversheet = params[:export][:project_details].present? + @show_sections_questions = params[:export][:question_headings].present? + @show_unanswered = params[:export][:unanswered_questions].present? + @show_custom_sections = params[:export][:custom_sections].present? + + @plan = Plan.includes(:answers).find(params[:plan_id]) + authorize @plan, :export? + @selected_phase = @plan.phases.find(params[:phase_id]) + @public_plan = false + @hash = @plan.as_pdf(@show_coversheet) + @formatting = params[:export][:formatting] || @plan.settings(:export).formatting + + respond_to do |format| + format.html { show_html } + format.csv { show_csv } + format.text { show_text } + format.docx { show_docx } + format.pdf { show_pdf } + end + end + + private + + def show_html + render layout: false + end + + def show_csv + send_data @plan.as_csv(@show_sections_questions, + @show_unanswered, + @selected_phase, + @show_custom_sections, + @show_coversheet), + filename: "#{file_name}.csv" + end + + def show_text + send_data render_to_string(partial: 'shared/export/plan_txt'), + filename: "#{file_name}.txt" + end + + def show_docx + render docx: "#{file_name}.docx", + content: render_to_string(partial: 'shared/export/plan') + end + + def show_pdf + render pdf: file_name, + margin: @formatting[:margin], + footer: { + center: _("Created using the %{application_name}. Last modified %{date}") % { + application_name: Rails.configuration.branding[:application][:name], + date: l(@plan.updated_at.to_date, formats: :short) + }, + font_size: 8, + spacing: (Integer(@formatting[:margin][:bottom]) / 2) - 4, + right: "[page] of [topage]" + } + end + + def file_name + @plan.title.gsub(/ /, "_") + end +end diff --git a/app/controllers/plans_controller.rb b/app/controllers/plans_controller.rb index 6875978..4891ce7 100644 --- a/app/controllers/plans_controller.rb +++ b/app/controllers/plans_controller.rb @@ -297,44 +297,6 @@ render "download" end - def export - @plan = Plan.includes(:answers).find(params[:id]) - authorize @plan - @selected_phase = @plan.phases.find(params[:phase_id]) - @show_coversheet = params[:export][:project_details].present? - @show_sections_questions = params[:export][:question_headings].present? - @show_unanswered = params[:export][:unanswered_questions].present? - @show_custom_sections = params[:export][:custom_sections].present? - @public_plan = false - @hash = @plan.as_pdf(@show_coversheet) - @formatting = params[:export][:formatting] || @plan.settings(:export).formatting - file_name = @plan.title.gsub(/ /, "_") - - # rubocop:disable Metrics/BlockLength - respond_to do |format| - format.html { render layout: false } - format.csv { send_data @plan.as_csv(@show_sections_questions, @show_unanswered, @selected_phase, @show_custom_sections, @show_coversheet), filename: "#{file_name}.csv" } - format.text { send_data render_to_string(partial: 'shared/export/plan_txt'), filename: "#{file_name}.txt" } - format.docx { render docx: "#{file_name}.docx", content: render_to_string(partial: 'shared/export/plan') } - format.pdf do - # rubocop:disable Metrics/LineLength - render pdf: file_name, - margin: @formatting[:margin], - footer: { - center: _("Created using the %{application_name}. Last modified %{date}") % { - application_name: Rails.configuration.branding[:application][:name], - date: l(@plan.updated_at.to_date, formats: :short) - }, - font_size: 8, - spacing: (Integer(@formatting[:margin][:bottom]) / 2) - 4, - right: "[page] of [topage]" - } - # rubocop:enable Metrics/LineLength - end - end - # rubocop:enable Metrics/BlockLength - end - def duplicate plan = Plan.find(params[:id]) authorize plan diff --git a/app/helpers/exports_helper.rb b/app/helpers/exports_helper.rb new file mode 100644 index 0000000..d60364b --- /dev/null +++ b/app/helpers/exports_helper.rb @@ -0,0 +1,52 @@ +# frozen_string_literal: true + +module ExportsHelper + + PAGE_MARGINS = { + top: '5', + bottom: "10", + left: "12", + right: "12", + } + + def font_face + @formatting[:font_face].presence || 'Arial, Helvetica, Sans-Serif' + end + + def font_size + @formatting[:font_size].presence || '12' + end + + def margin_top + get_margin_value_for_side(:top) + end + + def margin_bottom + get_margin_value_for_side(:bottom) + end + + def margin_left + get_margin_value_for_side(:left) + end + + def margin_right + get_margin_value_for_side(:right) + end + + def plan_attribution(attribution) + attribution = Array(attribution) + prefix = attribution.many? ? _("Creators:") : _("Creator:") + "#{prefix} #{attribution.join(', ')}" + end + + private + + def get_margin_value_for_side(side) + side = side.to_sym + if @formatting.dig(:margin, side).is_a?(Integer) + @formatting[:margin][side] * 4 + else + @formatting.dig(:margin, side).presence || PAGE_MARGINS[side] + end + end +end \ No newline at end of file diff --git a/app/helpers/plans_helper.rb b/app/helpers/plans_helper.rb index f761b11..a3db40d 100644 --- a/app/helpers/plans_helper.rb +++ b/app/helpers/plans_helper.rb @@ -47,7 +47,7 @@ # If there is more than one phase show the plan title and phase title return hash[:phases].many? ? "#{plan.title} - #{phase[:title]}" : plan.title end - + def display_questions_and_section_headings(section, show_sections_questions, show_custom_sections) # Return true if show_sections_questions is true and either section not customised, or section is customised # and show_custom_sections is true diff --git a/app/views/paginable/plans/_organisationally_or_publicly_visible.html.erb b/app/views/paginable/plans/_organisationally_or_publicly_visible.html.erb index e5b3d7c..40d947b 100644 --- a/app/views/paginable/plans/_organisationally_or_publicly_visible.html.erb +++ b/app/views/paginable/plans/_organisationally_or_publicly_visible.html.erb @@ -1,33 +1,47 @@ -<% if current_user.org_id.present? %> -
| <%= _('Project Title') %> <%= paginable_sort_link('plans.title') %> | -<%= _('Template') %> <%= paginable_sort_link('templates.title') %> | -<%= _('Owner') %> | -<%= _('Updated') %> <%= paginable_sort_link('plans.updated_at') %> | -<%= _('Download') %> | -
|---|---|---|---|---|
| <%= plan.title.length > 40 ? "#{plan.title[0..39]} ..." : plan.title %> | -<%= plan.template.title %> | -<%= plan.owner.present? ? plan.owner.name : _('Unknown') %> | -<%= l(plan.updated_at.to_date, formats: :short) %> | -- <%= link_to _('PDF'), plan_export_path(plan, format: :pdf), target: '_blank' %> - | -
| + <%= _('Project Title') %> + <%= paginable_sort_link('plans.title') %> + | ++ <%= _('Template') %> + <%= paginable_sort_link('templates.title') %> + | +<%= _('Owner') %> | ++ <%= _('Updated') %> + <%= paginable_sort_link('plans.updated_at') %> + | +<%= _('Download') %> | +
|---|---|---|---|---|
| + <%= truncate plan.title, length: 40 %> + | +<%= plan.template.title %> | +<%= plan.owner.present? ? plan.owner.name : _('Unknown') %> | +<%= l(plan.updated_at.to_date, formats: :short) %> | ++ <%= link_to _('PDF'), + plan_export_path(plan_id: plan.id, + format: :pdf), + target: '_blank' %> + | +
<%= _('Question not answered.') -%>
<% else %> + <%# case where Question has options %> <% if options.any? %><%= @hash[:attribution].length > 1 ? _("Creators: ") : _('Creator:') %> <%= @hash[:attribution].join(', ') %>
+ <%= plan_attribution(@hash[:attribution]) %> +
<%= _("Affiliation: ") + @hash[:affiliation] %>
<%= _("Copyright information:") %>
-<%= _(" The above plan creator(s) have agreed that others may use as much of the text of this plan as they would like in their own plans, and customise it as necessary. You do not need to credit the creator(s) as the source of the language used, but using any of the plan's text does not imply that the creator(s) endorse, or have any relationship to, your project or proposal") %>
+ <%= _(" The above plan creator(s) have agreed that others may use as much of the text of this plan as they would like in their own plans, and customise it as necessary. You do not need to credit the creator(s) as the source of the language used, but using any of the plan's text does not imply that the creator(s) endorse, or have any relationship to, your project or proposal") %> +
+