* @copyright 2015 PrestaShow.pl
* @license http://PrestaShow.pl/license
*/
class PShowModule extends Module
{
/**
* Add hooks here to register during installation
*/
public $hooks = array();
/**
* Module controller with tab in admin menu
*/
public $admin_menu_tab;
/**
* Module controllers without tab in admin menu
*/
public $controllers = array();
/**
* Append here all new versions which has got update functions
*
* example:
* $moduleVersionPath = array('1.0.0', '1.1.0', '1.2.0')
* during update from 1.0.0 to 1.2.0 functions with be called(if exists):
* update_from_1_1()
* update_from_1_2()
*/
public $moduleVersionPath = array();
/**
* Primary configuration
*/
public $version = '1.0.0';
public $author = 'PrestaShow.pl';
public $need_instance = 0;
public $bootstrap = true;
public $filepath = null;
public function __construct()
{
$reflection = new ReflectionClass($this);
$this->filepath = $reflection->getFileName();
parent::__construct();
if (version_compare(_PS_VERSION_, '1.5.6.1', '<')) {
$this->ps_versions_compliancy['max'] = '1.6';
}
$this->controllers[] = $reflection->getShortName() . 'Settings';
// reinstall hooks after upgrade
Configuration::loadConfiguration();
$hooks_install_version = Configuration::get($reflection->getName() . '_hooksInstallVersion');
if (!$hooks_install_version || version_compare($hooks_install_version, $this->version) < 0) {
foreach ($this->hooks as $hook_name) {
if (!$this->getPosition(Hook::getIdByName($hook_name))) {
$this->registerHook($hook_name);
}
}
Configuration::updateValue($reflection->getName() . '_hooksInstallVersion', $this->version);
$this->adminDisplayInformation($this->l('Hooks updated for this module :)'));
}
}
public function install()
{
if (version_compare(PHP_VERSION, '5.3.0', '<')) {
return die('Module require PHP version >= 5.3.0');
}
$reflection = new ReflectionClass($this);
if (Shop::isFeatureActive()) {
Shop::setContext(Shop::CONTEXT_ALL);
}
if (!parent::install()) {
return false;
}
foreach ($this->hooks as $hook_name) {
$this->registerHook($hook_name);
}
Configuration::updateValue($reflection->getName() . '_hooksInstallVersion', $this->version);
$this->createAdminTabs();
$q = "CREATE TABLE IF NOT EXISTS `" . _DB_PREFIX_ . "pshow_" . $this->name . "_hook` (
`id_hook` INT NOT NULL AUTO_INCREMENT ,
`hook_name` text NOT NULL ,
`presta_id_hook` INT NOT NULL ,
PRIMARY KEY (`id_hook`)
) ENGINE = " . _MYSQL_ENGINE_ . "; ";
Db::getInstance()->query($q);
$this->registerHook('displayBackOfficeTop');
return true;
}
public function uninstallControllers()
{
$idTab = Tab::getIdFromClassName($this->admin_menu_tab);
if ($idTab != 0) {
$tab = new Tab($idTab);
$tab->delete();
}
foreach ($this->controllers as $ctrl_name) {
$idTab = Tab::getIdFromClassName($ctrl_name);
if ($idTab != 0) {
$tab = new Tab($idTab);
$tab->delete();
}
}
}
public function reinstallControllers()
{
$this->uninstallControllers();
$this->_installControllers();
}
public function _installControllers()
{
if (!Tab::getIdFromClassName($this->admin_menu_tab)) {
$tabsub = new Tab();
$tabsub->class_name = $this->admin_menu_tab;
$tabsub->module = $this->name;
foreach (Language::getLanguages() as $lang) {
$tabsub->name[$lang['id_lang']] = $this->displayName;
}
$tabsub->id_parent = Tab::getIdFromClassName('PrestashowModules');
$tabsub->save();
}
foreach ($this->controllers as $ctrl_name) {
if (!Tab::getIdFromClassName($ctrl_name)) {
try {
$tabsub = new Tab();
$tabsub->class_name = $ctrl_name;
$tabsub->module = $this->name;
foreach (Language::getLanguages() as $lang) {
$tabsub->name[$lang['id_lang']] = $ctrl_name;
}
$tabsub->id_parent = -1;
$tabsub->save();
} catch (Exception $e) {
}
}
}
}
public function createAdminTabs()
{
if (!Tab::getIdFromClassName('PrestashowModules')) {
try {
$tabsub = new Tab();
$tabsub->class_name = 'PrestashowModules';
$tabsub->module = $this->name;
foreach (Language::getLanguages() as $lang) {
$tabsub->name[$lang['id_lang']] = $this->l('PrestaShow Modules');
}
$tabsub->id_parent = 0;
$tabsub->save();
} catch (Exception $e) {
return false;
}
}
$this->_installControllers();
}
public function uninstall()
{
if (!parent::uninstall()) {
return false;
}
$this->uninstallControllers();
return true;
}
/**
* catch all hooks
*
* @param string $method
* @param array $args
* @return boolean
*/
public function __call($method, $args = array())
{
$hook_name = str_replace('hook', '', $method);
if (!Validate::isHookName($hook_name)) {
return false;
}
if (method_exists($this, $method)) {
return $this->{$method}();
}
return false;
}
/**
* Check for modules updates
*/
public function hookDisplayBackOfficeTop()
{
$controller = Tools::getValue('controller');
if ($controller && in_array($controller, array('AdminLogin'))) {
return;
}
$moduleName = PShowUpdate::getInstance($this->filepath)->getModuleDisplayName();
$moduleVersion = PShowUpdate::getInstance($this->filepath)->getModuleVersionNumber();
ob_start();
if (!function_exists('sanitize_output')) {
function sanitize_output($_buffer)
{
$search = array(
'/\>[^\S ]+/s', // strip whitespaces after tags, except space
'/[^\S ]+\',
'<',
'\\1'
);
$buffer = preg_replace($search, $replace, $_buffer);
return $buffer;
}
?>
$value) {
$params[Tools::safeOutput($key)] = Tools::safeOutput($value);
}
}
$excluded_key = array('id', 'module', 'isolang', 'id_lang',
'controller', 'fc', 'id_product', 'id_category', 'id_manufacturer',
'id_supplier', 'id_cms');
foreach ($_GET as $key => $value) {
if (!in_array($key, $excluded_key) && Validate::isUrl($key) && Validate::isUrl($value)) {
$params[Tools::safeOutput($key)] = Tools::safeOutput($value);
}
}
$str_params = http_build_query($params, '', '&');
if (!empty($str_params)) {
$final_url = preg_replace('/^([^?]*)?.*$/', '$1', $canonical_url) . '?' . $str_params;
} else {
$final_url = preg_replace('/^([^?]*)?.*$/', '$1', $canonical_url);
}
// Don't send any cookie
Context::getContext()->cookie->disallowWriting();
if (defined('_PS_MODE_DEV_') && _PS_MODE_DEV_ && $_SERVER['REQUEST_URI'] != __PS_BASE_URI__) {
die('[Debug] This page has moved
Please use the following URL instead: ' . $final_url . '');
}
$redirect_type = Configuration::get('PS_CANONICAL_REDIRECT') == 2 ? '301' : '302';
header('HTTP/1.0 ' . $redirect_type . ' Moved');
header('Cache-Control: no-cache');
Tools::redirectLink($final_url);
}
}
}