From f573ccc1d6bfe4c8c75e090dd035d14c6538651c Mon Sep 17 00:00:00 2001 From: Khanh Le Date: Tue, 19 Feb 2013 10:53:01 +0700 Subject: [PATCH] Restructure code - Move function detect from T3 plugins file into include/core/t3.php - Add includes/core/bot.php with 3 functions: preload (before checking T3), beforeInit (before init T3) and afterInit (after init T3) --- .../plg_system_t3/includes/admin/megamenu.php | 7 +- source/plg_system_t3/includes/core/action.php | 2 +- source/plg_system_t3/includes/core/t3.php | 121 ++++++++++++++- .../plg_system_t3/includes/menu/megamenu.php | 7 +- source/plg_system_t3/t3.php | 143 ++---------------- 5 files changed, 144 insertions(+), 136 deletions(-) diff --git a/source/plg_system_t3/includes/admin/megamenu.php b/source/plg_system_t3/includes/admin/megamenu.php index 31c131702f..9c4feab85e 100644 --- a/source/plg_system_t3/includes/admin/megamenu.php +++ b/source/plg_system_t3/includes/admin/megamenu.php @@ -17,11 +17,8 @@ public static function display () { t3import('menu/megamenu'); $input = JFactory::getApplication()->input; $menutype = $input->get ('t3menu', 'mainmenu'); - //$file = T3_TEMPLATE_PATH.'/etc/megamenu.ini'; - //$currentconfig = json_decode(@file_get_contents ($file), true); - $tpl = JFactory::getApplication()->getTemplate(true); - $currentconfig = json_decode($tpl->params->get('mm_config', ''), true); - + $tplparams = $input->get('tplparams', '', 'raw'); + $currentconfig = $tplparams instanceof JRegistry ? json_decode($tplparams->get('mm_config', ''), true) : null; $mmconfig = ($currentconfig && isset($currentconfig[$menutype])) ? $currentconfig[$menutype] : array(); $mmconfig['editmode'] = true; $menu = new T3MenuMegamenu ($menutype, $mmconfig); diff --git a/source/plg_system_t3/includes/core/action.php b/source/plg_system_t3/includes/core/action.php index 6151d0d2dd..a1f932e715 100644 --- a/source/plg_system_t3/includes/core/action.php +++ b/source/plg_system_t3/includes/core/action.php @@ -15,7 +15,7 @@ // No direct access defined('_JEXEC') or die(); /** - * T3Less class compile less + * T3Action class * * @package T3 */ diff --git a/source/plg_system_t3/includes/core/t3.php b/source/plg_system_t3/includes/core/t3.php index 87bf3d3694..04a90d773c 100644 --- a/source/plg_system_t3/includes/core/t3.php +++ b/source/plg_system_t3/includes/core/t3.php @@ -50,7 +50,33 @@ public static function getApp($tpl = null){ /** * Initialize T3 */ - public static function init () { + public static function init ($template) { + define ('T3_TEMPLATE', $template); + define ('T3_TEMPLATE_URL', JURI::root(true).'/templates/'.T3_TEMPLATE); + define ('T3_TEMPLATE_PATH', JPATH_ROOT . '/templates/' . T3_TEMPLATE); + define ('T3_TEMPLATE_REL', 'templates/' . T3_TEMPLATE); + + //load T3 Framework language + JFactory::getLanguage()->load(T3_PLUGIN, JPATH_ADMINISTRATOR); + + $app = JFactory::getApplication(); + $input = $app->input; + $templateobj = $app->getTemplate(true); + + if ($input->getCmd('themer', 0)){ + define ('T3_THEMER', 1); + } + + if (!JFactory::getApplication()->isAdmin()) { + $t3assets = $templateobj->params->get ('t3-assets', 't3-assets'); + define ('T3_DEV_FOLDER', $t3assets . '/dev'); + } + + if($input->getCmd('t3lock', '')){ + JFactory::getSession()->set('T3.t3lock', $input->getCmd('t3lock', '')); + $input->set('t3lock', null); + } + // load core library t3import ('core/path'); @@ -76,6 +102,21 @@ public static function init () { } } else { } + + // capture for tm=1 => show theme magic + if ($input->getCmd('tm') == 1) { + $input->set('t3action', 'theme'); + $input->set('t3task', 'thememagic'); + } + + } + + public static function checkAction () { + // excute action by T3 + if ($action = JFactory::getApplication()->input->getCmd ('t3action')) { + t3import ('core/action'); + T3Action::run ($action); + } } public static function getAdmin(){ @@ -107,7 +148,83 @@ public static function error($msg, $code = 500){ throw new Exception($msg, $code); } } + + public static function detect() + { + static $t3; + + if (!isset($t3)) { + $t3 = false; // set false + $app = JFactory::getApplication(); + $input = JFactory::getApplication()->input; + // get template name + $tplname = ''; + if($input->getCmd ('t3action') && ($styleid = $input->getInt('styleid', ''))) { + $db = JFactory::getDbo(); + $query = $db->getQuery(true); + $query->select('template, params'); + $query->from('#__template_styles'); + $query->where('client_id = 0'); + $query->where('id = '.$styleid); + + $db->setQuery($query); + $template = $db->loadObject(); + if ($template) { + $tplname = $template->template; + $registry = new JRegistry; + $registry->loadString($template->params); + $input->set ('tplparams', $registry); + } + } elseif ($app->isAdmin()) { + // if not login, do nothing + $user = JFactory::getUser(); + if (!$user->id){ + return false; + } + + if($input->getCmd('option') == 'com_templates' && + (preg_match('/style\./', $input->getCmd('task')) || $input->getCmd('view') == 'style' || $input->getCmd('view') == 'template') + ){ + $db = JFactory::getDBO(); + $query = $db->getQuery(true); + $id = $input->getInt('id'); + + //when in POST the view parameter does not set + if ($input->getCmd('view') == 'template') { + $query + ->select('element') + ->from('#__extensions') + ->where('extension_id='.(int)$id . ' AND type=' . $db->quote('template')); + } else { + $query + ->select('template') + ->from('#__template_styles') + ->where('id='.(int)$id); + } + + $db->setQuery($query); + $tplname = $db->loadResult(); + } + + } else { + $tplname = $app->getTemplate(false); + } + + if ($tplname) { + // parse xml + $filePath = JPath::clean(JPATH_ROOT.'/templates/'.$tplname.'/templateDetails.xml'); + if (is_file ($filePath)) { + $xml = JInstaller::parseXMLInstallFile($filePath); + if (strtolower($xml['group']) == 't3') { + $t3 = $tplname; + } + } + } + + } + return $t3; + } + } -T3::init(); ?> \ No newline at end of file diff --git a/source/plg_system_t3/includes/menu/megamenu.php b/source/plg_system_t3/includes/menu/megamenu.php index 47d7865019..32c53128dd 100644 --- a/source/plg_system_t3/includes/menu/megamenu.php +++ b/source/plg_system_t3/includes/menu/megamenu.php @@ -171,7 +171,7 @@ function nav ($pitem, $start = 0, $end = 0) { } } if (!count($items)) return; - } else if ($start == 0){ + } else if ($start === 0){ $pid = $pitem->id; if (!isset($this->children[$pid])) return ; $items = $this->children[$pid]; @@ -266,4 +266,9 @@ function _ ($tmpl, $vars = array()) { $this->menu .= "$tmpl\n"; } } + + function get ($prop) { + if (isset($this->$prop)) return $this->$prop; + return null; + } } \ No newline at end of file diff --git a/source/plg_system_t3/t3.php b/source/plg_system_t3/t3.php index 437213c1df..3c03cd5e3a 100644 --- a/source/plg_system_t3/t3.php +++ b/source/plg_system_t3/t3.php @@ -27,51 +27,20 @@ class plgSystemT3 extends JPlugin //function onAfterInitialise(){ function onAfterRoute(){ include_once dirname(__FILE__) . '/includes/core/defines.php'; - $template = $this->detect(); - - if($template){ - define ('T3_TEMPLATE', $template); - define ('T3_TEMPLATE_URL', JURI::root(true).'/templates/'.T3_TEMPLATE); - define ('T3_TEMPLATE_PATH', JPATH_ROOT . '/templates/' . T3_TEMPLATE); - define ('T3_TEMPLATE_REL', 'templates/' . T3_TEMPLATE); - - //load T3 Framework language - JFactory::getLanguage()->load(T3_PLUGIN, JPATH_ADMINISTRATOR); - - $input = JFactory::getApplication()->input; - - if ($input->getCmd('themer', 0)){ - define ('T3_THEMER', 1); - } - - if (!JFactory::getApplication()->isAdmin()) { - $t3assets = JFactory::getApplication()->getTemplate(true)->params->get ('t3-assets', 't3-assets'); - define ('T3_DEV_FOLDER', $t3assets . '/dev'); - } - - if($input->getCmd('t3lock', '')){ - JFactory::getSession()->set('T3.t3lock', $input->getCmd('t3lock', '')); - $input->set('t3lock', null); - } - - include_once dirname(__FILE__) . '/includes/core/t3.php'; - - // capture for tm=1 => show theme magic - if ($input->getCmd('tm') == 1) { - $input->set('t3action', 'theme'); - $input->set('t3task', 'thememagic'); - } - - // excute action by T3 - if ($action = $input->getCmd ('t3action')) { - t3import ('core/action'); - T3Action::run ($action); - } + include_once dirname(__FILE__) . '/includes/core/t3.php'; + include_once dirname(__FILE__) . '/includes/core/bot.php'; + T3Bot::preload(); + $template = T3::detect(); + if($template){ + T3Bot::beforeInit(); + T3::init($template); + T3Bot::afterInit(); + T3::checkAction(); } } function onBeforeRender(){ - if($this->detect()){ + if(T3::detect()){ $japp = JFactory::getApplication(); if($japp->isAdmin()){ @@ -89,7 +58,7 @@ function onBeforeRender(){ function onBeforeCompileHead () { $app = JFactory::getApplication(); - if($this->detect() && !$app->isAdmin()){ + if(T3::detect() && !$app->isAdmin()){ // call update head for replace css to less if in devmode $t3app = T3::getApp(); if($t3app){ @@ -102,7 +71,7 @@ function onAfterRender () { $japp = JFactory::getApplication(); if($japp->isAdmin()){ - if($this->detect()){ + if(T3::detect()){ $t3app = T3::getApp(); $t3app->render(); } @@ -134,7 +103,7 @@ function onContentPrepareForm($form, $data) } else */ - if($this->detect() && $form->getName() == 'com_templates.style'){ + if(T3::detect() && $form->getName() == 'com_templates.style'){ $this->loadLanguage(); JForm::addFormPath(T3_PATH . DIRECTORY_SEPARATOR . 'params'); $form->loadFile('template', false); @@ -142,7 +111,7 @@ function onContentPrepareForm($form, $data) } function onExtensionAfterSave($option, $data){ - if($this->detect() && $option == 'com_templates.style' && !empty($data->id)){ + if(T3::detect() && $option == 'com_templates.style' && !empty($data->id)){ //get new params value $japp = JFactory::getApplication(); $params = new JRegistry; @@ -194,86 +163,6 @@ function onExtensionAfterSave($option, $data){ } } - function detect() - { - static $t3; - - if (!isset($t3)) { - $t3 = false; // set false - $app = JFactory::getApplication(); - $input = JFactory::getApplication()->input; - // get template name - $tplname = ''; - if ($app->isAdmin()) { - // if not login, do nothing - $user = JFactory::getUser(); - if (!$user->id){ - return false; - } - - if($input->getCmd('option') == 'com_templates' && - (preg_match('/style\./', $input->getCmd('task')) || $input->getCmd('view') == 'style' || $input->getCmd('view') == 'template') - ){ - $db = JFactory::getDBO(); - $query = $db->getQuery(true); - $id = $input->getInt('id'); - - //when in POST the view parameter does not set - if ($input->getCmd('view') == 'template') { - $query - ->select('element') - ->from('#__extensions') - ->where('extension_id='.(int)$id . ' AND type=' . $db->quote('template')); - } else { - $query - ->select('template') - ->from('#__template_styles') - ->where('id='.(int)$id); - } - - $db->setQuery($query); - $tplname = $db->loadResult(); - } - - } else { - if($input->getCmd ('t3action') && ($styleid = $input->getInt('styleid', ''))){ - $db = JFactory::getDbo(); - $query = $db->getQuery(true); - $query->select('template, params'); - $query->from('#__template_styles'); - $query->where('client_id = 0'); - $query->where('id = '.$styleid); - - $db->setQuery($query); - $template = $db->loadObject(); - if ($template) { - $registry = new JRegistry; - $registry->loadString($template->params); - $tplname = $template->template; - - // override template - $app->setTemplate ($tplname, $registry); - } - } else { - $tplname = $app->getTemplate(false); - } - } - - if ($tplname) { - // parse xml - $filePath = JPath::clean(JPATH_ROOT.'/templates/'.$tplname.'/templateDetails.xml'); - if (is_file ($filePath)) { - $xml = JInstaller::parseXMLInstallFile($filePath); - if (strtolower($xml['group']) == 't3') { - $t3 = $tplname; - } - } - } - - } - return $t3; - } - /** * Implement event onRenderModule to include the module chrome provide by T3 * This event is fired by overriding ModuleHelper class @@ -288,7 +177,7 @@ function onRenderModule (&$module, $attribs) { static $chromed = false; // Detect layout path in T3 themes - if ($this->detect()) { + if (T3::detect()) { // Chrome for module if (!$chromed) { $chromed = true; @@ -316,7 +205,7 @@ function onRenderModule (&$module, $attribs) function onGetLayoutPath($module, $layout) { // Detect layout path in T3 themes - if ($this->detect()) { + if (T3::detect()) { $tPath = T3Path::getPath('html/' . $module . '/' . $layout . '.php'); if ($tPath) return $tPath;