2021-11-25 16:26:55 +01:00
|
|
|
|
module Administrateurs
|
2020-07-29 12:11:30 +02:00
|
|
|
|
class AttestationTemplatesController < AdministrateurController
|
|
|
|
|
before_action :retrieve_procedure
|
|
|
|
|
|
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
|
2020-07-29 12:11:30 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def update
|
2022-11-09 12:09:24 +01:00
|
|
|
|
@attestation_template = @procedure.attestation_template
|
2022-02-02 16:10:29 +01:00
|
|
|
|
|
2022-11-09 12:09:24 +01:00
|
|
|
|
if @attestation_template.update(activated_attestation_params)
|
|
|
|
|
flash.notice = "Le model de l’attestation a bien été modifiée"
|
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-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 create
|
2022-11-09 12:09:24 +01:00
|
|
|
|
@attestation_template = build_attestation_template(activated_attestation_params)
|
|
|
|
|
|
|
|
|
|
if @attestation_template.save
|
|
|
|
|
flash.notice = "Le model de l’attestation a bien été enregistrée"
|
|
|
|
|
|
|
|
|
|
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 = {})
|
|
|
|
|
attestation_template = @procedure.attestation_template || @procedure.build_attestation_template
|
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?
|
|
|
|
|
@activated_attestation_params[:logo] = uninterlaced_png(logo_file)
|
|
|
|
|
end
|
|
|
|
|
if signature_file.present?
|
|
|
|
|
@activated_attestation_params[:signature] = uninterlaced_png(signature_file)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
@activated_attestation_params
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def uninterlaced_png(uploaded_file)
|
|
|
|
|
if uploaded_file&.content_type == 'image/png'
|
2021-02-16 12:02:53 +01:00
|
|
|
|
chunky_img = ChunkyPNG::Image.from_io(uploaded_file.to_io)
|
2020-07-29 12:11:30 +02:00
|
|
|
|
chunky_img.save(uploaded_file.tempfile.to_path, interlace: false)
|
|
|
|
|
uploaded_file.tempfile.reopen(uploaded_file.tempfile.to_path, 'rb')
|
|
|
|
|
end
|
|
|
|
|
uploaded_file
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|