2024-03-02 22:13:09 +01:00
|
|
|
class ExportTemplate < ApplicationRecord
|
|
|
|
include TagsSubstitutionConcern
|
|
|
|
|
|
|
|
belongs_to :groupe_instructeur
|
|
|
|
has_one :procedure, through: :groupe_instructeur
|
2024-03-12 17:28:43 +01:00
|
|
|
has_many :exports, dependent: :nullify
|
2024-03-03 10:11:26 +01:00
|
|
|
validates_with ExportTemplateValidator
|
2024-03-02 22:13:09 +01:00
|
|
|
|
|
|
|
DOSSIER_STATE = Dossier.states.fetch(:en_construction)
|
|
|
|
|
2024-03-03 10:19:02 +01:00
|
|
|
def set_default_values
|
|
|
|
content["default_dossier_directory"] = tiptap_json("dossier-")
|
|
|
|
content["pdf_name"] = tiptap_json("export_")
|
|
|
|
|
|
|
|
content["pjs"] = []
|
2024-04-26 15:04:34 +02:00
|
|
|
procedure.exportables_pieces_jointes.each do |pj|
|
2024-03-03 10:19:02 +01:00
|
|
|
content["pjs"] << { "stable_id" => pj.stable_id.to_s, "path" => tiptap_json("#{pj.libelle.parameterize}-") }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-03-02 22:13:09 +01:00
|
|
|
def tiptap_default_dossier_directory=(body)
|
|
|
|
self.content["default_dossier_directory"] = JSON.parse(body)
|
|
|
|
end
|
|
|
|
|
|
|
|
def tiptap_default_dossier_directory
|
|
|
|
tiptap_content("default_dossier_directory")
|
|
|
|
end
|
|
|
|
|
|
|
|
def tiptap_pdf_name=(body)
|
|
|
|
self.content["pdf_name"] = JSON.parse(body)
|
|
|
|
end
|
|
|
|
|
|
|
|
def tiptap_pdf_name
|
|
|
|
tiptap_content("pdf_name")
|
|
|
|
end
|
|
|
|
|
2024-03-03 10:03:12 +01:00
|
|
|
def content_for_pj(pj)
|
|
|
|
content_for_pj_id(pj.stable_id)&.to_json
|
|
|
|
end
|
|
|
|
|
2024-03-03 11:30:41 +01:00
|
|
|
def assign_pj_names(pj_params)
|
|
|
|
self.content["pjs"] = []
|
|
|
|
pj_params.each do |pj_param|
|
|
|
|
self.content["pjs"] << { stable_id: pj_param[0].delete_prefix("tiptap_pj_"), path: JSON.parse(pj_param[1]) }
|
|
|
|
end
|
2024-03-03 10:03:12 +01:00
|
|
|
end
|
|
|
|
|
2024-05-17 17:01:41 +02:00
|
|
|
def attachment_and_path(dossier, attachment, index: 0, row_index: nil, champ: nil)
|
2024-03-02 22:13:09 +01:00
|
|
|
[
|
|
|
|
attachment,
|
2024-05-28 11:06:31 +02:00
|
|
|
path(dossier, attachment, index:, row_index:, champ:)
|
2024-03-02 22:13:09 +01:00
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
def tiptap_convert(dossier, param)
|
|
|
|
if content[param]["content"]&.first&.[]("content")
|
|
|
|
render_attributes_for(content[param], dossier)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-03-19 11:03:29 +01:00
|
|
|
def tiptap_convert_pj(dossier, pj_stable_id, attachment = nil)
|
|
|
|
if content_for_pj_id(pj_stable_id)["content"]&.first&.[]("content")
|
|
|
|
render_attributes_for(content_for_pj_id(pj_stable_id), dossier, attachment)
|
2024-03-02 22:13:09 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-03-19 11:03:29 +01:00
|
|
|
def render_attributes_for(content_for, dossier, attachment = nil)
|
2024-03-02 22:13:09 +01:00
|
|
|
tiptap = TiptapService.new
|
|
|
|
used_tags = tiptap.used_tags_and_libelle_for(content_for.deep_symbolize_keys)
|
2024-05-15 10:30:51 +02:00
|
|
|
substitutions = tags_substitutions(used_tags, dossier, escape: false, memoize: true)
|
2024-03-19 11:03:29 +01:00
|
|
|
substitutions['original-filename'] = attachment.filename.base if attachment
|
2024-03-02 22:13:09 +01:00
|
|
|
tiptap.to_path(content_for.deep_symbolize_keys, substitutions)
|
|
|
|
end
|
|
|
|
|
2024-03-03 11:13:07 +01:00
|
|
|
def specific_tags
|
|
|
|
tags_categorized.slice(:individual, :etablissement, :dossier).values.flatten
|
|
|
|
end
|
|
|
|
|
2024-03-19 11:03:29 +01:00
|
|
|
def tags_for_pj
|
|
|
|
specific_tags.push({
|
|
|
|
libelle: 'nom original du fichier',
|
|
|
|
id: 'original-filename',
|
|
|
|
maybe_null: false
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2024-03-02 22:13:09 +01:00
|
|
|
private
|
|
|
|
|
|
|
|
def tiptap_content(key)
|
|
|
|
content[key]&.to_json
|
|
|
|
end
|
|
|
|
|
|
|
|
def tiptap_json(prefix)
|
|
|
|
{
|
|
|
|
"type" => "doc",
|
|
|
|
"content" => [
|
|
|
|
{ "type" => "paragraph", "content" => [{ "text" => prefix, "type" => "text" }, { "type" => "mention", "attrs" => DOSSIER_ID_TAG.stringify_keys }] }
|
|
|
|
]
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def content_for_pj_id(stable_id)
|
|
|
|
content_for_stable_id = content["pjs"].find { _1.symbolize_keys[:stable_id] == stable_id.to_s }
|
|
|
|
content_for_stable_id.symbolize_keys.fetch(:path)
|
|
|
|
end
|
|
|
|
|
2024-03-20 16:34:46 +01:00
|
|
|
def folder(dossier)
|
|
|
|
render_attributes_for(content["default_dossier_directory"], dossier)
|
|
|
|
end
|
|
|
|
|
|
|
|
def export_path(dossier)
|
|
|
|
File.join(folder(dossier), export_filename(dossier))
|
|
|
|
end
|
|
|
|
|
|
|
|
def export_filename(dossier)
|
|
|
|
"#{render_attributes_for(content["pdf_name"], dossier)}.pdf"
|
|
|
|
end
|
2024-03-02 22:13:09 +01:00
|
|
|
|
2024-05-28 11:06:31 +02:00
|
|
|
def path(dossier, attachment, index: 0, row_index: nil, champ: nil)
|
2024-03-02 22:13:09 +01:00
|
|
|
if attachment.name == 'pdf_export_for_instructeur'
|
|
|
|
return export_path(dossier)
|
|
|
|
end
|
|
|
|
|
|
|
|
dir_path = case attachment.record_type
|
|
|
|
when 'Dossier'
|
|
|
|
'dossier'
|
|
|
|
when 'Commentaire'
|
|
|
|
'messagerie'
|
|
|
|
when 'Avis'
|
|
|
|
'avis'
|
2024-05-28 11:35:49 +02:00
|
|
|
when 'Attestation'
|
|
|
|
'pieces_justificatives'
|
2024-03-02 22:13:09 +01:00
|
|
|
else
|
|
|
|
# for attachment
|
2024-05-17 17:01:41 +02:00
|
|
|
return attachment_path(dossier, attachment, index, row_index, champ)
|
2024-03-02 22:13:09 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
File.join(folder(dossier), dir_path, attachment.filename.to_s)
|
|
|
|
end
|
|
|
|
|
2024-05-17 17:01:41 +02:00
|
|
|
def attachment_path(dossier, attachment, index, row_index, champ)
|
|
|
|
stable_id = champ.stable_id
|
2024-03-02 22:13:09 +01:00
|
|
|
tiptap_pj = content["pjs"].find { |pj| pj["stable_id"] == stable_id.to_s }
|
|
|
|
if tiptap_pj
|
2024-03-19 11:03:29 +01:00
|
|
|
File.join(folder(dossier), tiptap_convert_pj(dossier, stable_id, attachment) + suffix(attachment, index, row_index))
|
2024-03-02 22:13:09 +01:00
|
|
|
else
|
|
|
|
File.join(folder(dossier), "erreur_renommage", attachment.filename.to_s)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def suffix(attachment, index, row_index)
|
2024-05-01 21:25:43 +02:00
|
|
|
suffix = "-#{index + 1}"
|
|
|
|
suffix += "-#{row_index + 1}" if row_index.present?
|
2024-03-02 22:13:09 +01:00
|
|
|
|
|
|
|
suffix + attachment.filename.extension_with_delimiter
|
|
|
|
end
|
|
|
|
end
|