Afficher les prochains événements du calendrier public sur la page d'accueil
- Ajoute web/inc/calendar.php : récupère le flux iCal via ?p=webcal, cache le résultat 1h dans /tmp, parse les VEVENT à venir - Intègre le bloc "Prochains événements" dans index.php avant les services Closes #1 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
4921a0691c
commit
4f6094c6c6
2 changed files with 62 additions and 0 deletions
47
web/inc/calendar.php
Normal file
47
web/inc/calendar.php
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
<?php
|
||||
|
||||
function calendar_next_events(int $n = 5): array {
|
||||
$url = 'https://alpinux.org/public-calendars/n5BWPYsxw7FCYozM?p=webcal';
|
||||
$cache = sys_get_temp_dir() . '/alpinux_calendar.ics';
|
||||
$ttl = 3600;
|
||||
|
||||
if (!file_exists($cache) || (time() - filemtime($cache)) >= $ttl) {
|
||||
$data = @file_get_contents($url);
|
||||
if ($data !== false) file_put_contents($cache, $data);
|
||||
}
|
||||
|
||||
if (!file_exists($cache)) return [];
|
||||
|
||||
return _ical_upcoming(file_get_contents($cache), $n);
|
||||
}
|
||||
|
||||
function _ical_upcoming(string $ical, int $n): array {
|
||||
$events = [];
|
||||
$now = time();
|
||||
|
||||
preg_match_all('/BEGIN:VEVENT(.+?)END:VEVENT/s', $ical, $blocks);
|
||||
|
||||
foreach ($blocks[1] as $b) {
|
||||
$e = [];
|
||||
|
||||
if (preg_match('/^SUMMARY[^:]*:(.+)$/m', $b, $m))
|
||||
$e['title'] = trim($m[1]);
|
||||
|
||||
if (preg_match('/^LOCATION[^:]*:(.+)$/m', $b, $m))
|
||||
$e['location'] = trim($m[1]);
|
||||
|
||||
if (preg_match('/^DTSTART[^:]*:(\d+)/m', $b, $m)) {
|
||||
$r = $m[1];
|
||||
$e['start'] = strlen($r) === 8
|
||||
? mktime(0, 0, 0, substr($r, 4, 2), substr($r, 6, 2), substr($r, 0, 4))
|
||||
: strtotime($r);
|
||||
}
|
||||
|
||||
if (isset($e['start'], $e['title']) && $e['start'] >= $now)
|
||||
$events[] = $e;
|
||||
}
|
||||
|
||||
usort($events, fn($a, $b) => $a['start'] - $b['start']);
|
||||
|
||||
return array_slice($events, 0, $n);
|
||||
}
|
||||
|
|
@ -2,6 +2,7 @@
|
|||
require_once __DIR__ . '/inc/config.php';
|
||||
require_once __DIR__ . '/inc/auth.php';
|
||||
require_once __DIR__ . '/inc/services.php';
|
||||
require_once __DIR__ . '/inc/calendar.php';
|
||||
|
||||
session_start_safe();
|
||||
|
||||
|
|
@ -64,6 +65,20 @@ require __DIR__ . '/views/layout.php';
|
|||
|
||||
<?php endif; ?>
|
||||
|
||||
<?php $calendar_events = calendar_next_events(5); if ($calendar_events): ?>
|
||||
<section class="calendar-section">
|
||||
<h2>Prochains événements</h2>
|
||||
<div class="about-grid">
|
||||
<?php foreach ($calendar_events as $ev): ?>
|
||||
<div class="about-card">
|
||||
<div class="about-title"><?= htmlspecialchars($ev['title']) ?></div>
|
||||
<p><?= date('d/m/Y', $ev['start']) ?><?= !empty($ev['location']) ? ' — ' . htmlspecialchars($ev['location']) : '' ?></p>
|
||||
</div>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
</section>
|
||||
<?php endif; ?>
|
||||
|
||||
<section class="services-section">
|
||||
<h2>Nos services</h2>
|
||||
<div class="grid">
|
||||
|
|
|
|||
Loading…
Reference in a new issue