diff options
| author | Simon Rettberg | 2025-07-14 16:50:29 +0200 |
|---|---|---|
| committer | Simon Rettberg | 2025-07-14 16:50:29 +0200 |
| commit | 83c4bb160cea6e7fbadbe4c0876b754f0acf24c5 (patch) | |
| tree | 96b8955da109cd15998e3a84699abb11627cb2c3 /modules-available/locationinfo/page.inc.php | |
| parent | [locationinfo] Remove debug spam (diff) | |
| download | slx-admin-83c4bb160cea6e7fbadbe4c0876b754f0acf24c5.tar.gz slx-admin-83c4bb160cea6e7fbadbe4c0876b754f0acf24c5.tar.xz slx-admin-83c4bb160cea6e7fbadbe4c0876b754f0acf24c5.zip | |
[locationinfo] Add UPCOMING panel type
Diffstat (limited to 'modules-available/locationinfo/page.inc.php')
| -rw-r--r-- | modules-available/locationinfo/page.inc.php | 180 |
1 files changed, 118 insertions, 62 deletions
diff --git a/modules-available/locationinfo/page.inc.php b/modules-available/locationinfo/page.inc.php index fd9abb18..538f2514 100644 --- a/modules-available/locationinfo/page.inc.php +++ b/modules-available/locationinfo/page.inc.php @@ -203,13 +203,19 @@ class Page_LocationInfo extends Page * and remove any ids that don't exist. The cleaned list will be returned. * Will show error and redirect to main page if parameter is missing * - * @param bool $failIfEmpty Show error and redirect to main page if parameter is missing or list is empty - * @return array list of locations from parameter + * @return int[] list of locations from parameter */ private function getLocationIdsFromRequest(): array { - $locationids = Request::post('locationids', Request::REQUIRED_EMPTY, 'string'); - $locationids = explode(',', $locationids); + $locationids = Request::post('locationids', Request::REQUIRED_EMPTY); + if (is_array($locationids)) { + // NOOP + } elseif (is_numeric($locationids)) { + $locationids = [(int)$locationids]; + } elseif (is_string($locationids)) { + $locationids = explode(',', $locationids); + } + $locationids = array_map('intval', $locationids); $all = Location::getAllLocationIds(); $locationids = array_filter($locationids, function ($item) use ($all) { return in_array($item, $all); }); if (empty($locationids)) { @@ -233,13 +239,24 @@ class Page_LocationInfo extends Page // Check panel type $paneltype = Request::post('ptype', false, 'string'); - if ($paneltype === 'DEFAULT') { +//Refactored Code + switch ($paneltype) { + case 'DEFAULT': $params = $this->preparePanelConfigDefault(); - } elseif ($paneltype === 'URL') { + break; + case 'URL': $params = $this->preparePanelConfigUrl(); - } elseif ($paneltype === 'SUMMARY') { + break; + case 'SUMMARY': $params = $this->preparePanelConfigSummary(); - } else { + break; + case 'UPCOMING': + $params = [ + 'config' => LocationInfo::panelConfigFromPost($paneltype), + 'locationids' => self::getLocationIdsFromRequest(), + ]; + break; + default: Message::addError('invalid-panel-type', $paneltype); Util::redirect('?do=locationinfo', 400); } @@ -257,7 +274,7 @@ class Page_LocationInfo extends Page WHERE paneluuid = :id"; } $params['id'] = $paneluuid; - $params['name'] = Request::post('name', '-', 'string'); + $params['name'] = Request::post('panelname', '-', 'string'); $params['type'] = $paneltype; $params['now'] = time(); $params['config'] = json_encode($params['config']); @@ -542,7 +559,8 @@ class Page_LocationInfo extends Page $locations = Location::getLocations(0, 0, false, true); // Get hidden state of all locations - $dbquery = Database::simpleQuery("SELECT li.locationid, li.serverid, li.serverlocationid, loc.openingtime, li.lastcalendarupdate, cb.servertype, cb.servername + $dbquery = Database::simpleQuery("SELECT li.locationid, li.serverid, li.serverlocationid, + li.lastcalendarupdate, loc.openingtime, cb.servertype, cb.servername FROM `locationinfo_locationconfig` AS li LEFT JOIN `locationinfo_coursebackend` AS cb USING (serverid) LEFT JOIN `location` AS loc USING (locationid)"); @@ -903,28 +921,10 @@ class Page_LocationInfo extends Page return; } $config = false; - if ($id === 'new-default') { - // Creating new panel - $panel = array( - 'panelname' => '', - 'locationids' => '', - 'paneltype' => 'DEFAULT', - ); - $id = 'new'; - } elseif ($id === 'new-summary') { - // Creating new panel - $panel = array( - 'panelname' => '', - 'locationids' => '', - 'paneltype' => 'SUMMARY', - ); - $id = 'new'; - } elseif ($id === 'new-url') { - // Creating new panel - $panel = array( - 'panelname' => '', - 'paneltype' => 'URL', - ); + if (substr($id, 0, 4) === 'new-') { + $panel = [ + 'paneltype' => substr($id, 4), + ]; $id = 'new'; } else { // Get Config data from db @@ -951,6 +951,7 @@ class Page_LocationInfo extends Page } else { $config += $def; } + $config['panelname'] = $panel['panelname']; $langs = Dictionary::getLanguages(true); if (isset($config['language'])) { @@ -965,7 +966,7 @@ class Page_LocationInfo extends Page Render::addTemplate('page-config-panel-default', array( 'new' => $id === 'new', 'uuid' => $id, - 'panelname' => $panel['panelname'], + 'panelname' => $panel['panelname'] ?? '', 'languages' => $langs, 'mode' => $config['mode'], 'vertical_checked' => $config['vertical'] ? 'checked' : '', @@ -981,7 +982,7 @@ class Page_LocationInfo extends Page 'calupdate' => $config['calupdate'], 'roomupdate' => $config['roomupdate'], 'locations' => Location::getLocations(), - 'locationids' => $panel['locationids'], + 'locationids' => $panel['locationids'] ?? '', 'overrides' => json_encode($config['overrides']), 'hostname_checked' => $config['hostname'] ? 'checked' : '', )); @@ -1006,7 +1007,7 @@ class Page_LocationInfo extends Page Render::addTemplate('page-config-panel-url', array( 'new' => $id === 'new', 'uuid' => $id, - 'panelname' => $panel['panelname'], + 'panelname' => $panel['panelname'] ?? '', 'url' => $config['url'], 'zoom-factor' => $config['zoom-factor'], 'language' => $config['language'], @@ -1022,16 +1023,27 @@ class Page_LocationInfo extends Page 'bookmarks' => $bookmarksArray, 'allow-tty_' . $config['allow-tty'] . '_checked' => 'checked', )); - } else { + } elseif ($panel['paneltype'] === 'UPCOMING') { + $configData = LocationInfo::getEditTemplateData($panel['paneltype'], $config); + LocationInfo::makeCheckedProperties($panel['paneltype'], $config); + Render::addTemplate('page-config-panel-upcoming', array( + 'new' => $id === 'new', + 'uuid' => $id, + 'sections' => $configData, + 'locationtree' => Render::parse('fragment-locationtree', [ + 'locations' => Location::getLocations(explode(',', $panel['locationids'] ?? '')), + ]), + )); + } elseif ($panel['paneltype'] === 'SUMMARY') { Render::addTemplate('page-config-panel-summary', array( 'new' => $id === 'new', 'uuid' => $id, - 'panelname' => $panel['panelname'], + 'panelname' => $panel['panelname'] ?? '', 'languages' => $langs, 'panelupdate' => $config['panelupdate'], 'roomplanner' => $config['roomplanner'], 'locations' => Location::getLocations(), - 'locationids' => $panel['locationids'], + 'locationids' => $panel['locationids'] ?? '', 'eco_checked' => $config['eco'] ? 'checked' : '', )); } @@ -1044,6 +1056,7 @@ class Page_LocationInfo extends Page http_response_code(400); die('Missing parameter uuid'); } + $config = []; $type = InfoPanel::getConfig($uuid, $config); if ($type === null) { http_response_code(404); @@ -1054,7 +1067,7 @@ class Page_LocationInfo extends Page Util::redirect($config['url']); } - $data = array(); + $data = []; preg_match('#^/(.*)/#', $_SERVER['PHP_SELF'], $script); preg_match('#^/([^?]+)/#', $_SERVER['REQUEST_URI'], $request); if ($script[1] !== $request[1]) { @@ -1065,36 +1078,56 @@ class Page_LocationInfo extends Page $data['api'] = 'api.php?do=locationinfo&'; } - if ($type === 'DEFAULT') { - $data += array( - 'uuid' => $uuid, - 'config' => json_encode($config), - 'language' => $config['language'], - ); + $lang = $config['language'] ?? 'en'; + $reqLang = Request::get('forcelang', false, 'string'); + if ($reqLang !== false && Dictionary::hasLanguage($reqLang)) { + $lang = $reqLang; + } elseif ($config['language-system'] ?? false) { + $langs = preg_split('/[,\s]+/', $_SERVER['HTTP_ACCEPT_LANGUAGE']); + foreach ($langs as $check) { + $check = substr($check, 0, 2); + if (Dictionary::hasLanguage($check)) { + $lang = $check; + break; + } + } + } + unset($config['language']); + unset($config['language-system']); + if ($type === 'SUMMARY') { + $locations = LocationInfo::getLocationsOr404($uuid, false); + $config['tree'] = Location::getTree(...$locations); + } + $data += array( + 'uuid' => $uuid, + 'config' => json_encode($config), + 'language' => $lang, + ) + $config; - die(Render::parse('frontend-default', $data, null, $config['language'])); + if ($type === 'DEFAULT') { + die(Render::parse('frontend-default', $data, null, $lang)); } if ($type === 'UPCOMING') { - $data += array( - 'uuid' => $uuid, - 'config' => json_encode($config), - 'language' => $config['language'], - ); - - die(Render::parse('frontend-kg2-upcoming', $data, null, $config['language'])); + $mod = ''; + if (!empty($config['mod']) && preg_match('#^[a-z0-9]+$#', $config['mod']) && file_exists('modules/locationinfo/templates/frontend-upcoming-' . $config['mod'] . '.html')) { + $mod = '-' . $config['mod']; + } + $bg = $this->parseCssColor($config['color_bg'] ?? '#000'); + if ($bg !== null) { + $data['color_grad1'] = 'rgba(' . $bg[0] . ',' . $bg[1] . ',' . $bg[2] . ', 0)'; + $data['color_grad2'] = 'rgba(' . $bg[0] . ',' . $bg[1] . ',' . $bg[2] . ', .1)'; + $data['color_grad3'] = 'rgba(' . $bg[0] . ',' . $bg[1] . ',' . $bg[2] . ', .9)'; + } + $fg = $this->parseCssColor($config['color_fg'] ?? '#fff'); + if ($fg !== null) { + $data['color_half'] = 'rgba(' . $fg[0] . ',' . $fg[1] . ',' . $fg[2] . ', .5)'; + } + die(Render::parse('frontend-upcoming' . $mod, $data, null, $lang)); } if ($type === 'SUMMARY') { - $locations = LocationInfo::getLocationsOr404($uuid, false); - $config['tree'] = Location::getTree(...$locations); - $data += array( - 'uuid' => $uuid, - 'config' => json_encode($config), - 'language' => $config['language'], - ); - - die(Render::parse('frontend-summary', $data, null, $config['language'])); + die(Render::parse('frontend-summary', $data, null, $lang)); } http_response_code(500); @@ -1138,4 +1171,27 @@ class Page_LocationInfo extends Page Util::redirect('?do=locationinfo'); } + /** + * Parse a CSS color parameter into an array of color components. + * @param string $param The CSS color parameter to parse. + * @return ?array An array containing the color components [R, G, B, A] if valid, or null if parsing fails. + */ + private function parseCssColor(string $param): ?array + { + $param = trim($param); + // #fff + if (preg_match('/^#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])$/', $param, $out)) + return [hexdec($out[1]) * 17, hexdec($out[2]) * 17, hexdec($out[3]) * 17, 1]; + // #ffffff + if (preg_match('/^#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})$/', $param, $out)) + return [hexdec($out[1]), hexdec($out[2]), hexdec($out[3]), 1]; + // rgb(255,255,255) + if (preg_match('/^rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)$/i', $param, $out)) + return [(int)($out[1]), (int)($out[2]), (int)($out[3]), 1]; + // rgba(255,255,255,1) + if (preg_match('/^rgba\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9.]{1,5})\s*\)$/i', $param, $out)) + return [(int)$out[1], (int)$out[2], (int)$out[3], (float)$out[4]]; + return null; + } + } |
