2023-11-14 17:46:52 +01:00
|
|
|
|
module Administrateurs
|
|
|
|
|
class AttestationTemplateV2sController < AdministrateurController
|
2023-12-22 14:13:20 +01:00
|
|
|
|
include UninterlacePngConcern
|
|
|
|
|
|
2024-02-20 13:12:14 +01:00
|
|
|
|
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
|
2024-01-18 12:38:46 +01:00
|
|
|
|
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
|
|
|
|
|
2023-11-15 09:55:33 +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])
|
|
|
|
|
|
2024-05-29 23:00:34 +02:00
|
|
|
|
pdf = WeasyprintService.generate_pdf(html, procedure_id: @procedure.id, path: request.path, user_id: current_user.id)
|
|
|
|
|
|
|
|
|
|
send_data(pdf, filename: 'attestation.pdf', type: 'application/pdf', disposition: 'inline')
|
2023-11-15 09:55:33 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
2023-11-14 17:46:52 +01:00
|
|
|
|
end
|
|
|
|
|
|
2023-11-15 09:53:07 +01:00
|
|
|
|
def edit
|
2023-11-28 11:44:32 +01:00
|
|
|
|
@buttons = [
|
|
|
|
|
[
|
|
|
|
|
['Gras', 'bold', 'bold'],
|
|
|
|
|
['Italic', 'italic', 'italic'],
|
|
|
|
|
['Souligner', 'underline', 'underline']
|
|
|
|
|
],
|
|
|
|
|
[
|
2024-01-25 13:20:43 +01:00
|
|
|
|
['Titre', 'title', :hidden], # only for "title" section, without any action possible
|
|
|
|
|
['Sous titre', 'heading2', 'h-1'],
|
|
|
|
|
['Titre de section', 'heading3', 'h-2']
|
2023-11-28 11:44:32 +01:00
|
|
|
|
],
|
|
|
|
|
[
|
|
|
|
|
['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']
|
|
|
|
|
]
|
|
|
|
|
]
|
2024-02-01 18:55:41 +01:00
|
|
|
|
|
|
|
|
|
@attestation_template.validate
|
2023-11-15 09:53:07 +01:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def update
|
2023-12-22 14:13:20 +01:00
|
|
|
|
attestation_params = editor_params
|
2024-05-28 17:47:05 +02:00
|
|
|
|
|
2024-06-01 22:39:04 +02:00
|
|
|
|
# toggle activation
|
|
|
|
|
if @attestation_template.persisted? && @attestation_template.activated? != cast_bool(attestation_params[:activated])
|
|
|
|
|
@procedure.attestation_templates.v2.update_all(activated: attestation_params[:activated])
|
|
|
|
|
render :update && return
|
|
|
|
|
end
|
|
|
|
|
|
2024-05-28 17:47:05 +02:00
|
|
|
|
if @attestation_template.published?
|
|
|
|
|
@attestation_template = @attestation_template.dup
|
|
|
|
|
@attestation_template.state = :draft
|
|
|
|
|
@attestation_template.procedure = @procedure
|
|
|
|
|
end
|
|
|
|
|
|
2023-12-22 14:13:20 +01:00
|
|
|
|
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
|
|
|
|
|
|
2024-05-28 17:47:05 +02:00
|
|
|
|
@attestation_template.assign_attributes(attestation_params)
|
|
|
|
|
|
|
|
|
|
if @attestation_template.invalid?
|
|
|
|
|
flash.alert = "L’attestation contient des erreurs et n'a pas pu être enregistrée. Corriger les erreurs."
|
|
|
|
|
else
|
|
|
|
|
# - draft just published
|
|
|
|
|
if @attestation_template.published? && should_edit_draft?
|
|
|
|
|
published = @procedure.attestation_templates.published
|
2024-02-01 18:55:41 +01:00
|
|
|
|
|
2024-05-28 17:47:05 +02:00
|
|
|
|
@attestation_template.transaction do
|
|
|
|
|
were_published = published.destroy_all
|
|
|
|
|
@attestation_template.save!
|
|
|
|
|
flash.notice = were_published.any? ? "La nouvelle version de l’attestation a été publiée." : "L’attestation a été publiée."
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
redirect_to edit_admin_procedure_attestation_template_v2_path(@procedure)
|
|
|
|
|
else
|
|
|
|
|
# - draft updated
|
|
|
|
|
# - or, attestation already published, without need for publication (draft procedure)
|
|
|
|
|
@attestation_template.save!
|
|
|
|
|
render :update
|
|
|
|
|
end
|
|
|
|
|
end
|
2024-01-12 12:49:49 +01:00
|
|
|
|
end
|
|
|
|
|
|
2024-01-22 17:29:24 +01:00
|
|
|
|
def create = update
|
|
|
|
|
|
2024-05-31 10:03:23 +02:00
|
|
|
|
def reset
|
|
|
|
|
@procedure.attestation_templates_v2.draft&.destroy_all
|
|
|
|
|
|
|
|
|
|
flash.notice = "Les modifications ont été réinitialisées."
|
|
|
|
|
redirect_to edit_admin_procedure_attestation_template_v2_path(@procedure)
|
|
|
|
|
end
|
|
|
|
|
|
2023-11-14 17:46:52 +01:00
|
|
|
|
private
|
|
|
|
|
|
2023-11-15 10:33:48 +01:00
|
|
|
|
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
|
2024-05-28 17:47:05 +02:00
|
|
|
|
v2s = @procedure.attestation_templates_v2
|
|
|
|
|
@attestation_template = v2s.find(&:draft?) || v2s.find(&:published?) || @procedure.build_attestation_template(version: 2, json_body: AttestationTemplate::TIPTAP_BODY_DEFAULT, state: :draft)
|
2023-11-14 17:46:52 +01:00
|
|
|
|
end
|
2023-11-15 09:53:07 +01:00
|
|
|
|
|
|
|
|
|
def editor_params
|
2024-06-01 22:39:04 +02:00
|
|
|
|
params.required(:attestation_template).permit(:activated, :official_layout, :label_logo, :label_direction, :tiptap_body, :footer, :logo, :signature, :activated, :state)
|
2023-11-15 09:53:07 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
2023-11-14 17:46:52 +01:00
|
|
|
|
end
|