* @copyright 2017 Agence Bondaty and Co * @license single * Agence Bondaty and Co */ if (!defined('_PS_VERSION_')) { exit; } class Checkage extends Module { protected $html = ''; public function __construct() { $this->name = 'checkage'; $this->tab = 'front_office_features'; $this->version = '1.2.0'; $this->author = 'Bondaty and Co'; $this->module_key = '09df8563ef7512d79032862d2c9bc346'; $this->need_instance = 0; parent::__construct(); $this->displayName = $this->l('Checking age for access to the shop'); $this->description = $this->l('Checking age for full shop access or certain categories only.'); $this->ps_versions_compliancy = array('min' => '1.7.0.0', 'max' => _PS_VERSION_); include_once dirname(__FILE__).'/classes/CheckageClass.php'; $this->bootstrap = true; } public function check() { $checkOK = $this->context->cookie->checkOK; if ($checkOK) { return '1'; } else { return '0'; } } public function install() { return parent::install() && $this->registerHook('displayHeader') && $this->registerHook('displayTop') && $this->installDB(); } public function installDB() { $res = Db::getInstance()->execute(' CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'checkage` ( `id_checkage` int(10) unsigned NOT NULL auto_increment, `id_shop` int(10) unsigned NOT NULL, `id_restriction` text NOT NULL, `age_restriction` varchar(3) NOT NULL, `format_date` int(10) NOT NULL, `show_logo` int(10) NOT NULL, `all_restriction` int(10) NOT NULL, `best_sales` int(10) NOT NULL, `new_products` int(10) NOT NULL, `discount` int(10) NOT NULL, `manufacturer` int(10) NOT NULL, `supplier` int(10) NOT NULL, `cms` int(10) NOT NULL, PRIMARY KEY (`id_checkage`)) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8'); if ($res) { $res &= Db::getInstance()->execute(' CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'checkage_lang` ( `id_checkage` int(10) unsigned NOT NULL, `id_lang` int(10) unsigned NOT NULL, `checkage_message` text NOT NULL, PRIMARY KEY (`id_checkage`, `id_lang`)) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8'); } if ($res) { foreach (Shop::getShops(false) as $shop) { $res &= $this->createExampleCheckage($shop['id_shop']); } } if (!$res) { $res &= $this->uninstall(); } return $res; } private function createExampleCheckage($id_shop) { $checkage = new CheckageClass(); $checkage->id_shop = (int)$id_shop; $checkage->id_restriction = ''; $checkage->age_restriction = '18'; $checkage->format_date = '1'; $checkage->show_logo = '1'; $checkage->all_restriction = '1'; $checkage->best_sales = '0'; $checkage->new_products = '0'; $checkage->discount = '0'; $checkage->manufacturer = '0'; $checkage->supplier = '0'; $checkage->cms = '0'; foreach (Language::getLanguages(false) as $lang) { $checkage->checkage_message[$lang['id_lang']] = '
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore...
'; } return $checkage->add(); } public function uninstall() { $res = Db::getInstance()->execute('DROP TABLE IF EXISTS `'._DB_PREFIX_.'checkage`'); $res &= Db::getInstance()->execute('DROP TABLE IF EXISTS `'._DB_PREFIX_.'checkage_lang`'); if (!$res || !parent::uninstall()) { return false; } return true; } public function initForm() { $languages = Language::getLanguages(false); foreach ($languages as $k => $language) { $languages[$k]['is_default'] = (int)($language['id_lang'] == Configuration::get('PS_LANG_DEFAULT')); } $id_shop = (int)$this->context->shop->id; $sql = 'SELECT `id_restriction` FROM `'._DB_PREFIX_.'checkage` WHERE `id_shop` ="'.(int)$id_shop.'"'; $id_restriction = Db::getInstance()->getValue($sql); $rootCategory_id = (Shop::getContext() == Shop::CONTEXT_SHOP ? Category::getRootCategory()->id_category : 0); $id_rcategories = new HelperTreeCategories('id_restriction'); $id_rcategories->setRootCategory($rootCategory_id)->setUseCheckBox(true)->setInputName('rcategories')->setSelectedCategories(explode('-', $id_restriction)); $helper = new HelperForm(); $helper->module = $this; $helper->name_controller = 'checkage'; $helper->identifier = $this->identifier; $helper->token = Tools::getAdminTokenLite('AdminModules'); $helper->languages = $languages; $helper->currentIndex = AdminController::$currentIndex.'&configure='.$this->name; $helper->default_form_language = (int)Configuration::get('PS_LANG_DEFAULT'); $helper->allow_employee_form_lang = true; $helper->toolbar_scroll = true; $helper->toolbar_btn = $this->initToolbar(); $helper->title = $this->displayName; $helper->submit_action = 'submitUpdateCheckage'; $this->fields_form[0]['form'] = array( 'tinymce' => true, 'legend' => array( 'title' => $this->l('Settings'), 'icon' => 'icon-cogs' ), 'submit'=> array( 'name' => 'submitUpdateCheckage', 'title' => $this->l('Save'), ), 'input' => array( array( 'type' => 'text', 'label' => $this->l('Age'), 'name' => 'age_restriction', 'class' => 'fixed-width-sm', 'desc' => $this->l('Minimum age'), ), array( 'type' => 'switch', 'is_bool' => true, 'label' => $this->l('Show the logo of the shop'), 'name' => 'show_logo', 'desc' => $this->l('Yes = Show logo / No = Disable logo'), 'values' => array( array( 'id' => 'show_logo_on', 'value' => 1, ), array( 'id' => 'show_logo_off', 'value' => 0, ), ), ), array( 'type' => 'textarea', 'label' => $this->l('Additional message'), 'name' => 'checkage_message', 'lang' => true, 'autoload_rte' => true, 'desc' => $this->l('Appears on top of the form under the logo of the shop'), ), array( 'type' => 'radio', 'is_bool' => true, 'label' => $this->l('Date format'), 'name' => 'format_date', 'desc' => $this->l('Choice the date format to display'), 'values' => array( array( 'id' => 'format_date', 'label' => $this->l('DD-MM-YYYY'), 'value' => 1, ), array( 'id' => 'format_date', 'label' => $this->l('MM-DD-YYYY'), 'value' => 2, ), array( 'id' => 'format_date', 'label' => $this->l('YYYY-MM-DD'), 'value' => 3, ), ), ), array( 'type' => 'switch', 'is_bool' => true, 'label' => $this->l('Checking the age on the whole shop'), 'name' => 'all_restriction', 'desc' => $this->l('Yes = Checking the age on the whole shop (You do not need to select a category or page) / No = Select a category or page for checking'), 'values' => array( array( 'id' => 'all_restriction_on', 'value' => 1, ), array( 'id' => 'all_restriction_off', 'value' => 0, ), ), ), array( 'type' => 'categories_select', 'label' => $this->l('Select a category'), 'name' => 'id_restriction', 'category_tree' => $id_rcategories->render(), 'desc' => $this->l('For verification only on certain categories check the desired categories, otherwise uncheck all categories for a check on the whole store'), ), array( 'type' => 'switch', 'is_bool' => true, 'label' => $this->l('Checking the age on the best sales'), 'name' => 'best_sales', 'desc' => $this->l('Yes = Checking the age on the best sales / No = No checking'), 'values' => array( array( 'id' => 'best_sales_on', 'value' => 1, ), array( 'id' => 'best_sales_off', 'value' => 0, ), ), ), array( 'type' => 'switch', 'is_bool' => true, 'label' => $this->l('Checking the age on the new products'), 'name' => 'new_products', 'desc' => $this->l('Yes = Checking the age on the new products / No = No checking'), 'values' => array( array( 'id' => 'new_products_on', 'value' => 1, ), array( 'id' => 'new_products_off', 'value' => 0, ), ), ), array( 'type' => 'switch', 'is_bool' => true, 'label' => $this->l('Checking the age on the prices drop'), 'name' => 'discount', 'desc' => $this->l('Yes = Checking the age on the prices drop / No = No checking'), 'values' => array( array( 'id' => 'discount_on', 'value' => 1, ), array( 'id' => 'discount_off', 'value' => 0, ), ), ), array( 'type' => 'switch', 'is_bool' => true, 'label' => $this->l('Checking the age on the manufacturer'), 'name' => 'manufacturer', 'desc' => $this->l('Yes = Checking the age on the manufacturer / No = No checking'), 'values' => array( array( 'id' => 'manufacturer_on', 'value' => 1, ), array( 'id' => 'manufacturer_off', 'value' => 0, ), ), ), array( 'type' => 'switch', 'is_bool' => true, 'label' => $this->l('Checking the age on the supplier'), 'name' => 'supplier', 'desc' => $this->l('Yes = Checking the age on the supplier / No = No checking'), 'values' => array( array( 'id' => 'supplier_on', 'value' => 1, ), array( 'id' => 'supplier_off', 'value' => 0, ), ), ), array( 'type' => 'switch', 'is_bool' => true, 'label' => $this->l('Checking the age on the cms pages'), 'name' => 'cms', 'desc' => $this->l('Yes = Checking the age on the cms pages / No = No checking'), 'values' => array( array( 'id' => 'cms_on', 'value' => 1, ), array( 'id' => 'cms_off', 'value' => 0, ), ), ), ), ); return $helper; } private function initToolbar() { $this->toolbar_btn['save'] = array( 'href' => '#', 'desc' => $this->l('Save') ); return $this->toolbar_btn; } public function getContent() { $this->html = ''; $this->context->controller->addjQueryPlugin('hoverIntent'); $this->context->controller->addJS($this->_path.'views/js/admin-app.js'); $this->postProcess(); $helper = $this->initForm(); $id_shop = (int)$this->context->shop->id; $checkage = CheckageClass::getByIdShop((int)$id_shop); if (!$checkage) { $this->createExampleCheckage((int)$id_shop); } foreach ($this->fields_form[0]['form']['input'] as $input) { $helper->fields_value[$input['name']] = $checkage->{$input['name']}; } if ($checkage->all_restriction == '1') { $this->html .= $this->allRestriction(); } else { $this->html .= $this->selectRestriction(); } $this->html .= $helper->generateForm($this->fields_form); return $this->html; } public function postProcess() { $errors = array(); if (Tools::isSubmit('submitUpdateCheckage')) { if (!Validate::isInt(Tools::getValue('age_restriction')) || (Tools::getValue('age_restriction') <= 0)) { $errors[] = $this->l('Invalid minimum age.'); $this->html .= $this->displayError($errors); } else { $id_shop = (int)$this->context->shop->id; $checkage = CheckageClass::getByIdShop((int)$id_shop); $rcategories = Tools::getValue('rcategories', array()); $id_restriction = implode('-', $rcategories); $checkage->copyFromPost(); $checkage->update(); $sql = 'UPDATE `'._DB_PREFIX_.'checkage` SET `id_restriction` = "'.(int)$id_restriction.'" WHERE `id_shop` = "'.(int)$id_shop.'"'; Db::getInstance()->execute($sql); $this->html .= $this->displayConfirmation($this->l('Settings updated successfully.')); } } } public function allRestriction() { $tpl = $this->context->smarty->createTemplate(dirname(__FILE__).'/views/templates/admin/all-restriction.tpl'); return $tpl->fetch(); } public function selectRestriction() { $tpl = $this->context->smarty->createTemplate(dirname(__FILE__).'/views/templates/admin/select-restriction.tpl'); return $tpl->fetch(); } public function getPages() { $id_shop = (int)$this->context->shop->id; $checkage = CheckageClass::getByIdShop((int)$id_shop); $checkage = new CheckageClass((int)$checkage->id, (int)$this->context->language->id); $page_name = Dispatcher::getInstance()->getController(); $page_restriction = '1'; if ($page_name == 'pricesdrop' && $checkage->discount == '1') { return $page_restriction; } elseif ($page_name == 'newproducts' && $checkage->new_products == '1') { return $page_restriction; } elseif ($page_name == 'bestsales' && $checkage->best_sales == '1') { return $page_restriction; } elseif ($page_name == 'manufacturer' && $checkage->manufacturer == '1') { return $page_restriction; } elseif ($page_name == 'supplier' && $checkage->supplier == '1') { return $page_restriction; } elseif ($page_name == 'cms' && $checkage->cms == '1') { return $page_restriction; } } public function getRestrictions() { $id_shop = (int)$this->context->shop->id; $id_category = (int)Tools::getValue('id_category'); $id_product = (int)Tools::getValue('id_product'); $product = new Product((int)$id_product); $show_restriction = '1'; $sql = 'SELECT `id_restriction` FROM `'._DB_PREFIX_.'checkage` WHERE `id_shop` ="'.(int)$id_shop.'"'; $ids_restrictions = Db::getInstance()->getValue($sql); $id_restriction = explode('-', $ids_restrictions); foreach ($id_restriction as $id_restriction) { $restriction = $id_restriction; if ($restriction && ($restriction == $id_category or $restriction == $product->id_category_default)) { return $show_restriction; } } } public function hookdisplayTop($params) { $id_shop = (int)$this->context->shop->id; $checkage = CheckageClass::getByIdShop((int)$id_shop); $checkage = new CheckageClass((int)$checkage->id, (int)$this->context->language->id); $id_restriction = $this->getRestrictions(); $page_restriction = $this->getPages(); $checkOK = $this->check(); if (Tools::usingSecureMode()) { $urlbase = Tools::getShopDomainSsl(true); } else { $urlbase = Tools::getShopDomain(true); } $this->smarty->assign(array( 'checkage' => $checkage, 'id_restriction' => (int)$id_restriction, 'page_restriction' => (int)$page_restriction, 'checkOK' => (int)$checkOK, 'token' => Tools::getToken(false), 'url' => $urlbase.__PS_BASE_URI__, 'urlmodule' => $this->_path, 'default_lang' => (int)$this->context->language->id, 'id_lang' => (int)$this->context->language->id, )); return $this->display(__FILE__, 'checkage.tpl'); } public function hookdisplayHeader($params) { $this->context->controller->registerStylesheet('modules-checkage', 'modules/'.$this->name.'/views/css/checkage.css', ['media' => 'all', 'priority' => 150]); } }