demarches-normaliennes/app/controllers/administrateurs/attestation_template_v2s_controller.rb

115 lines
3.5 KiB
Ruby
Raw Normal View History

2023-11-14 17:46:52 +01:00
module Administrateurs
class AttestationTemplateV2sController < AdministrateurController
include UninterlacePngConcern
before_action :retrieve_procedure
before_action :ensure_feature_active
before_action :retrieve_attestation_template
before_action :preload_revisions, only: [:edit, :update, :create]
2023-11-14 17:46:52 +01:00
def show
preview_dossier = @procedure.dossier_for_preview(current_user)
@body = @attestation_template.render_attributes_for(dossier: preview_dossier).fetch(:body)
2023-11-14 17:46:52 +01:00
respond_to do |format|
format.html do
render layout: 'attestation'
end
format.pdf do
html = render_to_string('/administrateurs/attestation_template_v2s/show', layout: 'attestation', formats: [:html])
headers = {
'Content-Type' => 'application/json',
'X-Request-Id' => Current.request_id
}
body = {
html: html,
upstream_context: {
procedure_id: @procedure.id,
path: request.path,
user_id: current_user.id
}
}.to_json
2024-03-14 14:04:43 +01:00
response = Typhoeus.post(WEASYPRINT_URL, headers:, body:)
2024-03-14 14:04:43 +01:00
if response.success?
send_data(response.body, filename: 'attestation.pdf', type: 'application/pdf', disposition: 'inline')
else
raise StandardError.new("PDF Generation failed: #{response.return_code} #{response.status_message}")
end
end
end
2023-11-14 17:46:52 +01:00
end
2023-11-15 09:53:07 +01:00
def edit
@buttons = [
[
['Gras', 'bold', 'bold'],
['Italic', 'italic', 'italic'],
['Souligner', 'underline', 'underline']
],
[
['Titre', 'title', :hidden], # only for "title" section, without any action possible
['Sous titre', 'heading2', 'h-1'],
['Titre de section', 'heading3', 'h-2']
],
[
['Liste à puces', 'bulletList', 'list-unordered'],
['Liste numérotée', 'orderedList', 'list-ordered']
],
[
['Aligner à gauche', 'left', 'align-left'],
['Aligner au centre', 'center', 'align-center'],
['Aligner à droite', 'right', 'align-right']
],
[
['Undo', 'undo', 'arrow-go-back-line'],
['Redo', 'redo', 'arrow-go-forward-line']
]
]
@attestation_template.validate
2023-11-15 09:53:07 +01:00
end
def update
attestation_params = editor_params
logo_file = attestation_params.delete(:logo)
signature_file = attestation_params.delete(:signature)
if logo_file
attestation_params[:logo] = uninterlace_png(logo_file)
end
if signature_file
attestation_params[:signature] = uninterlace_png(signature_file)
end
if !@attestation_template.update(attestation_params)
flash.alert = "Le modèle de lattestation contient des erreurs et n'a pas pu être enregistré. Corriger les erreurs."
2024-01-22 17:29:24 +01:00
end
render :update
end
2024-01-22 17:29:24 +01:00
def create = update
2023-11-14 17:46:52 +01:00
private
def ensure_feature_active
redirect_to root_path if !@procedure.feature_enabled?(:attestation_v2)
end
2023-11-14 17:46:52 +01:00
def retrieve_attestation_template
@attestation_template = @procedure.attestation_template_v2 || @procedure.build_attestation_template_v2(json_body: AttestationTemplate::TIPTAP_BODY_DEFAULT)
2023-11-14 17:46:52 +01:00
end
2023-11-15 09:53:07 +01:00
def editor_params
params.required(:attestation_template).permit(:official_layout, :label_logo, :label_direction, :tiptap_body, :footer, :logo, :signature, :activated)
2023-11-15 09:53:07 +01:00
end
end
2023-11-14 17:46:52 +01:00
end