2016-06-14 12:01:16 +02:00

178 lines
5.7 KiB
PHP

<?php
if (!defined('_CAN_LOAD_FILES_'))
exit;
include_once dirname(__FILE__).'/classes/AdvPictoClass.php';
class AdvPicto extends Module
{
public function __construct()
{
$this->name = 'advpicto';
$this->tab = 'front_office_features';
$this->version = '1.0';
$this->author = 'Antadis';
$this->need_instance = 0;
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('Gestionnaire de pictos');
$this->description = $this->l('Gérez vos pictos');
}
public function install()
{
return parent::install() && $this->registerHook('displayPictogrammesCategory') && $this->registerHook('displayPictogrammesProduct') && $this->createFolder() && $this->installDb() && $this->createTab();
}
public function uninstall()
{
if ( !parent::uninstall() ) {
return false;
}
$this->uninstallDb();
$this->deleteTab();
return true;
}
public function installDb()
{
$sql = array();
$sql[] =
'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'advpicto` (
`id_advpicto` int(10) unsigned NOT NULL auto_increment,
`active` int(11),
`external` int(10) unsigned NOT NULL,
`selfcategory_display` int(10) unsigned NOT NULL,
PRIMARY KEY (`id_advpicto`)
) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8';
$sql[] =
'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'advpicto_lang` (
`id_advpicto` int(10) unsigned NOT NULL,
`id_lang` int(10) unsigned NOT NULL,
`title` varchar(255) NOT NULL,
`alt` varchar(255) NOT NULL,
`link` varchar(255),
`content` text,
PRIMARY KEY (`id_advpicto`,`id_lang`)
) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8';
$sql[] =
'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'advpicto_shop` (
`id_advpicto` int(10) unsigned NOT NULL,
`id_shop` int(10) unsigned NOT NULL,
PRIMARY KEY (`id_advpicto`,`id_shop`)
) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8';
$sql[] =
'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'advpicto_category` (
`id_advpicto` int(10) unsigned NOT NULL,
`id_category` int(10) unsigned NOT NULL,
PRIMARY KEY (`id_advpicto`,`id_category`)
) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8';
$sql[] =
'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'advpicto_product` (
`id_advpicto` int(10) unsigned NOT NULL,
`id_product` int(10) unsigned NOT NULL,
PRIMARY KEY (`id_advpicto`,`id_product`)
) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8';
$sql[] =
'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'advpicto_product_shop` (
`id_advpicto_product` int(11) NOT NULL,
`id_shop` int(11) NOT NULL,
PRIMARY KEY (`id_advpicto_product`,`id_shop`)
) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=utf8';
return $this->uninstallDb() && $this->updateDb($sql);
}
public function uninstallDb()
{
$sql = array();
$sql[] = 'DROP TABLE IF EXISTS `'._DB_PREFIX_.'advpicto` ;';
$sql[] = 'DROP TABLE IF EXISTS `'._DB_PREFIX_.'advpicto_lang` ;';
$sql[] = 'DROP TABLE IF EXISTS `'._DB_PREFIX_.'advpicto_shop` ;';
$sql[] = 'DROP TABLE IF EXISTS `'._DB_PREFIX_.'advpicto_category` ;';
$sql[] = 'DROP TABLE IF EXISTS `'._DB_PREFIX_.'advpicto_product` ;';
return $this->updateDb($sql);
}
public function updateDb( $sqls )
{
foreach ( $sqls as $sql ) {
if ( !Db::getInstance()->execute($sql) ) {
return false;
}
}
return true;
}
public function createFolder()
{
$img_dir = _PS_IMG_DIR_ . 'picto';
$folder = is_dir($img_dir);
if (!$folder)
{
$folder = mkdir($img_dir, 0755, true);
}
return $folder;
}
public function createTab()
{
$languages = Language::getLanguages();
$new_tab = new Tab();
$new_tab->class_name = 'AdminAdvPicto';
$new_tab->id_parent = Tab::getCurrentParentId();
$new_tab->module = $this->name;
foreach ( $languages as $language ) {
$new_tab->name[$language['id_lang']] = 'Pictos';
}
return $new_tab->add();
}
public function deleteTab()
{
$idTab = Tab::getIdFromClassName('AdminAdvPicto');
if ( $idTab ) {
$tab = new Tab($idTab);
$tab->delete();
}
}
public function hookDisplayPictogrammesCategory($params)
{
$id_product = $params['id_product'];
$id_category = Tools::getValue('id_category');
$pictos = AdvPictoClass::getPictosForProductList($id_product, $id_category);
$this->smarty->assign('pictos', $pictos);
return $this->display(__FILE__, 'product-list.tpl');
}
public function hookDisplayPictogrammesProduct($params)
{
$id_product = $params['id_product'];
$is_listing = $params['is_listing'];
$pictos = AdvPictoClass::getPictosForProductDetails($id_product);
$this->smarty->assign('pictos', $pictos);
if($is_listing)
{
return $this->display(__FILE__, 'category.tpl');
}
else {
return $this->display(__FILE__, 'product.tpl');
}
}
}