Favorites

This commit is contained in:
Gogs
2026-07-07 11:32:03 +02:00
parent f3d25f20d3
commit c2da564dd6
5 changed files with 127 additions and 87 deletions
+83
View File
@@ -0,0 +1,83 @@
<?php
class HomeController
{
public function __construct(
private Config $config,
private XmlTv $xmltv,
private ChannelService $channelService = new ChannelService()
) {
}
public function handle(array $get, array $post, string $method): void
{
$this->xmltv->synchronizeChannels($this->config);
$view = $get['view'] ?? $this->config->get('default_view', 'today');
if ($view === 'channels' && $method === 'POST') {
$this->channelService->saveChannelsFromPost($this->config, $post);
header('Location: /?view=channels&saved=1');
exit;
}
[$channels, $programmes] = $this->xmltv->load($this->config);
$currentProgrammes = $this->xmltv->getCurrentProgrammes($this->config);
$tz = new DateTimeZone($this->config->get('timezone', 'Europe/Vienna'));
$selectedDate = $get['date'] ?? (new DateTime('now', $tz))->format('Y-m-d');
$selectedChannel = $get['channel'] ?? null;
if ($selectedChannel === '') {
$selectedChannel = null;
}
if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $selectedDate)) {
$selectedDate = (new DateTime('now', $tz))->format('Y-m-d');
}
$dayProgrammes = $this->xmltv->getDayProgrammes($this->config, $selectedDate, $selectedChannel);
$nowDateTime = new DateTime('now', $tz);
$isToday = ($selectedDate === $nowDateTime->format('Y-m-d'));
$nowMinute = null;
if ($isToday) {
$nowMinute =
((int)$nowDateTime->format('H') * 60) +
(int)$nowDateTime->format('i');
}
if ($view === 'favorites') {
$currentProgrammes = array_values(array_filter(
$currentProgrammes,
fn($item) => !empty($item['favorite'])
));
}
$contentTemplate = match ($view) {
'channels' => '/var/www/templates/channels.php',
'now' => '/var/www/templates/now.php',
'favorites' => '/var/www/templates/now.php',
'day' => '/var/www/templates/day.php',
default => '/var/www/templates/home.php',
};
View::render('layout', [
'title' => $this->config->get('title', 'TVGuide'),
'channels' => $channels,
'programmes' => $programmes,
'contentTemplate' => $contentTemplate,
'view' => $view,
'channelConfig' => $this->config->getChannels(),
'saved' => isset($get['saved']),
'currentProgrammes' => $currentProgrammes,
'selectedDate' => $selectedDate,
'selectedChannel' => $selectedChannel,
'dayProgrammes' => $dayProgrammes,
'isToday' => $isToday,
'nowMinute' => $nowMinute,
]);
}
}
+23 -2
View File
@@ -31,10 +31,31 @@ class ChannelService
}
}
uasort($storedChannels, function ($a, $b) {
$config->saveChannels($this->sortChannels($storedChannels));
}
public function saveChannelsFromPost(Config $config, array $post): void
{
$savedChannels = $config->getChannels();
foreach ($savedChannels as $id => &$channel) {
$channel['enabled'] = isset($post['enabled'][$id]);
$channel['favorite'] = isset($post['favorite'][$id]);
$channel['number'] = (int)($post['number'][$id] ?? $channel['number'] ?? 999999);
$channel['group'] = trim($post['group'][$id] ?? $channel['group'] ?? 'Unknown');
}
unset($channel);
$config->saveChannels($this->sortChannels($savedChannels));
}
private function sortChannels(array $channels): array
{
uasort($channels, function ($a, $b) {
return ((int)($a['number'] ?? 999999)) <=> ((int)($b['number'] ?? 999999));
});
$config->saveChannels($storedChannels);
return $channels;
}
}