File modules/cms/ui/action/index/IndexThemeAction.class.php

Last commit: Tue Oct 13 00:42:20 2026 +0200	Jan Dankert	PHP8: The user can be null here, if there is no user logged in.
1 <?php 2 namespace cms\ui\action\index; 3 use cms\action\Method; 4 use cms\base\Configuration; 5 use cms\base\Configuration as C; 6 use cms\model\User; 7 use cms\ui\action\IndexAction; 8 use cms\ui\themes\ThemeStyle; 9 use util\ArrayUtils; 10 use util\text\Converter; 11 12 class IndexThemeAction extends IndexAction implements Method { 13 14 const CSS_VARIABLE_PREFIX = '--cms-'; 15 16 public function view() { 17 18 //$this->lastModified( Config::filemtime($themeLessFile)); 19 20 $styleId = $this->getUserStyle($this->currentUser); 21 $schemaId = $this->currentUser ? $this->getUserScheme($this->currentUser) : User::STYLE_SCHEME_AUTO; 22 $css = match ($schemaId) { 23 User::STYLE_SCHEME_AUTO => [ 24 ':root' => $this->getThemeCSSVariables($styleId, 'light'), 25 '@media (prefers-color-scheme: dark)' => [ 26 ':root' => $this->getThemeCSSVariables($styleId, 'dark') 27 ] 28 ], 29 User::STYLE_SCHEME_LIGHT => [ 30 ':root' => $this->getThemeCSSVariables($styleId, 'light') 31 ], 32 User::STYLE_SCHEME_DARK => [ 33 ':root' => $this->getThemeCSSVariables($styleId, 'dark') 34 ], 35 default => throw new \LogicException("invalid schema id: " . $schemaId), 36 }; 37 38 $this->setTemplateVars($css); 39 $this->setTemplateVar('name','"'.$styleId.'"'); 40 } 41 42 43 public function post() { 44 } 45 46 47 /** 48 * Gets the theme CSS. 49 * 50 * @param $styleId string name of style 51 * @param $schema string schemaId, f.e. 'light','dark' 52 * @return array The css variables for this thema 53 */ 54 protected function getThemeCSSVariables($styleId, $schema ) 55 { 56 $styleConfig = C::subset( ['style',$styleId] ); 57 58 $schemeConfig = $styleConfig->subset('schemes');; 59 $config = $schemeConfig->subset($schema); 60 61 $schemeConfig = $config->merge( $styleConfig->subset('defaults') ); 62 $themeStyle = new ThemeStyle( $schemeConfig->getConfig() ); 63 64 $schemesConfig = ArrayUtils::mapKeys( function($prop) { 65 return self::CSS_VARIABLE_PREFIX.Converter::camelToUnderscore($prop, '-'); 66 },$themeStyle->getProperties()); 67 68 return $schemesConfig; 69 } 70 71 }
Download modules/cms/ui/action/index/IndexThemeAction.class.php
History Tue, 13 Oct 2026 00:42:20 +0200 Jan Dankert PHP8: The user can be null here, if there is no user logged in. Tue, 13 Oct 2026 00:22:21 +0200 Jan Dankert Refactoring: Fixing the flickering UI: Create CSS with dark/light-schemes, so the browser can decide which to use.