From 24e8a54ff9987a6f3624e13fd75c9707d27bcc47 Mon Sep 17 00:00:00 2001 From: jps Date: Thu, 22 Jan 2026 09:38:29 +0100 Subject: [PATCH] Ajout departements --- app/controllers/Event_Controller.php | 64 ++++++++++++++----- app/models/Departement_Model.php | 5 +- app/views/Agenda_View.php | 51 ++++++++++++++- .../js/modules/agenda-intervenant-calendar.js | 26 ++++++-- templates/admin/agenda-page.php | 10 ++- 5 files changed, 128 insertions(+), 28 deletions(-) diff --git a/app/controllers/Event_Controller.php b/app/controllers/Event_Controller.php index 5f20726..d12668c 100644 --- a/app/controllers/Event_Controller.php +++ b/app/controllers/Event_Controller.php @@ -469,20 +469,8 @@ class CRVI_Event_Controller { // Récupérer tous les départements actifs $departements = CRVI_Departement_Model::all(true, true); - // Récupérer tous les types d'intervention disponibles depuis les CPT - $types_intervention_posts = get_posts([ - 'post_type' => 'type_intervention', - 'numberposts' => -1, - 'post_status' => 'publish', - ]); - - $types_intervention = []; - foreach ($types_intervention_posts as $post) { - $types_intervention[] = [ - 'id' => $post->ID, - 'nom' => $post->post_title, - ]; - } + // Récupérer les types d'intervention groupés par département + $types_intervention_groupes = self::get_types_intervention_by_departement(); $response = [ 'intervenants' => $intervenants_formatted, @@ -490,7 +478,7 @@ class CRVI_Event_Controller { 'beneficiaires' => $beneficiaires, 'langues' => $langues, 'departements' => $departements, - 'types_intervention' => $types_intervention, + 'types_intervention_groupes' => $types_intervention_groupes, ]; // Inclure les traducteurs seulement si la vérification a été faite @@ -501,6 +489,52 @@ class CRVI_Event_Controller { return Api_Helper::json_success($response); } + /** + * Récupère les types d'intervention groupés par département + * @return array Structure: [departement_id => ['nom' => string, 'types' => [['id' => int, 'nom' => string]]]] + */ + private static function get_types_intervention_by_departement() { + $departements = CRVI_Departement_Model::all(true, true); + $result = []; + + foreach ($departements as $departement) { + $departement_id = $departement['id']; + $departement_nom = $departement['nom']; + + // Récupérer le repeater type_dinterventions + if (function_exists('get_field')) { + $type_dinterventions = get_field('type_dinterventions', $departement_id); + + if (is_array($type_dinterventions) && !empty($type_dinterventions)) { + $types = []; + foreach ($type_dinterventions as $item) { + // Le repeater contient 'type' (texte) et 'article_intervention' (ID du CPT) + $article_intervention_id = $item['article_intervention'] ?? null; + + if ($article_intervention_id) { + $type_intervention_post = get_post($article_intervention_id); + if ($type_intervention_post && $type_intervention_post->post_type === 'type_intervention') { + $types[] = [ + 'id' => $type_intervention_post->ID, + 'nom' => $type_intervention_post->post_title, + ]; + } + } + } + + if (!empty($types)) { + $result[$departement_id] = [ + 'nom' => $departement_nom, + 'types' => $types, + ]; + } + } + } + } + + return $result; + } + /** * Endpoint pour récupérer les permissions de l'utilisateur courant. * @param WP_REST_Request $request diff --git a/app/models/Departement_Model.php b/app/models/Departement_Model.php index 21cd5c3..55e571b 100644 --- a/app/models/Departement_Model.php +++ b/app/models/Departement_Model.php @@ -69,8 +69,9 @@ class CRVI_Departement_Model extends Main_Model if ($only_active && function_exists('get_field')) { $posts = array_filter($posts, function($post) { $actif = get_field('actif', $post->ID); - // Si le champ n'existe pas encore, considérer le département comme actif par défaut - return ($actif === null || $actif === true || $actif === 1 || $actif === '1'); + // N'afficher que les départements où le champ 'actif' est explicitement 'oui' (true, 1, '1', ou 'oui') + // Exclure ceux où le champ est null, false, 0, ou inexistant + return ($actif === true || $actif === 1 || $actif === '1' || $actif === 'oui'); }); } diff --git a/app/views/Agenda_View.php b/app/views/Agenda_View.php index b332ca7..8e7a9de 100644 --- a/app/views/Agenda_View.php +++ b/app/views/Agenda_View.php @@ -15,7 +15,10 @@ class CRVI_Agenda_View { $locals = CRVI_Local_Model::get_locals([],true); $intervenants = CRVI_Intervenant_Model::get_intervenants([],true); $departements = CRVI_Departement_Model::all(true); - $types_intervention = CRVI_Type_Intervention_Model::all(true); + + // Récupérer les types d'intervention groupés par département + $types_intervention_groupes = self::get_types_intervention_by_departement(); + $traducteurs = CRVI_Traducteur_Model::get_traducteurs([],true); $langues_beneficiaire = Api_Helper::get_languages(true); $genres = Api_Helper::get_acf_field_options('field_685e466352755'); @@ -45,4 +48,50 @@ class CRVI_Agenda_View { echo '

Template agenda-page.php introuvable.

'; } } + + /** + * Récupère les types d'intervention groupés par département + * @return array Structure: [departement_id => ['nom' => string, 'types' => [['id' => int, 'nom' => string]]]] + */ + private static function get_types_intervention_by_departement() { + $departements = CRVI_Departement_Model::all(true); + $result = []; + + foreach ($departements as $departement) { + $departement_id = $departement['id']; + $departement_nom = $departement['nom']; + + // Récupérer le repeater type_dinterventions + if (function_exists('get_field')) { + $type_dinterventions = get_field('type_dinterventions', $departement_id); + + if (is_array($type_dinterventions) && !empty($type_dinterventions)) { + $types = []; + foreach ($type_dinterventions as $item) { + // Le repeater contient 'type' (texte) et 'article_intervention' (ID du CPT) + $article_intervention_id = $item['article_intervention'] ?? null; + + if ($article_intervention_id) { + $type_intervention_post = get_post($article_intervention_id); + if ($type_intervention_post && $type_intervention_post->post_type === 'type_intervention') { + $types[] = [ + 'id' => $type_intervention_post->ID, + 'nom' => $type_intervention_post->post_title, + ]; + } + } + } + + if (!empty($types)) { + $result[$departement_id] = [ + 'nom' => $departement_nom, + 'types' => $types, + ]; + } + } + } + } + + return $result; + } } \ No newline at end of file diff --git a/assets/js/modules/agenda-intervenant-calendar.js b/assets/js/modules/agenda-intervenant-calendar.js index 578c3cf..93eff3f 100644 --- a/assets/js/modules/agenda-intervenant-calendar.js +++ b/assets/js/modules/agenda-intervenant-calendar.js @@ -775,14 +775,26 @@ async function loadFilterOptions(mode = 'intervenant') { }); } - // Types d'intervention + // Types d'intervention groupés par département const typeInterventionSelect = document.getElementById(`type_intervention${prefix}`); - if (typeInterventionSelect && disponibilites.types_intervention) { - disponibilites.types_intervention.forEach(type => { - const option = document.createElement('option'); - option.value = type.id; - option.textContent = type.nom; - typeInterventionSelect.appendChild(option); + if (typeInterventionSelect && disponibilites.types_intervention_groupes) { + // Parcourir les départements et créer des optgroup + Object.keys(disponibilites.types_intervention_groupes).forEach(departementId => { + const departementData = disponibilites.types_intervention_groupes[departementId]; + const optgroup = document.createElement('optgroup'); + optgroup.label = departementData.nom; + + // Ajouter les types d'intervention de ce département + if (departementData.types && Array.isArray(departementData.types)) { + departementData.types.forEach(type => { + const option = document.createElement('option'); + option.value = type.id; + option.textContent = type.nom; + optgroup.appendChild(option); + }); + } + + typeInterventionSelect.appendChild(optgroup); }); } diff --git a/templates/admin/agenda-page.php b/templates/admin/agenda-page.php index 252f717..0bc45ef 100644 --- a/templates/admin/agenda-page.php +++ b/templates/admin/agenda-page.php @@ -55,10 +55,14 @@