* @copyright 2007-2015 PrestaShop SA * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) * International Registered Trademark & Property of PrestaShop SA */ if (!defined('_PS_VERSION_')) { exit; } use PrestaShop\PrestaShop\Core\Module\WidgetInterface; require_once _PS_MODULE_DIR_.'ps_customtext/classes/CustomText.php'; class Ps_Customtext extends Module implements WidgetInterface { // Equivalent module on PrestaShop 1.6, sharing the same data const MODULE_16 = 'blockcmsinfo'; private $templateFile; public function __construct() { $this->name = 'ps_customtext'; $this->author = 'PrestaShop'; $this->version = '4.1.0'; $this->need_instance = 0; $this->bootstrap = true; parent::__construct(); Shop::addTableAssociation('info', array('type' => 'shop')); $this->displayName = $this->trans('Custom text blocks', array(), 'Modules.Customtext.Admin'); $this->description = $this->trans('Integrates custom text blocks anywhere in your store front', array(), 'Modules.Customtext.Admin'); $this->ps_versions_compliancy = array('min' => '1.7.4.0', 'max' => _PS_VERSION_); $this->templateFile = 'module:ps_customtext/ps_customtext.tpl'; } public function install() { // Remove 1.6 equivalent module to avoid DB issues if (Module::isInstalled(self::MODULE_16)) { return $this->installFrom16Version(); } return $this->runInstallSteps() && $this->installFixtures(); } public function runInstallSteps() { return parent::install() && $this->installDB() && $this->registerHook('displayHome') && $this->registerHook('actionShopDataDuplication'); } public function installFrom16Version() { require_once _PS_MODULE_DIR_.$this->name.'/classes/MigrateData.php'; $migration = new MigrateData(); $migration->retrieveOldData(); $oldModule = Module::getInstanceByName(self::MODULE_16); if ($oldModule) { $oldModule->uninstall(); } return $this->uninstallDB() && $this->runInstallSteps() && $migration->insertData(); } public function uninstall() { return parent::uninstall() && $this->uninstallDB(); } public function installDB() { $return = true; $return &= Db::getInstance()->execute(' CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'info` ( `id_info` INT UNSIGNED NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id_info`) ) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8 ;' ); $return &= Db::getInstance()->execute(' CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'info_shop` ( `id_info` INT(10) UNSIGNED NOT NULL, `id_shop` INT(10) UNSIGNED NOT NULL, PRIMARY KEY (`id_info`, `id_shop`) ) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8 ;' ); $return &= Db::getInstance()->execute(' CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'info_lang` ( `id_info` INT UNSIGNED NOT NULL, `id_shop` INT(10) UNSIGNED NOT NULL, `id_lang` INT(10) UNSIGNED NOT NULL , `text` text NOT NULL, PRIMARY KEY (`id_info`, `id_lang`, `id_shop`) ) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8 ;' ); return $return; } public function uninstallDB($drop_table = true) { $ret = true; if ($drop_table) { $ret &= Db::getInstance()->execute('DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'info`') && Db::getInstance()->execute('DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'info_shop`') && Db::getInstance()->execute('DROP TABLE IF EXISTS `' . _DB_PREFIX_ . 'info_lang`'); } return $ret; } public function getContent() { $output = ''; if (Tools::isSubmit('saveps_customtext')) { if (!Tools::getValue('text_'.(int)Configuration::get('PS_LANG_DEFAULT'), false)) { $output = $this->displayError($this->trans('Please fill out all fields.', array(), 'Admin.Notifications.Error')) . $this->renderForm(); } else { $update = $this->processSaveCustomText(); if (!$update) { $output = '
Lorem ipsum dolor sit amet conse ctetu
Sit amet conse ctetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit.
' ), ); $shopsIds = Shop::getShops(true, null, true); $languages = Language::getLanguages(false); $text = array(); foreach ($tabTexts as $tab) { $info = new CustomText(); foreach ($languages as $lang) { $text[$lang['id_lang']] = $tab['text']; } $info->text = $text; $return &= $info->add(); } if($return && sizeof($shopsIds) > 1) { foreach ($shopsIds as $idShop) { Shop::setContext(Shop::CONTEXT_SHOP,$idShop); $info->text = $text; $return &= $info->save(); } } return $return; } /** * Add CustomText when adding a new Shop * * @param array $params */ public function hookActionShopDataDuplication($params) { if ($infoId = CustomText::getCustomTextIdByShop($params['old_id_shop'])) { Shop::setContext(Shop::CONTEXT_SHOP, $params['old_id_shop']); $oldInfo = new CustomText($infoId); Shop::setContext(Shop::CONTEXT_SHOP, $params['new_id_shop']); $newInfo = new CustomText($infoId, null, $params['new_id_shop']); $newInfo->text = $oldInfo->text; $newInfo->save(); } } }