[+] BO : New AdminController system, including smarty backoffice, thanks to Raphael and Thomas

This commit is contained in:
mMarinetti
2011-09-23 15:01:49 +00:00
parent 230ed9d5ee
commit 6191e868f1
13 changed files with 833 additions and 220 deletions
+63 -19
View File
@@ -1,13 +1,16 @@
<?php
class AdminController extends Controller
class AdminControllerCore extends Controller
{
public $path;
public $content;
public $warnings;
public $content_only = false;
public $layout = 'index.tpl';
public $template = 'content.tpl';
/** @var string Associated table name */
public $table;
@@ -54,10 +57,17 @@ class AdminController extends Controller
if (!$this->identifier) $this->identifier = 'id_'.$this->table;
if (!$this->_defaultOrderBy) $this->_defaultOrderBy = $this->identifier;
$className = get_class($this);
if ($className == 'AdminCategories' OR $className == 'AdminProducts')
$className = 'AdminCatalog';
$this->token = Tools::getAdminToken($className.(int)$this->id.(int)$this->context->employee->id);
// temporary fix for Token retrocompatibility
// This has to be done when url is built instead of here)
if(strpos($className,'Controller'))
$className = substr($className,0,-10);
$this->token = Tools::getAdminToken($className.(int)$this->id.(int)$this->context->employee->id);
if (!Shop::isMultiShopActivated())
$this->shopLinkType = '';
}
@@ -87,17 +97,14 @@ class AdminController extends Controller
return (!empty($token) AND $token === $this->token);
}
public function run()
public function action()
{
$this->checkAccess();
$this->init();
$this->postProcess();
$this->setMedia();
$this->initHeader();
$this->initFooter();
//$adminObj->displayConf();
//$adminObj->displayErrors();
$this->display();
}
/**
@@ -112,23 +119,57 @@ class AdminController extends Controller
$url = preg_replace('/([&?]token=)[^&]*(&.*)?$/', '${1}'.$this->token.'$2', $_SERVER['REQUEST_URI']);
if (false === strpos($url, '?token=') AND false === strpos($url, '&token='))
$url .= '&token='.$this->token;
if(strpos($url,'?') === false)
$url = str_replace('&token', '?controller=AdminHome&token', $url);
$this->context->smarty->assign('url', htmlentities($url));
$this->context->smarty->display(_PS_ADMIN_DIR_.'/themes/invalid_token.tpl');
$this->context->smarty->display('invalid_token.tpl');
die;
}
}
public function displayNoSmarty()
{
}
public function display()
{
if(!empty($this->content))
$this->assign('content', $this->content);
if (empty($this->template) or $this->template == 'content.tpl')
{
$default_tpl = substr(lcfirst(get_class($this)),0,-10).'.tpl';
if (file_exists($this->context->smarty->template_dir.'/'.$default_tpl))
{
$this->template = $default_tpl;
}
else
return $this->displayNoSmarty();
}
else
$this->content = $this->context->smarty->fetch($this->template);
if ($this->content_only)
$this->context->smarty->display($this->template);
echo $this->content;
else
{
$this->context->smarty->assign('warnings',$this->warnings);
$this->content = $this->context->smarty->fetch($this->template);
$this->context->smarty->assign('content', $this->content);
$this->context->smarty->display($this->layout);
}
$this->context->smarty->assign('content',$this->content);
$this->context->smarty->display($this->layout);
}
/**
* add a warning message to display at the top of the page
*
* @param string $msg
*/
protected function displayWarning($msg)
{
$this->warnings[] = $msg;
}
/**
@@ -221,14 +262,14 @@ class AdminController extends Controller
}
// Breadcrumbs
$home_token = Tools::getAdminToken('AdminHome'.intval(Tab::getIdFromClassName('AdminHome')).(int)$this->context->employee->id);
$tabs_breadcrumb = array();
$tabs_breadcrumb = recursiveTab($this->id, $tabs_breadcrumb);
$tabs_breadcrumb = Tab::recursiveTab($this->id, $tabs_breadcrumb);
$tabs_breadcrumb = array_reverse($tabs_breadcrumb);
foreach ($tabs_breadcrumb AS $key => $item)
for ($i = 0; $i < (count($tabs_breadcrumb) - 1); $i++)
$tabs_breadcrumb[$key]['token'] = Tools::getAdminToken($item['class_name'].intval($item['id_tab']).(int)$this->context->employee->id);
for ($i = 0; $i < (count($tabs_breadcrumb) - 1); $i++)
$tabs_breadcrumb[$key]['token'] = Tools::getAdminToken($item['class_name'].intval($item['id_tab']).(int)$this->context->employee->id);
/* Hooks are volontary out the initialize array (need those variables already assigned) */
$this->context->smarty->assign(array(
@@ -309,8 +350,7 @@ class AdminController extends Controller
$this->addCSS(_PS_JS_DIR_.'jquery/datepicker/datepicker.css', 'all');
$this->addCSS(_PS_CSS_DIR_.'admin.css', 'all');
$this->addCSS(_PS_CSS_DIR_.'jquery.cluetip.css', 'all');
$this->addCSS($this->path.'/themes/'.'default/admin.css', 'all');
$this->addCSS(__PS_BASE_URI__.'admin-dev/themes/default/admin.css', 'all');
if ($this->context->language->is_rtl)
$this->addCSS(_THEME_CSS_DIR_.'rtl.css');
@@ -335,7 +375,10 @@ class AdminController extends Controller
return Module::findTranslation(Module::$classInModule[$class], $string, $class);
}
global $_LANGADM;
$_LANGADM = array_change_key_case($_LANGADM);
if(is_array($_LANGADM))
$_LANGADM = array_change_key_case($_LANGADM);
else
$_LANGADM = array();
//if ($class == __CLASS__)
// $class = 'AdminTab';
@@ -363,7 +406,8 @@ class AdminController extends Controller
public function init()
{
ob_start();
// ob_start();
$this->checkAccess();
$this->timerStart = microtime(true);
if (isset($_GET['logout']))
@@ -462,4 +506,4 @@ class AdminController extends Controller
}
}
}
+1 -1
View File
@@ -197,7 +197,7 @@ abstract class AdminTabCore
{
$this->context = Context::getContext();
$this->id = Tab::getCurrentTabId();
$this->id = Tab::getIdFromClassName(get_class($this));
$this->_conf = array(
1 => $this->l('Deletion successful'), 2 => $this->l('Selection successfully deleted'),
3 => $this->l('Creation successful'), 4 => $this->l('Update successful'),
+9 -8
View File
@@ -47,16 +47,13 @@ class ChartCore
if (!self::$poolId)
{
++self::$poolId;
echo '
<!--[if IE]><script type="text/javascript" src="'.PS_BASE_URI.'js/jquery/excanvas.min.js"></script><![endif]-->
<script type="text/javascript" src="'.PS_BASE_URI.'js/jquery/jquery.flot.min.js"></script>';
return true;
}
}
/** @prototype void public function __construct() */
public function __construct()
{
self::init();
++self::$poolId;
}
@@ -101,7 +98,11 @@ class ChartCore
/** @prototype void public function display() */
public function display()
{
$options = '';
echo $this->fetch();
}
public function fetch()
{
if ($this->timeMode)
{
$options = 'xaxis:{mode:"time",timeformat:\''.addslashes($this->format).'\',min:'.$this->from.'000,max:'.$this->to.'000}';
@@ -117,7 +118,7 @@ class ChartCore
$jsCurves[] = $curve->getValues($this->timeMode);
if (count($jsCurves))
echo '
return '
<div id="flot'.self::$poolId.'" style="width:'.$this->width.'px;height:'.$this->height.'px"></div>
<script type="text/javascript">
$(function () {
@@ -125,7 +126,7 @@ class ChartCore
});
</script>';
else
echo ErrorFacade::Display(PS_ERROR_UNDEFINED, 'No values for this chart.');
return ErrorFacade::Display(PS_ERROR_UNDEFINED, 'No values for this chart.');
}
}
@@ -175,4 +176,4 @@ class Curve
if (array_key_exists((string)$x, $this->values))
return $this->values[(string)$x];
}
}
}
+45 -18
View File
@@ -35,6 +35,10 @@ class DispatcherCore
*/
public static $instance = null;
/*
* @var array
*/
public static $controllers = array();
/**
* List of default routes
*
@@ -196,12 +200,34 @@ class DispatcherCore
}
// Get and instantiate controller
$this->getController();
$controllers = Dispatcher::getControllers(_PS_CONTROLLER_DIR_);
if(defined('_PS_ADMIN_DIR_'))
Dispatcher::getControllers(_PS_ADMIN_DIR_.'/tabs/','');
// getControllers load all controller files from the specified dir
Dispatcher::getControllers(_PS_CONTROLLER_DIR_);
if (!$this->controller)
$this->controller = 'index';
if (!isset($controllers[$this->controller]))
$this->controller = 'pagenotfound';
Controller::getController($controllers[$this->controller])->run();
{
if (defined('_PS_ADMIN_DIR_'))
$this->controller = 'adminhome';
else
$this->controller = 'index';
}
// temporary code for AdminTab / AdminController
if (defined('_PS_ADMIN_DIR_') && $this->controller != 'adminhome')
{
if (file_exists(_PS_ADMIN_DIR_.'/tabs/'.self::$controllers[$this->controller].'.php'))
{
// Tools::displayAsDeprecated('AdminTab is deprecated'); // To put in AdminTab::__construct()
require_once(_PS_ADMIN_DIR_.'/functions.php');
runAdminTab();
die('');
}
}
else if (!isset(self::$controllers[$this->controller]))
$this->controller = 'pagenotfound';
Controller::getController(self::$controllers[$this->controller])->run();
}
/**
@@ -427,33 +453,34 @@ class DispatcherCore
}
/**
* Get list of available controllers
*
* Get list of available controllers from the specified dir
* @param string dir directory to scan (recursively)
* @param filename suffix (without .php extension). Others files will be ignored.
* @return array
*/
public static function getControllers($dir)
public static function getControllers($dir, $suffix = 'Controller')
{
static $controllers;
$controller_files = scandir($dir);
foreach ($controller_files as $controller_filename)
{
if (!in_array($controller_filename, array('.','..','.svn')) AND is_dir($dir.$controller_filename))
$controllers = self::getControllers($dir.$controller_filename.DIRECTORY_SEPARATOR);
else if (substr($controller_filename, -14, 14) == 'Controller.php')
{
self::$controllers = self::getControllers($dir.$controller_filename.DIRECTORY_SEPARATOR);
}
else if (substr($controller_filename, - (strlen($suffix)+4), - 4) == $suffix)
{
$subdir = str_replace(_PS_CONTROLLER_DIR_,'',$dir);
$controllers[strtolower(substr($controller_filename, 0, -14))] = basename($controller_filename, '.php');
self::$controllers[strtolower(substr($controller_filename, 0, - (strlen($suffix)+ 4)))] = basename($controller_filename, '.php');
}
}
// add default controller
$controllers['index'] = 'IndexController';
if (isset($controllers['auth']))
$controllers['authentication'] = $controllers['auth'];
self::$controllers['index'] = 'IndexController';
if (isset(self::$controllers['auth']))
self::$controllers['authentication'] = self::$controllers['auth'];
if (isset($controllers['compare']))
$controllers['productscomparison'] = $controllers['compare'];
self::$controllers['productscomparison'] = self::$controllers['compare'];
return $controllers;
return self::$controllers;
}
}
+11
View File
@@ -252,4 +252,15 @@ class TabCore extends ObjectModel
return ($tabAccesses[(int)($id_tab)]['view'] === '1');
return false;
}
public static function recursiveTab($id_tab, $tabs)
{
$adminTab = Tab::getTab((int)Context::getContext()->language->id, $id_tab);
$tabs[]= $adminTab;
if ($adminTab['id_parent'] > 0)
{
$tabs[] = Tab::recursiveTab($adminTab['id_parent'], $tabs);
}
return $tabs;
}
}