[+] Add abstract controller

This commit is contained in:
rMalie
2011-09-16 14:49:47 +00:00
parent 221af18406
commit 72e890b65c
3 changed files with 129 additions and 85 deletions
+9 -9
View File
@@ -1,6 +1,6 @@
<?php <?php
/* /*
* 2007-2011 PrestaShop * 2007-2011 PrestaShop
* *
* NOTICE OF LICENSE * NOTICE OF LICENSE
* *
@@ -25,7 +25,7 @@
* International Registered Trademark & Property of PrestaShop SA * International Registered Trademark & Property of PrestaShop SA
*/ */
require_once(dirname(__FILE__).'/../images.inc.php'); require_once(dirname(__FILE__).'/../images.inc.php');
function bindDatepicker($id, $time) function bindDatepicker($id, $time)
{ {
@@ -132,7 +132,7 @@ function getPath($urlBase, $id_category, $path = '', $highlight = '', $categoryT
{ {
$context = Context::getContext(); $context = Context::getContext();
if ($categoryType == 'catalog') if ($categoryType == 'catalog')
{ {
$category = Db::getInstance()->getRow(' $category = Db::getInstance()->getRow('
SELECT id_category, level_depth, nleft, nright SELECT id_category, level_depth, nleft, nright
FROM '._DB_PREFIX_.'category FROM '._DB_PREFIX_.'category
@@ -162,7 +162,7 @@ function getPath($urlBase, $id_category, $path = '', $highlight = '', $categoryT
($n < $nCategories ? '</a>' : ''). ($n < $nCategories ? '</a>' : '').
(($n++ != $nCategories OR !empty($path)) ? ' > ' : ''); (($n++ != $nCategories OR !empty($path)) ? ' > ' : '');
} }
return $fullPath.$path; return $fullPath.$path;
} }
} }
@@ -235,7 +235,7 @@ function recursiveTab($id_tab, $tabs)
/** /**
* Returns a new Tab object * Returns a new Tab object
* *
* @param string $tab class name * @param string $tab class name
* @return mixed(AdminTab, bool) tab object or false if failed * @return mixed(AdminTab, bool) tab object or false if failed
*/ */
@@ -244,7 +244,7 @@ function checkingTab($tab)
$tab = trim($tab); $tab = trim($tab);
if (!Validate::isTabName($tab)) if (!Validate::isTabName($tab))
return false; return false;
$row = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('SELECT id_tab, module FROM `'._DB_PREFIX_.'tab` WHERE class_name = \''.pSQL($tab).'\''); $row = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow('SELECT id_tab, module FROM `'._DB_PREFIX_.'tab` WHERE class_name = \''.pSQL($tab).'\'');
if (!$row['id_tab']) if (!$row['id_tab'])
{ {
@@ -276,7 +276,7 @@ function checkingTab($tab)
function checkTabRights($id_tab) function checkTabRights($id_tab)
{ {
static $tabAccesses = NULL; static $tabAccesses = NULL;
if ($tabAccesses === NULL) if ($tabAccesses === NULL)
$tabAccesses = Profile::getProfileAccesses(Context::getContext()->employee->id_profile); $tabAccesses = Profile::getProfileAccesses(Context::getContext()->employee->id_profile);
@@ -391,14 +391,14 @@ function simpleXMLToArray ($xml, $flattenValues = true, $flattenAttributes = tru
/** /**
* Generate a sweet HTML list for shop selection * Generate a sweet HTML list for shop selection
* *
* @todo move in adminTab * @todo move in adminTab
* @return string * @return string
*/ */
function generateShopList() function generateShopList()
{ {
$tree = Shop::getTree(); $tree = Shop::getTree();
// Get default value // Get default value
list($shopID, $shopGroupID) = Shop::getContext(); list($shopID, $shopGroupID) = Shop::getContext();
if ($shopID) if ($shopID)
+97 -3
View File
@@ -27,8 +27,102 @@
abstract class ControllerCore abstract class ControllerCore
{ {
public static function getController($className, $auth = false, $ssl = false) /**
* @var Context
*/
protected $context;
/**
* @var array list of css files
*/
public $css_files = array();
/**
* @var array list of javascript files
*/
public $js_files = array();
/**
* Set default media list for controller
*/
abstract public function setMedia();
/**
* Get an instance of a controller
*
* @param string $class_name
* @param bool $auth
* @param bool $ssl
*/
public static function getController($class_name, $auth = false, $ssl = false)
{ {
return new $className($auth, $ssl); return new $class_name($auth, $ssl);
} }
}
public function __construct()
{
$this->context = Context::getContext();
}
/**
* Add a new stylesheet in page header.
*
* @param mixed $css_uri Path to css file, or list of css files like this : array(array(uri => media_type), ...)
* @param string $css_media_type
* @return true
*/
public function addCSS($css_uri, $css_media_type = 'all')
{
if (is_array($css_uri))
{
foreach ($css_uri as $file => $media_type)
self::addCSS($file, $media_type);
return true;
}
// remove PS_BASE_URI on _PS_ROOT_DIR_ for the following
$url_data = parse_url($css_uri);
$file_uri = _PS_ROOT_DIR_.Tools::str_replace_once(__PS_BASE_URI__, DIRECTORY_SEPARATOR, $url_data['path']);
// check if css files exists
if (!file_exists($file_uri))
return true;
// adding file to the big array...
$this->css_files[$css_uri] = $css_media_type;
return true;
}
/**
* Add a new javascript file in page header.
*
* @param mixed $js_uri
* @return void
*/
public function addJS($js_uri)
{
if (is_array($js_uri))
{
foreach ($js_uri as $file)
self::addJS($file);
return true;
}
if (in_array($js_uri, $this->js_files))
return true;
// remove PS_BASE_URI on _PS_ROOT_DIR_ for the following
$url_data = parse_url($js_uri);
$file_uri = _PS_ROOT_DIR_.Tools::str_replace_once(__PS_BASE_URI__, DIRECTORY_SEPARATOR, $url_data['path']);
// check if js files exists
if (!preg_match('/^http(s?):\/\//i', $file_uri) && !file_exists($file_uri))
return true;
// adding file to the big array...
$this->js_files[] = $js_uri;
return true;
}
}
+23 -73
View File
@@ -28,10 +28,6 @@
class FrontControllerCore extends ControllerCore class FrontControllerCore extends ControllerCore
{ {
public $errors = array(); public $errors = array();
/**
* @var Context
*/
protected $context;
/* Deprecated shortcuts as of 1.5 - Use $context->var instead */ /* Deprecated shortcuts as of 1.5 - Use $context->var instead */
protected static $smarty; protected static $smarty;
@@ -67,6 +63,8 @@ class FrontControllerCore extends ControllerCore
public function __construct() public function __construct()
{ {
global $useSSL; global $useSSL;
parent::__construct();
$useSSL = $this->ssl; $useSSL = $this->ssl;
} }
@@ -93,8 +91,6 @@ class FrontControllerCore extends ControllerCore
return; return;
self::$initialized = true; self::$initialized = true;
$this->context = Context::getContext();
$this->id_current_shop = Context::getContext()->shop->getID(); $this->id_current_shop = Context::getContext()->shop->getID();
$this->id_current_group_shop = Context::getContext()->shop->getGroupID(); $this->id_current_group_shop = Context::getContext()->shop->getGroupID();
@@ -658,80 +654,47 @@ class FrontControllerCore extends ControllerCore
$allowed = false; $allowed = false;
$userIp = Tools::getRemoteAddr(); $userIp = Tools::getRemoteAddr();
$ips = explode(';', Configuration::get('PS_GEOLOCATION_WHITELIST')); $ips = explode(';', Configuration::get('PS_GEOLOCATION_WHITELIST'));
if (is_array($ips) AND sizeof($ips)) if (is_array($ips) && sizeof($ips))
foreach ($ips AS $ip) foreach ($ips as $ip)
if (!empty($ip) AND strpos($userIp, $ip) === 0) if (!empty($ip) && strpos($userIp, $ip) === 0)
$allowed = true; $allowed = true;
return $allowed; return $allowed;
} }
/** /**
* addCSS allows you to add stylesheet at any time. * Add one or several CSS for front, checking if css files are overriden in theme/css/modules/ directory
* *
* @param mixed $css_uri * @see Controller::addCSS()
* @param string $css_media_type
* @return true
*/ */
public function addCSS($css_uri, $css_media_type = 'all') public function addCSS($css_uri, $css_media_type = 'all')
{ {
if (is_array($css_uri)) if (!is_array($css_uri))
$css_uri = array($css_uri => $css_media_type);
$list_uri = array();
foreach ($css_uri as $file => $media)
{ {
foreach ($css_uri as $file => $media_type) $different = 0;
$this->addCSS($file, $media_type); $override_path = str_replace(__PS_BASE_URI__.'modules/', _PS_ROOT_DIR_.'/themes/'._THEME_NAME_.'/css/modules/', $file, $different);
return true; if ($different && file_exists($override_path))
$file = str_replace(__PS_BASE_URI__.'modules/', __PS_BASE_URI__.'themes/'._THEME_NAME_.'/css/modules/', $file, $different);
$list_uri[$file] = $media;
} }
//overriding of modules css files return parent::addCSS($list_uri, $css_media_type);
$different = 0;
$override_path = str_replace(__PS_BASE_URI__.'modules/', _PS_ROOT_DIR_.'/themes/'._THEME_NAME_.'/css/modules/', $css_uri, $different);
if ($different && file_exists($override_path))
$css_uri = str_replace(__PS_BASE_URI__.'modules/', __PS_BASE_URI__.'themes/'._THEME_NAME_.'/css/modules/', $css_uri, $different);
else
{
// remove PS_BASE_URI on _PS_ROOT_DIR_ for the following
$url_data = parse_url($css_uri);
$file_uri = _PS_ROOT_DIR_.Tools::str_replace_once(__PS_BASE_URI__, DIRECTORY_SEPARATOR, $url_data['path']);
// check if css files exists
if (!file_exists($file_uri))
return true;
}
// detect mass add
$css_uri = array($css_uri => $css_media_type);
// adding file to the big array...
if (is_array($this->css_files))
$this->css_files = array_merge($this->css_files, $css_uri);
else
$this->css_files = $css_uri;
return true;
} }
/** /**
* addJS load a javascript file in the header * Add one or several JS files for front, checking if js files are overriden in theme/js/modules/ directory
* *
* @param mixed $js_uri * @see Controller::addJS()
* @return void
*/ */
public function addJS($js_uri) public function addJS($js_uri)
{ {
if (!isset($this->js_files)) if (!is_array($js_uri))
$this->js_files = array();
// avoid useless operation...
if (in_array($js_uri, $this->js_files))
return true;
// detect mass add
if (!is_array($js_uri) && !in_array($js_uri, $this->js_files))
$js_uri = array($js_uri); $js_uri = array($js_uri);
else
foreach($js_uri as $key => $js)
if (in_array($js, $this->js_files))
unset($js_uri[$key]);
//overriding of modules js files foreach ($js_uri as $key => &$file)
foreach ($js_uri AS $key => &$file)
{ {
if (!preg_match('/^http(s?):\/\//i', $file)) if (!preg_match('/^http(s?):\/\//i', $file))
{ {
@@ -739,23 +702,10 @@ class FrontControllerCore extends ControllerCore
$override_path = str_replace(__PS_BASE_URI__.'modules/', _PS_ROOT_DIR_.'/themes/'._THEME_NAME_.'/js/modules/', $file, $different); $override_path = str_replace(__PS_BASE_URI__.'modules/', _PS_ROOT_DIR_.'/themes/'._THEME_NAME_.'/js/modules/', $file, $different);
if ($different && file_exists($override_path)) if ($different && file_exists($override_path))
$file = str_replace(__PS_BASE_URI__.'modules/', __PS_BASE_URI__.'themes/'._THEME_NAME_.'/js/modules/', $file, $different); $file = str_replace(__PS_BASE_URI__.'modules/', __PS_BASE_URI__.'themes/'._THEME_NAME_.'/js/modules/', $file, $different);
else
{
// remove PS_BASE_URI on _PS_ROOT_DIR_ for the following
$url_data = parse_url($file);
$file_uri = _PS_ROOT_DIR_.Tools::str_replace_once(__PS_BASE_URI__, DIRECTORY_SEPARATOR, $url_data['path']);
// check if js files exists
if (!file_exists($file_uri))
unset($js_uri[$key]);
}
} }
} }
// adding file to the big array... return parent::addJS($js_uri);
$this->js_files = array_merge($this->js_files, $js_uri);
return true;
} }
} }