2024-04-29 00:17:15 +02:00
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2021-11-25 16:26:55 +01:00
|
|
|
|
module Administrateurs
|
2020-07-29 12:11:30 +02:00
|
|
|
|
class AttestationTemplatesController < AdministrateurController
|
2023-09-19 15:37:28 +02:00
|
|
|
|
include UninterlacePngConcern
|
|
|
|
|
|
2020-07-29 12:11:30 +02:00
|
|
|
|
before_action :retrieve_procedure
|
2024-02-13 16:52:09 +01:00
|
|
|
|
before_action :preload_revisions
|
2020-07-29 12:11:30 +02:00
|
|
|
|
|
2022-11-09 12:09:24 +01:00
|
|
|
|
def show
|
|
|
|
|
redirect_to edit_admin_procedure_attestation_template_path(@procedure)
|
|
|
|
|
end
|
|
|
|
|
|
2020-07-29 12:11:30 +02:00
|
|
|
|
def edit
|
2022-11-09 12:09:24 +01:00
|
|
|
|
@attestation_template = build_attestation_template
|
2023-01-26 19:35:42 +01:00
|
|
|
|
@attestation_template.validate
|
2020-07-29 12:11:30 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def update
|
2024-01-10 13:37:55 +01:00
|
|
|
|
@attestation_template = @procedure.attestation_template_v1
|
2022-02-02 16:10:29 +01:00
|
|
|
|
|
2022-11-09 12:09:24 +01:00
|
|
|
|
if @attestation_template.update(activated_attestation_params)
|
2022-12-15 13:08:20 +01:00
|
|
|
|
flash.notice = "Le modèle de l’attestation a bien été modifié"
|
2022-02-02 16:10:29 +01:00
|
|
|
|
|
2022-11-09 12:09:24 +01:00
|
|
|
|
redirect_to edit_admin_procedure_attestation_template_path(@procedure)
|
2020-07-29 12:11:30 +02:00
|
|
|
|
else
|
2022-12-16 14:56:46 +01:00
|
|
|
|
flash.now.alert = "Le modèle de l’attestation contient des erreurs et n'a pas pu être enregistré. Veuiller les corriger"
|
2020-07-29 12:11:30 +02:00
|
|
|
|
|
2022-11-09 12:09:24 +01:00
|
|
|
|
render :edit
|
|
|
|
|
end
|
2020-07-29 12:11:30 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def create
|
2022-11-09 12:09:24 +01:00
|
|
|
|
@attestation_template = build_attestation_template(activated_attestation_params)
|
|
|
|
|
|
|
|
|
|
if @attestation_template.save
|
2022-12-15 13:08:20 +01:00
|
|
|
|
flash.notice = "Le modèle de l’attestation a bien été enregistré"
|
2022-11-09 12:09:24 +01:00
|
|
|
|
|
|
|
|
|
redirect_to edit_admin_procedure_attestation_template_path(@procedure)
|
2020-07-29 12:11:30 +02:00
|
|
|
|
else
|
2022-11-09 12:09:24 +01:00
|
|
|
|
flash.now.alert = @attestation_template.errors.full_messages
|
2020-07-29 12:11:30 +02:00
|
|
|
|
|
2022-11-09 12:09:24 +01:00
|
|
|
|
render :edit
|
|
|
|
|
end
|
2020-07-29 12:11:30 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def preview
|
2022-11-09 12:09:24 +01:00
|
|
|
|
@attestation = build_attestation_template.render_attributes_for({})
|
2020-07-29 12:11:30 +02:00
|
|
|
|
|
2021-11-25 16:26:55 +01:00
|
|
|
|
render 'administrateurs/attestation_templates/show', formats: [:pdf]
|
2020-07-29 12:11:30 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
2022-11-09 12:09:24 +01:00
|
|
|
|
def build_attestation_template(attributes = {})
|
2024-01-10 13:37:55 +01:00
|
|
|
|
attestation_template = @procedure.attestation_template_v1 || @procedure.build_attestation_template_v1
|
2022-02-02 16:10:29 +01:00
|
|
|
|
attestation_template.attributes = attributes
|
|
|
|
|
attestation_template
|
|
|
|
|
end
|
|
|
|
|
|
2020-07-29 12:11:30 +02:00
|
|
|
|
def activated_attestation_params
|
|
|
|
|
# cache result to avoid multiple uninterlaced computations
|
|
|
|
|
if @activated_attestation_params.nil?
|
|
|
|
|
@activated_attestation_params = params.require(:attestation_template)
|
|
|
|
|
.permit(:title, :body, :footer, :activated, :logo, :signature)
|
|
|
|
|
|
|
|
|
|
logo_file = params['attestation_template'].delete('logo')
|
|
|
|
|
signature_file = params['attestation_template'].delete('signature')
|
|
|
|
|
|
|
|
|
|
if logo_file.present?
|
2023-09-19 15:37:28 +02:00
|
|
|
|
@activated_attestation_params[:logo] = uninterlace_png(logo_file)
|
2020-07-29 12:11:30 +02:00
|
|
|
|
end
|
|
|
|
|
if signature_file.present?
|
2023-09-19 15:37:28 +02:00
|
|
|
|
@activated_attestation_params[:signature] = uninterlace_png(signature_file)
|
2020-07-29 12:11:30 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
@activated_attestation_params
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|