dates de distribution fixes et site màj dynamiquement

This commit is contained in:
Pierre Martin
2025-11-16 21:41:11 +01:00
parent d4c06939b8
commit 21c34a2cd9
4 changed files with 439 additions and 78 deletions
+208
View File
@@ -0,0 +1,208 @@
<?php
/**
* Fonctions utilitaires pour gérer les distributions
*/
/**
* Charge les distributions depuis le fichier de configuration
*/
function chargerDistributions(): array {
return require __DIR__ . '/distributions.php';
}
/**
* Trouve la prochaine distribution
*/
function trouverProchaineDistribution(array $distributions): ?array {
$maintenant = new DateTime();
foreach ($distributions as $distrib) {
$date = new DateTime($distrib['date']);
if ($date >= $maintenant) {
return $distrib;
}
}
return null;
}
/**
* Vérifie si une distribution est passée
*/
function estPassee(array $distribution): bool {
$maintenant = new DateTime();
$date = new DateTime($distribution['date']);
return $date < $maintenant;
}
/**
* Formate une date de distribution
*/
function formaterDate(string $date): string {
$dateObj = new DateTime($date);
$formatter = new IntlDateFormatter(
'fr_FR',
IntlDateFormatter::LONG,
IntlDateFormatter::NONE
);
// Retirer l'année si c'est l'année courante
$dateFormatee = $formatter->format($dateObj);
// Ajouter le jour de la semaine si ce n'est pas un samedi
$jourSemaine = $dateObj->format('N'); // 1=lundi, 6=samedi
if ($jourSemaine != 6) {
$joursTraduction = [
1 => 'Lundi',
2 => 'Mardi',
3 => 'Mercredi',
4 => 'Jeudi',
5 => 'Vendredi',
7 => 'Dimanche'
];
return $joursTraduction[$jourSemaine] . ' ' . $dateObj->format('d') . ' ' .
ucfirst($formatter->format($dateObj));
}
return ucfirst($dateFormatee);
}
/**
* Formate une date courte (pour le calendrier)
*/
function formaterDateCourte(string $date): string {
$dateObj = new DateTime($date);
$formatter = new IntlDateFormatter(
'fr_FR',
IntlDateFormatter::LONG,
IntlDateFormatter::NONE,
'Europe/Paris',
IntlDateFormatter::GREGORIAN,
'd MMMM yyyy'
);
// Vérifier si ce n'est pas un samedi
$jourSemaine = $dateObj->format('N');
if ($jourSemaine != 6) {
$joursTraduction = [
1 => 'Lundi',
2 => 'Mardi',
3 => 'Mercredi',
4 => 'Jeudi',
5 => 'Vendredi',
7 => 'Dimanche'
];
return '<strong>' . $joursTraduction[$jourSemaine] . '</strong> ' .
$formatter->format($dateObj);
}
return $formatter->format($dateObj);
}
/**
* Formate une date complète avec jour de la semaine
*/
function formaterDateComplete(DateTime $date): string {
$formatter = new IntlDateFormatter(
'fr_FR',
IntlDateFormatter::FULL,
IntlDateFormatter::NONE,
'Europe/Paris',
IntlDateFormatter::GREGORIAN,
'EEEE d MMMM'
);
return ucfirst($formatter->format($date));
}
/**
* Formate une date pour le calendrier
*/
function formaterDateCalendrier(DateTime $date): string {
$formatter = new IntlDateFormatter(
'fr_FR',
IntlDateFormatter::LONG,
IntlDateFormatter::NONE,
'Europe/Paris',
IntlDateFormatter::GREGORIAN,
'd MMMM yyyy'
);
return $formatter->format($date);
}
/**
* Retourne l'info du lieu de distribution
*/
function getLieuInfo(string $lieu): array {
$lieux = [
'grange' => [
'nom' => 'La Grange',
'adresse' => '7 rue du Vieux Moulin',
'url' => 'https://www.openstreetmap.org/#map=19/43.51294/1.32234',
'description' => 'à « La Grange » de <abbr title="Frouzins Initiatives Citoyennes">FIC</abbr> 7 rue du Vieux Moulin'
],
'secours' => [
'nom' => 'Secours Populaire',
'adresse' => '28 rue Guillaume Berdeil',
'url' => 'https://www.openstreetmap.org/?mlat=43.516927&mlon=1.319663#map=19/43.516927/1.319663',
'description' => 'au Secours Populaire de Frouzins 28 rue Guillaume Berdeil'
]
];
return $lieux[$lieu] ?? [];
}
/**
* Classe CSS pour le type de distribution
*/
function getClasseDistribution(array $distribution, bool $estProchaine): string {
if ($estProchaine) {
return 'bg-green-500';
}
if (estPassee($distribution)) {
return 'bg-gray-50 opacity-50';
}
if ($distribution['type'] === 'annulee') {
return 'bg-red-100';
}
return 'bg-green-100';
}
/**
* Classe CSS pour le texte
*/
function getClasseTexte(array $distribution, bool $estProchaine): string {
if ($estProchaine) {
return 'text-white';
}
if ($distribution['type'] === 'annulee') {
return 'text-red-800';
}
if (estPassee($distribution)) {
return 'text-gray-800';
}
return 'text-green-800';
}
/**
* Label du statut
*/
function getLabelStatut(array $distribution, bool $estProchaine): ?string {
if ($estProchaine) {
return 'Prochaine';
}
if (estPassee($distribution)) {
return 'Passée';
}
return null;
}