ajout notif conflit indispo - events
This commit is contained in:
parent
227f39c50f
commit
3e2d5743ff
@ -558,4 +558,156 @@ class CRVI_Notifications_Controller {
|
|||||||
|
|
||||||
return $results;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -988,7 +988,7 @@ class CRVI_Plugin {
|
|||||||
delete_transient('crvi_intervenants_disponibilites');
|
delete_transient('crvi_intervenants_disponibilites');
|
||||||
|
|
||||||
global $wpdb;
|
global $wpdb;
|
||||||
$table_name = $wpdb->prefix . 'crvi_agenda_events';
|
$table_name = $wpdb->prefix . 'crvi_agenda';
|
||||||
$conflicts = [];
|
$conflicts = [];
|
||||||
|
|
||||||
// 1. Vérifier les jours de disponibilité
|
// 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
|
// Stocker le message dans un transient pour l'afficher après la redirection
|
||||||
set_transient('crvi_intervenant_conflicts_' . $user_id, $message, 30);
|
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)
|
* Affiche les messages de conflit d'indisponibilités stockés dans les transients (admin)
|
||||||
*/
|
*/
|
||||||
public static function display_intervenant_conflicts_notice() {
|
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
|
// Récupérer l'ID utilisateur courant si on est sur une page de profil
|
||||||
$user_id = 0;
|
$user_id = 0;
|
||||||
|
|
||||||
|
|||||||
223
templates/email/conflict-notification.php
Normal file
223
templates/email/conflict-notification.php
Normal file
@ -0,0 +1,223 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Template email : Notification de conflit de disponibilité
|
||||||
|
*
|
||||||
|
* Variables disponibles :
|
||||||
|
* @var WP_User $user L'utilisateur (intervenant) concerné
|
||||||
|
* @var array $conflicts Les conflits détectés
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (!defined('ABSPATH')) {
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="fr">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Alerte : Conflit de disponibilité</title>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
font-family: Arial, sans-serif;
|
||||||
|
line-height: 1.6;
|
||||||
|
color: #333;
|
||||||
|
max-width: 800px;
|
||||||
|
margin: 0 auto;
|
||||||
|
padding: 20px;
|
||||||
|
background-color: #f5f5f5;
|
||||||
|
}
|
||||||
|
.email-container {
|
||||||
|
background-color: #ffffff;
|
||||||
|
border-radius: 8px;
|
||||||
|
padding: 30px;
|
||||||
|
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
||||||
|
}
|
||||||
|
.header {
|
||||||
|
background-color: #dc3545;
|
||||||
|
color: #ffffff;
|
||||||
|
padding: 20px;
|
||||||
|
border-radius: 6px;
|
||||||
|
margin-bottom: 25px;
|
||||||
|
}
|
||||||
|
.header h1 {
|
||||||
|
margin: 0;
|
||||||
|
font-size: 24px;
|
||||||
|
}
|
||||||
|
.intervenant-info {
|
||||||
|
background-color: #f8f9fa;
|
||||||
|
padding: 15px;
|
||||||
|
border-radius: 6px;
|
||||||
|
margin-bottom: 25px;
|
||||||
|
}
|
||||||
|
.intervenant-info h2 {
|
||||||
|
margin-top: 0;
|
||||||
|
color: #333;
|
||||||
|
font-size: 18px;
|
||||||
|
}
|
||||||
|
.intervenant-info p {
|
||||||
|
margin: 5px 0;
|
||||||
|
}
|
||||||
|
.conflict-section {
|
||||||
|
margin-bottom: 30px;
|
||||||
|
border-left: 4px solid #dc3545;
|
||||||
|
padding-left: 20px;
|
||||||
|
}
|
||||||
|
.conflict-section h3 {
|
||||||
|
color: #dc3545;
|
||||||
|
margin-top: 0;
|
||||||
|
font-size: 18px;
|
||||||
|
}
|
||||||
|
.conflict-period {
|
||||||
|
background-color: #fff3cd;
|
||||||
|
padding: 12px;
|
||||||
|
border-radius: 4px;
|
||||||
|
margin-bottom: 15px;
|
||||||
|
border-left: 3px solid #ffc107;
|
||||||
|
}
|
||||||
|
.event-item {
|
||||||
|
background-color: #f8f9fa;
|
||||||
|
padding: 12px;
|
||||||
|
margin: 10px 0;
|
||||||
|
border-radius: 4px;
|
||||||
|
border-left: 3px solid #007bff;
|
||||||
|
}
|
||||||
|
.event-item strong {
|
||||||
|
color: #007bff;
|
||||||
|
}
|
||||||
|
.event-details {
|
||||||
|
margin-top: 8px;
|
||||||
|
font-size: 14px;
|
||||||
|
color: #666;
|
||||||
|
}
|
||||||
|
.event-details span {
|
||||||
|
display: inline-block;
|
||||||
|
margin-right: 15px;
|
||||||
|
}
|
||||||
|
.beneficiaires-list {
|
||||||
|
margin-top: 5px;
|
||||||
|
padding-left: 0;
|
||||||
|
color: #495057;
|
||||||
|
}
|
||||||
|
.footer {
|
||||||
|
margin-top: 30px;
|
||||||
|
padding-top: 20px;
|
||||||
|
border-top: 1px solid #dee2e6;
|
||||||
|
color: #666;
|
||||||
|
font-size: 14px;
|
||||||
|
}
|
||||||
|
.footer ul {
|
||||||
|
padding-left: 20px;
|
||||||
|
}
|
||||||
|
.footer li {
|
||||||
|
margin-bottom: 8px;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div class="email-container">
|
||||||
|
<div class="header">
|
||||||
|
<h1>⚠️ Alerte : Conflit de disponibilité détecté</h1>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="intervenant-info">
|
||||||
|
<h2>Intervenant concerné</h2>
|
||||||
|
<p><strong>Nom :</strong> <?php echo esc_html($user->display_name); ?></p>
|
||||||
|
<p><strong>Email :</strong> <?php echo esc_html($user->user_email); ?></p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<p style="font-size: 16px; margin-bottom: 25px;">
|
||||||
|
Des événements sont planifiés pendant les périodes d'indisponibilité de cet intervenant.
|
||||||
|
Veuillez vérifier et prendre les mesures nécessaires.
|
||||||
|
</p>
|
||||||
|
|
||||||
|
<?php foreach ($conflicts as $key => $conflict): ?>
|
||||||
|
<div class="conflict-section">
|
||||||
|
<?php if ($key === 'jours_indisponibles'): ?>
|
||||||
|
<!-- Conflit de jours indisponibles -->
|
||||||
|
<h3>Jours non disponibles</h3>
|
||||||
|
<p><strong><?php echo count($conflict['events']); ?> événement(s)</strong> planifié(s) sur des jours où l'intervenant n'est pas disponible.</p>
|
||||||
|
|
||||||
|
<?php foreach ($conflict['events'] as $event): ?>
|
||||||
|
<?php
|
||||||
|
$enriched = $event['enriched'] ?? null;
|
||||||
|
if ($enriched):
|
||||||
|
$date_formatted = date_i18n('d/m/Y', strtotime($enriched['date_rdv']));
|
||||||
|
$day_name = date_i18n('l', strtotime($enriched['date_rdv']));
|
||||||
|
?>
|
||||||
|
<div class="event-item">
|
||||||
|
<strong><?php echo esc_html($date_formatted); ?> (<?php echo esc_html($day_name); ?>) à <?php echo esc_html(substr($enriched['heure_rdv'], 0, 5)); ?></strong>
|
||||||
|
<div class="event-details">
|
||||||
|
<span><strong>Type :</strong> <?php echo esc_html(ucfirst($enriched['type_rdv'])); ?></span>
|
||||||
|
<span><strong>Département :</strong> <?php echo esc_html($enriched['departement']); ?></span>
|
||||||
|
<span><strong>Type intervention :</strong> <?php echo esc_html($enriched['type_intervention']); ?></span>
|
||||||
|
|
||||||
|
<?php if (!empty($enriched['beneficiaires'])): ?>
|
||||||
|
<div class="beneficiaires-list">
|
||||||
|
<strong>Bénéficiaire(s) :</strong> <?php echo esc_html(implode(', ', $enriched['beneficiaires'])); ?>
|
||||||
|
</div>
|
||||||
|
<?php endif; ?>
|
||||||
|
|
||||||
|
<span style="color: #999;"><strong>ID :</strong> <?php echo esc_html($enriched['id']); ?></span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<?php endif; ?>
|
||||||
|
<?php endforeach; ?>
|
||||||
|
|
||||||
|
<?php else: ?>
|
||||||
|
<!-- Conflit de période d'indisponibilité -->
|
||||||
|
<?php
|
||||||
|
$periode = $conflict['periode'];
|
||||||
|
$debut_formatted = date_i18n('d/m/Y', strtotime($periode['debut']));
|
||||||
|
$fin_formatted = date_i18n('d/m/Y', strtotime($periode['fin']));
|
||||||
|
?>
|
||||||
|
|
||||||
|
<h3><?php echo esc_html($periode['type']); ?></h3>
|
||||||
|
<div class="conflict-period">
|
||||||
|
<strong>Période :</strong> du <?php echo esc_html($debut_formatted); ?> au <?php echo esc_html($fin_formatted); ?>
|
||||||
|
</div>
|
||||||
|
<p><strong><?php echo count($conflict['events']); ?> événement(s)</strong> planifié(s) pendant cette période.</p>
|
||||||
|
|
||||||
|
<?php foreach ($conflict['events'] as $event): ?>
|
||||||
|
<?php
|
||||||
|
$enriched = $event['enriched'] ?? null;
|
||||||
|
if ($enriched):
|
||||||
|
$date_formatted = date_i18n('d/m/Y', strtotime($enriched['date_rdv']));
|
||||||
|
?>
|
||||||
|
<div class="event-item">
|
||||||
|
<strong><?php echo esc_html($date_formatted); ?> à <?php echo esc_html(substr($enriched['heure_rdv'], 0, 5)); ?></strong>
|
||||||
|
<div class="event-details">
|
||||||
|
<span><strong>Type :</strong> <?php echo esc_html(ucfirst($enriched['type_rdv'])); ?></span>
|
||||||
|
<span><strong>Département :</strong> <?php echo esc_html($enriched['departement']); ?></span>
|
||||||
|
<span><strong>Type intervention :</strong> <?php echo esc_html($enriched['type_intervention']); ?></span>
|
||||||
|
|
||||||
|
<?php if (!empty($enriched['beneficiaires'])): ?>
|
||||||
|
<div class="beneficiaires-list">
|
||||||
|
<strong>Bénéficiaire(s) :</strong> <?php echo esc_html(implode(', ', $enriched['beneficiaires'])); ?>
|
||||||
|
</div>
|
||||||
|
<?php endif; ?>
|
||||||
|
|
||||||
|
<span style="color: #999;"><strong>ID :</strong> <?php echo esc_html($enriched['id']); ?></span>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<?php endif; ?>
|
||||||
|
<?php endforeach; ?>
|
||||||
|
<?php endif; ?>
|
||||||
|
</div>
|
||||||
|
<?php endforeach; ?>
|
||||||
|
|
||||||
|
<div class="footer">
|
||||||
|
<p><strong>Actions recommandées :</strong></p>
|
||||||
|
<ul>
|
||||||
|
<li>Vérifier les événements listés ci-dessus</li>
|
||||||
|
<li>Contacter l'intervenant pour confirmer sa disponibilité</li>
|
||||||
|
<li>Annuler ou déplacer les événements en conflit si nécessaire</li>
|
||||||
|
<li>Ajuster les périodes d'indisponibilité si besoin</li>
|
||||||
|
</ul>
|
||||||
|
<p style="margin-top: 20px; color: #999;">
|
||||||
|
Cet email a été envoyé automatiquement par le système Agenda CRVI.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Loading…
Reference in New Issue
Block a user