agenda : clean code + nouveau format de dates
- Renommer les variables (fromTs→startTimestamp, toTs→endTimestamp, etc.) - Nouveau format : 01/01 / 01/01 & 02/01 / 01/01 — 07/01 - Supprimer $frenchDays devenu inutile Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
6f5c8f02e2
commit
05eabf328d
1 changed files with 24 additions and 32 deletions
|
|
@ -6,29 +6,21 @@ return function ($site) {
|
|||
5 => 'mai', 6 => 'juin', 7 => 'juill.', 8 => 'août',
|
||||
9 => 'sept.', 10 => 'oct.', 11 => 'nov.', 12 => 'déc.',
|
||||
];
|
||||
$frenchDays = ['dim.', 'lun.', 'mar.', 'mer.', 'jeu.', 'ven.', 'sam.'];
|
||||
$formatDateRange = function (int $startTimestamp, ?int $endTimestamp): string {
|
||||
$startDate = date('d/m', $startTimestamp);
|
||||
|
||||
$formatDateRange = function (int $fromTs, ?int $toTs) use ($frenchMonths, $frenchDays): string {
|
||||
$fromDay = (int) date('j', $fromTs);
|
||||
$fromMonth = (int) date('n', $fromTs);
|
||||
|
||||
if ($toTs === null) {
|
||||
return $frenchDays[(int) date('w', $fromTs)] . ' ' . $fromDay . ' ' . $frenchMonths[$fromMonth];
|
||||
if ($endTimestamp === null) {
|
||||
return $startDate;
|
||||
}
|
||||
|
||||
$toDay = (int) date('j', $toTs);
|
||||
$toMonth = (int) date('n', $toTs);
|
||||
$diffDays = (int) round(($toTs - $fromTs) / 86400);
|
||||
$endDate = date('d/m', $endTimestamp);
|
||||
$durationInDays = (int) round(($endTimestamp - $startTimestamp) / 86400);
|
||||
|
||||
if ($diffDays === 1 && $fromMonth === $toMonth) {
|
||||
return $fromDay . ' — ' . $toDay . '<br>' . $frenchMonths[$fromMonth];
|
||||
if ($durationInDays === 1) {
|
||||
return $startDate . ' & ' . $endDate;
|
||||
}
|
||||
|
||||
if ($fromMonth === $toMonth) {
|
||||
return $fromDay . ' — ' . $toDay . '<br>' . $frenchMonths[$fromMonth];
|
||||
}
|
||||
|
||||
return $fromDay . ' ' . $frenchMonths[$fromMonth] . ' — ' . $toDay . '<br>' . $frenchMonths[$toMonth];
|
||||
return $startDate . ' — ' . $endDate;
|
||||
};
|
||||
|
||||
$eventsByMonth = [];
|
||||
|
|
@ -40,28 +32,28 @@ return function ($site) {
|
|||
$eventDates = [];
|
||||
|
||||
foreach ($event->dates()->toStructure() as $dateEntry) {
|
||||
$fromValue = $dateEntry->from()->value();
|
||||
if (!$fromValue) {
|
||||
$startDateValue = $dateEntry->from()->value();
|
||||
if (!$startDateValue) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$fromTs = strtotime($fromValue);
|
||||
$toTs = $dateEntry->to()->value() ? strtotime($dateEntry->to()->value()) : null;
|
||||
$endTs = $toTs ?? $fromTs;
|
||||
$startTimestamp = strtotime($startDateValue);
|
||||
$endTimestamp = $dateEntry->to()->value() ? strtotime($dateEntry->to()->value()) : null;
|
||||
$coverageEndTimestamp = $endTimestamp ?? $startTimestamp;
|
||||
|
||||
$entry = [
|
||||
'fromTs' => $fromTs,
|
||||
'toTs' => $toTs,
|
||||
'label' => $formatDateRange($fromTs, $toTs),
|
||||
'name' => $dateEntry->name()->value(),
|
||||
'link' => $dateEntry->link()->value(),
|
||||
'startTimestamp' => $startTimestamp,
|
||||
'endTimestamp' => $endTimestamp,
|
||||
'label' => $formatDateRange($startTimestamp, $endTimestamp),
|
||||
'name' => $dateEntry->name()->value(),
|
||||
'link' => $dateEntry->link()->value(),
|
||||
];
|
||||
|
||||
// Ajoute l'entrée dans chaque mois couvert par la plage
|
||||
$monthCursor = strtotime(date('Y-m-01', $fromTs));
|
||||
$lastMonthTs = strtotime(date('Y-m-01', $endTs));
|
||||
$monthCursor = strtotime(date('Y-m-01', $startTimestamp));
|
||||
$lastMonthTimestamp = strtotime(date('Y-m-01', $coverageEndTimestamp));
|
||||
|
||||
while ($monthCursor <= $lastMonthTs) {
|
||||
while ($monthCursor <= $lastMonthTimestamp) {
|
||||
$monthNum = (int) date('n', $monthCursor);
|
||||
$year = date('Y', $monthCursor);
|
||||
$monthKey = date('Y-m', $monthCursor);
|
||||
|
|
@ -105,9 +97,9 @@ return function ($site) {
|
|||
|
||||
foreach ($eventsByMonth as &$month) {
|
||||
foreach ($month['events'] as &$event) {
|
||||
usort($event['dates'], fn ($a, $b) => $a['fromTs'] <=> $b['fromTs']);
|
||||
usort($event['dates'], fn ($a, $b) => $a['startTimestamp'] <=> $b['startTimestamp']);
|
||||
}
|
||||
uasort($month['events'], fn ($a, $b) => $a['dates'][0]['fromTs'] <=> $b['dates'][0]['fromTs']);
|
||||
uasort($month['events'], fn ($a, $b) => $a['dates'][0]['startTimestamp'] <=> $b['dates'][0]['startTimestamp']);
|
||||
}
|
||||
|
||||
$monthSlugs = array_column(array_values($eventsByMonth), 'slug');
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue