This commit is contained in:
O K
2025-02-22 10:59:30 +02:00
commit af907b25a7
3 changed files with 344 additions and 0 deletions

178
urlredirection.php Normal file
View File

@@ -0,0 +1,178 @@
<?php
class UrlRedirection extends Module
{
public function __construct()
{
$this->name = 'urlredirection';
$this->tab = 'front_office_features';
$this->version = '1.0.0';
$this->author = 'panariga';
$this->need_instance = 0;
parent::__construct();
$this->displayName = $this->l('urlredirection');
$this->description = $this->l('urlredirection');
$this->ps_versions_compliancy = array('min' => '1.7', 'max' => _PS_VERSION_);
}
public function install()
{
if (
!parent::install() ||
!$this->registerHook('actionDispatcher') ||
!$this->registerHook('ActionObjectCategoryUpdateAfter') ||
!$this->registerHook('ActionObjectProductUpdateAfter')
) {
return false;
}
// Create table (you might want to handle this in a separate function with more sophisticated error checking)
$sql = "CREATE TABLE IF NOT EXISTS `" . _DB_PREFIX_ . "url_redirection` (
`id_url_redirection` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`url` VARCHAR(255) NOT NULL,
`object_name` VARCHAR(64) NOT NULL,
`object_id` INT(10) UNSIGNED NOT NULL,
`date_add` DATETIME DEFAULT NULL,
`date_upd` DATETIME DEFAULT NULL,
PRIMARY KEY (`id_url_redirection`),
UNIQUE KEY `url_unique` (`url`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
if (!Db::getInstance()->execute($sql)) {
return false;
}
return true;
}
public function uninstall()
{
if (!parent::uninstall()) {
return false;
}
/* // Drop table
$sql = "DROP TABLE IF EXISTS `" . _DB_PREFIX_ . "url_redirection`";
if (!Db::getInstance()->execute($sql)) {
return false;
} */
return true;
}
public function hookactionDispatcher($params)
{
URLRedirect::hookActionDispatcher($params);
}
public function hookActionObjectCategoryUpdateAfter($params)
{
$category = $params['object'];
$idCategory = (int) $category->id;
$languages = Language::getLanguages(false, Context::getContext()->shop->id); // Get all languages for this shop
foreach ($languages as $lang) {
$link = Context::getContext()->link;
$url = $link->getCategoryLink($idCategory, null, $lang['id_lang'],Context::getContext()->shop->id); // Generate new URL
URLRedirect::saveUrl($url, 'category', $idCategory, true); // true for update if exists
}
}
public function hookActionObjectProductUpdateAfter($params)
{
$product = $params['object'];
$id_product = (int)$product->id;
$languages = Language::getLanguages(false, Context::getContext()->shop->id); // get all languages for this shop
foreach ($languages as $lang) {
$link = Context::getContext()->link;
$url = $link->getProductLink($id_product, null, null, null, $lang['id_lang']); // generate new URL
// $new_url = trim(str_replace(Context::getContext()->shop->getBaseURL(true, false), '', $url), '/'); // Get the URL part after base URL
URLRedirect::saveUrl($url, 'product', $id_product, true); // true for update if exists
}
}
public function generateAndSaveAllUrls()
{
$savedUrls = [
'category' => 0,
'product' => 0,
'cms' => 0,
];
$link = Context::getContext()->link;
// Categories
$categories = Category::getCategories(null, true, false); // all categories
foreach ($categories as $category) {
$categoryId = (int)$category['id_category'];
$languages = Language::getLanguages(true); // Get all available languages
foreach ($languages as $language) {
$categoryLink = $link->getCategoryLink($categoryId, null, $language['id_lang']);
// $url = trim(str_replace(Context::getContext()->shop->getBaseURL(true, false), '', $categoryLink), '/');
if (URLRedirect::saveUrl($categoryLink, 'category', $categoryId, true)) { // update existing
$savedUrls['category']++;
}
}
}
// Products
$products = Product::getProducts(Context::getContext()->language->id, 0, 0, 'id_product', 'ASC'); //Get all products
foreach ($products as $product) {
$productId = (int)$product['id_product'];
$languages = Language::getLanguages(true);
foreach ($languages as $language) {
$productLink = $link->getProductLink($productId, null, null, null, $language['id_lang']);
// $url = trim(str_replace(Context::getContext()->shop->getBaseURL(true, false), '', $productLink), '/');
if (URLRedirect::saveUrl($productLink, 'product', $productId, true)) { // update existing
$savedUrls['product']++;
}
}
}
// CMS Pages
$cmsPages = CMS::getCMSPages(null, true, Context::getContext()->language->id, Context::getContext()->shop->id);
foreach ($cmsPages as $cmsPage) {
$cmsId = (int)$cmsPage['id_cms'];
$languages = Language::getLanguages(true);
foreach ($languages as $language) {
$cmsLink = $link->getCMSLink($cmsId, null, null, $language['id_lang']);
// $url = trim(str_replace(Context::getContext()->shop->getBaseURL(true, false), '', $cmsLink), '/');
if (URLRedirect::saveUrl($cmsLink, 'cms', $cmsId, true)) { // update existing
$savedUrls['cms']++;
}
}
}
return $savedUrls;
}
public function getContent()
{
$output = '';
if (Tools::isSubmit('generate_urls')) {
$savedUrls = $this->generateAndSaveAllUrls();
$output .= $this->displayConfirmation(
$this->l('URLs generated successfully.') . ' ' .
$this->l('Categories:') . ' ' . $savedUrls['category'] . ', ' .
$this->l('Products:') . ' ' . $savedUrls['product'] . ', ' .
$this->l('CMS:') . ' ' . $savedUrls['cms']
);
}
$output .= '
<form action="' . $_SERVER['REQUEST_URI'] . '" method="post">
<button type="submit" name="generate_urls" class="btn btn-default">
<i class="process-icon-refresh"></i> ' . $this->l('Generate All URLs') . '
</button>
</form>
';
return $output;
}
}