openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

IndexThemestyleAction.class.php (3216B)


      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 }