118 lines
2.9 KiB
PHP

<?php
if (!defined('_CAN_LOAD_FILES_'))
exit;
class FeaturedCategories extends Module
{
const CATEGORY_PARENT = 22;
public function __construct()
{
$this->name = 'featuredcategories';
if (version_compare(_PS_VERSION_, '1.4.0.0') >= 0)
$this->tab = 'front_office_features';
else
$this->tab = 'Blocks';
$this->version = '1.0';
$this->author = 'Antadis';
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('Bloc liste des catégories');
$this->description = $this->l('Affiche une liste des catégories/sous-catégories');
$this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_);
}
public function install()
{
return parent::install() && $this->registerHook('displaySubCategories');
}
public function uninstall()
{
// Delete configuration
return parent::uninstall();
}
public function hookHeader($params)
{
}
public function getBlock($id_category)
{
$categories = self::getCategories($id_category);
return $categories;
}
static function getCategories($id_parent, $limit = null)
{
$context = Context::getContext();
$query = '
SELECT DISTINCT c.`id_category`, cl.`name`, c.`home`, cl.`description_univers`, cl.`liste_icons_univers`
FROM `'._DB_PREFIX_.'category` c
LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON cl.`id_category` = c.`id_category`
LEFT JOIN `'._DB_PREFIX_.'category_shop` cs ON cs.`id_category` = c.`id_category`
WHERE c.`id_parent` = '.$id_parent.'
AND cl.`id_lang` = '.$context->language->id.'
AND cs.`id_shop` = '.$context->shop->id.'
AND cl.`id_shop` = '.$context->shop->id.'
ORDER BY cs.`position` ASC
';
if ($limit) {
$query .= 'LIMIT 0, '.$limit;
}
$categories = Db::getInstance()->executeS($query);
if (!$categories) {
return false;
}
foreach ($categories as $key => $category) {
if($categories[$key]['home'] == 1) {
$categories[$key]['hasImg'] = file_exists(_PS_CAT_IMG_DIR_ . 'home/' . $category['id_category'] . '.jpg');
$categories[$key]['subcategories'] = Category::getChildren($category['id_category'], $context->language->id);
} else {
unset($categories[$key]);
}
}
return $categories;
}
public function hookDisplaySubCategories($params)
{
$id_category = Tools::getValue('id_category', FeaturedCategories::CATEGORY_PARENT);
if (!$this->isCached('featuredcategories.tpl', $this->getCacheId())) {
$blocks = $this->getBlock($id_category);
$this->context->smarty->assign(array(
'blocks' => $blocks,
));
}
return $this->display(__FILE__, 'featuredcategories.tpl', $this->getCacheId());
}
public function hookActionCategoryAdd($params)
{
$this->_clearCache('featuredcategories.tpl');
}
public function hookActionCategoryDelete($params)
{
$this->_clearCache('featuredcategories.tpl');
}
public function hookActionCategoryUpdate($params)
{
$this->_clearCache('featuredcategories.tpl');
}
}