From 12a2d97956cc6373cf8dcaeaced079d479505460 Mon Sep 17 00:00:00 2001 From: Michael Dekker Date: Fri, 12 May 2017 03:21:52 +0200 Subject: [PATCH] Show live conf for non default tb themes --- .gitignore | 1 + CHANGELOG.txt | 25 - build.sh | 25 + composer.json | 19 - config.xml | 12 - .../AdminThemeConfiguratorController.php | 6 +- themeconfigurator.php | 1601 +++++++++-------- upgrade/install-0.8.php | 4 +- 8 files changed, 866 insertions(+), 827 deletions(-) create mode 100644 .gitignore delete mode 100644 CHANGELOG.txt create mode 100755 build.sh delete mode 100644 composer.json delete mode 100644 config.xml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0eb6b98 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*themeconfigurator*.zip diff --git a/CHANGELOG.txt b/CHANGELOG.txt deleted file mode 100644 index f514b92..0000000 --- a/CHANGELOG.txt +++ /dev/null @@ -1,25 +0,0 @@ -2014-04-17 17:34:04 +0200 [-] MO : themeconfigurator - rigth item pane is now displayed depending on default language #PSCSX-1555 -2014-04-17 10:50:50 +0200 [*] MO : themeconfigurator - Improved image name for SEO #PSCSX-1800 -2014-04-14 16:49:10 +0200 [-] BO : themeconfigurator - FixBug #PSCSX-1707 -2014-04-14 09:30:40 +0200 [-] MO : themeconfigurator - Fixed sprintf error value on upgrade 0.8 -2014-04-14 09:28:03 +0200 [-] MO : themeconfigurator - fixed traduction on upgrade error -2014-04-11 16:16:28 +0200 [-] MO : themeconfigurator - Corrected error message for traduction in 0.8 upgrade file -2014-04-10 09:58:03 +0200 [*] MO : themeconfigurator - Corrected #PSCSX-1713 issue. Image upload now uses general image settings -2014-04-09 14:12:00 +0200 [*] MO : themeconfigurator - adjusted css for top content -2014-04-07 15:55:48 +0200 [-] MO : themeconfigurator - added hook.css rules to sass file related to #PSCSX-1529 -2014-04-04 12:06:26 +0200 [-] MO : corrected URL lenght limitation issue #PSCSX-1500 for themeconfigurator -2014-04-03 14:58:57 +0200 [-] FO : Fixed display issues #PSCSX-1529 -2014-04-02 14:12:56 +0200 [*] BO : Fix #PSCSX-1529 reponsive slider issue -2014-03-28 17:54:36 +0100 // fix html content -2014-03-27 10:20:03 +0100 // removed console.log -2014-03-27 10:15:26 +0100 [-] MO : themeconfigurator : fix an issue with the deletion of items -2014-03-25 14:13:38 +0100 Merge branch 'master' of https://github.com/djfm/themeconfigurator -2014-03-25 14:13:13 +0100 // make string cleaner for translators -2014-03-25 11:56:59 +0100 Merge branch 'master' of https://github.com/PrestaShop/themeconfigurator -2014-03-25 10:52:11 +0100 [-] MO : themeconfigurator : wrong import links -2014-03-24 17:37:16 +0100 // bad font link -2014-03-24 15:34:14 +0100 // themeconfigurator -2014-03-24 15:23:06 +0100 / MO themeconfigurator : ps_versions_compliancy added -2014-03-24 10:44:47 +0100 // missing files -2014-03-24 10:33:03 +0100 [-] MO : theme configurator : fix issue with webkit when submiting form -2014-03-20 14:40:18 +0100 Initial commit diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..3c1b3d9 --- /dev/null +++ b/build.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash +CWD_BASENAME=${PWD##*/} + +FILES+=("logo.gif") +FILES+=("logo.png") +FILES+=("${CWD_BASENAME}.php") +FILES+=("index.php") +FILES+=("controllers/**") +FILES+=("css/**") +FILES+=("img/**") +FILES+=("js/**") +FILES+=("sass/**") +FILES+=("upgrade/**") +FILES+=("views/**") + +MODULE_VERSION="$(sed -ne "s/\\\$this->version *= *['\"]\([^'\"]*\)['\"] *;.*/\1/p" ${CWD_BASENAME}.php)" +MODULE_VERSION=${MODULE_VERSION//[[:space:]]} +ZIP_FILE="${CWD_BASENAME}/${CWD_BASENAME}-v${MODULE_VERSION}.zip" + +echo "Going to zip ${CWD_BASENAME} version ${MODULE_VERSION}" + +cd .. +for E in "${FILES[@]}"; do + find ${CWD_BASENAME}/${E} -type f -exec zip -9 ${ZIP_FILE} {} \; +done diff --git a/composer.json b/composer.json deleted file mode 100644 index b0ceb79..0000000 --- a/composer.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "name": "prestashop/themeconfigurator", - "description": "PrestaShop module themeconfigurator", - "homepage": "https://github.com/PrestaShop/themeconfigurator", - "license": "AFL - Academic Free License (AFL 3.0)", - "authors": [ - { - "name": "PrestaShop SA", - "email": "contact@prestashop.com" - } - ], - "require": { - "php": ">=5.3.2" - }, - "config": { - "preferred-install": "dist" - }, - "type": "prestashop-module" -} diff --git a/config.xml b/config.xml deleted file mode 100644 index ebba8fb..0000000 --- a/config.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - themeconfigurator - - - - - - 1 - 1 - - diff --git a/controllers/admin/AdminThemeConfiguratorController.php b/controllers/admin/AdminThemeConfiguratorController.php index 8c6565f..e436d8b 100644 --- a/controllers/admin/AdminThemeConfiguratorController.php +++ b/controllers/admin/AdminThemeConfiguratorController.php @@ -34,9 +34,9 @@ public function ajaxProcessUpdatePosition() $total = count($items); $success = true; for ($i = 1; $i <= $total; $i++) - $success &= Db::getInstance()->update('themeconfigurator', array('item_order' => $i), '`id_item` = '.preg_replace('/(item-)([0-9]+)/', '${2}', $items[$i - 1])); + $success &= Db::getInstance()->update('themeconfigurator', ['item_order' => $i], '`id_item` = '.preg_replace('/(item-)([0-9]+)/', '${2}', $items[$i - 1])); if (!$success) - die(Tools::jsonEncode(array('error' => 'Update Fail'))); - die(Tools::jsonEncode(array('success' => 'Update Success !', 'error' => false))); + die(Tools::jsonEncode(['error' => 'Update Fail'])); + die(Tools::jsonEncode(['success' => 'Update Success !', 'error' => false])); } } diff --git a/themeconfigurator.php b/themeconfigurator.php index a2fa438..f02f2f6 100644 --- a/themeconfigurator.php +++ b/themeconfigurator.php @@ -24,114 +24,99 @@ * International Registered Trademark & Property of PrestaShop SA */ -if (!defined('_TB_VERSION_')) - exit; +if (!defined('_TB_VERSION_')) { + exit; +} +/** + * Class ThemeConfigurator + */ class ThemeConfigurator extends Module { - protected $max_image_size = 1048576; - protected $default_language; - protected $languages; - - public function __construct() - { - $this->name = 'themeconfigurator'; - $this->tab = 'front_office_features'; - $this->version = '3.0.0'; - $this->bootstrap = true; - $this->secure_key = Tools::encrypt($this->name); - $this->default_language = Language::getLanguage(Configuration::get('PS_LANG_DEFAULT')); - $this->languages = Language::getLanguages(); - $this->author = 'thirty bees'; - parent::__construct(); - $this->displayName = $this->l('Theme configurator'); - $this->description = $this->l('Configure the main elements of your theme.'); - $this->ps_versions_compliancy = array('min' => '1.6', 'max' => '1.6.99.99'); - $this->module_path = _PS_MODULE_DIR_.$this->name.'/'; - $this->uploads_path = _PS_MODULE_DIR_.$this->name.'/img/'; - $this->admin_tpl_path = _PS_MODULE_DIR_.$this->name.'/views/templates/admin/'; - $this->hooks_tpl_path = _PS_MODULE_DIR_.$this->name.'/views/templates/hooks/'; - } - - public function createAjaxController() - { - $tab = new Tab(); - $tab->active = 1; - $languages = Language::getLanguages(false); - if (is_array($languages)) - foreach ($languages as $language) - $tab->name[$language['id_lang']] = 'themeconfigurator'; - $tab->class_name = 'AdminThemeConfigurator'; - $tab->module = $this->name; - $tab->id_parent = - 1; - return (bool)$tab->add(); - } - - private function _removeAjaxContoller() - { - if ($tab_id = (int)Tab::getIdFromClassName('AdminThemeConfigurator')) - { - $tab = new Tab($tab_id); - $tab->delete(); - } - return true; - } - - public function install() - { - $themes_colors = array( - 'theme1', - 'theme2', - 'theme3', - 'theme4', - 'theme5', - 'theme6', - 'theme7', - 'theme8', - 'theme9' - ); - $themes_fonts = array( - 'font1' => 'Open Sans', - 'font2' => 'Josefin Slab', - 'font3' => 'Arvo', - 'font4' => 'Lato', - 'font5' => 'Volkorn', - 'font6' => 'Abril Fatface', - 'font7' => 'Ubuntu', - 'font8' => 'PT Sans', - 'font9' => 'Old Standard TT', - 'font10' => 'Droid Sans' - ); - - if (!parent::install() - || !$this->installDB() - || !$this->installFixtures(Language::getLanguages(true)) || - !$this->registerHook('displayHeader') || - !$this->registerHook('displayTopColumn') || - !$this->registerHook('displayLeftColumn') || - !$this->registerHook('displayRightColumn') || - !$this->registerHook('displayHome') || - !$this->registerHook('displayFooter') || - !$this->registerHook('displayBackOfficeHeader') || - !$this->registerHook('actionObjectLanguageAddAfter') || - !Configuration::updateValue('PS_TC_THEMES', serialize($themes_colors)) || - !Configuration::updateValue('PS_TC_FONTS', serialize($themes_fonts)) || - !Configuration::updateValue('PS_TC_THEME', '') || - !Configuration::updateValue('PS_TC_FONT', '') || - !Configuration::updateValue('PS_TC_ACTIVE', 1) || - !Configuration::updateValue('PS_SET_DISPLAY_SUBCATEGORIES', 1) || - !$this->createAjaxController() - ) - return false; - - return true; - } - - private function installDB() - { - return ( - Db::getInstance()->Execute('DROP TABLE IF EXISTS `'._DB_PREFIX_.'themeconfigurator`') && - Db::getInstance()->Execute(' + protected $max_image_size = 1048576; + protected $default_language; + protected $languages; + + /** + * ThemeConfigurator constructor. + */ + public function __construct() + { + $this->name = 'themeconfigurator'; + $this->tab = 'front_office_features'; + $this->version = '3.0.1'; + $this->bootstrap = true; + $this->secure_key = Tools::encrypt($this->name); + $this->default_language = Language::getLanguage(Configuration::get('PS_LANG_DEFAULT')); + $this->languages = Language::getLanguages(); + $this->author = 'thirty bees'; + parent::__construct(); + $this->displayName = $this->l('Theme configurator'); + $this->description = $this->l('Configure the main elements of your theme.'); + $this->ps_versions_compliancy = ['min' => '1.6', 'max' => '1.6.99.99']; + $this->module_path = _PS_MODULE_DIR_.$this->name.'/'; + $this->uploads_path = _PS_MODULE_DIR_.$this->name.'/img/'; + $this->admin_tpl_path = _PS_MODULE_DIR_.$this->name.'/views/templates/admin/'; + $this->hooks_tpl_path = _PS_MODULE_DIR_.$this->name.'/views/templates/hooks/'; + } + + public function install() + { + $themesColors = [ + 'theme1', + 'theme2', + 'theme3', + 'theme4', + 'theme5', + 'theme6', + 'theme7', + 'theme8', + 'theme9', + ]; + $themesFonts = [ + 'font1' => 'Open Sans', + 'font2' => 'Josefin Slab', + 'font3' => 'Arvo', + 'font4' => 'Lato', + 'font5' => 'Volkorn', + 'font6' => 'Abril Fatface', + 'font7' => 'Ubuntu', + 'font8' => 'PT Sans', + 'font9' => 'Old Standard TT', + 'font10' => 'Droid Sans', + ]; + + if (!parent::install() + || !$this->installDB() + || !$this->installFixtures(Language::getLanguages(true)) || + !$this->registerHook('displayHeader') || + !$this->registerHook('displayTopColumn') || + !$this->registerHook('displayLeftColumn') || + !$this->registerHook('displayRightColumn') || + !$this->registerHook('displayHome') || + !$this->registerHook('displayFooter') || + !$this->registerHook('displayBackOfficeHeader') || + !$this->registerHook('actionObjectLanguageAddAfter') || + !Configuration::updateValue('PS_TC_THEMES', serialize($themesColors)) || + !Configuration::updateValue('PS_TC_FONTS', serialize($themesFonts)) || + !Configuration::updateValue('PS_TC_THEME', '') || + !Configuration::updateValue('PS_TC_FONT', '') || + !Configuration::updateValue('PS_TC_ACTIVE', 1) || + !Configuration::updateValue('PS_SET_DISPLAY_SUBCATEGORIES', 1) || + !$this->createAjaxController() + ) { + return false; + } + + return true; + } + + private function installDB() + { + return ( + Db::getInstance()->Execute('DROP TABLE IF EXISTS `'._DB_PREFIX_.'themeconfigurator`') && + Db::getInstance()->Execute( + ' CREATE TABLE `'._DB_PREFIX_.'themeconfigurator` ( `id_item` int(10) unsigned NOT NULL AUTO_INCREMENT, `id_shop` int(10) unsigned NOT NULL, @@ -148,701 +133,785 @@ private function installDB() `html` TEXT, `active` tinyint(1) unsigned NOT NULL DEFAULT \'1\', PRIMARY KEY (`id_item`) - ) ENGINE = '._MYSQL_ENGINE_.' DEFAULT CHARSET=UTF8;') - ); + ) ENGINE = '._MYSQL_ENGINE_.' DEFAULT CHARSET=UTF8;' + ) + ); + + } + + public function installFixtures($languages = null) + { + $result = true; + + if ($languages === null) { + $languages = Language::getLanguages(true); + } + + foreach ($languages as $language) { + for ($i = 1; $i < 6; $i++) { + $result &= $this->installFixture('home', $i, $this->context->shop->id, $language['id_lang']); + } - } + for ($i = 6; $i < 8; $i++) { + $result &= $this->installFixture('top', $i, $this->context->shop->id, $language['id_lang']); + } + } - protected function installFixture($hook, $id_image, $id_shop, $id_lang) - { - $result = true; + return $result; + } - $sizes = @getimagesize((dirname(__FILE__).DIRECTORY_SEPARATOR.'img'.DIRECTORY_SEPARATOR.'banner-img'.(int)$id_image.'.jpg')); - $width = (isset($sizes[0]) && $sizes[0])? (int)$sizes[0] : 0; - $height = (isset($sizes[1]) && $sizes[1])? (int)$sizes[1] : 0; + protected function installFixture($hook, $idImage, $idShop, $idLang) + { + $result = true; - $result &= Db::getInstance()->Execute(' + $sizes = @getimagesize((dirname(__FILE__).DIRECTORY_SEPARATOR.'img'.DIRECTORY_SEPARATOR.'banner-img'.(int) $idImage.'.jpg')); + $width = (isset($sizes[0]) && $sizes[0]) ? (int) $sizes[0] : 0; + $height = (isset($sizes[1]) && $sizes[1]) ? (int) $sizes[1] : 0; + + $result &= Db::getInstance()->Execute( + ' INSERT INTO `'._DB_PREFIX_.'themeconfigurator` ( `id_shop`, `id_lang`, `item_order`, `title`, `title_use`, `hook`, `url`, `target`, `image`, `image_w`, `image_h`, `html`, `active` ) VALUES ( - \''.(int)$id_shop.'\', - \''.(int)$id_lang.'\', - \''.(int)$id_image.'\', + \''.(int) $idShop.'\', + \''.(int) $idLang.'\', + \''.(int) $idImage.'\', \'\', \'0\', \''.pSQL($hook).'\', \'http://www.prestashop.com/\', \'0\', - \'banner-img'.(int)$id_image.'.jpg\', + \'banner-img'.(int) $idImage.'.jpg\', '.$width.', '.$height.', \'\', 1) - '); - - return $result; - } - - public function installFixtures($languages = null) - { - $result = true; - - if ($languages === null) - $languages = Language::getLanguages(true); - - foreach ($languages as $language) - { - for ($i = 1; $i < 6; $i++) - $result &= $this->installFixture('home', $i, $this->context->shop->id, $language['id_lang']); - - for ($i = 6; $i < 8; $i++) - $result &= $this->installFixture('top', $i, $this->context->shop->id, $language['id_lang']); - } - - return $result; - } - - - public function uninstall() - { - $images = array(); - if (count(Db::getInstance()->executeS('SHOW TABLES LIKE \''._DB_PREFIX_.'themeconfigurator\''))) - $images = Db::getInstance()->executeS('SELECT image FROM `'._DB_PREFIX_.'themeconfigurator`'); - foreach ($images as $image) - $this->deleteImage($image['image']); - - if (!Db::getInstance()->Execute('DROP TABLE IF EXISTS `'._DB_PREFIX_.'themeconfigurator`') || !$this->_removeAjaxContoller() || !parent::uninstall()) - return false; - - return true; - } - - public function hookDisplayBackOfficeHeader() - { - if (Tools::getValue('configure') != $this->name) - return; - - $this->context->controller->addCSS($this->_path.'css/admin.css'); - $this->context->controller->addJquery(); - $this->context->controller->addJS($this->_path.'js/admin.js'); - } - - protected function checkEnvironment() - { - $cookie = new Cookie('psAdmin', '', (int)Configuration::get('PS_COOKIE_LIFETIME_BO')); - return isset($cookie->id_employee) && isset($cookie->passwd) && Employee::checkPassword($cookie->id_employee, $cookie->passwd); - } - - public function hookdisplayHeader() - { - $this->context->controller->addCss($this->_path.'css/hooks.css', 'all'); - - if ((int)Configuration::get('PS_TC_ACTIVE') == 1 && Tools::getValue('live_configurator_token') && Tools::getValue('live_configurator_token') == $this->getLiveConfiguratorToken() && $this->checkEnvironment()) - { - $this->context->controller->addCSS($this->_path.'css/live_configurator.css'); - $this->context->controller->addJS($this->_path.'js/live_configurator.js'); - - if (Tools::getValue('theme')) - $this->context->controller->addCss($this->_path.'css/'.Tools::getValue('theme').'.css', 'all'); - - if (Tools::getValue('theme_font')) - $this->context->controller->addCss($this->_path.'css/'.Tools::getValue('theme_font').'.css', 'all'); - } - else - { - if (Configuration::get('PS_TC_THEME') != '') - $this->context->controller->addCss($this->_path.'css/'.Configuration::get('PS_TC_THEME').'.css', 'all'); - - if (Configuration::get('PS_TC_FONT') != '') - $this->context->controller->addCss($this->_path.'css/'.Configuration::get('PS_TC_FONT').'.css', 'all'); - } - - if (isset($this->context->controller->php_self) && $this->context->controller->php_self == 'category') - { - $this->context->smarty->assign(array( - 'display_subcategories' => (int)Configuration::get('PS_SET_DISPLAY_SUBCATEGORIES') - )); - - return $this->display(__FILE__, 'hook.tpl'); - } - } - - public function hookActionObjectLanguageAddAfter($params) - { - return $this->installFixtures(array(array('id_lang' => (int)$params['object']->id))); - } - - public function hookdisplayTopColumn() - { - return $this->hookdisplayTop(); - } - - public function hookdisplayTop() - { - if (!isset($this->context->controller->php_self) || $this->context->controller->php_self != 'index') - return ; - $this->context->smarty->assign(array( - 'htmlitems' => $this->getItemsFromHook('top'), - 'hook' => 'top' - )); - - return $this->display(__FILE__, 'hook.tpl'); - } - - public function hookDisplayHome() - { - $this->context->smarty->assign(array( - 'htmlitems' => $this->getItemsFromHook('home'), - 'hook' => 'home' - )); - - return $this->display(__FILE__, 'hook.tpl'); - } - - public function hookDisplayLeftColumn() - { - $this->context->smarty->assign(array( - 'htmlitems' => $this->getItemsFromHook('left'), - 'hook' => 'left' - )); - - return $this->display(__FILE__, 'hook.tpl'); - } - - public function hookDisplayRightColumn() - { - $this->context->smarty->assign(array( - 'htmlitems' => $this->getItemsFromHook('right'), - 'hook' => 'right' - )); - - return $this->display(__FILE__, 'hook.tpl'); - } - - public function hookDisplayFooter() - { - $html = ''; - - if ((int)Configuration::get('PS_TC_ACTIVE') == 1 && Tools::getValue('live_configurator_token') && Tools::getValue('live_configurator_token') == $this->getLiveConfiguratorToken() && Tools::getIsset('id_employee') && $this->checkEnvironment()) - { - if (Tools::isSubmit('submitLiveConfigurator')) - { - Configuration::updateValue('PS_TC_THEME', Tools::getValue('theme')); - Configuration::updateValue('PS_TC_FONT', Tools::getValue('theme_font')); - } - - $ad_image = $this->_path.'img/'.$this->context->language->iso_code.'/advertisement.png'; - - if (!file_exists($ad_image)) - $ad_image = $this->_path.'img/en/advertisement.png'; - - $this->smarty->assign(array( - 'themes' => Tools::unserialize(Configuration::get('PS_TC_THEMES')), - 'fonts' => Tools::unserialize(Configuration::get('PS_TC_FONTS')), - 'theme_font' => Tools::getValue('theme_font', Configuration::get('PS_TC_FONT')), - 'live_configurator_token' => $this->getLiveConfiguratorToken(), - 'id_shop' => (int)$this->context->shop->id, - 'id_employee' => is_object($this->context->employee) ? (int)$this->context->employee->id : - Tools::getValue('id_employee'), - 'advertisement_image' => $ad_image, - 'advertisement_url' => 'http://addons.prestashop.com/en/205-premium-templates?utm_source=back-office' - .'&utm_medium=theme-configurator' - .'&utm_campaign=back-office-'.Tools::strtoupper($this->context->language->iso_code) - .'&utm_content='.(defined('_PS_HOST_MODE_') ? 'ondemand' : 'download'), - 'advertisement_text' => $this->l('Over 800 PrestaShop premium templates! Browse now!') - )); - - $html .= $this->display(__FILE__, 'live_configurator.tpl'); - } - - $this->context->smarty->assign(array( - 'htmlitems' => $this->getItemsFromHook('footer'), - 'hook' => 'footer' - )); - - return $html.$this->display(__FILE__, 'hook.tpl'); - } - - protected function getItemsFromHook($hook) - { - if (!$hook) - return false; - - return Db::getInstance()->ExecuteS(' + ' + ); + + return $result; + } + + public function createAjaxController() + { + $tab = new Tab(); + $tab->active = 1; + $languages = Language::getLanguages(false); + if (is_array($languages)) { + foreach ($languages as $language) { + $tab->name[$language['id_lang']] = 'themeconfigurator'; + } + } + $tab->class_name = 'AdminThemeConfigurator'; + $tab->module = $this->name; + $tab->id_parent = -1; + + return (bool) $tab->add(); + } + + public function uninstall() + { + $images = []; + if (count(Db::getInstance()->executeS('SHOW TABLES LIKE \''._DB_PREFIX_.'themeconfigurator\''))) { + $images = Db::getInstance()->executeS('SELECT image FROM `'._DB_PREFIX_.'themeconfigurator`'); + } + foreach ($images as $image) { + $this->deleteImage($image['image']); + } + + if (!Db::getInstance()->Execute('DROP TABLE IF EXISTS `'._DB_PREFIX_.'themeconfigurator`') || !$this->_removeAjaxContoller() || !parent::uninstall()) { + return false; + } + + return true; + } + + protected function deleteImage($image) + { + $fileName = $this->uploads_path.$image; + + if (realpath(dirname($fileName)) != realpath($this->uploads_path)) { + Tools::dieOrLog(sprintf('Could not find upload directory')); + } + + if ($image != '' && is_file($fileName) && !strpos($fileName, 'banner-img') && !strpos($fileName, 'bg-theme') && !strpos($fileName, 'footer-bg')) { + unlink($fileName); + } + } + + private function _removeAjaxContoller() + { + if ($idTab = (int) Tab::getIdFromClassName('AdminThemeConfigurator')) { + $tab = new Tab($idTab); + $tab->delete(); + } + + return true; + } + + public function hookDisplayBackOfficeHeader() + { + if (Tools::getValue('configure') != $this->name) { + return; + } + + $this->context->controller->addCSS($this->_path.'css/admin.css'); + $this->context->controller->addJquery(); + $this->context->controller->addJS($this->_path.'js/admin.js'); + } + + public function hookdisplayHeader() + { + $this->context->controller->addCss($this->_path.'css/hooks.css', 'all'); + + if ((int) Configuration::get('PS_TC_ACTIVE') == 1 && Tools::getValue('live_configurator_token') && Tools::getValue('live_configurator_token') == $this->getLiveConfiguratorToken() && $this->checkEnvironment()) { + $this->context->controller->addCSS($this->_path.'css/live_configurator.css'); + $this->context->controller->addJS($this->_path.'js/live_configurator.js'); + + if (Tools::getValue('theme')) { + $this->context->controller->addCss($this->_path.'css/'.Tools::getValue('theme').'.css', 'all'); + } + + if (Tools::getValue('theme_font')) { + $this->context->controller->addCss($this->_path.'css/'.Tools::getValue('theme_font').'.css', 'all'); + } + } else { + if (Configuration::get('PS_TC_THEME') != '') { + $this->context->controller->addCss($this->_path.'css/'.Configuration::get('PS_TC_THEME').'.css', 'all'); + } + + if (Configuration::get('PS_TC_FONT') != '') { + $this->context->controller->addCss($this->_path.'css/'.Configuration::get('PS_TC_FONT').'.css', 'all'); + } + } + + if (isset($this->context->controller->php_self) && $this->context->controller->php_self == 'category') { + $this->context->smarty->assign( + [ + 'display_subcategories' => (int) Configuration::get('PS_SET_DISPLAY_SUBCATEGORIES'), + ] + ); + + return $this->display(__FILE__, 'hook.tpl'); + } + } + + public function getLiveConfiguratorToken() + { + return Tools::getAdminToken( + $this->name.(int) Tab::getIdFromClassName($this->name) + .(is_object(Context::getContext()->employee) ? (int) Context::getContext()->employee->id : + Tools::getValue('id_employee')) + ); + } + + protected function checkEnvironment() + { + $cookie = new Cookie('psAdmin', '', (int) Configuration::get('PS_COOKIE_LIFETIME_BO')); + + return isset($cookie->id_employee) && isset($cookie->passwd) && Employee::checkPassword($cookie->id_employee, $cookie->passwd); + } + + public function hookActionObjectLanguageAddAfter($params) + { + return $this->installFixtures([['id_lang' => (int) $params['object']->id]]); + } + + public function hookdisplayTopColumn() + { + return $this->hookdisplayTop(); + } + + public function hookdisplayTop() + { + if (!isset($this->context->controller->php_self) || $this->context->controller->php_self != 'index') { + return; + } + $this->context->smarty->assign( + [ + 'htmlitems' => $this->getItemsFromHook('top'), + 'hook' => 'top', + ] + ); + + return $this->display(__FILE__, 'hook.tpl'); + } + + protected function getItemsFromHook($hook) + { + if (!$hook) { + return false; + } + + return Db::getInstance()->ExecuteS( + ' SELECT * FROM `'._DB_PREFIX_.'themeconfigurator` - WHERE id_shop = '.(int)$this->context->shop->id.' AND id_lang = '.(int)$this->context->language->id.' + WHERE id_shop = '.(int) $this->context->shop->id.' AND id_lang = '.(int) $this->context->language->id.' AND hook = \''.pSQL($hook).'\' AND active = 1 - ORDER BY item_order ASC'); - } - - protected function deleteImage($image) - { - $file_name = $this->uploads_path.$image; - - if (realpath(dirname($file_name)) != realpath($this->uploads_path)) - Tools::dieOrLog(sprintf('Could not find upload directory')); - - if ($image != '' && is_file($file_name) && !strpos($file_name, 'banner-img') && !strpos($file_name, 'bg-theme') && !strpos($file_name, 'footer-bg')) - unlink($file_name); - } - - protected function removeItem() - { - $id_item = (int)Tools::getValue('item_id'); - - if ($image = Db::getInstance()->getValue('SELECT image FROM `'._DB_PREFIX_.'themeconfigurator` WHERE id_item = '.(int)$id_item)) - $this->deleteImage($image); - - Db::getInstance()->delete(_DB_PREFIX_.'themeconfigurator', 'id_item = '.(int)$id_item); - - if (Db::getInstance()->Affected_Rows() == 1) - { - Db::getInstance()->execute(' - UPDATE `'._DB_PREFIX_.'themeconfigurator` - SET item_order = item_order-1 - WHERE ( - item_order > '.(int)Tools::getValue('item_order').' AND - id_shop = '.(int)$this->context->shop->id.' AND - hook = \''.pSQL(Tools::getValue('item_hook')).'\')' - ); - Tools::redirectAdmin('index.php?tab=AdminModules&configure='.$this->name.'&conf=6&token='.Tools::getAdminTokenLite('AdminModules')); - } - else - $this->context->smarty->assign('error', $this->l('Can\'t delete the slide.')); - } - - protected function updateItem() - { - $id_item = (int)Tools::getValue('item_id'); - $title = Tools::getValue('item_title'); - $content = Tools::getValue('item_html'); - - if (!Validate::isCleanHtml($title, (int)Configuration::get('PS_ALLOW_HTML_IFRAME')) || !Validate::isCleanHtml($content, (int)Configuration::get('PS_ALLOW_HTML_IFRAME'))) - { - $this->context->smarty->assign('error', $this->l('Invalid content')); - return false; - } - - $new_image = ''; - $image_w = (is_numeric(Tools::getValue('item_img_w'))) ? (int)Tools::getValue('item_img_w') : ''; - $image_h = (is_numeric(Tools::getValue('item_img_h'))) ? (int)Tools::getValue('item_img_h') : ''; - - if (!empty($_FILES['item_img']['name'])) - { - if ($old_image = Db::getInstance()->getValue('SELECT image FROM `'._DB_PREFIX_.'themeconfigurator` WHERE id_item = '.(int)$id_item)) - if (file_exists(dirname(__FILE__).'/img/'.$old_image)) - @unlink(dirname(__FILE__).'/img/'.$old_image); - - if (!$image = $this->uploadImage($_FILES['item_img'], $image_w, $image_h)) - return false; - - $new_image = 'image = \''.pSQL($image).'\','; - } - - if (!Db::getInstance()->execute(' - UPDATE `'._DB_PREFIX_.'themeconfigurator` SET - title = \''.pSQL($title).'\', - title_use = '.(int)Tools::getValue('item_title_use').', - hook = \''.pSQL(Tools::getValue('item_hook')).'\', - url = \''.pSQL(Tools::getValue('item_url')).'\', - target = '.(int)Tools::getValue('item_target').', - '.$new_image.' - image_w = '.(int)$image_w.', - image_h = '.(int)$image_h.', - active = '.(int)Tools::getValue('item_active').', - html = \''.pSQL($this->filterVar($content), true).'\' - WHERE id_item = '.(int)Tools::getValue('item_id') - )) - { - if ($image = Db::getInstance()->getValue('SELECT image FROM `'._DB_PREFIX_.'themeconfigurator` WHERE id_item = '.(int)Tools::getValue('item_id'))) - $this->deleteImage($image); - - $this->context->smarty->assign('error', $this->l('An error occurred while saving data.')); - - return false; - } - - $this->context->smarty->assign('confirmation', $this->l('Successfully updated.')); - - return true; - } - - protected function uploadImage($image, $image_w = '', $image_h = '') - { - $res = false; - if (is_array($image) && (ImageManager::validateUpload($image, $this->max_image_size) === false) && ($tmp_name = tempnam(_PS_TMP_IMG_DIR_, 'PS')) && move_uploaded_file($image['tmp_name'], $tmp_name)) - { - $salt = sha1(microtime()); - $pathinfo = pathinfo($image['name']); - $img_name = $salt.'_'.Tools::str2url($pathinfo['filename']).'.'.$pathinfo['extension']; - - if (ImageManager::resize($tmp_name, dirname(__FILE__).'/img/'.$img_name, $image_w, $image_h)) - $res = true; - } - - if (!$res) - { - $this->context->smarty->assign('error', $this->l('An error occurred during the image upload.')); - return false; - } - - return $img_name; - } - - public function getContent() - { - if (Tools::isSubmit('submitModule')) - { - Configuration::updateValue('PS_QUICK_VIEW', (int)Tools::getValue('quick_view')); - Configuration::updateValue('PS_TC_ACTIVE', (int)Tools::getValue('live_conf')); - Configuration::updateValue('PS_GRID_PRODUCT', (int)Tools::getValue('grid_list')); - Configuration::updateValue('PS_SET_DISPLAY_SUBCATEGORIES', (int)Tools::getValue('sub_cat')); - foreach ($this->getConfigurableModules() as $module) - { - if (!isset($module['is_module']) || !$module['is_module'] || !Validate::isModuleName($module['name']) || !Tools::isSubmit($module['name'])) - continue; - - $module_instance = Module::getInstanceByName($module['name']); - if ($module_instance === false || !is_object($module_instance)) - continue; - - $is_installed = (int)Validate::isLoadedObject($module_instance); - if ($is_installed) - { - if (($active = (int)Tools::getValue($module['name'])) == $module_instance->active) - continue; - - if ($active) - $module_instance->enable(); - else - $module_instance->disable(); - } - else - if ((int)Tools::getValue($module['name'])) - $module_instance->install(); - } - } - - if (Tools::isSubmit('newItem')) - $this->addItem(); - elseif (Tools::isSubmit('updateItem')) - $this->updateItem(); - elseif (Tools::isSubmit('removeItem')) - $this->removeItem(); - - $html = $this->renderConfigurationForm(); - $html .= $this->renderThemeConfiguratorForm(); - - return $html; - } - - protected function addItem() - { - $title = Tools::getValue('item_title'); - $content = Tools::getValue('item_html'); - - if (!Validate::isCleanHtml($title, (int)Configuration::get('PS_ALLOW_HTML_IFRAME')) - || !Validate::isCleanHtml($content, (int)Configuration::get('PS_ALLOW_HTML_IFRAME'))) - { - $this->context->smarty->assign('error', $this->l('Invalid content')); - return false; - } - - if (!$current_order = (int)Db::getInstance()->getValue(' + ORDER BY item_order ASC' + ); + } + + public function hookDisplayHome() + { + $this->context->smarty->assign( + [ + 'htmlitems' => $this->getItemsFromHook('home'), + 'hook' => 'home', + ] + ); + + return $this->display(__FILE__, 'hook.tpl'); + } + + public function hookDisplayLeftColumn() + { + $this->context->smarty->assign( + [ + 'htmlitems' => $this->getItemsFromHook('left'), + 'hook' => 'left', + ] + ); + + return $this->display(__FILE__, 'hook.tpl'); + } + + public function hookDisplayRightColumn() + { + $this->context->smarty->assign( + [ + 'htmlitems' => $this->getItemsFromHook('right'), + 'hook' => 'right', + ] + ); + + return $this->display(__FILE__, 'hook.tpl'); + } + + public function hookDisplayFooter() + { + $html = ''; + + if ((int) Configuration::get('PS_TC_ACTIVE') == 1 && Tools::getValue('live_configurator_token') && Tools::getValue('live_configurator_token') == $this->getLiveConfiguratorToken() && Tools::getIsset('id_employee') && $this->checkEnvironment()) { + if (Tools::isSubmit('submitLiveConfigurator')) { + Configuration::updateValue('PS_TC_THEME', Tools::getValue('theme')); + Configuration::updateValue('PS_TC_FONT', Tools::getValue('theme_font')); + } + + $adImage = $this->_path.'img/'.$this->context->language->iso_code.'/advertisement.png'; + + if (!file_exists($adImage)) { + $adImage = $this->_path.'img/en/advertisement.png'; + } + + $this->smarty->assign( + [ + 'themes' => Tools::unserialize(Configuration::get('PS_TC_THEMES')), + 'fonts' => Tools::unserialize(Configuration::get('PS_TC_FONTS')), + 'theme_font' => Tools::getValue('theme_font', Configuration::get('PS_TC_FONT')), + 'live_configurator_token' => $this->getLiveConfiguratorToken(), + 'id_shop' => (int) $this->context->shop->id, + 'id_employee' => is_object($this->context->employee) ? (int) $this->context->employee->id : + Tools::getValue('id_employee'), + 'advertisement_image' => $adImage, + 'advertisement_url' => 'http://addons.prestashop.com/en/205-premium-templates?utm_source=back-office' + .'&utm_medium=theme-configurator' + .'&utm_campaign=back-office-'.Tools::strtoupper($this->context->language->iso_code) + .'&utm_content='.(defined('_PS_HOST_MODE_') ? 'ondemand' : 'download'), + 'advertisement_text' => $this->l('Over 800 PrestaShop premium templates! Browse now!'), + ] + ); + + $html .= $this->display(__FILE__, 'live_configurator.tpl'); + } + + $this->context->smarty->assign( + [ + 'htmlitems' => $this->getItemsFromHook('footer'), + 'hook' => 'footer', + ] + ); + + return $html.$this->display(__FILE__, 'hook.tpl'); + } + + public function getContent() + { + if (Tools::isSubmit('submitModule')) { + Configuration::updateValue('PS_QUICK_VIEW', (int) Tools::getValue('quick_view')); + Configuration::updateValue('PS_TC_ACTIVE', (int) Tools::getValue('live_conf')); + Configuration::updateValue('PS_GRID_PRODUCT', (int) Tools::getValue('grid_list')); + Configuration::updateValue('PS_SET_DISPLAY_SUBCATEGORIES', (int) Tools::getValue('sub_cat')); + foreach ($this->getConfigurableModules() as $module) { + if (!isset($module['is_module']) || !$module['is_module'] || !Validate::isModuleName($module['name']) || !Tools::isSubmit($module['name'])) { + continue; + } + + $moduleInstance = Module::getInstanceByName($module['name']); + if ($moduleInstance === false || !is_object($moduleInstance)) { + continue; + } + + $is_installed = (int) Validate::isLoadedObject($moduleInstance); + if ($is_installed) { + if (($active = (int) Tools::getValue($module['name'])) == $moduleInstance->active) { + continue; + } + + if ($active) { + $moduleInstance->enable(); + } else { + $moduleInstance->disable(); + } + } else { + if ((int) Tools::getValue($module['name'])) { + $moduleInstance->install(); + } + } + } + } + + if (Tools::isSubmit('newItem')) { + $this->addItem(); + } elseif (Tools::isSubmit('updateItem')) { + $this->updateItem(); + } elseif (Tools::isSubmit('removeItem')) { + $this->removeItem(); + } + + $html = $this->renderConfigurationForm(); + $html .= $this->renderThemeConfiguratorForm(); + + return $html; + } + + protected function getConfigurableModules() + { + // Construct the description for the 'Enable Live Configurator' switch + if ($this->context->shop->getBaseURL()) { + $request = + 'live_configurator_token='.$this->getLiveConfiguratorToken() + .'&id_employee='.(int) $this->context->employee->id + .'&id_shop='.(int) $this->context->shop->id + .(Configuration::get('PS_TC_THEME') != '' ? '&theme='.Configuration::get('PS_TC_THEME') : '') + .(Configuration::get('PS_TC_FONT') != '' ? '&theme_font='.Configuration::get('PS_TC_FONT') : ''); + $url = $this->context->link->getPageLink('index', null, $id_lang = null, $request); + + $desc = '' + .$this->l('View').'
' + .$this->l('Only you can see this on your front office - your visitors will not see this tool.'); + } else { + $desc = $this->l('Only you can see this on your front office - your visitors will not see this tool.'); + } + + $ret = [ + [ + 'label' => $this->l('Display links to your store\'s social accounts (Twitter, Facebook, etc.)'), + 'name' => 'blocksocial', + 'value' => (int) Validate::isLoadedObject($module = Module::getInstanceByName('blocksocial')) && $module->isEnabledForShopContext(), + 'is_module' => true, + ], + [ + 'label' => $this->l('Display your contact information'), + 'name' => 'blockcontactinfos', + 'value' => (int) Validate::isLoadedObject($module = Module::getInstanceByName('blockcontactinfos')) && $module->isEnabledForShopContext(), + 'is_module' => true, + ], + [ + 'label' => $this->l('Display social sharing buttons on the product\'s page'), + 'name' => 'socialsharing', + 'value' => (int) Validate::isLoadedObject($module = Module::getInstanceByName('socialsharing')) && $module->isEnabledForShopContext(), + 'is_module' => true, + ], + [ + 'label' => $this->l('Display the Facebook block on the home page'), + 'name' => 'blockfacebook', + 'value' => (int) Validate::isLoadedObject($module = Module::getInstanceByName('blockfacebook')) && $module->isEnabledForShopContext(), + 'is_module' => true, + ], + [ + 'label' => $this->l('Display the custom CMS information block'), + 'name' => 'blockcmsinfo', + 'value' => (int) Validate::isLoadedObject($module = Module::getInstanceByName('blockcmsinfo')) && $module->isEnabledForShopContext(), + 'is_module' => true, + ], + [ + 'label' => $this->l('Display quick view window on homepage and category pages'), + 'name' => 'quick_view', + 'value' => (int) Tools::getValue('PS_QUICK_VIEW', Configuration::get('PS_QUICK_VIEW')), + ], + [ + 'label' => $this->l('Display categories as a list of products instead of the default grid-based display'), + 'name' => 'grid_list', + 'value' => (int) Configuration::get('PS_GRID_PRODUCT'), + 'desc' => $this->l('Works only for first-time users. This setting is overridden by the user\'s choice as soon as the user cookie is set.'), + ], + [ + 'label' => $this->l('Display top banner'), + 'name' => 'blockbanner', + 'value' => (int) Validate::isLoadedObject($module = Module::getInstanceByName('blockbanner')) && $module->isEnabledForShopContext(), + 'is_module' => true, + ], + [ + 'label' => $this->l('Display logos of available payment methods'), + 'name' => 'productpaymentlogos', + 'value' => (int) Validate::isLoadedObject($module = Module::getInstanceByName('productpaymentlogos')) && $module->isEnabledForShopContext(), + 'is_module' => true, + ], + ]; + + if ($this->context->theme->name !== 'community-theme-default') { + $ret[] = [ + 'label' => $this->l('Display Live Configurator'), + 'name' => 'live_conf', + 'value' => (int) Tools::getValue('PS_TC_ACTIVE', Configuration::get('PS_TC_ACTIVE')), + 'hint' => $this->l('This customization tool allows you to make color and font changes in your theme.'), + 'desc' => $desc, + ]; + } + + $ret[] = [ + 'label' => $this->l('Display subcategories'), + 'name' => 'sub_cat', + 'value' => (int) Tools::getValue('PS_SET_DISPLAY_SUBCATEGORIES', Configuration::get('PS_SET_DISPLAY_SUBCATEGORIES')), + ]; + + return $ret; + } + + protected function addItem() + { + $title = Tools::getValue('item_title'); + $content = Tools::getValue('item_html'); + + if (!Validate::isCleanHtml($title, (int) Configuration::get('PS_ALLOW_HTML_IFRAME')) + || !Validate::isCleanHtml($content, (int) Configuration::get('PS_ALLOW_HTML_IFRAME')) + ) { + $this->context->smarty->assign('error', $this->l('Invalid content')); + + return false; + } + + if (!$currentOrder = (int) Db::getInstance()->getValue( + ' SELECT item_order + 1 FROM `'._DB_PREFIX_.'themeconfigurator` WHERE - id_shop = '.(int)$this->context->shop->id.' - AND id_lang = '.(int)Tools::getValue('id_lang').' + id_shop = '.(int) $this->context->shop->id.' + AND id_lang = '.(int) Tools::getValue('id_lang').' AND hook = \''.pSQL(Tools::getValue('item_hook')).'\' ORDER BY item_order DESC' - )) - $current_order = 1; - - $image_w = is_numeric(Tools::getValue('item_img_w')) ? (int)Tools::getValue('item_img_w') : ''; - $image_h = is_numeric(Tools::getValue('item_img_h')) ? (int)Tools::getValue('item_img_h') : ''; - - if (!empty($_FILES['item_img']['name'])) - { - if (!$image = $this->uploadImage($_FILES['item_img'], $image_w, $image_h)) - return false; - } - else - { - $image = ''; - $image_w = ''; - $image_h = ''; - } - - if (!Db::getInstance()->Execute(' + ) + ) { + $currentOrder = 1; + } + + $imageW = is_numeric(Tools::getValue('item_img_w')) ? (int) Tools::getValue('item_img_w') : ''; + $imageH = is_numeric(Tools::getValue('item_img_h')) ? (int) Tools::getValue('item_img_h') : ''; + + if (!empty($_FILES['item_img']['name'])) { + if (!$image = $this->uploadImage($_FILES['item_img'], $imageW, $imageH)) { + return false; + } + } else { + $image = ''; + $imageW = ''; + $imageH = ''; + } + + if (!Db::getInstance()->Execute( + ' INSERT INTO `'._DB_PREFIX_.'themeconfigurator` ( `id_shop`, `id_lang`, `item_order`, `title`, `title_use`, `hook`, `url`, `target`, `image`, `image_w`, `image_h`, `html`, `active` ) VALUES ( - \''.(int)$this->context->shop->id.'\', - \''.(int)Tools::getValue('id_lang').'\', - \''.(int)$current_order.'\', + \''.(int) $this->context->shop->id.'\', + \''.(int) Tools::getValue('id_lang').'\', + \''.(int) $currentOrder.'\', \''.pSQL($title).'\', - \''.(int)Tools::getValue('item_title_use').'\', + \''.(int) Tools::getValue('item_title_use').'\', \''.pSQL(Tools::getValue('item_hook')).'\', \''.pSQL(Tools::getValue('item_url')).'\', - \''.(int)Tools::getValue('item_target').'\', + \''.(int) Tools::getValue('item_target').'\', \''.pSQL($image).'\', - \''.pSQL($image_w).'\', - \''.pSQL($image_h).'\', + \''.pSQL($imageW).'\', + \''.pSQL($imageH).'\', \''.pSQL($this->filterVar($content), true).'\', 1)' - )) - { - if (!Tools::isEmpty($image)) - $this->deleteImage($image); - - $this->context->smarty->assign('error', $this->l('An error occurred while saving data.')); - return false; - } - - $this->context->smarty->assign('confirmation', $this->l('New item successfully added.')); - return true; - } - - public function renderConfigurationForm() - { - $inputs = array(); - - foreach ($this->getConfigurableModules() as $module) - { - $desc = ''; - - if (isset($module['is_module']) && $module['is_module']) - { - $module_instance = Module::getInstanceByName($module['name']); - if (Validate::isLoadedObject($module_instance) && method_exists($module_instance, 'getContent')) - $desc = ''.$this->l('Configure').' '; - } - if (!$desc && isset($module['desc']) && $module['desc']) - $desc = $module['desc']; - - $inputs[] = array( - 'type' => 'switch', - 'label' => $module['label'], - 'name' => $module['name'], - 'desc' => $desc, - 'values' => array( - array( - 'id' => 'active_on', - 'value' => 1, - 'label' => $this->l('Enabled') - ), - array( - 'id' => 'active_off', - 'value' => 0, - 'label' => $this->l('Disabled') - ) - ), - ); - } - - $fields_form = array( - 'form' => array( - 'legend' => array( - 'title' => $this->l('Settings'), - 'icon' => 'icon-cogs' - ), - 'input' => $inputs, - 'submit' => array( - 'title' => $this->l('Save'), - 'class' => 'btn btn-default pull-right' - ) - ), - ); - - $helper = new HelperForm(); - $helper->show_toolbar = false; - $helper->table = $this->table; - $lang = new Language((int)Configuration::get('PS_LANG_DEFAULT')); - $helper->default_form_language = $lang->id; - $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0; - $this->fields_form = array(); - - $helper->identifier = $this->identifier; - $helper->submit_action = 'submitModule'; - $helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false).'&configure='.$this->name.'&tab_module='.$this->tab.'&module_name='.$this->name; - $helper->token = Tools::getAdminTokenLite('AdminModules'); - $helper->tpl_vars = array( - 'fields_value' => $this->getConfigFieldsValues(), - 'languages' => $this->context->controller->getLanguages(), - 'id_language' => $this->context->language->id - ); - - return $helper->generateForm(array($fields_form)); - } - - protected function renderThemeConfiguratorForm() - { - $id_shop = (int)$this->context->shop->id; - $items = array(); - $hooks = array(); - - $this->context->smarty->assign('htmlcontent', array( - 'admin_tpl_path' => $this->admin_tpl_path, - 'hooks_tpl_path' => $this->hooks_tpl_path, - - 'info' => array( - 'module' => $this->name, - 'name' => $this->displayName, - 'version' => $this->version, - 'psVersion' => _PS_VERSION_, - 'context' => (Configuration::get('PS_MULTISHOP_FEATURE_ACTIVE') == 0) ? 1 : ($this->context->shop->getTotalShops() != 1) ? $this->context->shop->getContext() : 1 - ) - )); - - foreach ($this->languages as $language) - { - $hooks[$language['id_lang']] = array( - 'home', - 'top', - 'left', - 'right', - 'footer' - ); - - foreach ($hooks[$language['id_lang']] as $hook) - $items[$language['id_lang']][$hook] = Db::getInstance()->ExecuteS(' + ) + ) { + if (!Tools::isEmpty($image)) { + $this->deleteImage($image); + } + + $this->context->smarty->assign('error', $this->l('An error occurred while saving data.')); + + return false; + } + + $this->context->smarty->assign('confirmation', $this->l('New item successfully added.')); + + return true; + } + + protected function uploadImage($image, $imageW = '', $imageH = '') + { + $res = false; + if (is_array($image) && (ImageManager::validateUpload($image, $this->max_image_size) === false) && ($tmpName = tempnam(_PS_TMP_IMG_DIR_, 'PS')) && move_uploaded_file($image['tmp_name'], $tmpName)) { + $salt = sha1(microtime()); + $pathinfo = pathinfo($image['name']); + $imgName = $salt.'_'.Tools::str2url($pathinfo['filename']).'.'.$pathinfo['extension']; + + if (ImageManager::resize($tmpName, dirname(__FILE__).'/img/'.$imgName, $imageW, $imageH)) { + $res = true; + } + } + + if (!$res) { + $this->context->smarty->assign('error', $this->l('An error occurred during the image upload.')); + + return false; + } + + return $imgName; + } + + protected function filterVar($value) + { + if (version_compare(_PS_VERSION_, '1.6.0.7', '>=') === true) { + return Tools::purifyHTML($value); + } else { + return filter_var($value, FILTER_SANITIZE_STRING); + } + } + + protected function updateItem() + { + $idItem = (int) Tools::getValue('item_id'); + $title = Tools::getValue('item_title'); + $content = Tools::getValue('item_html'); + + if (!Validate::isCleanHtml($title, (int) Configuration::get('PS_ALLOW_HTML_IFRAME')) || !Validate::isCleanHtml($content, (int) Configuration::get('PS_ALLOW_HTML_IFRAME'))) { + $this->context->smarty->assign('error', $this->l('Invalid content')); + + return false; + } + + $newImage = ''; + $imageW = (is_numeric(Tools::getValue('item_img_w'))) ? (int) Tools::getValue('item_img_w') : ''; + $imageH = (is_numeric(Tools::getValue('item_img_h'))) ? (int) Tools::getValue('item_img_h') : ''; + + if (!empty($_FILES['item_img']['name'])) { + if ($oldImage = Db::getInstance()->getValue('SELECT image FROM `'._DB_PREFIX_.'themeconfigurator` WHERE id_item = '.(int) $idItem)) { + if (file_exists(dirname(__FILE__).'/img/'.$oldImage)) { + @unlink(dirname(__FILE__).'/img/'.$oldImage); + } + } + + if (!$image = $this->uploadImage($_FILES['item_img'], $imageW, $imageH)) { + return false; + } + + $newImage = 'image = \''.pSQL($image).'\','; + } + + if (!Db::getInstance()->execute( + ' + UPDATE `'._DB_PREFIX_.'themeconfigurator` SET + title = \''.pSQL($title).'\', + title_use = '.(int) Tools::getValue('item_title_use').', + hook = \''.pSQL(Tools::getValue('item_hook')).'\', + url = \''.pSQL(Tools::getValue('item_url')).'\', + target = '.(int) Tools::getValue('item_target').', + '.$newImage.' + image_w = '.(int) $imageW.', + image_h = '.(int) $imageH.', + active = '.(int) Tools::getValue('item_active').', + html = \''.pSQL($this->filterVar($content), true).'\' + WHERE id_item = '.(int) Tools::getValue('item_id') + ) + ) { + if ($image = Db::getInstance()->getValue('SELECT image FROM `'._DB_PREFIX_.'themeconfigurator` WHERE id_item = '.(int) Tools::getValue('item_id'))) { + $this->deleteImage($image); + } + + $this->context->smarty->assign('error', $this->l('An error occurred while saving data.')); + + return false; + } + + $this->context->smarty->assign('confirmation', $this->l('Successfully updated.')); + + return true; + } + + protected function removeItem() + { + $idItem = (int) Tools::getValue('item_id'); + + if ($image = Db::getInstance()->getValue('SELECT image FROM `'._DB_PREFIX_.'themeconfigurator` WHERE id_item = '.(int) $idItem)) { + $this->deleteImage($image); + } + + Db::getInstance()->delete(_DB_PREFIX_.'themeconfigurator', 'id_item = '.(int) $idItem); + + if (Db::getInstance()->Affected_Rows() == 1) { + Db::getInstance()->execute( + ' + UPDATE `'._DB_PREFIX_.'themeconfigurator` + SET item_order = item_order-1 + WHERE ( + item_order > '.(int) Tools::getValue('item_order').' AND + id_shop = '.(int) $this->context->shop->id.' AND + hook = \''.pSQL(Tools::getValue('item_hook')).'\')' + ); + Tools::redirectAdmin('index.php?tab=AdminModules&configure='.$this->name.'&conf=6&token='.Tools::getAdminTokenLite('AdminModules')); + } else { + $this->context->smarty->assign('error', $this->l('Can\'t delete the slide.')); + } + } + + public function renderConfigurationForm() + { + $inputs = []; + + foreach ($this->getConfigurableModules() as $module) { + $desc = ''; + + if (isset($module['is_module']) && $module['is_module']) { + $moduleInstance = Module::getInstanceByName($module['name']); + if (Validate::isLoadedObject($moduleInstance) && method_exists($moduleInstance, 'getContent')) { + $desc = ''.$this->l('Configure').' '; + } + } + if (!$desc && isset($module['desc']) && $module['desc']) { + $desc = $module['desc']; + } + + $inputs[] = [ + 'type' => 'switch', + 'label' => $module['label'], + 'name' => $module['name'], + 'desc' => $desc, + 'values' => [ + [ + 'id' => 'active_on', + 'value' => 1, + 'label' => $this->l('Enabled'), + ], + [ + 'id' => 'active_off', + 'value' => 0, + 'label' => $this->l('Disabled'), + ], + ], + ]; + } + + $fieldsForm = [ + 'form' => [ + 'legend' => [ + 'title' => $this->l('Settings'), + 'icon' => 'icon-cogs', + ], + 'input' => $inputs, + 'submit' => [ + 'title' => $this->l('Save'), + 'class' => 'btn btn-default pull-right', + ], + ], + ]; + + $helper = new HelperForm(); + $helper->show_toolbar = false; + $helper->table = $this->table; + $lang = new Language((int) Configuration::get('PS_LANG_DEFAULT')); + $helper->default_form_language = $lang->id; + $helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') ? Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG') : 0; + $this->fields_form = []; + + $helper->identifier = $this->identifier; + $helper->submit_action = 'submitModule'; + $helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false).'&configure='.$this->name.'&tab_module='.$this->tab.'&module_name='.$this->name; + $helper->token = Tools::getAdminTokenLite('AdminModules'); + $helper->tpl_vars = [ + 'fields_value' => $this->getConfigFieldsValues(), + 'languages' => $this->context->controller->getLanguages(), + 'id_language' => $this->context->language->id, + ]; + + return $helper->generateForm([$fieldsForm]); + } + + public function getConfigFieldsValues() + { + $values = []; + foreach ($this->getConfigurableModules() as $module) { + $values[$module['name']] = $module['value']; + } + + return $values; + } + + protected function renderThemeConfiguratorForm() + { + $idShop = (int) $this->context->shop->id; + $items = []; + $hooks = []; + + $this->context->smarty->assign( + 'htmlcontent', [ + 'admin_tpl_path' => $this->admin_tpl_path, + 'hooks_tpl_path' => $this->hooks_tpl_path, + + 'info' => [ + 'module' => $this->name, + 'name' => $this->displayName, + 'version' => $this->version, + 'psVersion' => _PS_VERSION_, + 'context' => (Configuration::get('PS_MULTISHOP_FEATURE_ACTIVE') == 0) ? 1 : ($this->context->shop->getTotalShops() != 1) ? $this->context->shop->getContext() : 1, + ], + ] + ); + + foreach ($this->languages as $language) { + $hooks[$language['id_lang']] = [ + 'home', + 'top', + 'left', + 'right', + 'footer', + ]; + + foreach ($hooks[$language['id_lang']] as $hook) { + $items[$language['id_lang']][$hook] = Db::getInstance()->ExecuteS( + ' SELECT * FROM `'._DB_PREFIX_.'themeconfigurator` - WHERE id_shop = '.(int)$id_shop.' - AND id_lang = '.(int)$language['id_lang'].' + WHERE id_shop = '.(int) $idShop.' + AND id_lang = '.(int) $language['id_lang'].' AND hook = \''.pSQL($hook).'\' ORDER BY item_order ASC' - ); - } - - $this->context->smarty->assign('htmlitems', array( - 'items' => $items, - 'theme_url' => $this->context->link->getAdminLink('AdminThemeConfigurator'), - 'lang' => array( - 'default' => $this->default_language, - 'all' => $this->languages, - 'lang_dir' => _THEME_LANG_DIR_, - 'user' => $this->context->language->id - ), - 'postAction' => 'index.php?tab=AdminModules&configure='.$this->name.'&token='.Tools::getAdminTokenLite('AdminModules').'&tab_module=other&module_name='.$this->name.'', - 'id_shop' => $id_shop - )); - - $this->context->controller->addJqueryUI('ui.sortable'); - return $this->display(__FILE__, 'views/templates/admin/admin.tpl'); - } - - protected function getConfigurableModules() - { - // Construct the description for the 'Enable Live Configurator' switch - if ($this->context->shop->getBaseURL()) - { - $request = - 'live_configurator_token='.$this->getLiveConfiguratorToken() - .'&id_employee='.(int)$this->context->employee->id - .'&id_shop='.(int)$this->context->shop->id - .(Configuration::get('PS_TC_THEME') != '' ? '&theme='.Configuration::get('PS_TC_THEME') : '') - .(Configuration::get('PS_TC_FONT') != '' ? '&theme_font='.Configuration::get('PS_TC_FONT') : ''); - $url = $this->context->link->getPageLink('index', null, $id_lang = null, $request); - - $desc = '' - .$this->l('View').'
' - .$this->l('Only you can see this on your front office - your visitors will not see this tool.'); - } - else - $desc = $this->l('Only you can see this on your front office - your visitors will not see this tool.'); - - return array( - array( - 'label' => $this->l('Display links to your store\'s social accounts (Twitter, Facebook, etc.)'), - 'name' => 'blocksocial', - 'value' => (int)Validate::isLoadedObject($module = Module::getInstanceByName('blocksocial')) && $module->isEnabledForShopContext(), - 'is_module' => true, - ), - array( - 'label' => $this->l('Display your contact information'), - 'name' => 'blockcontactinfos', - 'value' => (int)Validate::isLoadedObject($module = Module::getInstanceByName('blockcontactinfos')) && $module->isEnabledForShopContext(), - 'is_module' => true, - ), - array( - 'label' => $this->l('Display social sharing buttons on the product\'s page'), - 'name' => 'socialsharing', - 'value' => (int)Validate::isLoadedObject($module = Module::getInstanceByName('socialsharing')) && $module->isEnabledForShopContext(), - 'is_module' => true, - ), - array( - 'label' => $this->l('Display the Facebook block on the home page'), - 'name' => 'blockfacebook', - 'value' => (int)Validate::isLoadedObject($module = Module::getInstanceByName('blockfacebook')) && $module->isEnabledForShopContext(), - 'is_module' => true, - ), - array( - 'label' => $this->l('Display the custom CMS information block'), - 'name' => 'blockcmsinfo', - 'value' => (int)Validate::isLoadedObject($module = Module::getInstanceByName('blockcmsinfo')) && $module->isEnabledForShopContext(), - 'is_module' => true, - ), - array( - 'label' => $this->l('Display quick view window on homepage and category pages'), - 'name' => 'quick_view', - 'value' => (int)Tools::getValue('PS_QUICK_VIEW', Configuration::get('PS_QUICK_VIEW')) - ), - array( - 'label' => $this->l('Display categories as a list of products instead of the default grid-based display'), - 'name' => 'grid_list', - 'value' => (int)Configuration::get('PS_GRID_PRODUCT'), - 'desc' => $this->l('Works only for first-time users. This setting is overridden by the user\'s choice as soon as the user cookie is set.'), - ), - array( - 'label' => $this->l('Display top banner'), - 'name' => 'blockbanner', - 'value' => (int)Validate::isLoadedObject($module = Module::getInstanceByName('blockbanner')) && $module->isEnabledForShopContext(), - 'is_module' => true, - ), - array( - 'label' => $this->l('Display logos of available payment methods'), - 'name' => 'productpaymentlogos', - 'value' => (int)Validate::isLoadedObject($module = Module::getInstanceByName('productpaymentlogos')) && $module->isEnabledForShopContext(), - 'is_module' => true, - ), - array( - 'label' => $this->l('Display Live Configurator'), - 'name' => 'live_conf', - 'value' => (int)Tools::getValue('PS_TC_ACTIVE', Configuration::get('PS_TC_ACTIVE')), - 'hint' => $this->l('This customization tool allows you to make color and font changes in your theme.'), - 'desc' => $desc - ), - array( - 'label' => $this->l('Display subcategories'), - 'name' => 'sub_cat', - 'value' => (int)Tools::getValue('PS_SET_DISPLAY_SUBCATEGORIES', Configuration::get('PS_SET_DISPLAY_SUBCATEGORIES')), - ) - ); - } - - public function getConfigFieldsValues() - { - $values = array(); - foreach ($this->getConfigurableModules() as $module) - $values[$module['name']] = $module['value']; - - return $values; - } - - public function getLiveConfiguratorToken() - { - return Tools::getAdminToken($this->name.(int)Tab::getIdFromClassName($this->name) - .(is_object(Context::getContext()->employee) ? (int)Context::getContext()->employee->id : - Tools::getValue('id_employee'))); - } - - protected function filterVar($value) - { - if (version_compare(_PS_VERSION_, '1.6.0.7', '>=') === true) - return Tools::purifyHTML($value); - else - return filter_var($value, FILTER_SANITIZE_STRING); - } + ); + } + } + + $this->context->smarty->assign( + 'htmlitems', [ + 'items' => $items, + 'theme_url' => $this->context->link->getAdminLink('AdminThemeConfigurator'), + 'lang' => [ + 'default' => $this->default_language, + 'all' => $this->languages, + 'lang_dir' => _THEME_LANG_DIR_, + 'user' => $this->context->language->id, + ], + 'postAction' => 'index.php?tab=AdminModules&configure='.$this->name.'&token='.Tools::getAdminTokenLite('AdminModules').'&tab_module=other&module_name='.$this->name.'', + 'id_shop' => $idShop, + ] + ); + + $this->context->controller->addJqueryUI('ui.sortable'); + + return $this->display(__FILE__, 'views/templates/admin/admin.tpl'); + } } diff --git a/upgrade/install-0.8.php b/upgrade/install-0.8.php index da779b2..4ca2930 100644 --- a/upgrade/install-0.8.php +++ b/upgrade/install-0.8.php @@ -10,7 +10,7 @@ function upgrade_module_0_8($object) { $upgrade_version = '0.8'; - $object->upgrade_detail[$upgrade_version] = array(); + $object->upgrade_detail[$upgrade_version] = []; // Change url type from varchar to text to avoid url length issues $query = 'ALTER TABLE `'._DB_PREFIX_.'themeconfigurator` CHANGE `url` `url` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL'; @@ -20,4 +20,4 @@ function upgrade_module_0_8($object) return (bool)!count($object->upgrade_detail[$upgrade_version]); -} \ No newline at end of file +}