2017-06-09 13:28:47 +02:00
|
|
|
class Admin::AttestationTemplatesController < AdminController
|
|
|
|
before_action :retrieve_procedure
|
|
|
|
|
|
|
|
def edit
|
|
|
|
@attestation_template = @procedure.attestation_template || AttestationTemplate.new(procedure: @procedure)
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
attestation_template = @procedure.attestation_template
|
|
|
|
|
2018-03-02 16:27:03 +01:00
|
|
|
if attestation_template.update(activated_attestation_params)
|
2017-06-09 13:28:47 +02:00
|
|
|
flash.notice = "L'attestation a bien été modifiée"
|
|
|
|
else
|
|
|
|
flash.alert = attestation_template.errors.full_messages.join('<br>')
|
|
|
|
end
|
|
|
|
|
|
|
|
redirect_to edit_admin_procedure_attestation_template_path(@procedure)
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
attestation_template = AttestationTemplate.new(activated_attestation_params.merge(procedure_id: @procedure.id))
|
|
|
|
|
|
|
|
if attestation_template.save
|
|
|
|
flash.notice = "L'attestation a bien été sauvegardée"
|
|
|
|
else
|
|
|
|
flash.alert = attestation_template.errors.full_messages.join('<br>')
|
|
|
|
end
|
|
|
|
|
|
|
|
redirect_to edit_admin_procedure_attestation_template_path(@procedure)
|
|
|
|
end
|
|
|
|
|
|
|
|
def disactivate
|
|
|
|
attestation_template = @procedure.attestation_template
|
|
|
|
attestation_template.activated = false
|
|
|
|
attestation_template.save
|
|
|
|
|
|
|
|
flash.notice = "L'attestation a bien été désactivée"
|
|
|
|
|
|
|
|
redirect_to edit_admin_procedure_attestation_template_path(@procedure)
|
|
|
|
end
|
|
|
|
|
2017-05-31 17:39:15 +02:00
|
|
|
def preview
|
2020-02-24 11:17:52 +01:00
|
|
|
attestation = (@procedure.attestation_template || AttestationTemplate.new)
|
|
|
|
attestation.assign_attributes(activated_attestation_params)
|
2017-05-31 17:39:15 +02:00
|
|
|
|
2020-02-24 11:17:52 +01:00
|
|
|
if attestation.valid?
|
|
|
|
@attestation = attestation.render_attributes_for(activated_attestation_params)
|
|
|
|
|
|
|
|
render 'admin/attestation_templates/show', formats: [:pdf]
|
|
|
|
else
|
2020-02-28 16:33:07 +01:00
|
|
|
flash.alert = attestation.errors.full_messages.join('<br>')
|
2020-02-24 11:17:52 +01:00
|
|
|
end
|
2017-05-31 17:39:15 +02:00
|
|
|
end
|
|
|
|
|
2018-09-24 10:13:46 +02:00
|
|
|
def delete_logo
|
2019-08-28 14:27:41 +02:00
|
|
|
@procedure.attestation_template.logo.purge_later
|
2018-09-24 10:13:46 +02:00
|
|
|
|
|
|
|
flash.notice = 'le logo a bien été supprimée'
|
|
|
|
redirect_to edit_admin_procedure_attestation_template_path(@procedure)
|
|
|
|
end
|
|
|
|
|
2018-09-24 10:13:30 +02:00
|
|
|
def delete_signature
|
2019-08-28 14:27:41 +02:00
|
|
|
@procedure.attestation_template.signature.purge_later
|
2018-09-24 10:13:30 +02:00
|
|
|
|
|
|
|
flash.notice = 'la signature a bien été supprimée'
|
|
|
|
redirect_to edit_admin_procedure_attestation_template_path(@procedure)
|
|
|
|
end
|
|
|
|
|
2017-06-09 13:28:47 +02:00
|
|
|
private
|
|
|
|
|
|
|
|
def activated_attestation_params
|
2017-07-05 16:05:37 +02:00
|
|
|
# cache result to avoid multiple uninterlaced computations
|
|
|
|
if @activated_attestation_params.nil?
|
|
|
|
@activated_attestation_params = params.require(:attestation_template)
|
2019-08-27 17:42:52 +02:00
|
|
|
.permit(:title, :body, :footer)
|
2017-07-05 16:05:37 +02:00
|
|
|
.merge(activated: true)
|
|
|
|
|
2019-08-27 17:42:52 +02:00
|
|
|
logo_file = params['attestation_template'].delete('logo')
|
|
|
|
signature_file = params['attestation_template'].delete('signature')
|
|
|
|
|
|
|
|
if logo_file.present?
|
2019-08-28 13:11:58 +02:00
|
|
|
@activated_attestation_params[:logo] = uninterlaced_png(logo_file)
|
2019-08-27 17:42:52 +02:00
|
|
|
end
|
|
|
|
if signature_file.present?
|
2019-08-28 13:11:58 +02:00
|
|
|
@activated_attestation_params[:signature] = uninterlaced_png(signature_file)
|
2019-08-27 17:42:52 +02:00
|
|
|
end
|
2017-07-05 16:05:37 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
@activated_attestation_params
|
|
|
|
end
|
|
|
|
|
|
|
|
def uninterlaced_png(uploaded_file)
|
2018-05-30 18:45:46 +02:00
|
|
|
if uploaded_file&.content_type == 'image/png'
|
2017-08-28 11:18:38 +02:00
|
|
|
chunky_img = ChunkyPNG::Image.from_io(uploaded_file)
|
|
|
|
chunky_img.save(uploaded_file.tempfile.to_path, interlace: false)
|
|
|
|
uploaded_file.tempfile.reopen(uploaded_file.tempfile.to_path, 'rb')
|
2017-07-05 16:05:37 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
uploaded_file
|
2017-06-09 13:28:47 +02:00
|
|
|
end
|
|
|
|
end
|