bebeboutik/modules/purchaseguide/purchaseguide.php
2016-08-31 17:05:56 +02:00

252 lines
5.9 KiB
PHP

<?php
if (!defined('_PS_VERSION_'))
exit;
require_once(__DIR__.'/classes/GuideCategory.php');
class PurchaseGuide extends Module
{
const MODULE_NAME = 'purchaseguide';
public function __construct() {
$this->name = self::MODULE_NAME;
$this->tab = 'front_office_features';
$this->author = 'Antadis';
$this->version = '1.0';
parent::__construct();
$this->displayName = $this->l('Purchase Guide');
$this->description = $this->l('Purchase Guide editor for Prestashop');
}
public function install()
{
if(!parent::install()
|| !$this->installTabs()
|| !$this->createTables()
|| !$this->installImageAddon()
) {
$this->uninstall();
return FALSE;
}
return TRUE;
}
public function uninstall()
{
$this->uninstallTabs();
$this->dropTables();
$this->uninstallImageAddon();
return parent::uninstall();
}
private function installTabs()
{
$tab_params = [
'class_name' => 'AdminGuide',
'id_parent' => 0,
'tabs_i18n' => array(
'en' => 'Purchase guide',
'fr' => 'Guide d\'achat',
)
];
$id_tab_guide = $this->addTab($tab_params);
$tab_params = [
'class_name' => 'AdminGuideCategories',
'id_parent' => $id_tab_guide,
'tabs_i18n' => array(
'en' => 'Guide categories',
'fr' => 'Catégories',
)
];
$this->addTab($tab_params);
$tab_params = [
'class_name' => 'AdminGuidePosts',
'id_parent' => $id_tab_guide,
'tabs_i18n' => array(
'en' => 'Pages',
'fr' => 'Pages',
)
];
$this->addTab($tab_params);
return TRUE;
}
private function addTab($param)
{
$last_position = Db::getInstance()->getValue(
'SELECT MAX(position)
FROM `'._DB_PREFIX_.'tab`
WHERE `id_parent` = '.$param['id_parent'].'
');
Db::getInstance()->Execute('
INSERT INTO `'._DB_PREFIX_.'tab`
VALUES (DEFAULT, "'.$param['id_parent'].'", "'.$param['class_name'].'", "'.self::MODULE_NAME.'", '.($last_position+1).')
');
$tab_id = Db::getInstance()->Insert_ID();
Db::getInstance()->Execute('
INSERT INTO `'._DB_PREFIX_.'access`
VALUES (1, '.$tab_id.', 1, 1, 1, 1)
');
$langs = Db::getInstance()->ExecuteS('
SELECT `id_lang`, `iso_code`
FROM `'._DB_PREFIX_.'lang`
');
foreach($langs as $lang) {
if(isset($param['tabs_i18n'][$lang['iso_code']])) {
Db::getInstance()->Execute('
INSERT INTO `'._DB_PREFIX_.'tab_lang`
VALUES ('.$tab_id.', '.$lang['id_lang'].', "'.$param['tabs_i18n'][$lang['iso_code']].'")
');
} else {
Db::getInstance()->Execute('
INSERT INTO `'._DB_PREFIX_.'tab_lang`
VALUES ('.$tab_id.', '.$lang['id_lang'].', "'.$param['tabs_i18n']['en'].'")
');
}
}
return $tab_id;
}
private function uninstallTabs()
{
$rows = Db::getInstance()->ExecuteS('
SELECT `id_tab`
FROM `'._DB_PREFIX_.'tab`
WHERE `module` = "'.self::MODULE_NAME.'"
');
foreach ($rows as $row) {
Db::getInstance()->Execute('
DELETE FROM `'._DB_PREFIX_.'tab_lang`
WHERE `id_tab` = '.$row['id_tab']
);
Db::getInstance()->Execute('
DELETE FROM `'._DB_PREFIX_.'access`
WHERE `id_tab` = '.$row['id_tab']
);
}
Db::getInstance()->Execute('
DELETE FROM `'._DB_PREFIX_.'tab`
WHERE `module` = "'.self::MODULE_NAME.'"
');
}
private function createTables()
{
$queries[] = '
CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'guide_category` (
`id_guide_category` INTEGER NOT NULL AUTO_INCREMENT,
`id_parent` INTEGER NOT NULL,
`id_category_family` INTEGER NOT NULL,
`level_depth` tinyint(3) not null default 0,
`position` int(10) unsigned not null default 0,
`active` tinyint(1) not null default 0,
PRIMARY KEY(`id_guide_category`),
INDEX(`id_parent`)
) ENGINE=MyIsam DEFAULT CHARSET=utf8
';
$queries[] = '
CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'guide_category_lang` (
`id_guide_category` INTEGER NOT NULL,
`id_lang` INTEGER NOT NULL,
`name` varchar(128) null,
`description` text null,
`link_rewrite` varchar(128) not null,
`meta_title` varchar(128) not null,
`meta_description` varchar(255) null,
`meta_keywords` varchar(255) null,
PRIMARY KEY(`id_guide_category`, `id_lang`)
) ENGINE=MyIsam DEFAULT CHARSET=utf8
';
$queries[] = '
CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'guide_post` (
`id_guide_post` INTEGER NOT NULL AUTO_INCREMENT,
`id_guide_category` INTEGER NOT NULL,
`position` int(10) unsigned not null default 0,
`active` tinyint(1) not null default 0,
PRIMARY KEY(`id_guide_post`),
INDEX(`id_guide_category`)
) ENGINE=MyIsam DEFAULT CHARSET=utf8
';
$queries[] = '
CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'guide_post_lang` (
`id_guide_post` INTEGER NOT NULL,
`id_lang` INTEGER NOT NULL,
`meta_title` varchar(255) not null,
`meta_description` varchar(255) null,
`meta_keywords` varchar(255) null,
`content` longtext null,
`link_rewrite` varchar(128) not null,
PRIMARY KEY(`id_guide_post`, `id_lang`)
) ENGINE=MyIsam DEFAULT CHARSET=utf8
';
foreach ($queries as $query) {
if (!Db::getInstance()->Execute($query)) {
return FALSE;
}
}
return TRUE;
}
private function dropTables()
{
$queries[] = 'DROP TABLE `'._DB_PREFIX_.'guide_post_lang`';
$queries[] = 'DROP TABLE `'._DB_PREFIX_.'guide_post`';
$queries[] = 'DROP TABLE `'._DB_PREFIX_.'guide_category_lang`';
$queries[] = 'DROP TABLE `'._DB_PREFIX_.'guide_category`';
foreach ($queries as $query) {
Db::getInstance()->Execute($query);
}
}
private function installImageAddon()
{
// Add image type
Db::getInstance()->Execute('
INSERT INTO `'._DB_PREFIX_.'image_type` VALUES (
DEFAULT, "guide_category", 980, 480, 0, 0, 0, 0, 0, 0
)
');
// Add image folder
$path = GuideCategory::getImagePath();
if(!is_dir($path)) {
mkdir($path, 0775);
}
return true;
}
private function uninstallImageAddon()
{
// Remove image type
Db::getInstance()->ExecuteS('
DELETE FROM `'._DB_PREFIX_.'image_type` WHERE `name` = "guide_category"
');
}
}