117 lines
3.6 KiB
PHP
Raw Normal View History

<?php
if (!defined('_CAN_LOAD_FILES_'))
exit;
require_once dirname(__FILE__). '/classes/seourl.php';
class SeoUrl extends Module {
public function __construct() {
$this->name = 'seourl';
$this->tab = 'front_office_features';
$this->version = '1.0';
$this->author = 'Antadis';
$this->need_instance = 0;
parent::__construct();
$this->displayName = $this->l('SEO Url');
$this->description = $this->l('Manage Seo Url');
$this->secure_key = Tools::encrypt($this->name);
$this->confirmUninstall = $this->l('Are you sure ?');
}
public function install() {
if (Shop::isFeatureActive())
Shop::setContext(Shop::CONTEXT_ALL);
if (!parent::install() || !$this->registerHook('actionProductDelete')
|| !$this->registerHook('seoUrlCmsCategory')
|| !$this->registerHook('actionObjectCmsUpdateAfter')
|| !$this->registerHook('actionObjectCmsDeleteAfter')
|| !$this->registerHook('displayHeader')
|| !$this->registerHook('actionCategoryUpdate')
|| !$this->registerHook('addFeatureProduct')
|| !$this->registerHook('actionCategoryAdd')
|| !$this->registerHook('actionProductSave') )
return false;
$res = Db::getInstance()->execute('
CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'seourl` (
`id_seourl` int(11) NOT NULL AUTO_INCREMENT,
`type` tinyint(10) NOT NULL,
`id_element` int(11) NOT NULL,
PRIMARY KEY (`id_seourl`))
ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8');
if (!$res)
return false;
$res = Db::getInstance()->execute('
CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'seourl_lang` (
`id_seourl` int(11) NOT NULL,
`id_lang` int(11) NOT NULL,
`id_shop` int(11) NOT NULL,
`link_rewrite` VARCHAR(255) NOT NULL,
`redirect` text,
`canonical` text,
PRIMARY KEY (`id_seourl`, `id_lang`))
ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8');
if (!$res)
return false;
$res = Db::getInstance()->execute('
CREATE TABLE IF NOT EXISTS `'._DB_PREFIX_.'seourl_shop` (
`id_seourl` int(11) NOT NULL,
`id_shop` int(11) NOT NULL,
PRIMARY KEY (`id_seourl`, `id_shop`))
ENGINE='._MYSQL_ENGINE_.' DEFAULT CHARSET=utf8');
return $res;
}
public function uninstall(){
$res = Db::getInstance()->execute('DROP TABLE IF EXISTS `'._DB_PREFIX_.'seourl_lang`');
$res = Db::getInstance()->execute('DROP TABLE IF EXISTS `'._DB_PREFIX_.'seourl_shop`');
$res &= Db::getInstance()->execute('DROP TABLE IF EXISTS `'._DB_PREFIX_.'seourl`');
if (!$res || !parent::uninstall())
return false;
return true;
}
/**
* Hook ajout/modification url catégorie de CMS
*/
public function hookseoUrlCmsCategory($params) {
$id_shop = Context::getContext()->shop->id;
$seo_url = ManageSeoUrl::getByType($params['object']->id, ManageSeoUrl::TYPE_CATEGORY_CMS, FALSE, NULL, $id_shop);
$urls = array();
//Check parent
if ($params['object']->id_parent) {
$seo_url_parent = ManageSeoUrl::getByType($params['object']->id_parent, ManageSeoUrl::TYPE_CATEGORY_CMS, FALSE, NULL, $id_shop);
foreach (Language::getLanguages() as $language) {
$id_lang = (int)$language['id_lang'];
$urls[$id_lang] = $seo_url_parent->link_rewrite[$id_lang].$params['object']->id.'-'.$params['object']->slug[$id_lang].'/';
}
} else {
foreach (Language::getLanguages() as $language) {
$id_lang = (int)$language['id_lang'];
$urls[$id_lang] = $params['object']->id.'-'.$params['object']->slug[$id_lang].'/';
}
}
if (!$seo_url instanceOf ManageSeoUrl
&& !$seo_url->id) {
ManageSeoUrl::createUrl(ManageSeoUrl::TYPE_CATEGORY_CMS, $params['object']->id, $urls);
} else {
$seo_url->link_rewrite = $urls;
$seo_url->save();
}
}
}