* @copyright 2007-2015 Orange35 * @license http://creativecommons.org/licenses/by-nc-sa/3.0/ */ require_once(dirname(__FILE__).'/controllers/front/pagination.php'); if (!defined('_PS_VERSION_')) exit; class HomeFeaturedPaginated extends Module { const PS_CAT_ID = 'HOME_FEATURED_PAGINATED_CAT_ID'; const PS_CAT_ID_STANDARD_MODULE = 'HOME_FEATURED_CAT'; const PS_CAT_ID_DEFAULT_VALUE = 2; /* 2 - Home Category */ const PS_SUBMIT_ACTION_NAME = 'submitHomeFeaturedPaginated'; const PS_IPP = 'HOME_FEATURED_PAGINATED_IPP'; const PS_IPP_DEFAULT_VALUE = 4; const PS_TITLE = 'HOME_FEATURED_PAGINATED_TAB_TITLE'; const PS_TITLE_DEFAULT_VALUE = 'POPULAR'; protected static $cache_products; public function __construct() { $this->name = 'homefeaturedpaginated'; $this->tab = 'front_office_features'; $this->version = '1.0.0'; $this->author = 'Orange35'; $this->need_instance = 0; $this->ps_versions_compliancy = array('min' => '1.6', 'max' => '1.6'); $this->bootstrap = true; parent::__construct(); $this->displayName = $this->l('Paginated Featured Products'); $this->description = $this->l('Featured (popular) products with pagination'); $this->confirmUninstall = $this->l('Are you sure you want to uninstall '.$this->displayName.'?'); } public function install() { $this->clearCache('*'); $cat_id = Configuration::get(self::PS_CAT_ID_STANDARD_MODULE); if (!$cat_id) $cat_id = self::PS_CAT_ID_DEFAULT_VALUE; Configuration::updateValue(self::PS_CAT_ID, $cat_id); Configuration::updateValue(self::PS_IPP, self::PS_IPP_DEFAULT_VALUE); Configuration::updateValue(self::PS_TITLE, self::PS_TITLE_DEFAULT_VALUE); if (!parent::install() || !$this->registerHook('header') || !$this->registerHook('addproduct') || !$this->registerHook('updateproduct') || !$this->registerHook('deleteproduct') || !$this->registerHook('categoryUpdate') || !$this->registerHook('displayHomeTab') || !$this->registerHook('displayHomeTabContent')) return false; return true; } public function uninstall() { $this->clearCache('*'); if (!parent::uninstall() || !Configuration::deleteByName(self::PS_CAT_ID) || !Configuration::deleteByName(self::PS_IPP) || !Configuration::deleteByName(self::PS_TITLE)) return false; return true; } public function getContent() { $before_form_content = ''; $errors = array(); if (Tools::isSubmit(self::PS_SUBMIT_ACTION_NAME)) { $cat = Tools::getValue(self::PS_CAT_ID); if (!Validate::isInt($cat) || $cat <= 0) $errors[] = $this->l('The category ID is invalid. Please choose an existing category ID.'); $ipp = Tools::getValue(self::PS_IPP); if (!Validate::isInt($ipp) || $ipp <= 0) $errors[] = $this->l('The Items Per Page value is invalid.'); if (isset($errors) && count($errors)) $before_form_content .= $this->displayError(implode('
', $errors)); else { Tools::clearCache(Context::getContext()->smarty, $this->getTemplatePath('homefeaturedpaginated.tpl')); Configuration::updateValue(self::PS_CAT_ID, (int)$cat); Configuration::updateValue(self::PS_IPP, (int)$ipp); Configuration::updateValue(self::PS_TITLE, Tools::getValue(self::PS_TITLE)); $before_form_content .= $this->displayConfirmation($this->l('Your settings have been updated.')); } } return $before_form_content.$this->renderForm(); } public function renderForm() { $description = 'To add products to your homepage, simply add them to the corresponding product category (default: "Home").'; $cat_description = 'Choose the category ID of the products that you would like to display on homepage (default: 2 for "Home").'; $fields_form = array( 'form' => array( 'legend' => array( 'title' => $this->l('Settings'), 'icon' => 'icon-cogs' ), 'description' => $this->l($description), 'input' => array( array( 'type' => 'text', 'label' => $this->l('Title on the home tab'), 'name' => self::PS_TITLE ), array( 'type' => 'text', 'label' => $this->l('Items per page'), 'name' => self::PS_IPP, 'class' => 'fixed-width-xs', ), array( 'type' => 'text', 'label' => $this->l('Category from which to pick products to be displayed'), 'name' => self::PS_CAT_ID, 'class' => 'fixed-width-xs', 'desc' => $this->l($cat_description), ), ), 'submit' => array( 'title' => $this->l('Save'), ) ), ); $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->id = (int)Tools::getValue('id_carrier'); $helper->identifier = $this->identifier; $helper->submit_action = self::PS_SUBMIT_ACTION_NAME; $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( self::PS_IPP => Tools::getValue(self::PS_IPP, Configuration::get(self::PS_IPP)), self::PS_CAT_ID => Tools::getValue(self::PS_CAT_ID, Configuration::get(self::PS_CAT_ID)), self::PS_TITLE => Tools::getValue(self::PS_TITLE, Configuration::get(self::PS_TITLE)), ); } public function hookAddProduct() { $this->clearCache('*'); } public function hookUpdateProduct() { $this->clearCache('*'); } public function hookDeleteProduct() { $this->clearCache('*'); } public function hookCategoryUpdate() { $this->clearCache('*'); } protected function clearCache($template, $cache_id = null, $compile_id = null) { parent::_clearCache('homefeaturedpaginated.tpl'); parent::_clearCache('tab.tpl', 'homefeaturedpaginated-tab'); parent::_clearCache($template, $cache_id, $compile_id); } public function hookDisplayHeader($params) { $this->hookHeader($params); } public function hookHeader() { if (isset($this->context->controller->php_self) && $this->context->controller->php_self == 'index') $this->context->controller->addCSS(_THEME_CSS_DIR_.'product_list.css'); $this->context->controller->addCSS(($this->_path).'homefeaturedpaginated.css', 'all'); } public function hookDisplayHomeTab() { $this->smarty->assign(array('title' => Configuration::get(self::PS_TITLE))); return $this->display(__FILE__, 'tab.tpl', $this->getCacheId('homefeaturedpaginated-tab')); } public function hookDisplayHome() { $this->context->controller->addJS($this->_path.'views/js/pagination.js'); $controller = new HomefeaturedpaginatedPaginationModuleFrontController(); $language_id = (int)Context::getContext()->language->id; $controller->productSort(); $category = new Category((int)Configuration::get(self::PS_CAT_ID), $language_id); $nbProducts = $category->getProducts($language_id, null, null, 'position', null, true); $controller->pagination($nbProducts); $products = $category->getProducts($language_id, (int)$controller->p, (int)$controller->n, $controller->orderBy, $controller->orderWay); if (!$this->isCached('homefeaturedpaginated.tpl', $this->getCacheId('homefeaturedpaginated'))) { $base_template_dir = _PS_ROOT_DIR_.'/modules/'.$this->name.'/views/templates/hook/'; $this->smarty->assign( array( 'products' => $products, 'add_prod_display' => Configuration::get('PS_ATTRIBUTE_CATEGORY_DISPLAY'), 'homeSize' => Image::getSize(ImageType::getFormatedName('home')), 'nb_products' => $nbProducts, 'id_category' => (int)$category->id, 'id_category_parent' => (int)$category->id_parent, 'g_teste' => $_REQUEST, 'id' => 'homefeaturedpaginated', 'class' => 'tab-pane', 'productTpl' => $base_template_dir.'product.tpl', 'paginationTpl' => $base_template_dir.'pagination.tpl', 'innerPaginatedTpl' => $base_template_dir.'innerpaginated.tpl', ) ); } return $this->display(__FILE__, 'homefeaturedpaginated.tpl', $this->getCacheId('homefeaturedpaginated')); } public function getRenderedContent() { $controller = new HomefeaturedpaginatedPaginationModuleFrontController(); $controller->productSort(); $language_id = (int)Context::getContext()->language->id; $category = new Category((int)Configuration::get(self::PS_CAT_ID), $language_id); $nbProducts = $category->getProducts($language_id, null, null, 'position', null, true); $controller->pagination($nbProducts); $products = $category->getProducts($language_id, (int)$controller->p, (int)$controller->n, $controller->orderBy, $controller->orderWay); $base_template_dir = _PS_ROOT_DIR_.'/modules/'.$this->name.'/views/templates/hook/'; $smarty_data = array( 'products' => $products, 'add_prod_display' => Configuration::get('PS_ATTRIBUTE_CATEGORY_DISPLAY'), 'homeSize' => Image::getSize(ImageType::getFormatedName('home')), 'nb_products' => $nbProducts, 'id_category' => (int)$category->id, 'id_category_parent' => (int)$category->id_parent, 'g_teste' => $_REQUEST, 'id' => 'homefeaturedpaginated', 'class' => 'tab-pane', 'productTpl' => $base_template_dir.'product.tpl', 'paginationTpl' => $base_template_dir.'pagination.tpl', 'innerPaginatedTpl' => $base_template_dir.'innerpaginated.tpl', 'page_name' => 'index', ); $this->smarty->assign($smarty_data); return $this->display(__FILE__, 'homefeaturedpaginated.tpl'); } public function hookDisplayHomeTabContent($params) { return $this->hookDisplayHome($params); } }