2024-04-29 00:17:15 +02:00
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2024-03-03 11:30:41 +01:00
|
|
|
|
module Instructeurs
|
|
|
|
|
class ExportTemplatesController < InstructeurController
|
2024-07-15 23:26:49 +02:00
|
|
|
|
before_action :set_procedure_and_groupe_instructeurs
|
2024-03-03 11:30:41 +01:00
|
|
|
|
before_action :set_export_template, only: [:edit, :update, :destroy]
|
2024-07-15 23:26:49 +02:00
|
|
|
|
before_action :ensure_legitimate_groupe_instructeur, only: [:create, :update]
|
2024-10-25 14:50:51 +02:00
|
|
|
|
before_action :set_types_de_champ, only: [:new, :edit]
|
2024-03-03 11:30:41 +01:00
|
|
|
|
|
|
|
|
|
def new
|
2024-10-25 14:50:51 +02:00
|
|
|
|
@export_template = export_template
|
2024-03-03 11:30:41 +01:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def create
|
2024-07-15 23:26:49 +02:00
|
|
|
|
@export_template = ExportTemplate.new(export_template_params)
|
|
|
|
|
|
2024-03-03 11:30:41 +01:00
|
|
|
|
if @export_template.save
|
2024-07-15 23:26:49 +02:00
|
|
|
|
redirect_to [:exports, :instructeur, @procedure], notice: "Le modèle d'export #{@export_template.name} a bien été créé"
|
2024-03-03 11:30:41 +01:00
|
|
|
|
else
|
|
|
|
|
flash[:alert] = @export_template.errors.full_messages
|
|
|
|
|
render :new
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def edit
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def update
|
2024-07-15 23:26:49 +02:00
|
|
|
|
if @export_template.update(export_template_params)
|
|
|
|
|
redirect_to [:exports, :instructeur, @procedure], notice: "Le modèle d'export #{@export_template.name} a bien été modifié"
|
2024-03-03 11:30:41 +01:00
|
|
|
|
else
|
|
|
|
|
flash[:alert] = @export_template.errors.full_messages
|
|
|
|
|
render :edit
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2024-03-12 17:28:43 +01:00
|
|
|
|
def destroy
|
|
|
|
|
if @export_template.destroy
|
2024-07-15 23:26:49 +02:00
|
|
|
|
redirect_to [:exports, :instructeur, @procedure], notice: "Le modèle d'export #{@export_template.name} a bien été supprimé"
|
2024-03-12 17:28:43 +01:00
|
|
|
|
else
|
2024-07-15 23:26:49 +02:00
|
|
|
|
redirect_to [:exports, :instructeur, @procedure], alert: "Le modèle d'export #{@export_template.name} n'a pu être supprimé"
|
2024-03-12 17:28:43 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2024-03-03 11:56:08 +01:00
|
|
|
|
def preview
|
2024-07-15 23:26:49 +02:00
|
|
|
|
export_template = ExportTemplate.new(export_template_params)
|
2024-04-30 00:01:45 +02:00
|
|
|
|
|
2024-07-15 23:26:49 +02:00
|
|
|
|
render turbo_stream: turbo_stream.replace('preview', partial: 'preview', locals: { export_template: })
|
2024-03-03 11:56:08 +01:00
|
|
|
|
end
|
|
|
|
|
|
2024-03-03 11:30:41 +01:00
|
|
|
|
private
|
|
|
|
|
|
2024-10-25 14:50:51 +02:00
|
|
|
|
def export_template = @export_template ||= ExportTemplate.default(groupe_instructeur: @groupe_instructeurs.first, kind:)
|
|
|
|
|
|
|
|
|
|
def kind = params[:kind] == 'zip' ? 'zip' : 'xlsx'
|
|
|
|
|
|
|
|
|
|
def set_types_de_champ
|
|
|
|
|
if export_template.tabular?
|
|
|
|
|
@types_de_champ_public = @procedure.all_revisions_types_de_champ(parent: nil, with_header_section: true).public_only
|
|
|
|
|
@types_de_champ_private = @procedure.all_revisions_types_de_champ(parent: nil, with_header_section: true).private_only
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2024-03-03 11:30:41 +01:00
|
|
|
|
def export_template_params
|
2024-10-25 14:50:51 +02:00
|
|
|
|
params
|
|
|
|
|
.require(:export_template)
|
|
|
|
|
.permit(
|
|
|
|
|
:name,
|
|
|
|
|
:kind,
|
|
|
|
|
:groupe_instructeur_id,
|
|
|
|
|
dossier_folder: [:enabled, :template],
|
|
|
|
|
export_pdf: [:enabled, :template],
|
|
|
|
|
pjs: [:stable_id, :enabled, :template],
|
|
|
|
|
exported_columns: []
|
|
|
|
|
)
|
2024-03-03 11:30:41 +01:00
|
|
|
|
end
|
|
|
|
|
|
2024-07-15 23:26:49 +02:00
|
|
|
|
def set_procedure_and_groupe_instructeurs
|
|
|
|
|
@procedure = current_instructeur.procedures.find(params[:procedure_id])
|
|
|
|
|
@groupe_instructeurs = current_instructeur.groupe_instructeurs.where(procedure: @procedure)
|
|
|
|
|
|
|
|
|
|
Sentry.configure_scope { |scope| scope.set_tags(procedure: @procedure.id) }
|
2024-03-03 11:30:41 +01:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def set_export_template
|
|
|
|
|
@export_template = current_instructeur.export_templates.find(params[:id])
|
|
|
|
|
end
|
|
|
|
|
|
2024-07-15 23:26:49 +02:00
|
|
|
|
def ensure_legitimate_groupe_instructeur
|
|
|
|
|
return if export_template_params[:groupe_instructeur_id].in?(@groupe_instructeurs.map { _1.id.to_s })
|
2024-03-03 11:30:41 +01:00
|
|
|
|
|
2024-07-15 23:26:49 +02:00
|
|
|
|
redirect_to [:exports, :instructeur, @procedure], alert: 'Vous n’avez pas le droit de créer un modèle d’export pour ce groupe'
|
2024-03-03 11:30:41 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|