[+] Add abstract controller
This commit is contained in:
+96
-2
@@ -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
@@ -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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user