2017-12-28 19:44:11 +01:00
module Manager
class ProceduresController < Manager :: ApplicationController
2022-12-18 11:52:38 +01:00
CSV_MAX_SIZE = 1 . megabytes
CSV_ACCEPTED_CONTENT_TYPES = [
" text/csv " ,
" application/vnd.ms-excel "
]
2019-10-31 16:30:39 +01:00
#
# Administrate overrides
#
# Override this if you have certain roles that require a subset
# this will be used to set the records shown on the `index` action.
def scoped_resource
if unfiltered_list?
2020-03-26 09:08:52 +01:00
# Don't display discarded demarches in the unfiltered list…
2020-02-05 16:09:03 +01:00
Procedure . kept
2019-10-31 16:30:39 +01:00
else
# … but allow them to be searched and displayed.
2020-02-05 16:09:03 +01:00
Procedure . with_discarded
2019-10-31 16:30:39 +01:00
end
end
2018-01-10 16:46:12 +01:00
def whitelist
procedure . whitelist!
2018-09-05 14:48:42 +02:00
flash [ :notice ] = " Démarche whitelistée. "
2018-01-10 16:46:12 +01:00
redirect_to manager_procedure_path ( procedure )
end
2018-10-09 12:11:13 +02:00
2023-07-21 16:15:25 +02:00
def hide_as_template
procedure . hide_as_template!
flash [ :notice ] = " Démarche non visible dans les modèles. "
redirect_to manager_procedure_path ( procedure )
end
def unhide_as_template
procedure . unhide_as_template!
flash [ :notice ] = " Démarche visible dans les modèles. "
redirect_to manager_procedure_path ( procedure )
end
2020-03-26 09:08:52 +01:00
def discard
2020-11-05 15:09:11 +01:00
procedure . discard_and_keep_track! ( current_super_admin )
2020-03-26 09:08:52 +01:00
2020-11-05 15:09:11 +01:00
logger . info ( " La démarche #{ procedure . id } est supprimée par #{ current_super_admin . email } " )
2020-03-26 09:08:52 +01:00
flash [ :notice ] = " La démarche #{ procedure . id } a été supprimée. "
redirect_to manager_procedure_path ( procedure )
2018-11-20 17:38:04 +01:00
end
2019-02-27 14:42:21 +01:00
2020-03-26 17:35:50 +01:00
def restore
2020-11-05 15:09:11 +01:00
procedure . restore ( current_super_admin )
2020-03-26 17:35:50 +01:00
flash [ :notice ] = " La démarche #{ procedure . id } a été restauré. "
redirect_to manager_procedure_path ( procedure )
end
2020-05-18 11:07:51 +02:00
def export_mail_brouillons
2020-05-18 12:17:21 +02:00
dossiers = procedure . dossiers . state_brouillon . includes ( :user )
2021-05-01 12:20:24 +02:00
emails = dossiers . map { | dossier | dossier . user_email_for ( :display ) } . sort . uniq
2020-05-18 11:07:51 +02:00
date = Time . zone . now . strftime ( '%d-%m-%Y' )
send_data ( emails . join ( " \n " ) , :filename = > " brouillons- #{ procedure . id } -au- #{ date } .csv " )
end
2022-07-21 13:23:13 +02:00
def add_administrateur_and_instructeur
administrateur = Administrateur . by_email ( current_super_admin . email )
instructeur = Instructeur . by_email ( current_super_admin . email )
if administrateur && instructeur
ActiveRecord :: Base . transaction do
AdministrateursProcedure . create! ( procedure : procedure , administrateur : administrateur , manager : true )
procedure . groupe_instructeurs . map do | groupe_instructeur |
instructeur . assign_to . create ( groupe_instructeur : groupe_instructeur , manager : true )
end
2021-10-14 17:16:12 +02:00
end
2022-07-21 13:23:13 +02:00
2022-08-23 13:25:58 +02:00
flash [ :notice ] = " L’ administrateur \" #{ administrateur . email } \" a été ajouté à la démarche. L'instructeur \" #{ instructeur . email } \" a été ajouté aux #{ procedure . groupe_instructeurs . count } groupes d'instructeurs "
2019-02-27 14:42:21 +01:00
else
2022-07-21 13:23:13 +02:00
flash [ :alert ] = " L’ administrateur \" #{ administrateur . email } \" est introuvable. "
end
redirect_to manager_procedure_path ( procedure )
end
def add_administrateur_with_confirmation
2022-09-09 11:45:14 +02:00
redirect_to new_manager_procedure_confirmation_url_path ( procedure , email : params [ :email ] )
2022-07-21 13:23:13 +02:00
end
2022-08-10 16:25:50 +02:00
def delete_administrateur
administrateur = procedure . administrateurs . find { | admin | admin . email == current_super_admin . email }
if administrateur . present?
procedure . administrateurs . delete ( administrateur )
end
2022-09-13 08:45:36 +02:00
instructeur = Instructeur . by_email ( current_super_admin . email )
if instructeur . present?
procedure . groupe_instructeurs . map do | groupe_instructeur |
groupe_instructeur . assign_tos . where ( instructeur : instructeur ) . destroy_all
end
end
2022-08-10 16:25:50 +02:00
redirect_to manager_procedure_path ( procedure )
end
2019-04-17 16:36:57 +02:00
def change_piece_justificative_template
if type_de_champ . update ( type_de_champ_params )
flash [ :notice ] = " Le modèle est mis à jour. "
else
flash [ :alert ] = type_de_champ . errors . full_messages . join ( ', ' )
end
redirect_to manager_procedure_path ( procedure )
end
2022-10-31 19:30:18 +01:00
def add_tags
tags_h = { tags : JSON . parse ( tags_params [ :tags ] ) }
if procedure . update ( tags_h )
flash . notice = " Le modèle est mis à jour. "
else
flash . alert = procedure . errors . full_messages . join ( ', ' )
end
redirect_to manager_procedure_path ( procedure )
end
2024-01-26 10:30:16 +01:00
def update_template_status
2024-01-30 11:16:24 +01:00
if procedure . update ( template_params )
2024-01-26 10:30:16 +01:00
redirect_to manager_procedure_path ( procedure ) , notice : 'Le statut de modèle a été mis à jour.'
else
flash . alert = procedure . errors . full_messages . join ( ', ' )
end
end
2022-12-18 11:52:38 +01:00
def import_data
end
def import_tags
if ! CSV_ACCEPTED_CONTENT_TYPES . include? ( tags_csv_file . content_type ) && ! CSV_ACCEPTED_CONTENT_TYPES . include? ( marcel_content_type )
flash [ :alert ] = " Importation impossible : veuillez importer un fichier CSV "
elsif tags_csv_file . size > CSV_MAX_SIZE
flash [ :alert ] = " Importation impossible : le poids du fichier est supérieur à #{ number_to_human_size ( CSV_MAX_SIZE ) } "
else
file = tags_csv_file . read
base_encoding = CharlockHolmes :: EncodingDetector . detect ( file )
procedure_tags = ACSV :: CSV . new_for_ruby3 ( file . encode ( " UTF-8 " , base_encoding [ :encoding ] , invalid : :replace , replace : " " ) , headers : true , header_converters : :downcase )
. map { | r | r . to_h . slice ( 'demarche' , 'tag' ) }
invalid_ids = [ ]
procedure_tags . each do | procedure_tag |
procedure = Procedure . find_by ( id : procedure_tag [ 'demarche' ] )
tags = procedure_tag [ " tag " ] . split ( ',' ) . map ( & :strip ) . map ( & :capitalize )
if procedure . nil?
invalid_ids << procedure_tag [ 'demarche' ]
next
end
tags . each do | tag |
procedure . tags . push ( tag )
end
procedure . save
end
end
message = " Import des tags terminé. "
message += " Ces démarches n'existent pas : #{ invalid_ids . to_sentence } " if invalid_ids . any?
flash . notice = message
redirect_to manager_administrateurs_path
end
2019-04-17 16:33:24 +02:00
private
def procedure
2020-03-26 09:08:52 +01:00
@procedure || = Procedure . with_discarded . find ( params [ :id ] )
2019-04-17 16:33:24 +02:00
end
2019-04-17 16:36:57 +02:00
def type_de_champ
TypeDeChamp . find ( params [ :type_de_champ ] [ :id ] )
end
def type_de_champ_params
params . require ( :type_de_champ ) . permit ( :piece_justificative_template )
end
2019-10-31 16:30:39 +01:00
2022-10-31 19:30:18 +01:00
def tags_params
params . require ( :procedure ) . permit ( :tags )
end
2024-01-30 11:16:24 +01:00
def template_params
params . require ( :procedure ) . permit ( :template )
2024-01-26 10:30:16 +01:00
end
2022-12-18 11:52:38 +01:00
def tags_csv_file
params [ :tags_csv_file ]
end
def marcel_content_type
Marcel :: MimeType . for ( tags_csv_file . read , name : tags_csv_file . original_filename , declared_type : tags_csv_file . content_type )
end
2019-10-31 16:30:39 +01:00
def unfiltered_list?
action_name == " index " && ! params [ :search ]
end
2017-12-28 19:44:11 +01:00
end
end