Ajout departements

This commit is contained in:
Jean-Philippe Staelen 2026-01-22 09:38:29 +01:00
parent b123b75b27
commit 24e8a54ff9
5 changed files with 128 additions and 28 deletions

View File

@ -469,20 +469,8 @@ class CRVI_Event_Controller {
// Récupérer tous les départements actifs // Récupérer tous les départements actifs
$departements = CRVI_Departement_Model::all(true, true); $departements = CRVI_Departement_Model::all(true, true);
// Récupérer tous les types d'intervention disponibles depuis les CPT // Récupérer les types d'intervention groupés par département
$types_intervention_posts = get_posts([ $types_intervention_groupes = self::get_types_intervention_by_departement();
'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,
];
}
$response = [ $response = [
'intervenants' => $intervenants_formatted, 'intervenants' => $intervenants_formatted,
@ -490,7 +478,7 @@ class CRVI_Event_Controller {
'beneficiaires' => $beneficiaires, 'beneficiaires' => $beneficiaires,
'langues' => $langues, 'langues' => $langues,
'departements' => $departements, 'departements' => $departements,
'types_intervention' => $types_intervention, 'types_intervention_groupes' => $types_intervention_groupes,
]; ];
// Inclure les traducteurs seulement si la vérification a été faite // 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); 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. * Endpoint pour récupérer les permissions de l'utilisateur courant.
* @param WP_REST_Request $request * @param WP_REST_Request $request

View File

@ -69,8 +69,9 @@ class CRVI_Departement_Model extends Main_Model
if ($only_active && function_exists('get_field')) { if ($only_active && function_exists('get_field')) {
$posts = array_filter($posts, function($post) { $posts = array_filter($posts, function($post) {
$actif = get_field('actif', $post->ID); $actif = get_field('actif', $post->ID);
// Si le champ n'existe pas encore, considérer le département comme actif par défaut // N'afficher que les départements où le champ 'actif' est explicitement 'oui' (true, 1, '1', ou 'oui')
return ($actif === null || $actif === true || $actif === 1 || $actif === '1'); // Exclure ceux où le champ est null, false, 0, ou inexistant
return ($actif === true || $actif === 1 || $actif === '1' || $actif === 'oui');
}); });
} }

View File

@ -15,7 +15,10 @@ class CRVI_Agenda_View {
$locals = CRVI_Local_Model::get_locals([],true); $locals = CRVI_Local_Model::get_locals([],true);
$intervenants = CRVI_Intervenant_Model::get_intervenants([],true); $intervenants = CRVI_Intervenant_Model::get_intervenants([],true);
$departements = CRVI_Departement_Model::all(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); $traducteurs = CRVI_Traducteur_Model::get_traducteurs([],true);
$langues_beneficiaire = Api_Helper::get_languages(true); $langues_beneficiaire = Api_Helper::get_languages(true);
$genres = Api_Helper::get_acf_field_options('field_685e466352755'); $genres = Api_Helper::get_acf_field_options('field_685e466352755');
@ -45,4 +48,50 @@ class CRVI_Agenda_View {
echo '<p style="color:red">Template agenda-page.php introuvable.</p>'; echo '<p style="color:red">Template agenda-page.php introuvable.</p>';
} }
} }
/**
* 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;
}
} }

View File

@ -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}`); const typeInterventionSelect = document.getElementById(`type_intervention${prefix}`);
if (typeInterventionSelect && disponibilites.types_intervention) { if (typeInterventionSelect && disponibilites.types_intervention_groupes) {
disponibilites.types_intervention.forEach(type => { // Parcourir les départements et créer des optgroup
const option = document.createElement('option'); Object.keys(disponibilites.types_intervention_groupes).forEach(departementId => {
option.value = type.id; const departementData = disponibilites.types_intervention_groupes[departementId];
option.textContent = type.nom; const optgroup = document.createElement('optgroup');
typeInterventionSelect.appendChild(option); 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);
}); });
} }

View File

@ -55,10 +55,14 @@
<label for="type_intervention">Type d'intervention</label> <label for="type_intervention">Type d'intervention</label>
<select id="type_intervention" name="type_intervention" class="select2"> <select id="type_intervention" name="type_intervention" class="select2">
<option value="">Tous</option> <option value="">Tous</option>
<!-- Options dynamiques --> <!-- Options dynamiques groupées par département -->
<?php <?php
foreach ($types_intervention as $type_intervention) { foreach ($types_intervention_groupes as $departement_id => $departement_data) {
echo '<option value="' . $type_intervention['id'] . '">' . $type_intervention['nom'] . '</option>'; echo '<optgroup label="' . esc_attr($departement_data['nom']) . '">';
foreach ($departement_data['types'] as $type_intervention) {
echo '<option value="' . esc_attr($type_intervention['id']) . '">' . esc_html($type_intervention['nom']) . '</option>';
}
echo '</optgroup>';
} }
?> ?>
</select> </select>