* @copyright 2007-2014 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\Adapter\Image\ImageRetriever; use PrestaShop\PrestaShop\Adapter\Product\PriceFormatter; use PrestaShop\PrestaShop\Core\Product\ProductListingPresenter; use PrestaShop\PrestaShop\Adapter\Product\ProductColorsRetriever; include_once(dirname(__FILE__).'/classes/StSpecialSliderClass.php'); include_once(_PS_MODULE_DIR_.'stthemeeditor/classes/BaseProductsSlider.php'); class StSpecialSlider extends BaseProductsSlider { protected static $cache_products = array(); public $_prefix_st = 'ST_SPECIAL_'; public $_prefix_stsn = 'STSN_SPECIAL_'; function __construct() { $this->name = 'stspecialslider'; $this->version = '1.1.0'; $this->title = $this->getTranslator()->trans('Specials', array(), 'Shop.Theme.Panda'); // Front office title block. $this->url_entity = 'prices-drop'; $this->displayName = $this->getTranslator()->trans('Special Products Slider', array(), 'Modules.Stspecialslider.Admin'); $this->description = $this->getTranslator()->trans('Display special products slider on hompage.', array(), 'Modules.Stspecialslider.Admin'); parent::__construct(); } function install() { if (!parent::install() || !$this->installDB() || !$this->registerHook('displayFooter') || !$this->registerHook('displayAdminProductPriceFormFooter') ) return false; $this->clearSliderCache(); return true; } public function uninstall() { $this->clearSliderCache(); if (!parent::uninstall() || !$this->uninstallDB() ) return false; return true; } private function installDB() { return Db::getInstance()->execute(' CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'st_special_product` ( `id_product` int(10) NOT NULL, `id_shop` int(11) NOT NULL, PRIMARY KEY (`id_product`,`id_shop`), KEY `id_shop` (`id_shop`) ) ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8 ;'); } private function uninstallDB() { return Db::getInstance()->execute('DROP TABLE IF EXISTS `'._DB_PREFIX_.'st_special_product`'); } public function getContent() { if (Tools::getValue('act') == 'gsp' && Tools::getValue('ajax')==1) { if(!$q = Tools::getValue('q')) die; $result = $this->getAllSpecialProducts($q, Tools::getValue('excludeIds')); foreach ($result AS $value) echo trim($value['name']).'['.trim($value['reference']).']|'.(int)($value['id_product'])."\n"; die; } if (Tools::getValue('act') == 'setstspecial' && Tools::getValue('ajax')==1) { $ret = array('r'=>false,'msg'=>''); if(!$id_product = Tools::getValue('id_product')) $ret['msg'] = $this->getTranslator()->trans('Product ID error', array(), 'Modules.Stspecialslider.Admin'); else { if (StSpecialSliderClass::setByProductId($id_product, (int)Tools::getValue('fl'), $this->context->shop->id)) { $ret['r'] = true; $ret['msg'] = $this->getTranslator()->trans('Successful update', array(), 'Admin.Theme.Panda'); $this->clearSliderCache(); } else $ret['msg'] = $this->getTranslator()->trans('Error occurred when updating', array(), 'Admin.Theme.Panda'); } echo json_encode($ret); die; } $this->context->controller->addJS($this->_path.'views/js/admin.js'); return parent::getContent(); } protected function saveForm() { if (isset($_POST['savesliderform'])) { StSpecialSliderClass::deleteByShop((int)$this->context->shop->id); $res = true; if($id_product= Tools::getValue('id_product')) foreach($id_product AS $value) { $res &= Db::getInstance()->insert('st_special_product', array( 'id_product' => (int)$value, 'id_shop' => (int)$this->context->shop->id, )); } if ($res) { parent::saveForm(); } } } public function initFieldsForm() { parent::initFieldsForm(); $custom_fields['products'] = array( 'type' => 'text', 'label' => $this->getTranslator()->trans('Specific special products:', array(), 'Modules.Stspecialslider.Admin'), 'name' => 'products', 'autocomplete' => false, 'class' => 'fixed-width-xxl', 'desc' => '', ); $this->fields_form[0]['form']['input'] = array_merge($custom_fields, $this->fields_form[0]['form']['input']); } public function hookDisplayAdminProductPriceFormFooter($params) { $this->smarty->assign(array( 'id_product' => Tools::getValue('id_product'), 'currentIndex' => $this->context->link->getAdminLink('AdminModules', true).'&configure='.$this->name, 'checked' => StSpecialSliderClass::exists(Tools::getValue('id_product') ,$this->context->shop->id) )); return $this->display(__FILE__, 'views/templates/admin/stspecialslider.tpl'); } public function getProducts($ext='') { if ($ext && strpos($ext, '_') === false) { $ext = '_'.strtoupper($ext); } if (isset(self::$cache_products[$ext]) && self::$cache_products[$ext]) return self::$cache_products[$ext]; if (!$products = StSpecialSliderClass::getByShop($this->context->shop->id)) { $nbr = Configuration::get($this->_prefix_st.'NBR'.$ext); if(!$nbr) $nbr = 8; $order_by = 'price'; $order_way = 'ASC'; $soby = (int)Configuration::get($this->_prefix_st.'SOBY'.$ext); if (key_exists($soby, $this->sort_by)) { $order_by = $this->sort_by[$soby]['orderBy']; $order_way = $this->sort_by[$soby]['orderWay']; } $products = Product::getPricesDrop( (int)Context::getContext()->language->id, 0, $nbr, false, $order_by, $order_way ); } $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) { $prod = new Product((int)$rawProduct['id_product']); if (!$prod->id || !$prod->active) { continue; } //why? the present function would add images to the data. if (!key_exists('id_image', $rawProduct)) { $cover = Product::getCover((int)$rawProduct['id_product']); $rawProduct['id_image'] = $cover['id_image']; $rawProduct['id_image'] = Product::defineProductImage($rawProduct, $this->context->language->id); } // $products_for_template[] = $presenter->present( $presentationSettings, $assembler->assembleProduct($rawProduct), $this->context->language ); } } return self::$cache_products[$ext] = $products_for_template; } public function hookActionObjectSpecificPriceCoreDeleteAfter($params) { $this->clearSliderCache(); } public function hookActionObjectSpecificPriceCoreAddAfter($params) { $this->clearSliderCache(); } public function hookActionObjectSpecificPriceCoreUpdateAfter($params) { $this->clearSliderCache(); } public function getConfigFieldsValues() { $fields_values = array( 'products' => '', ); $products_html = ''; foreach(StSpecialSliderClass::getByShop((int)$this->context->shop->id) AS $value) { $product = new Product($value['id_product'], false, Context::getContext()->language->id); $products_html .= '