diff --git a/app/config/channels.json b/app/config/channels.json index 0967ef4..ec0bc15 100644 --- a/app/config/channels.json +++ b/app/config/channels.json @@ -1 +1,24 @@ -{} +{ + "orf1.at": { + "uuid": "58a965b21f48319d1a4051476667f5e2", + "name": "ORF 1", + "icon": "\/logos\/orf1.png", + "country": "", + "language": "", + "enabled": true, + "favorite": false, + "number": 1, + "group": "Unknown" + }, + "orf2.at": { + "uuid": "4c9dba34a821b5b25c8e1e2b65abc4df", + "name": "ORF 2", + "icon": "\/logos\/orf2.png", + "country": "", + "language": "", + "enabled": true, + "favorite": false, + "number": 2, + "group": "Unknown" + } +} \ No newline at end of file diff --git a/app/public/index.php b/app/public/index.php index 61e5910..c016681 100644 --- a/app/public/index.php +++ b/app/public/index.php @@ -1,8 +1,8 @@ get('timezone', 'Europe/Vienna') ); -$xmltv = new XmlTv('/var/www/epg/guide.xml', 'Europe/Vienna'); -[$channels, $programmes] = $xmltv->load(); +$xmltv->synchronizeChannels($config); + +[$channels, $programmes] = $xmltv->load($config); $contentTemplate = '/var/www/templates/home.php'; View::render('layout', [ - 'title' => $config->get('title'), + 'title' => $config->get('title', 'TVGuide'), 'channels' => $channels, 'programmes' => $programmes, 'contentTemplate' => $contentTemplate, diff --git a/app/src/XmlTv.php b/app/src/XmlTv.php index 201cbd6..9f0d101 100644 --- a/app/src/XmlTv.php +++ b/app/src/XmlTv.php @@ -11,10 +11,11 @@ class XmlTv $this->timezone = $timezone; } - public function load(): array + public function load(?Config $config = null): array { $channels = []; $programmes = []; + $channelConfig = $config ? $config->getChannels() : []; if (!file_exists($this->file)) { return [$channels, $programmes]; @@ -25,15 +26,27 @@ class XmlTv foreach ($xml->channel as $channel) { $id = (string)$channel['id']; + $cfg = $channelConfig[$id] ?? []; + + if (($cfg['enabled'] ?? true) !== true) { + continue; + } + $channels[$id] = [ 'id' => $id, - 'name' => (string)$channel->{'display-name'}, - 'icon' => isset($channel->icon) ? (string)$channel->icon['src'] : null, + 'name' => $cfg['name'] ?? (string)$channel->{'display-name'}, + 'icon' => $cfg['icon'] ?? (isset($channel->icon) ? (string)$channel->icon['src'] : null), + 'number' => (int)($cfg['number'] ?? 999999), + 'favorite' => (bool)($cfg['favorite'] ?? false), ]; } foreach ($xml->programme as $programme) { $channelId = (string)$programme['channel']; + if (!isset($channels[$channelId])) { + continue; + } + $start = $this->parseTime((string)$programme['start']); $stop = $this->parseTime((string)$programme['stop']); @@ -50,9 +63,59 @@ class XmlTv ]; } + uasort($channels, function ($a, $b) { + return ($a['number'] ?? 999999) <=> ($b['number'] ?? 999999); + }); + return [$channels, $programmes]; } + public function synchronizeChannels(Config $config): void + { + if (!file_exists($this->file)) { + return; + } + + $xml = simplexml_load_file($this->file); + $storedChannels = $config->getChannels(); + + $maxNumber = 0; + foreach ($storedChannels as $stored) { + $maxNumber = max($maxNumber, (int)($stored['number'] ?? 0)); + } + + foreach ($xml->channel as $channel) { + $id = (string)$channel['id']; + $name = (string)$channel->{'display-name'}; + $icon = isset($channel->icon) ? (string)$channel->icon['src'] : ''; + + if (!isset($storedChannels[$id])) { + $maxNumber++; + + $storedChannels[$id] = [ + 'uuid' => bin2hex(random_bytes(16)), + 'name' => $name, + 'icon' => $icon, + 'country' => '', + 'language' => '', + 'enabled' => true, + 'favorite' => false, + 'number' => $maxNumber, + 'group' => 'Unknown', + ]; + } else { + $storedChannels[$id]['name'] = $name; + $storedChannels[$id]['icon'] = $icon; + } + } + + uasort($storedChannels, function ($a, $b) { + return ((int)($a['number'] ?? 999999)) <=> ((int)($b['number'] ?? 999999)); + }); + + $config->saveChannels($storedChannels); + } + private function parseTime(string $value): ?DateTime { if (!preg_match('/^(\d{14})\s+([+-]\d{4})$/', trim($value), $m)) {