-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport.php
More file actions
392 lines (362 loc) · 19.6 KB
/
export.php
File metadata and controls
392 lines (362 loc) · 19.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<?php
require_once __DIR__ . '/src/lib/common.php';
$config = gridtv_load_config();
[$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;
}
$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'], 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' : 'Dense timeline';
?><!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}
.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{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 à l’envoi à quelqu’un, 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="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="/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>
</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 endif; ?>
<div class="note">Astuce: “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>