From 87b9489976749f2b43f655703131ac78a145572b Mon Sep 17 00:00:00 2001 From: Cameron Date: Thu, 2 Feb 2017 16:42:11 -0800 Subject: [PATCH] Theme handler optimization --- e107_handlers/e107_class.php | 19 +++- e107_handlers/theme_handler.php | 177 ++++++++++++++++++++++++++++++- e107_themes/bootstrap3/theme.xml | 10 +- e107_web/js/core/admin.jquery.js | 4 +- 4 files changed, 194 insertions(+), 16 deletions(-) diff --git a/e107_handlers/e107_class.php b/e107_handlers/e107_class.php index c5c3f9bdd..2cf838e93 100644 --- a/e107_handlers/e107_class.php +++ b/e107_handlers/e107_class.php @@ -1387,14 +1387,23 @@ class e107 } - /** - * Retrieve rater singleton object - * + /** + * Retrieve e_theme singleton object * @return e_theme */ - public static function getTheme() + public static function getTheme($themedir='front', $clearCache=false) { - return self::getSingleton('e_theme', true); + if($themedir === 'front') + { + $themedir= self::getPref('sitetheme'); + } + + if($themedir === 'admin') + { + $themedir = self::getPref('admintheme'); + } + + return self::getSingleton('e_theme', true, null, array('themedir'=> $themedir, 'force'=> $clearCache)); } diff --git a/e107_handlers/theme_handler.php b/e107_handlers/theme_handler.php index 5e38e7d37..81d67476f 100644 --- a/e107_handlers/theme_handler.php +++ b/e107_handlers/theme_handler.php @@ -40,17 +40,77 @@ class e_theme 'multimedia' ); + private $_data = array(); + + private $_current = null; + + private $_frontcss = null; + + private $_admincss = null; + + private $_legacy_themes = array(); + const CACHETIME = 120; // 2 hours const CACHETAG = "Meta_theme"; - function __construct() + function __construct($options=array()) { + if(!empty($options['themedir'])) + { + $this->_current = $options['themedir']; + } + + if(!defined('E107_INSTALL')) + { + $this->_frontcss = e107::getPref('themecss'); + $this->_admincss = e107::getPref('admincss'); + } + + if(empty($this->_data) || $options['force'] === true) + { + + $this->load($options['force']); + } + } + + /** + * Get info on the current front or admin theme and selected style. (ie. as found in theme.xml ) + * @param string $mode + * @param null $var file | name | scope | library + * @return bool + */ + public function cssAttribute($mode='front', $var=null) + { + $css = $this->get('css'); + + if(empty($css)) + { + return false; + } + + foreach($css as $k=>$v) + { + if($mode === 'front' && $v['name'] === $this->_frontcss) + { + return !empty($var) ? varset($v[$var],null) : $v; + } + + if($mode === 'admin' && $v['name'] === $this->_admincss) + { + return !empty($var) ? varset($v[$var],null) : $v; + } + + + } + + return false; + } @@ -91,11 +151,101 @@ class e_theme } + /** + * Load data for all themes in theme directory. + * @param bool|false $force + * @return $this + */ + private function load($force=false) + { + $themeArray = array(); + $tloop = 1; + + $cacheTag = self::CACHETAG; + + if($force === false && $tmp = e107::getCache()->retrieve($cacheTag, self::CACHETIME, true, true)) + { + $this->_data = e107::unserialize($tmp); + return $this; + } + + $array = scandir(e_THEME); + + foreach($array as $file) + { + if($file != "." && $file != ".." && $file != "CVS" && $file != "templates" && is_dir(e_THEME.$file) && is_readable(e_THEME.$file."/theme.php")) + { + + $themeArray[$file] = self::getThemeInfo($file); + $themeArray[$file]['id'] = $tloop; + + $tloop++; + } + } + + + $cacheSet = e107::serialize($themeArray,'json'); + + e107::getCache()->set($cacheTag,$cacheSet,true,true,true); + + $this->_data = $themeArray; + + + } + /** + * Return a var from the current theme. + * @param $var + * @param null $key + * @return array|bool + */ + public function get($var, $key=null) + { + return isset($this->_data[$this->_current][$var]) ? $this->_data[$this->_current][$var] : false; + } + + /** + * Return a list of all local themes in various formats. + * Replaces getThemeList + * @param null|string $mode null, 'version' | 'id' + * @return array|bool a list or false if no results + */ + public function getList($mode=null) + { + $arr = array(); + + if($mode === 'version') + { + foreach($this->_data as $dir=>$v) + { + $arr[$dir] = $v['version']; + } + + } + elseif($mode === 'id') + { + foreach($this->_data as $dir=>$v) + { + $arr[] = $dir; + } + } + else + { + $arr = $this->_data; + } + + + return !empty($arr) ? $arr : false; + + + } + /** * Get a list of all themes in theme folder and its data. + * @deprecated + * @see load(); * @param bool|false xml|false * @param bool|false $force force a refresh ie. ignore cached list. * @return array @@ -157,8 +307,12 @@ class e_theme } - - + /** + * Internal Use. Heavy CPU usage. + * Use e107::getTheme($themeDir,$force)->get() instead. + * @param $file + * @return mixed + */ public static function getThemeInfo($file) { $reject = array('e_.*'); @@ -233,7 +387,10 @@ class e_theme public static function parse_theme_php($path) { $CUSTOMPAGES = ""; - $tp = e107::getParser(); + + $tp = e107::getParser(); // could be used by a theme file. + $sql = e107::getDb(); // could be used by a theme file. + $fp = fopen(e_THEME.$path."/theme.php", "r"); $themeContents = fread($fp, filesize(e_THEME.$path."/theme.php")); fclose($fp); @@ -341,6 +498,7 @@ class e_theme } // echo "

".$themeArray['name']."

"; // print_a($lays); + $themeArray['legacy'] = true; return $themeArray; } @@ -453,6 +611,7 @@ class e_theme $vars['layouts'] = $lays; $vars['path'] = $path; $vars['custompages'] = $custom; + $vars['legacy'] = false; if(!empty($vars['stylesheets']['css'])) { @@ -462,7 +621,15 @@ class e_theme { // $notadmin = vartrue($val['@attributes']['admin']) ? false : true; $notadmin = (varset($val['@attributes']['scope']) !== 'admin') ? true : false; - $vars['css'][] = array("name" => $val['@attributes']['file'], "info"=> $val['@attributes']['name'], "nonadmin"=>$notadmin, 'scope'=> vartrue($val['@attributes']['scope'])); + + $vars['css'][] = array( + "name" => $val['@attributes']['file'], + "info" => $val['@attributes']['name'], + "nonadmin" => $notadmin, + 'scope' => vartrue($val['@attributes']['scope']), + 'library' => vartrue($val['@attributes']['library']) + + ); } unset($vars['stylesheets']); diff --git a/e107_themes/bootstrap3/theme.xml b/e107_themes/bootstrap3/theme.xml index 4cf75191e..e7a0d985c 100644 --- a/e107_themes/bootstrap3/theme.xml +++ b/e107_themes/bootstrap3/theme.xml @@ -18,11 +18,11 @@ preview_frontend.png - - - - - + + + + + diff --git a/e107_web/js/core/admin.jquery.js b/e107_web/js/core/admin.jquery.js index 2b786f65d..3c044f21d 100644 --- a/e107_web/js/core/admin.jquery.js +++ b/e107_web/js/core/admin.jquery.js @@ -538,6 +538,7 @@ $(document).ready(function() $(this).switchClass( "link", "link-active", 30 ); $(this).closest("li").addClass("active"); + $(id).removeClass('e-hideme').show({ effect: "slide" }); @@ -545,7 +546,8 @@ $(document).ready(function() if(hash) { window.location.hash = 'nav-' + hash; if(form) { - $(form).attr('action', $(form).attr('action').split('#')[0] + '#nav-' + hash); + + // $(form).attr('action', $(form).attr('action').split('#')[0] + '#nav-' + hash); // breaks menu-manager nav. } return false; }