From 72e890b65c8be3eba6cd927775b4441e1d9b2ac1 Mon Sep 17 00:00:00 2001 From: rMalie Date: Fri, 16 Sep 2011 14:49:47 +0000 Subject: [PATCH] [+] Add abstract controller --- admin-dev/functions.php | 18 +++---- classes/Controller.php | 100 ++++++++++++++++++++++++++++++++++-- classes/FrontController.php | 96 +++++++++------------------------- 3 files changed, 129 insertions(+), 85 deletions(-) diff --git a/admin-dev/functions.php b/admin-dev/functions.php index a5bf74437..d7c618206 100644 --- a/admin-dev/functions.php +++ b/admin-dev/functions.php @@ -1,6 +1,6 @@ getRow(' SELECT id_category, level_depth, nleft, nright FROM '._DB_PREFIX_.'category @@ -162,7 +162,7 @@ function getPath($urlBase, $id_category, $path = '', $highlight = '', $categoryT ($n < $nCategories ? '' : ''). (($n++ != $nCategories OR !empty($path)) ? ' > ' : ''); } - + return $fullPath.$path; } } @@ -235,7 +235,7 @@ function recursiveTab($id_tab, $tabs) /** * Returns a new Tab object - * + * * @param string $tab class name * @return mixed(AdminTab, bool) tab object or false if failed */ @@ -244,7 +244,7 @@ function checkingTab($tab) $tab = trim($tab); if (!Validate::isTabName($tab)) return false; - + $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']) { @@ -276,7 +276,7 @@ function checkingTab($tab) function checkTabRights($id_tab) { static $tabAccesses = NULL; - + if ($tabAccesses === NULL) $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 - * + * * @todo move in adminTab * @return string */ function generateShopList() { $tree = Shop::getTree(); - + // Get default value list($shopID, $shopGroupID) = Shop::getContext(); if ($shopID) diff --git a/classes/Controller.php b/classes/Controller.php index 56b775a83..005ed7b14 100644 --- a/classes/Controller.php +++ b/classes/Controller.php @@ -27,8 +27,102 @@ 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); } -} \ No newline at end of file + + 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; + } +} diff --git a/classes/FrontController.php b/classes/FrontController.php index 95525c6ce..5633022d7 100755 --- a/classes/FrontController.php +++ b/classes/FrontController.php @@ -28,10 +28,6 @@ class FrontControllerCore extends ControllerCore { public $errors = array(); - /** - * @var Context - */ - protected $context; /* Deprecated shortcuts as of 1.5 - Use $context->var instead */ protected static $smarty; @@ -67,6 +63,8 @@ class FrontControllerCore extends ControllerCore public function __construct() { global $useSSL; + + parent::__construct(); $useSSL = $this->ssl; } @@ -93,8 +91,6 @@ class FrontControllerCore extends ControllerCore return; self::$initialized = true; - $this->context = Context::getContext(); - $this->id_current_shop = Context::getContext()->shop->getID(); $this->id_current_group_shop = Context::getContext()->shop->getGroupID(); @@ -658,80 +654,47 @@ class FrontControllerCore extends ControllerCore $allowed = false; $userIp = Tools::getRemoteAddr(); $ips = explode(';', Configuration::get('PS_GEOLOCATION_WHITELIST')); - if (is_array($ips) AND sizeof($ips)) - foreach ($ips AS $ip) - if (!empty($ip) AND strpos($userIp, $ip) === 0) + if (is_array($ips) && sizeof($ips)) + foreach ($ips as $ip) + if (!empty($ip) && strpos($userIp, $ip) === 0) $allowed = true; 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 - * @param string $css_media_type - * @return true + * @see Controller::addCSS() */ 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) - $this->addCSS($file, $media_type); - return true; + $different = 0; + $override_path = str_replace(__PS_BASE_URI__.'modules/', _PS_ROOT_DIR_.'/themes/'._THEME_NAME_.'/css/modules/', $file, $different); + 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 - $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; + return parent::addCSS($list_uri, $css_media_type); } /** - * 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 - * @return void + * @see Controller::addJS() */ public function addJS($js_uri) { - if (!isset($this->js_files)) - $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)) + if (!is_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)) { @@ -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); if ($different && file_exists($override_path)) $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... - $this->js_files = array_merge($this->js_files, $js_uri); - - return true; + return parent::addJS($js_uri); } - }