Skip to content

Commit

Permalink
Restructure code
Browse files Browse the repository at this point in the history
- 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)
  • Loading branch information
joomlart committed Feb 19, 2013
1 parent 9c7101e commit f573ccc
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 136 deletions.
7 changes: 2 additions & 5 deletions source/plg_system_t3/includes/admin/megamenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion source/plg_system_t3/includes/core/action.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// No direct access
defined('_JEXEC') or die();
/**
* T3Less class compile less
* T3Action class
*
* @package T3
*/
Expand Down
121 changes: 119 additions & 2 deletions source/plg_system_t3/includes/core/t3.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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(){
Expand Down Expand Up @@ -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();
?>
7 changes: 6 additions & 1 deletion source/plg_system_t3/includes/menu/megamenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -266,4 +266,9 @@ function _ ($tmpl, $vars = array()) {
$this->menu .= "$tmpl\n";
}
}

function get ($prop) {
if (isset($this->$prop)) return $this->$prop;
return null;
}
}
143 changes: 16 additions & 127 deletions source/plg_system_t3/t3.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()){

Expand All @@ -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){
Expand All @@ -102,7 +71,7 @@ function onAfterRender ()
{
$japp = JFactory::getApplication();
if($japp->isAdmin()){
if($this->detect()){
if(T3::detect()){
$t3app = T3::getApp();
$t3app->render();
}
Expand Down Expand Up @@ -134,15 +103,15 @@ 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);
}
}

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;
Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit f573ccc

Please sign in to comment.