diff --git a/app/controllers/Notifications_Controller.php b/app/controllers/Notifications_Controller.php index 1a71bba..74ed17e 100644 --- a/app/controllers/Notifications_Controller.php +++ b/app/controllers/Notifications_Controller.php @@ -558,4 +558,156 @@ class CRVI_Notifications_Controller { return $results; } + + /** + * Enrichit les données d'un événement avec les informations complètes + * + * @param object $event L'événement de base + * @return array|null L'événement enrichi avec département, type, bénéficiaire, etc. + */ + public static function enrich_event_data($event) { + global $wpdb; + $table_name = $wpdb->prefix . 'crvi_agenda'; + + // Récupérer les informations complètes de l'événement + $full_event = $wpdb->get_row($wpdb->prepare( + "SELECT * FROM {$table_name} WHERE id = %d", + $event->id + )); + + if (!$full_event) { + return null; + } + + $enriched = [ + 'id' => $full_event->id, + 'date_rdv' => $full_event->date_rdv, + 'heure_rdv' => $full_event->heure_rdv, + 'type' => $full_event->type, + 'statut' => $full_event->statut, + 'commentaire' => $full_event->commentaire, + ]; + + // Récupérer le département + if (!empty($full_event->id_departement)) { + $departement = get_term($full_event->id_departement, 'departement'); + $enriched['departement'] = $departement && !is_wp_error($departement) ? $departement->name : '-'; + } else { + $enriched['departement'] = '-'; + } + + // Récupérer le type d'intervention + if (!empty($full_event->id_type_intervention)) { + $type_intervention = get_term($full_event->id_type_intervention, 'type_intervention'); + $enriched['type_intervention'] = $type_intervention && !is_wp_error($type_intervention) ? $type_intervention->name : '-'; + } else { + $enriched['type_intervention'] = '-'; + } + + // Déterminer si c'est un rendez-vous de groupe ou individuel + $enriched['type_rdv'] = $full_event->type === 'groupe' ? 'Groupe' : 'Individuel'; + + // Récupérer le(s) bénéficiaire(s) + $enriched['beneficiaires'] = []; + + if ($full_event->type === 'groupe') { + // Pour les groupes, récupérer tous les bénéficiaires du groupe + $beneficiaires_ids = $wpdb->get_col($wpdb->prepare( + "SELECT id_beneficiaire FROM {$wpdb->prefix}crvi_agenda_groupe_beneficiaires WHERE id_event = %d", + $full_event->id + )); + + foreach ($beneficiaires_ids as $benef_id) { + $benef = get_post($benef_id); + if ($benef) { + $enriched['beneficiaires'][] = get_the_title($benef_id); + } + } + } else { + // Pour les RDV individuels + if (!empty($full_event->id_beneficiaire)) { + $benef = get_post($full_event->id_beneficiaire); + if ($benef) { + $enriched['beneficiaires'][] = get_the_title($full_event->id_beneficiaire); + } + } + } + + return $enriched; + } + + /** + * Envoie un email de notification aux administrateurs en cas de conflit de disponibilité + * + * @param int $user_id L'ID de l'utilisateur (intervenant) + * @param array $conflicts Les conflits détectés + * @return bool True si l'email a été envoyé, false sinon + */ + public static function send_conflict_notification($user_id, $conflicts) { + // Récupérer l'email admin depuis les options ACF + $admin_email = get_field('email_admin', 'option'); + + // Fallback sur l'email WordPress si l'option ACF n'existe pas + if (empty($admin_email)) { + $admin_email = get_option('admin_email'); + } + + if (empty($admin_email)) { + error_log('CRVI: Aucun email admin trouvé pour l\'envoi de notification de conflit'); + return false; + } + + // Récupérer les informations de l'intervenant + $user = get_user_by('id', $user_id); + if (!$user) { + error_log('CRVI: Utilisateur introuvable (ID: ' . $user_id . ')'); + return false; + } + + // Enrichir les données de tous les événements en conflit + foreach ($conflicts as $key => &$conflict) { + if (isset($conflict['events'])) { + foreach ($conflict['events'] as &$event) { + $event['enriched'] = self::enrich_event_data($event); + } + } + } + unset($conflict, $event); // Libérer les références + + // Créer le sujet de l'email + $subject = sprintf( + '[Agenda CRVI] Alerte : Conflit de disponibilité - %s', + $user->display_name + ); + + // Charger le template email + $template_path = plugin_dir_path(__FILE__) . '../../templates/email/conflict-notification.php'; + + if (!file_exists($template_path)) { + error_log('CRVI: Template email introuvable : ' . $template_path); + return false; + } + + // Exécuter le template PHP avec output buffering pour capturer le HTML généré + ob_start(); + include $template_path; + $message = ob_get_clean(); + + // Configuration des en-têtes pour l'email HTML + $headers = [ + 'Content-Type: text/html; charset=UTF-8', + 'From: Agenda CRVI <' . get_option('admin_email') . '>' + ]; + + // Envoyer l'email + $sent = wp_mail($admin_email, $subject, $message, $headers); + + if ($sent) { + error_log('CRVI: Email de notification de conflit envoyé avec succès à ' . $admin_email); + } else { + error_log('CRVI: Échec de l\'envoi de l\'email de notification de conflit à ' . $admin_email); + } + + return $sent; + } } diff --git a/app/controllers/Plugin.php b/app/controllers/Plugin.php index 700d137..668a099 100644 --- a/app/controllers/Plugin.php +++ b/app/controllers/Plugin.php @@ -988,7 +988,7 @@ class CRVI_Plugin { delete_transient('crvi_intervenants_disponibilites'); global $wpdb; - $table_name = $wpdb->prefix . 'crvi_agenda_events'; + $table_name = $wpdb->prefix . 'crvi_agenda'; $conflicts = []; // 1. Vérifier les jours de disponibilité @@ -1163,6 +1163,9 @@ class CRVI_Plugin { // Stocker le message dans un transient pour l'afficher après la redirection set_transient('crvi_intervenant_conflicts_' . $user_id, $message, 30); + + // Envoyer un email de notification aux administrateurs via le contrôleur de notifications + CRVI_Notifications_Controller::send_conflict_notification($user_id, $conflicts); } } @@ -1199,6 +1202,9 @@ class CRVI_Plugin { * Affiche les messages de conflit d'indisponibilités stockés dans les transients (admin) */ public static function display_intervenant_conflicts_notice() { + // DEBUG: Vérifier que l'action est bien appelée + // error_log('CRVI DEBUG: display_intervenant_conflicts_notice appelé'); + // Récupérer l'ID utilisateur courant si on est sur une page de profil $user_id = 0; diff --git a/templates/email/conflict-notification.php b/templates/email/conflict-notification.php new file mode 100644 index 0000000..6b16699 --- /dev/null +++ b/templates/email/conflict-notification.php @@ -0,0 +1,223 @@ + + + + + + + Alerte : Conflit de disponibilité + + + +
+
+

⚠️ Alerte : Conflit de disponibilité détecté

+
+ +
+

Intervenant concerné

+

Nom : display_name); ?>

+

Email : user_email); ?>

+
+ +

+ Des événements sont planifiés pendant les périodes d'indisponibilité de cet intervenant. + Veuillez vérifier et prendre les mesures nécessaires. +

+ + $conflict): ?> +
+ + +

Jours non disponibles

+

événement(s) planifié(s) sur des jours où l'intervenant n'est pas disponible.

+ + + +
+ () à +
+ Type : + Département : + Type intervention : + + +
+ Bénéficiaire(s) : +
+ + + ID : +
+
+ + + + + + + +

+
+ Période : du au +
+

événement(s) planifié(s) pendant cette période.

+ + + +
+ à +
+ Type : + Département : + Type intervention : + + +
+ Bénéficiaire(s) : +
+ + + ID : +
+
+ + + +
+ + + +
+ +