toutpratique/modules/seourl/seourl.php
2015-12-14 16:07:18 +01:00

178 lines
5.7 KiB
PHP

<?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('seoUrlCmsPost')
|| !$this->registerHook('seoUrlCmsEdito')
|| !$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 (!Validate::isLoadedObject($seo_url)) {
ManageSeoUrl::createUrl(ManageSeoUrl::TYPE_CATEGORY_CMS, $params['object']->id, $urls);
} else {
$seo_url->link_rewrite = $urls;
$seo_url->save();
}
}
/**
* Hook ajout/modification url post de CMS
*/
public function hookseoUrlCmsPost($params) {
$id_shop = Context::getContext()->shop->id;
$seo_url = ManageSeoUrl::getByType($params['object']->id, ManageSeoUrl::TYPE_POST_CMS, FALSE, NULL, $id_shop);
$urls = array();
$url_category = ManageSeoUrl::getByType($params['object']->id_category, ManageSeoUrl::TYPE_CATEGORY_CMS, FALSE, NULL, $id_shop);
if (Validate::isLoadedObject($url_category)) {
foreach (Language::getLanguages() as $language) {
$id_lang = (int)$language['id_lang'];
$urls[$id_lang] = $url_category->link_rewrite[$id_lang].$params['object']->id.'-'.$params['object']->slug[$id_lang].'.php';
}
}
if (!$seo_url instanceOf ManageSeoUrl) {
ManageSeoUrl::createUrl(ManageSeoUrl::TYPE_POST_CMS, $params['object']->id, $urls);
} else {
$seo_url->link_rewrite = $urls;
$seo_url->save();
}
}
public function hookseoUrlCmsEdito($params) {
$id_shop = Context::getContext()->shop->id;
$seo_url = ManageSeoUrl::getByType($params['object']->id, ManageSeoUrl::TYPE_POST_EDITO, FALSE, NULL, $id_shop);
$urls = array();
$canonicals = array();
foreach (Language::getLanguages() as $language) {
$id_lang = (int)$language['id_lang'];
$urls[$id_lang] = ManageSeoUrl::PREFIX_EDITO.$params['object']->id.'-'.$params['object']->slug[$id_lang].'.php';
if (isset($params['canonical'])) {
$canonicals[$id_lang] = $params['canonical'];
}
}
if (!$seo_url instanceOf ManageSeoUrl) {
if (!isset($params['canonical'])) {
$canonical = '';
} else {
$canonical = $params['canonical'];
}
ManageSeoUrl::createUrl(ManageSeoUrl::TYPE_POST_EDITO, $params['object']->id, $urls, $canonical);
} else {
$seo_url->link_rewrite = $urls;
$seo_url->canonical = $canonicals;
$seo_url->save();
}
}
public function hookdisplayHeader()
{
if (Tools::getValue('seo_canonical')) {
$url = Tools::getValue('seo_canonical');
return "<link rel='canonical' href='$url'/>";
}
}
}