From c2da564dd6b7961ba47f3ebe5fdb8932471ab04d Mon Sep 17 00:00:00 2001 From: Gogs Date: Tue, 7 Jul 2026 11:32:03 +0200 Subject: [PATCH] Favorites --- app/config/channels.json | 2 +- app/public/index.php | 88 ++------------------------ app/src/Controllers/HomeController.php | 83 ++++++++++++++++++++++++ app/src/Services/ChannelService.php | 25 +++++++- app/templates/now.php | 16 ++++- 5 files changed, 127 insertions(+), 87 deletions(-) create mode 100644 app/src/Controllers/HomeController.php diff --git a/app/config/channels.json b/app/config/channels.json index 31157af..5eb7dbe 100644 --- a/app/config/channels.json +++ b/app/config/channels.json @@ -17,7 +17,7 @@ "country": "", "language": "", "enabled": true, - "favorite": false, + "favorite": true, "number": 2, "group": "Unknown" } diff --git a/app/public/index.php b/app/public/index.php index 2134728..60c2cf3 100644 --- a/app/public/index.php +++ b/app/public/index.php @@ -1,11 +1,14 @@ get('timezone', 'Europe/Vienna') ); -$xmltv->synchronizeChannels($config); - -$view = $_GET['view'] ?? $config->get('default_view', 'today'); - -if ($view === 'channels' && $_SERVER['REQUEST_METHOD'] === 'POST') { - $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); - - uasort($savedChannels, function ($a, $b) { - return ((int)($a['number'] ?? 999999)) <=> ((int)($b['number'] ?? 999999)); - }); - - $config->saveChannels($savedChannels); - - header('Location: /?view=channels&saved=1'); - exit; -} - -[$channels, $programmes] = $xmltv->load($config); - -$currentProgrammes = $xmltv->getCurrentProgrammes($config); - -$tz = new DateTimeZone($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 = date('Y-m-d'); -} - -$dayProgrammes = $xmltv->getDayProgrammes($config, $selectedDate, $selectedChannel); - -$tz = new DateTimeZone($config->get('timezone', 'Europe/Vienna')); -$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'); -} - -$contentTemplate = match ($view) { - 'channels' => '/var/www/templates/channels.php', - 'now' => '/var/www/templates/now.php', - 'day' => '/var/www/templates/day.php', - default => '/var/www/templates/home.php', -}; - -View::render('layout', [ - 'title' => $config->get('title', 'TVGuide'), - 'channels' => $channels, - 'programmes' => $programmes, - 'contentTemplate' => $contentTemplate, - 'view' => $view, - 'channelConfig' => $config->getChannels(), - 'saved' => isset($_GET['saved']), - 'currentProgrammes' => $currentProgrammes, - 'selectedDate' => $selectedDate, - 'selectedChannel' => $selectedChannel, - 'dayProgrammes' => $dayProgrammes, - 'isToday' => $isToday, - 'nowMinute' => $nowMinute, -]); +$controller = new HomeController($config, $xmltv); +$controller->handle($_GET, $_POST, $_SERVER['REQUEST_METHOD']); diff --git a/app/src/Controllers/HomeController.php b/app/src/Controllers/HomeController.php new file mode 100644 index 0000000..33d09f7 --- /dev/null +++ b/app/src/Controllers/HomeController.php @@ -0,0 +1,83 @@ +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, + ]); + } +} diff --git a/app/src/Services/ChannelService.php b/app/src/Services/ChannelService.php index a7f36ae..3512fd0 100644 --- a/app/src/Services/ChannelService.php +++ b/app/src/Services/ChannelService.php @@ -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; } } diff --git a/app/templates/now.php b/app/templates/now.php index c5331f6..5b8614d 100644 --- a/app/templates/now.php +++ b/app/templates/now.php @@ -1,10 +1,20 @@ + +
-

Jetzt läuft

-

Aktuelle Sendungen auf deinen aktivierten Sendern.

+

+

+ +

-
Aktuell wurden keine laufenden Sendungen gefunden.
+
+ +