File modules/cms/ui/action/index/IndexThemestyleAction.class.php
Last commit: Sun Feb 6 21:56:52 2022 +0100 dankert New CssOutput which outputs the CSS in a cleaner way.
1 <?php 2 namespace cms\ui\action\index; 3 use cms\action\Method; 4 use cms\base\Configuration as C; 5 use cms\base\Startup; 6 use cms\ui\action\IndexAction; 7 use cms\action\RequestParams; 8 use cms\auth\Auth; 9 use cms\auth\AuthRunner; 10 use cms\base\Configuration; 11 use cms\model\BaseObject; 12 use cms\model\Project; 13 use cms\model\User; 14 use cms\model\Value; 15 use cms\ui\themes\Theme; 16 use cms\ui\themes\ThemeStyle; 17 use Exception; 18 use language\Messages; 19 use util\Html; 20 use util\json\JSON; 21 use logger\Logger; 22 use util\Less; 23 use util\text\Converter; 24 use util\UIUtils; 25 use \util\exception\ObjectNotFoundException; 26 use util\Session; 27 class IndexThemestyleAction extends IndexAction implements Method { 28 29 const DEFAULT_COLOR_SCHEME = 'light'; 30 31 public function view() { 32 33 $themeLessFile = Startup::THEMES_DIR . 'default/style/theme/openrat-theme.less'; 34 $this->lastModified(filemtime($themeLessFile)); 35 36 $styleName = $this->request->getText('style'); 37 38 $this->setTemplateVar('style',$this->getThemeCSS( $styleName) ); 39 } 40 41 42 public function post() { 43 } 44 45 46 /** 47 * Gets the theme CSS. 48 * 49 * @param $styleId string name of style 50 * @return string The ready to use CSS 51 */ 52 protected function getThemeCSS( $styleId ) 53 { 54 // Je Theme die Theme-CSS-Datei ausgeben. 55 $lessFile = Startup::THEMES_DIR . 'default/style/theme/openrat-theme.less'; 56 $css = ''; 57 58 $styleConfig = C::subset( ['style',$styleId] ); 59 foreach( ['light','dark'] as $scheme ) { 60 61 $schemeConfig = $styleConfig->subset('defaults')->merge( $styleConfig->subset('schemes')->subset($scheme) ); 62 63 $colorScheme = $scheme!=self::DEFAULT_COLOR_SCHEME ? $scheme : ''; // "Light" is the default 64 65 if ( $colorScheme ) 66 $css .= '@media screen and (prefers-color-scheme: '.$scheme.') {'; 67 try 68 { 69 $themeStyle = new ThemeStyle( $schemeConfig->getConfig() ); 70 71 if ( DEVELOPMENT ) 72 $css .= "\n".'/* Theme: '.$styleId.' */'."\n"; 73 74 $lessVars = array( 75 'cms-theme-id' => strtolower($styleId), 76 'cms-image-path' => '"'.Startup::THEMES_DIR.'default/images/'.'"', 77 ); 78 79 foreach ( $themeStyle->getProperties() as $styleSetting => $value) 80 $lessVars['cms-' . Converter::camelToUnderscore($styleSetting, '-')] = $value; 81 82 if ( DEVELOPMENT ) 83 $css .= "\n".'/* Theme-Properties: '.print_r( $lessVars,true).' */'."\n"; 84 85 // we must create a new instance here, because the less parser is buggy when changing vars. 86 $parser = new Less(array( 87 'sourceMap' => DEVELOPMENT, 88 'indentation' => DEVELOPMENT?"\t":'', 89 'outputSourceFiles' => false, 90 'compress' => PRODUCTION 91 )); 92 $parser->parseFile($lessFile,basename($lessFile)); 93 $parser->modifyVars($lessVars); 94 $css .= $parser->getCss(); 95 } 96 catch (Exception $e) 97 { 98 Logger::warn( new \RuntimeException("LESS Parser failed on file '$lessFile'.", 0,$e) ); 99 100 // For not confusing the browser we are displaying a CSS with a comment. 101 if ( DEVELOPMENT ) 102 $css .= "\n\n/* ERROR!\n LESS Parser failed on file '$lessFile'. Reason: " . $e->__toString() . " */\nhtml { content: \"Theme not available\";}\n"; 103 } 104 105 if ( $colorScheme ) 106 $css .= '}'; 107 } 108 109 return $css; 110 } 111 112 }
Downloadmodules/cms/ui/action/index/IndexThemestyleAction.class.php
History Sun, 6 Feb 2022 21:56:52 +0100 dankert New CssOutput which outputs the CSS in a cleaner way. Mon, 29 Nov 2021 01:22:40 +0100 Jan Dankert New: Themes may contain multiple color schemes (for now only "dark" and "light"), modern browsers are selecting the right scheme. Sat, 27 Mar 2021 10:40:59 +0100 Jan Dankert Only generate the actual necessary theme style. Wed, 10 Feb 2021 22:04:26 +0100 Jan Dankert Provide the theme configuration through a class. Wed, 18 Nov 2020 20:23:57 +0100 Jan Dankert Cleaning up the UI actions. Tue, 17 Nov 2020 23:51:00 +0100 Jan Dankert Refactoring: Every Actionmethod has now its own class.