132 lines
3.8 KiB
PHP
132 lines
3.8 KiB
PHP
<?php
|
|
|
|
class ManageSeoUrl extends ObjectModel {
|
|
|
|
const TYPE_CATEGORY = 1;
|
|
const TYPE_PRODUCT = 2;
|
|
const TYPE_MANUFACTURER = 3;
|
|
const TYPE_CATEGORY_CMS = 4;
|
|
const TYPE_POST_CMS = 5;
|
|
const TYPE_POST_EDITO = 6;
|
|
const TYPE_CMS = 7;
|
|
const TYPE_301 = 8;
|
|
const TYPE_STORE_HOME = 9;
|
|
const TYPE_HOME_EDITO = 10;
|
|
const TYPE_VIDEO = 11;
|
|
|
|
const SUFFIX_HTML = '.html';
|
|
const SUFFIX_PHP = '.php';
|
|
const PREFIX_EDITO = 'edito-';
|
|
|
|
public $id_seourl;
|
|
public $link_rewrite;
|
|
public $type;
|
|
public $id_element;
|
|
public $redirect;
|
|
public $canonical;
|
|
|
|
/**
|
|
* @see ObjectModel::$definition
|
|
*/
|
|
public static $definition = array(
|
|
'table' => 'seourl',
|
|
'primary' => 'id_seourl',
|
|
'multilang' => true,
|
|
'multilang_shop' => true,
|
|
'fields' => array(
|
|
'link_rewrite' => array('lang' => true, 'type' => self::TYPE_STRING, 'validate' => 'isString'),
|
|
'type' => array('type' => self::TYPE_INT, 'validate' => 'isInt'),
|
|
'id_element' => array('type' => self::TYPE_INT, 'validate' => 'isInt'),
|
|
'redirect' => array('lang' => true, 'type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isString'),
|
|
'canonical' => array('lang' => true, 'type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isString'),
|
|
)
|
|
);
|
|
|
|
public function __construct($id = null, $id_lang = null, $id_shop = null) {
|
|
parent::__construct($id, $id_lang, $id_shop);
|
|
}
|
|
|
|
/**
|
|
* Get Url info
|
|
*
|
|
* @param striung $url request uri
|
|
* @param int $id_lang
|
|
* @param int $id_shop
|
|
*
|
|
* @return array
|
|
*/
|
|
public static function getUrl($url, $id_lang = 1, $id_shop = 1) {
|
|
if (!$id_lang)
|
|
$id_lang = Context::getContext()->language->id;
|
|
$sql = 'SELECT s.`id_seourl`,
|
|
s.`type`,
|
|
s.`id_element`,
|
|
sl.`redirect`,
|
|
sl.`canonical`
|
|
FROM `'._DB_PREFIX_.'seourl` s
|
|
LEFT JOIN `'._DB_PREFIX_.'seourl_lang` sl ON (s.`id_seourl` = sl.`id_seourl`)
|
|
LEFT JOIN `'._DB_PREFIX_.'seourl_shop` shop ON (shop.`id_seourl` = s.`id_seourl`)
|
|
WHERE sl.`link_rewrite` = "' . pSQL($url) .'"
|
|
AND sl.`id_lang`='.(int)$id_lang .'
|
|
AND shop.`id_shop`='.(int)$id_shop;
|
|
if ($results = Db::getInstance()->getRow($sql)){
|
|
return $results;
|
|
}else{
|
|
return "notfound";
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Créer un lien
|
|
* @param int $id_type Type d'url
|
|
* @param int $id_element id de l'element
|
|
* @param array $slugs Tableau des liens par langue
|
|
*/
|
|
public static function createUrl($id_type, $id_element, $slugs, $canonical = null) {
|
|
$url_rewrite = new ManageSeoUrl();
|
|
$url_rewrite->type = (int)$id_type;
|
|
$url_rewrite->id_element = $id_element;
|
|
|
|
$link_rewrite = array();
|
|
$canonical_array = array();
|
|
foreach (Language::getLanguages() as $language) {
|
|
$id_lang = (int)$language['id_lang'];
|
|
$link_rewrite[$id_lang] = $slugs[$id_lang];
|
|
$canonical_array[$id_lang] = $canonical;
|
|
}
|
|
|
|
$url_rewrite->canonical = $canonical_array;
|
|
$url_rewrite->link_rewrite = $link_rewrite;
|
|
return $url_rewrite->save();
|
|
}
|
|
|
|
|
|
/**
|
|
* Retourne une ManageSeoUrl en fonction de son type et son id_element
|
|
*
|
|
* @param int $id_element Id de l'element de retour
|
|
* @param int $type Constante de la classe pour le type d'url
|
|
* @param int $id_only Si true, ne retourne que l'id de l'url
|
|
* @param int $id_lang Id_lang
|
|
* @param int $id_shop Id shop
|
|
*/
|
|
public static function getByType($id_element, $type = self::TYPE_PRODUCT, $id_only = FALSE, $id_lang = NULL, $id_shop = NULL) {
|
|
if ($id_shop == null) {
|
|
$id_shop = Context::getContext()->shop->id;
|
|
}
|
|
$sql = '
|
|
SELECT s.`id_seourl`
|
|
FROM `'._DB_PREFIX_.'seourl` s
|
|
LEFT JOIN `'._DB_PREFIX_.'seourl_shop` shop ON(shop.`id_seourl` = s.`id_seourl`)
|
|
WHERE s.`type` = "'.$type.'"
|
|
AND s.`id_element` = '.(int)$id_element .'
|
|
AND shop.`id_shop` = '.(int)$id_shop;
|
|
|
|
if (!$id = Db::getInstance()->getValue($sql))
|
|
return false;
|
|
if ($id_only)
|
|
return $id;
|
|
return new ManageSeoUrl((int)$id, $id_lang, $id_shop);
|
|
}
|
|
|
|
} |