Files
GridTV/export.php
T

651 lines
34 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
require_once __DIR__ . '/src/lib/admin.php';
$config = gridtv_load_config();
gridtv_require_admin($config, 'Export');
[$locale, $L] = gridtv_load_locale($config);
function export_parse_xmltv_programmes(string $xml, string $target_day): array {
if (!class_exists('DOMDocument')) {
return ['channels' => [], 'programmes' => [], 'error' => 'PHP DOM/XML extension is not installed'];
}
libxml_use_internal_errors(true);
$dom = new DOMDocument();
if (!$dom->loadXML($xml, LIBXML_NOERROR | LIBXML_NOWARNING)) {
return ['channels' => [], 'programmes' => [], 'error' => 'Invalid XMLTV response'];
}
$xp = new DOMXPath($dom);
$channels = [];
foreach ($xp->query('/tv/channel') as $channel) {
$id = $channel->getAttribute('id');
$name = trim($xp->evaluate('string(display-name[1])', $channel));
$icon = trim($xp->evaluate('string(icon/@src)', $channel));
$channels[$id] = ['id' => $id, 'name' => $name ?: $id, 'icon' => $icon];
}
$start_day = new DateTimeImmutable($target_day . ' 00:00:00');
$end_day = $start_day->modify('+1 day');
$rows = [];
foreach ($xp->query('/tv/programme') as $programme) {
$channel_id = $programme->getAttribute('channel');
$start = DateTimeImmutable::createFromFormat('YmdHis O', $programme->getAttribute('start'));
$stop = DateTimeImmutable::createFromFormat('YmdHis O', $programme->getAttribute('stop'));
if (!$start || !$stop) continue;
if ($stop <= $start_day || $start >= $end_day) continue;
$title = trim($xp->evaluate('string(title)', $programme));
$subtitle = trim($xp->evaluate('string(sub-title)', $programme));
$desc = trim($xp->evaluate('string(desc)', $programme));
$rating = trim($xp->evaluate('string(rating/value)', $programme));
$cats = [];
foreach ($xp->query('category', $programme) as $cat) {
$value = trim($cat->textContent);
if ($value !== '') $cats[strtolower($value)] = $value;
}
$rows[$channel_id][] = [
'start' => $start,
'stop' => $stop,
'title' => $title,
'subtitle' => $subtitle,
'desc' => $desc,
'rating' => $rating,
'categories' => array_values($cats),
];
}
uasort($channels, static function ($a, $b) {
preg_match('/^\d+/', $a['name'], $ma);
preg_match('/^\d+/', $b['name'], $mb);
$na = isset($ma[0]) ? (int) $ma[0] : 9999;
$nb = isset($mb[0]) ? (int) $mb[0] : 9999;
return $na <=> $nb ?: strcmp($a['name'], $b['name']);
});
foreach ($rows as &$programmes) {
usort($programmes, static fn($a, $b) => $a['start'] <=> $b['start']);
}
return ['channels' => $channels, 'programmes' => $rows, 'error' => ''];
}
function export_window_definitions(string $target_day): array {
return [
'full' => [
'label' => '00h-24h',
'start' => new DateTimeImmutable($target_day . ' 00:00:00'),
'end' => new DateTimeImmutable($target_day . ' 23:59:59'),
],
'morning' => [
'label' => 'Morning',
'start' => new DateTimeImmutable($target_day . ' 06:00:00'),
'end' => new DateTimeImmutable($target_day . ' 11:59:59'),
],
'afternoon' => [
'label' => 'Afternoon',
'start' => new DateTimeImmutable($target_day . ' 12:00:00'),
'end' => new DateTimeImmutable($target_day . ' 17:59:59'),
],
'evening' => [
'label' => 'Evening',
'start' => new DateTimeImmutable($target_day . ' 18:00:00'),
'end' => new DateTimeImmutable($target_day . ' 23:59:59'),
],
'prime' => [
'label' => 'Prime time',
'start' => new DateTimeImmutable($target_day . ' 20:00:00'),
'end' => new DateTimeImmutable($target_day . ' 23:59:59'),
],
];
}
function export_filter_programmes(array $programmes, DateTimeImmutable $start, DateTimeImmutable $end): array {
return array_values(array_filter($programmes, static fn($p) => $p['stop'] > $start && $p['start'] < $end));
}
function export_hour_slots(DateTimeImmutable $start, DateTimeImmutable $end): array {
$slots = [];
$cursor = $start;
while ($cursor < $end) {
$slot_end = $cursor->modify('+1 hour');
if ($slot_end > $end) $slot_end = $end;
$slots[] = ['start' => $cursor, 'end' => $slot_end];
$cursor = $slot_end;
}
return $slots;
}
function export_programme_weight(array $programme): int {
$duration = max(1, (int) round(($programme['stop']->getTimestamp() - $programme['start']->getTimestamp()) / 60));
$weight = min($duration, 240);
if (!empty($programme['subtitle'])) $weight += 10;
if (!empty($programme['categories'])) $weight += 8;
if (!empty($programme['desc'])) $weight += min(40, (int) floor(strlen($programme['desc']) / 20));
return $weight;
}
function export_programme_duration(array $programme): int {
return max(1, (int) round(($programme['stop']->getTimestamp() - $programme['start']->getTimestamp()) / 60));
}
function export_programme_is_series(array $programme): bool {
return !empty($programme['subtitle']);
}
function export_programme_has_category(array $programme, array $needles): bool {
foreach ($programme['categories'] as $category) {
$value = strtolower($category);
foreach ($needles as $needle) {
if (str_contains($value, strtolower($needle))) return true;
}
}
return false;
}
function export_take_entries(array $entries, callable $filter, int $limit = 4): array {
$picked = [];
foreach ($entries as $entry) {
if (!$filter($entry)) continue;
$picked[] = $entry;
if (count($picked) >= $limit) break;
}
return $picked;
}
function export_programme_label(array $programme): string {
if (export_programme_has_category($programme, ['document', 'docu', 'biopic', 'history', 'histoire'])) {
return 'Documentaire';
}
if (export_programme_is_series($programme)) {
return 'Serie';
}
if (export_programme_duration($programme) >= 80) {
return 'Film';
}
return 'Selection';
}
function export_entry_tease(array $entry): string {
$programme = $entry['programme'];
if (!empty($programme['subtitle'])) {
return $programme['subtitle'];
}
if (!empty($programme['categories'])) {
return implode(' · ', array_slice($programme['categories'], 0, 3));
}
if (!empty($programme['desc'])) {
return mb_strimwidth($programme['desc'], 0, 120, '…');
}
return 'A suivre sur ' . $entry['channel_name'];
}
function export_build_magazine(array $channels): array {
$all = [];
foreach ($channels as $channel) {
foreach ($channel['programmes'] as $programme) {
$all[] = [
'channel_name' => $channel['name'],
'channel_icon' => $channel['icon'],
'programme' => $programme,
'weight' => export_programme_weight($programme),
];
}
}
usort($all, static function ($a, $b) {
return $b['weight'] <=> $a['weight']
?: $a['programme']['start'] <=> $b['programme']['start']
?: strcmp($a['channel_name'], $b['channel_name']);
});
$highlights = array_slice($all, 0, 5);
$series = export_take_entries($all, static fn($entry) => export_programme_is_series($entry['programme']), 4);
$movies = export_take_entries($all, static fn($entry) => !export_programme_is_series($entry['programme']) && export_programme_duration($entry['programme']) >= 80, 4);
$late_picks = export_take_entries($all, static fn($entry) => (int) $entry['programme']['start']->format('H') >= 21, 4);
$docs = export_take_entries($all, static fn($entry) => export_programme_has_category($entry['programme'], ['document', 'biopic', 'history', 'histoire']), 4);
$premieres = export_take_entries(
$all,
static fn($entry) => export_programme_is_series($entry['programme']) && !empty($entry['programme']['subtitle']),
4
);
$by_channel = [];
foreach ($channels as $channel) {
$by_channel[] = [
'name' => $channel['name'],
'icon' => $channel['icon'],
'programmes' => array_slice($channel['programmes'], 0, 6),
];
}
$sections = [
[
'slug' => 'top-picks',
'title' => 'Ce soir a la tele',
'kicker' => 'Selection de la redaction',
'intro' => 'Les programmes qui donnent envie de garder la tele allumee ce soir.',
'items' => array_slice($highlights, 0, 4),
],
[
'slug' => 'series',
'title' => 'Nouvelles series diffusees',
'kicker' => 'Episodes et rendez-vous',
'intro' => 'Les feuilletons, rediffusions premium et lancements du moment.',
'items' => $premieres ?: ($series ?: array_slice($highlights, 0, 4)),
],
[
'slug' => 'movies',
'title' => 'Films de la selection',
'kicker' => 'Longs formats',
'intro' => 'Le bloc cinema de la journee, entre classiques, blockbusters et belles surprises.',
'items' => $movies ?: array_slice($highlights, 0, 4),
],
[
'slug' => 'docs',
'title' => 'Docs et recits de vie',
'kicker' => 'Documentaires / biopics',
'intro' => 'Pour les amateurs de recits, d histoires vraies et de programmes a contexte.',
'items' => $docs ?: $late_picks ?: array_slice($highlights, 0, 4),
],
[
'slug' => 'prime',
'title' => 'Prime time',
'kicker' => 'A partir de 21h',
'intro' => 'Les rendez-vous les plus costauds de la tranche du soir.',
'items' => $late_picks ?: array_slice($highlights, 0, 4),
],
];
return ['lead' => $highlights[0] ?? null, 'highlights' => $highlights, 'channels' => $by_channel, 'sections' => $sections];
}
$sources = array_values($config['epg_sources'] ?? []);
$source_index = max(0, min((int) ($_GET['source'] ?? 0), max(count($sources) - 1, 0)));
$source = $sources[$source_index] ?? null;
$target_day = preg_match('/^\d{4}-\d{2}-\d{2}$/', (string) ($_GET['day'] ?? '')) ? (string) $_GET['day'] : (new DateTimeImmutable('today'))->format('Y-m-d');
$layout_input = isset($_GET['layout']) ? (string) $_GET['layout'] : 'cards';
$limit_input = isset($_GET['limit']) ? (string) $_GET['limit'] : '8';
$window_input = isset($_GET['window']) ? (string) $_GET['window'] : 'full';
$layout = in_array($layout_input, ['cards', 'timeline', 'magazine'], true) ? $layout_input : 'cards';
$limit = in_array($limit_input, ['4', '8', 'all'], true) ? $limit_input : '8';
$windows = export_window_definitions($target_day);
$window_key = array_key_exists($window_input, $windows) ? $window_input : 'full';
$window = $windows[$window_key];
$slots = export_hour_slots($window['start'], $window['end']->modify('+1 second'));
$fetch = $source ? gridtv_fetch_url((string) $source['epg_url'], 25) : ['ok' => false, 'error' => 'No source configured', 'body' => ''];
$parsed = $fetch['ok'] ? export_parse_xmltv_programmes($fetch['body'], $target_day) : ['channels' => [], 'programmes' => [], 'error' => $fetch['error'] ?: 'Unable to fetch XMLTV'];
$visible_channels = [];
foreach ($parsed['channels'] as $channel_id => $channel) {
$filtered = export_filter_programmes($parsed['programmes'][$channel_id] ?? [], $window['start'], $window['end']->modify('+1 second'));
if (!$filtered) continue;
$visible_channels[$channel_id] = $channel + ['programmes' => $filtered];
}
if ($limit !== 'all') {
$visible_channels = array_slice($visible_channels, 0, (int) $limit, true);
}
$day_label = (new DateTimeImmutable($target_day))->format('l d F Y');
$layout_label = $layout === 'cards' ? 'Readable cards' : ($layout === 'timeline' ? 'Dense timeline' : 'TV magazine');
$magazine = export_build_magazine($visible_channels);
?><!DOCTYPE html>
<html lang="<?= htmlspecialchars($locale) ?>">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>GridTV — Export 24h</title>
<link rel="stylesheet" href="/assets/fonts/fonts.css">
<script src="/assets/vendor/html2canvas.min.js"></script>
<style>
:root{--paper:#f4ecda;--ink:#22201b;--ink-soft:#675f52;--accent:#aa4231;--accent-2:#264653;--line:#d3c5ab;--paper-2:#fbf6ea}
*{box-sizing:border-box}body{margin:0;font-family:'Barlow Condensed',sans-serif;background:linear-gradient(180deg,#1f2125 0,#121317 100%);color:#f7f4ee}
.page{max-width:1380px;margin:0 auto;padding:26px 18px 40px}
.toolbar{display:flex;justify-content:space-between;gap:16px;align-items:flex-start;flex-wrap:wrap;margin-bottom:18px}
.heading{max-width:760px}.heading h1{margin:0 0 6px;font-size:34px;letter-spacing:.06em;text-transform:uppercase}.heading p{margin:0;color:#bcb7ad;font-size:14px;line-height:1.5}
.controls,.filter-form{display:flex;gap:10px;flex-wrap:wrap;align-items:center}
.controls a,.controls button,.controls select,.controls input,.filter-form select,.filter-form input,.filter-form button{height:42px;border:none}
.controls a,.controls button,.filter-form button{display:inline-flex;align-items:center;justify-content:center;padding:0 14px;background:#ebd58a;color:#1f1d18;text-decoration:none;font-size:13px;font-weight:700;letter-spacing:.08em;text-transform:uppercase;cursor:pointer}
.ghost{background:#262a31 !important;color:#f7f4ee !important;border:1px solid #3c4450 !important}
.controls select,.controls input,.filter-form select,.filter-form input{padding:0 12px;background:#262a31;color:#fff;border:1px solid #3c4450;font-family:'Share Tech Mono',monospace}
.sheet-wrap{overflow:auto;padding:8px 0}
.sheet{width:1120px;margin:0 auto;background:var(--paper);color:var(--ink);padding:30px 34px 36px;box-shadow:0 26px 70px rgba(0,0,0,.45);border:10px solid #f8f2e7;position:relative}
.sheet::before{content:'';position:absolute;inset:14px;border:1px solid rgba(38,70,83,.18);pointer-events:none}
.sheet-head{display:flex;justify-content:space-between;gap:20px;align-items:flex-end;border-bottom:3px solid var(--accent);padding-bottom:16px;margin-bottom:18px}
.sheet-title{font-family:'IM Fell English',serif;font-size:44px;line-height:1.05;letter-spacing:.01em}
.sheet-sub{font-family:'Share Tech Mono',monospace;font-size:12px;letter-spacing:.16em;text-transform:uppercase;color:var(--accent-2)}
.sheet-meta{text-align:right}.sheet-meta .group{font-size:22px;font-weight:700}.sheet-meta .date{font-size:14px;color:var(--ink-soft)}
.legend{display:flex;gap:12px;flex-wrap:wrap;margin-bottom:18px}
.legend span{padding:6px 10px;border:1px solid var(--line);font-family:'Share Tech Mono',monospace;font-size:11px;letter-spacing:.08em;text-transform:uppercase}
.empty{color:var(--ink-soft);font-style:italic;font-size:13px;padding-top:24px}
.note{margin-top:16px;font-size:12px;color:var(--ink-soft);text-align:right}
.cards-grid{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:18px}
.channel-card{background:rgba(255,255,255,.35);border:1px solid var(--line);padding:14px 14px 12px;break-inside:avoid}
.channel-head{display:flex;gap:12px;align-items:center;border-bottom:2px solid var(--accent-2);padding-bottom:10px;margin-bottom:12px}
.channel-head img{width:46px;height:46px;object-fit:contain;background:#fff;border:1px solid var(--line);padding:4px}
.channel-name{font-size:24px;font-weight:700;line-height:1.05}
.channel-count{font-family:'Share Tech Mono',monospace;font-size:11px;color:var(--ink-soft);letter-spacing:.08em;text-transform:uppercase}
.prog-list{display:grid;gap:8px}
.prog{display:grid;grid-template-columns:90px 1fr;gap:10px;padding:8px 0;border-top:1px dashed var(--line)}
.prog:first-child{border-top:none;padding-top:0}
.prog-time{font-family:'Share Tech Mono',monospace;font-size:11px;color:var(--accent-2);letter-spacing:.04em}
.prog-title{font-size:16px;font-weight:700;line-height:1.08}
.prog-sub,.prog-cats,.prog-desc{font-size:12px;color:var(--ink-soft);line-height:1.3;margin-top:2px}
.prog-desc{display:-webkit-box;-webkit-line-clamp:2;-webkit-box-orient:vertical;overflow:hidden}
.magazine{display:grid;gap:20px}
.mag-banner{display:grid;grid-template-columns:1.1fr .9fr;gap:18px;background:linear-gradient(135deg,#8b2d20 0,#4c2019 60%,#1f1a17 100%);color:#f7efe3;padding:18px;border:1px solid rgba(255,255,255,.14)}
.mag-banner-title{font-family:'IM Fell English',serif;font-size:48px;line-height:1.02;margin:0 0 8px}
.mag-banner-sub{font-size:15px;line-height:1.45;color:#f0dcca;max-width:540px}
.mag-banner-meta{display:flex;gap:10px;flex-wrap:wrap;align-content:flex-start;justify-content:flex-end}
.mag-banner-meta span{border:1px solid rgba(255,255,255,.22);padding:7px 10px;font-family:'Share Tech Mono',monospace;font-size:11px;letter-spacing:.08em;text-transform:uppercase}
.mag-hero{display:grid;grid-template-columns:1.3fr .9fr;gap:18px}
.mag-lead,.mag-side,.mag-channel{background:rgba(255,255,255,.35);border:1px solid var(--line);padding:16px}
.mag-kicker{font-family:'Share Tech Mono',monospace;font-size:11px;letter-spacing:.12em;text-transform:uppercase;color:var(--accent-2);margin-bottom:8px}
.mag-title{font-family:'IM Fell English',serif;font-size:38px;line-height:1.02;margin-bottom:10px}
.mag-meta{font-family:'Share Tech Mono',monospace;font-size:11px;color:var(--ink-soft);letter-spacing:.04em;margin-bottom:10px}
.mag-desc{font-size:14px;line-height:1.45;color:var(--ink-soft)}
.mag-side-list,.mag-channels,.mag-sections{display:grid;gap:14px}
.mag-mini-title{font-size:18px;font-weight:700;line-height:1.08}
.mag-mini{padding-top:12px;border-top:1px dashed var(--line)}
.mag-mini:first-child{padding-top:0;border-top:none}
.mag-channels{grid-template-columns:repeat(2,minmax(0,1fr))}
.mag-channel-head{display:flex;gap:10px;align-items:center;border-bottom:2px solid var(--accent-2);padding-bottom:8px;margin-bottom:10px}
.mag-channel-head img{width:38px;height:38px;object-fit:contain;background:#fff;border:1px solid var(--line);padding:4px}
.mag-channel-name{font-size:20px;font-weight:700;line-height:1.05}
.mag-list{display:grid;gap:8px}
.mag-item{display:grid;grid-template-columns:74px 1fr;gap:10px;padding-top:8px;border-top:1px dashed var(--line)}
.mag-item:first-child{padding-top:0;border-top:none}
.mag-time{font-family:'Share Tech Mono',monospace;font-size:11px;color:var(--accent-2)}
.mag-item-title{font-size:15px;font-weight:700;line-height:1.08}
.mag-sections{grid-template-columns:repeat(2,minmax(0,1fr))}
.mag-section{background:rgba(255,255,255,.48);border:1px solid var(--line);padding:18px;position:relative;overflow:hidden}
.mag-section::before{content:'';position:absolute;left:0;top:0;bottom:0;width:6px;background:linear-gradient(180deg,var(--accent),rgba(170,66,49,.1))}
.mag-section.featured{background:linear-gradient(180deg,rgba(170,66,49,.16),rgba(255,255,255,.48))}
.mag-section-head{padding-left:8px;margin-bottom:12px}
.mag-section-title{font-family:'IM Fell English',serif;font-size:32px;line-height:1;margin:0 0 8px}
.mag-section-intro{font-size:13px;line-height:1.45;color:var(--ink-soft);max-width:44ch}
.mag-spotlight{display:grid;grid-template-columns:110px 1fr;gap:14px;padding:14px 0;border-top:1px solid rgba(38,70,83,.15);border-bottom:1px solid rgba(38,70,83,.15);margin-bottom:12px}
.mag-spotlight-time{font-family:'Share Tech Mono',monospace;font-size:12px;color:var(--accent-2);letter-spacing:.06em}
.mag-badge{display:inline-flex;align-items:center;padding:4px 8px;background:#f1dfc2;color:#5f2e20;font-family:'Share Tech Mono',monospace;font-size:10px;letter-spacing:.08em;text-transform:uppercase;border:1px solid rgba(170,66,49,.22);margin-bottom:8px}
.mag-spotlight-title{font-size:24px;font-weight:700;line-height:1.03;margin-bottom:6px}
.mag-spotlight-tease{font-size:13px;line-height:1.45;color:var(--ink-soft)}
.mag-briefs{display:grid;gap:10px}
.mag-brief{display:grid;grid-template-columns:74px 1fr;gap:10px;padding-top:10px;border-top:1px dashed var(--line)}
.mag-brief:first-child{padding-top:0;border-top:none}
.mag-brief-time{font-family:'Share Tech Mono',monospace;font-size:11px;color:var(--accent-2)}
.mag-brief-title{font-size:16px;font-weight:700;line-height:1.08}
.mag-brief-tease{font-size:12px;color:var(--ink-soft);line-height:1.35;margin-top:2px}
.mag-footer-title{margin-top:8px}
.timeline-wrap{display:grid;gap:14px}
.timeline-row{display:grid;grid-template-columns:180px 1fr;gap:14px;align-items:stretch}
.timeline-channel{border-top:2px solid var(--accent-2);padding-top:8px}
.timeline-grid{display:grid;grid-template-columns:repeat(var(--cols),minmax(0,1fr));border-top:2px solid var(--accent-2);min-height:108px}
.timeline-slot{border-left:1px dashed var(--line);padding:8px 8px 10px}
.timeline-slot:first-child{border-left:none}
.slot-label{font-family:'Share Tech Mono',monospace;font-size:10px;letter-spacing:.14em;text-transform:uppercase;color:var(--ink-soft);margin-bottom:8px}
.mini{display:block;padding:7px;background:rgba(170,66,49,.08);border-left:3px solid var(--accent);margin-bottom:6px}
.mini-title{font-size:13px;font-weight:700;line-height:1.05}
.mini-meta{font-family:'Share Tech Mono',monospace;font-size:10px;color:var(--ink-soft);margin-top:3px}
@media print{body{background:#fff}.page{padding:0}.toolbar{display:none}.sheet{box-shadow:none;border:none;width:auto;margin:0}.sheet-wrap{overflow:visible}.cards-grid{grid-template-columns:1fr 1fr}}
@media (max-width:1200px){.sheet{width:100%}.cards-grid,.mag-channels,.mag-hero,.mag-sections,.mag-banner,.mag-spotlight{grid-template-columns:1fr}.timeline-row{grid-template-columns:1fr}.timeline-grid{overflow:auto}}
</style>
</head>
<body>
<div class="page">
<div class="toolbar">
<div class="heading">
<h1>Export 24h</h1>
<p>Version imprimable pensée pour être vraiment lisible. Le mode “cartes” sert au PDF ou à lenvoi à quelquun, la timeline dense reste disponible pour une vue plus technique.</p>
</div>
<div class="controls">
<form method="GET" class="filter-form">
<select name="source">
<?php foreach ($sources as $i => $src): ?>
<option value="<?= $i ?>"<?= $i === $source_index ? ' selected' : '' ?>><?= htmlspecialchars($src['name']) ?></option>
<?php endforeach; ?>
</select>
<input type="date" name="day" value="<?= htmlspecialchars($target_day) ?>">
<select name="window">
<?php foreach ($windows as $key => $def): ?>
<option value="<?= htmlspecialchars($key) ?>"<?= $key === $window_key ? ' selected' : '' ?>><?= htmlspecialchars($def['label']) ?></option>
<?php endforeach; ?>
</select>
<select name="limit">
<option value="4"<?= $limit === '4' ? ' selected' : '' ?>>4 channels</option>
<option value="8"<?= $limit === '8' ? ' selected' : '' ?>>8 channels</option>
<option value="all"<?= $limit === 'all' ? ' selected' : '' ?>>All channels</option>
</select>
<select name="layout">
<option value="cards"<?= $layout === 'cards' ? ' selected' : '' ?>>Readable cards</option>
<option value="magazine"<?= $layout === 'magazine' ? ' selected' : '' ?>>TV magazine</option>
<option value="timeline"<?= $layout === 'timeline' ? ' selected' : '' ?>>Dense timeline</option>
</select>
<button type="submit" class="ghost">Refresh</button>
</form>
<button type="button" onclick="window.print()">PDF / Print</button>
<button type="button" class="ghost" onclick="downloadImage()">Image</button>
<a href="/health.php" class="ghost">Health</a>
<a href="/index.php" class="ghost">Guide</a>
</div>
</div>
<div class="sheet-wrap">
<div class="sheet" id="exportSheet">
<div class="sheet-head">
<div>
<div class="sheet-sub">TV Guide · Printable edition · <?= htmlspecialchars($layout_label) ?></div>
<div class="sheet-title">Le programme télé de la journée</div>
</div>
<div class="sheet-meta">
<div class="group"><?= htmlspecialchars($config['group_name'] ?? 'GridTV') ?></div>
<div class="date"><?= htmlspecialchars($day_label) ?> · <?= htmlspecialchars($source['name'] ?? 'Source') ?></div>
</div>
</div>
<div class="legend">
<span><?= htmlspecialchars($window['label']) ?></span>
<span><?= count($visible_channels) ?> channel(s)</span>
<span><?= htmlspecialchars($layout_label) ?></span>
<span>Generated from XMLTV</span>
</div>
<?php if (!$fetch['ok'] || $parsed['error']): ?>
<div class="empty">Impossible de générer la feuille: <?= htmlspecialchars($parsed['error'] ?: ($fetch['error'] ?? 'Unknown error')) ?></div>
<?php elseif (!$visible_channels): ?>
<div class="empty">Aucun programme trouvé avec ce filtre.</div>
<?php elseif ($layout === 'cards'): ?>
<div class="cards-grid">
<?php foreach ($visible_channels as $channel): ?>
<section class="channel-card">
<div class="channel-head">
<?php if ($channel['icon']): ?><img src="<?= htmlspecialchars($channel['icon']) ?>" alt=""><?php endif; ?>
<div>
<div class="channel-name"><?= htmlspecialchars($channel['name']) ?></div>
<div class="channel-count"><?= count($channel['programmes']) ?> programme(s)</div>
</div>
</div>
<div class="prog-list">
<?php foreach ($channel['programmes'] as $programme): ?>
<?php
$meta = $programme['rating'];
$cats = $programme['categories'] ? implode(' · ', $programme['categories']) : '';
?>
<article class="prog">
<div class="prog-time"><?= htmlspecialchars($programme['start']->format('H:i')) ?><br><?= htmlspecialchars($programme['stop']->format('H:i')) ?></div>
<div>
<div class="prog-title"><?= htmlspecialchars($programme['title']) ?></div>
<?php if ($programme['subtitle']): ?><div class="prog-sub"><?= htmlspecialchars($programme['subtitle']) ?></div><?php endif; ?>
<?php if ($meta): ?><div class="prog-sub"><?= htmlspecialchars($meta) ?></div><?php endif; ?>
<?php if ($cats): ?><div class="prog-cats"><?= htmlspecialchars($cats) ?></div><?php endif; ?>
<?php if ($programme['desc']): ?><div class="prog-desc"><?= htmlspecialchars($programme['desc']) ?></div><?php endif; ?>
</div>
</article>
<?php endforeach; ?>
</div>
</section>
<?php endforeach; ?>
</div>
<?php elseif ($layout === 'timeline'): ?>
<div class="timeline-wrap">
<?php foreach ($visible_channels as $channel): ?>
<div class="timeline-row">
<div class="timeline-channel">
<div class="channel-head">
<?php if ($channel['icon']): ?><img src="<?= htmlspecialchars($channel['icon']) ?>" alt=""><?php endif; ?>
<div>
<div class="channel-name"><?= htmlspecialchars($channel['name']) ?></div>
<div class="channel-count"><?= count($channel['programmes']) ?> programme(s)</div>
</div>
</div>
</div>
<div class="timeline-grid" style="--cols:<?= count($slots) ?>">
<?php foreach ($slots as $slot): ?>
<?php
$items = array_values(array_filter($channel['programmes'], static fn($p) => $p['stop'] > $slot['start'] && $p['start'] < $slot['end']));
?>
<div class="timeline-slot">
<div class="slot-label"><?= htmlspecialchars($slot['start']->format('H:i')) ?></div>
<?php if (!$items): ?>
<div class="empty">—</div>
<?php else: ?>
<?php foreach ($items as $programme): ?>
<div class="mini">
<div class="mini-title"><?= htmlspecialchars($programme['title']) ?></div>
<div class="mini-meta"><?= htmlspecialchars($programme['start']->format('H:i') . ' - ' . $programme['stop']->format('H:i')) ?></div>
</div>
<?php endforeach; ?>
<?php endif; ?>
</div>
<?php endforeach; ?>
</div>
</div>
<?php endforeach; ?>
</div>
<?php else: ?>
<div class="magazine">
<section class="mag-banner">
<div>
<div class="mag-kicker" style="color:#f0dcca">Supplement TV</div>
<div class="mag-banner-title">Le magazine de la soiree</div>
<div class="mag-banner-sub">Une version plus editoriale de la grille, pensee comme un vrai programme tele: les temps forts, les series a suivre, les films du soir et une selection par chaine.</div>
</div>
<div class="mag-banner-meta">
<span><?= htmlspecialchars($window['label']) ?></span>
<span><?= count($visible_channels) ?> chaines</span>
<span><?= htmlspecialchars($source['name'] ?? 'Source') ?></span>
</div>
</section>
<div class="mag-hero">
<section class="mag-lead">
<div class="mag-kicker">Cover story</div>
<?php if (!empty($magazine['lead'])): $lead = $magazine['lead']; $p = $lead['programme']; ?>
<div class="mag-title"><?= htmlspecialchars($p['title']) ?></div>
<div class="mag-meta"><?= htmlspecialchars($lead['channel_name']) ?> · <?= htmlspecialchars($p['start']->format('H:i') . ' - ' . $p['stop']->format('H:i')) ?><?= $p['rating'] ? ' · ' . htmlspecialchars($p['rating']) : '' ?></div>
<?php if ($p['subtitle']): ?><div class="mag-desc" style="font-style:italic;margin-bottom:8px"><?= htmlspecialchars($p['subtitle']) ?></div><?php endif; ?>
<div class="mag-desc"><?= htmlspecialchars($p['desc'] ?: 'Programme mis en avant pour cette plage horaire.') ?></div>
<?php else: ?>
<div class="empty">No highlight available for this selection.</div>
<?php endif; ?>
</section>
<aside class="mag-side">
<div class="mag-kicker">Tonights highlights</div>
<div class="mag-side-list">
<?php foreach (array_slice($magazine['highlights'], 1) as $entry): $p = $entry['programme']; ?>
<div class="mag-mini">
<div class="mag-mini-title"><?= htmlspecialchars($p['title']) ?></div>
<div class="mag-meta"><?= htmlspecialchars($entry['channel_name']) ?> · <?= htmlspecialchars($p['start']->format('H:i') . ' - ' . $p['stop']->format('H:i')) ?></div>
<?php if ($p['desc']): ?><div class="mag-desc"><?= htmlspecialchars(mb_strimwidth($p['desc'], 0, 180, '…')) ?></div><?php endif; ?>
</div>
<?php endforeach; ?>
</div>
</aside>
</div>
<div class="mag-kicker">Nos cartes editoriales</div>
<div class="mag-sections">
<?php foreach ($magazine['sections'] as $index => $section): ?>
<section class="mag-section<?= $index === 0 ? ' featured' : '' ?>">
<div class="mag-section-head">
<div class="mag-kicker"><?= htmlspecialchars($section['kicker']) ?></div>
<h2 class="mag-section-title"><?= htmlspecialchars($section['title']) ?></h2>
<div class="mag-section-intro"><?= htmlspecialchars($section['intro']) ?></div>
</div>
<?php $spotlight = $section['items'][0] ?? null; ?>
<?php if ($spotlight): $p = $spotlight['programme']; ?>
<article class="mag-spotlight">
<div class="mag-spotlight-time"><?= htmlspecialchars($p['start']->format('H:i')) ?><br><?= htmlspecialchars($p['stop']->format('H:i')) ?></div>
<div>
<div class="mag-badge"><?= htmlspecialchars(export_programme_label($p)) ?></div>
<div class="mag-spotlight-title"><?= htmlspecialchars($p['title']) ?></div>
<div class="mag-meta"><?= htmlspecialchars($spotlight['channel_name']) ?><?= $p['rating'] ? ' · ' . htmlspecialchars($p['rating']) : '' ?></div>
<div class="mag-spotlight-tease"><?= htmlspecialchars(export_entry_tease($spotlight)) ?></div>
</div>
</article>
<?php endif; ?>
<div class="mag-briefs">
<?php foreach (array_slice($section['items'], 1) as $entry): $p = $entry['programme']; ?>
<article class="mag-brief">
<div class="mag-brief-time"><?= htmlspecialchars($p['start']->format('H:i')) ?></div>
<div>
<div class="mag-brief-title"><?= htmlspecialchars($p['title']) ?></div>
<div class="mag-meta"><?= htmlspecialchars($entry['channel_name']) ?></div>
<div class="mag-brief-tease"><?= htmlspecialchars(export_entry_tease($entry)) ?></div>
</div>
</article>
<?php endforeach; ?>
</div>
</section>
<?php endforeach; ?>
</div>
<div class="mag-kicker mag-footer-title">Les chaines a la loupe</div>
<div class="mag-channels">
<?php foreach ($magazine['channels'] as $channel): ?>
<section class="mag-channel">
<div class="mag-channel-head">
<?php if ($channel['icon']): ?><img src="<?= htmlspecialchars($channel['icon']) ?>" alt=""><?php endif; ?>
<div class="mag-channel-name"><?= htmlspecialchars($channel['name']) ?></div>
</div>
<div class="mag-list">
<?php foreach (array_slice($channel['programmes'], 0, 3) as $programme): ?>
<div class="mag-item">
<div class="mag-time"><?= htmlspecialchars($programme['start']->format('H:i')) ?><br><?= htmlspecialchars($programme['stop']->format('H:i')) ?></div>
<div>
<div class="mag-item-title"><?= htmlspecialchars($programme['title']) ?></div>
<?php if ($programme['subtitle']): ?><div class="prog-sub"><?= htmlspecialchars($programme['subtitle']) ?></div><?php endif; ?>
<?php if ($programme['categories']): ?><div class="prog-cats"><?= htmlspecialchars(implode(' · ', array_slice($programme['categories'], 0, 3))) ?></div><?php endif; ?>
</div>
</div>
<?php endforeach; ?>
</div>
</section>
<?php endforeach; ?>
</div>
</div>
<?php endif; ?>
<div class="note">Astuce: “TV magazine” est parfait pour une belle feuille éditoriale. “Readable cards” reste le plus propre pour une impression exhaustive.</div>
</div>
</div>
</div>
<script>
async function downloadImage() {
const sheet = document.getElementById('exportSheet');
if (!sheet || typeof html2canvas !== 'function') return;
const canvas = await html2canvas(sheet, {backgroundColor: '#f4ecda', scale: 2, useCORS: true});
const link = document.createElement('a');
link.href = canvas.toDataURL('image/png');
link.download = 'gridtv-<?= htmlspecialchars($window_key) ?>-<?= htmlspecialchars($target_day) ?>.png';
link.click();
}
</script>
</body>
</html>