name = 'roy_specials'; $this->author = 'RoyThemes'; $this->version = '1.0.0'; $this->need_instance = 0; $this->ps_versions_compliancy = array( 'min' => '1.7.0.0', 'max' => _PS_VERSION_ ); $this->bootstrap = true; parent::__construct(); $this->displayName = $this->trans('Roy Specials block'); $this->description = $this->trans('Displays your products that are currently on sale in a dedicated block.'); $this->templateFile = 'module:roy_specials/views/templates/hook/roy_specials.tpl'; $this->templateFileCol = 'module:roy_specials/views/templates/hook/roy_specials_col.tpl'; } public function install() { $this->_clearCache('*'); Configuration::updateValue('ROYSPECIALS_SPECIALS_NBR', 8); return parent::install() && $this->registerHook('actionProductAdd') && $this->registerHook('actionProductUpdate') && $this->registerHook('actionProductDelete') && $this->registerHook('actionObjectSpecificPriceCoreDeleteAfter') && $this->registerHook('actionObjectSpecificPriceCoreAddAfter') && $this->registerHook('actionObjectSpecificPriceCoreUpdateAfter') && $this->registerHook('displayHome') && $this->registerHook('displayLeftColumn') && $this->registerHook('displayRightColumn'); } public function uninstall() { $this->_clearCache('*'); return parent::uninstall(); } public function hookActionProductAdd($params) { $this->_clearCache('*'); } public function hookActionProductUpdate($params) { $this->_clearCache('*'); } public function hookActionProductDelete($params) { $this->_clearCache('*'); } public function hookActionObjectSpecificPriceCoreDeleteAfter($params) { $this->_clearCache('*'); } public function hookActionObjectSpecificPriceCoreAddAfter($params) { $this->_clearCache('*'); } public function hookActionObjectSpecificPriceCoreUpdateAfter($params) { $this->_clearCache('*'); } public function _clearCache($template, $cache_id = null, $compile_id = null) { parent::_clearCache($this->templateFile); parent::_clearCache($this->templateFileCol); } public function getContent() { $output = ''; if (Tools::isSubmit('submitSpecials')) { Configuration::updateValue('ROYSPECIALS_SPECIALS_NBR', (int)Tools::getValue('ROYSPECIALS_SPECIALS_NBR')); $this->_clearCache('*'); $output .= $this->displayConfirmation($this->trans('The settings have been updated.', array(), 'Admin.Notifications.Success')); } return $output.$this->renderForm(); } public function renderForm() { $fields_form = array( 'form' => array( 'legend' => array( 'title' => $this->trans('Settings', array(), 'Admin.Global'), 'icon' => 'icon-cogs' ), 'input' => array( array( 'type' => 'text', 'label' => $this->trans('Products to display', array(), 'Modules.Specials.Admin'), 'name' => 'ROYSPECIALS_SPECIALS_NBR', 'class' => 'fixed-width-xs', 'desc' => $this->trans('Define the number of products to be displayed in this block on home page.', array(), 'Modules.Specials.Admin'), ), ), 'submit' => array( 'title' => $this->trans('Save', array(), 'Admin.Actions'), ), ), ); $lang = new Language((int)Configuration::get('PS_LANG_DEFAULT')); $helper = new HelperForm(); $helper->show_toolbar = false; $helper->table = $this->table; $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; $helper->identifier = $this->identifier; $helper->submit_action = 'submitSpecials'; $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)); } public function getConfigFieldsValues() { return array( 'ROYSPECIALS_SPECIALS_NBR' => Tools::getValue('ROYSPECIALS_SPECIALS_NBR', Configuration::get('ROYSPECIALS_SPECIALS_NBR')), ); } public function renderWidget($hookName = null, array $configuration = []) { if (!$this->isCached($this->templateFile, $this->getCacheId('roy_specials'))) { $variables = $this->getWidgetVariables($hookName, $configuration); if (empty($variables)) { return false; } $this->smarty->assign($variables); } return $this->fetch($this->templateFile, $this->getCacheId('roy_specials')); } public function hookDisplayLeftColumn($params) { global $smarty; if (!$this->active) { return; } $products = $this->getSpecialProducts(); $smarty->assign(array( 'products' => $products, 'allSpecialProductsLink' => Context::getContext()->link->getPageLink('prices-drop') )); return $this->fetch('module:roy_specials/views/templates/hook/roy_specials_col.tpl'); } public function hookDisplayRightColumn($params) { global $smarty; if (!$this->active) { return; } $products = $this->getSpecialProducts(); $smarty->assign(array( 'products' => $products, 'allSpecialProductsLink' => Context::getContext()->link->getPageLink('prices-drop') )); return $this->fetch('module:roy_specials/views/templates/hook/roy_specials_col.tpl'); } public function getWidgetVariables($hookName = null, array $configuration = []) { $products = $this->getSpecialProducts(); if (!empty($products)) { return array( 'products' => $products, 'allSpecialProductsLink' => Context::getContext()->link->getPageLink('prices-drop'), ); } return false; } private function getSpecialProducts() { $products = Product::getPricesDrop( (int)Context::getContext()->language->id, 0, (int)Configuration::get('ROYSPECIALS_SPECIALS_NBR') ); $assembler = new ProductAssembler($this->context); $presenterFactory = new ProductPresenterFactory($this->context); $presentationSettings = $presenterFactory->getPresentationSettings(); $presenter = new ProductListingPresenter( new ImageRetriever( $this->context->link ), $this->context->link, new PriceFormatter(), new ProductColorsRetriever(), $this->context->getTranslator() ); $products_for_template = array(); if (is_array($products)) { foreach ($products as $rawProduct) { $products_for_template[] = $presenter->present( $presentationSettings, $assembler->assembleProduct($rawProduct), $this->context->language ); } } return $products_for_template; } }