Refactor product loading logic and update URL routing

- Refactored the product loading logic in the ShopProduct class to improve performance and readability.
- Updated the URL routing in the index.php file to handle redirects and routes based on the request URI.
- Added a new method in the S class to check if adding a redirect will create a cycle in the redirect map.
- Updated the S class to use the new method for adding redirects and generating URL routes.
- Removed unnecessary code related to generating .htaccess rules.
This commit is contained in:
2024-10-26 18:18:55 +02:00
parent faeb2e52e8
commit 7d0749d692
5 changed files with 167 additions and 744 deletions

View File

@@ -701,6 +701,10 @@ class ShopProduct
$mdb -> delete( 'pp_shop_products_attributes', ['product_id' => $product_id ] );
$mdb -> delete( 'pp_shop_products', ['id' => $product_id ] );
$mdb -> delete( 'pp_shop_product_sets_products', [ 'product_id' => $product_id ] );
// pp_routes
$mdb -> delete( 'pp_routes', [ 'product_id' => $product_id ] );
// pp_redirects
$mdb -> delete( 'pp_redirects', [ 'product_id' => $product_id ] );
\S::delete_dir( '../upload/product_images/product_' . $product_id . '/' );
\S::delete_dir( '../upload/product_files/product_' . $product_id . '/' );
@@ -1055,6 +1059,30 @@ class ShopProduct
foreach ( $name as $key => $val )
{
if ( $translation_id = $mdb -> get( 'pp_shop_products_langs', 'id', [ 'AND' => [ 'product_id' => $product_id, 'lang_id' => $key ] ] ) )
{
$current_seo_link = $mdb -> get( 'pp_shop_products_langs', 'seo_link', [ 'id' => $translation_id ] );
if ( $seo_link[$key] )
$new_seo_link = \S::seo( $seo_link[$key] );
else
$new_seo_link = \S::seo( 'p-' . $product_id . '-' . $name[$key] );
if ( $new_seo_link !== $current_seo_link )
{
if ( !$mdb -> count( 'pp_redirects', [ 'from' => $current_seo_link, 'to' => $new_seo_link, 'lang_id' => $key, 'product_id' => $product_id ] ) )
{
if ( $mdb -> count( 'pp_redirects', [ 'from' => $new_seo_link, 'to' => $current_seo_link, 'lang_id' => $key, 'product_id' => $product_id ] ) )
$mdb -> delete( 'pp_redirects', [ 'from' => $new_seo_link, 'to' => $current_seo_link, 'lang_id' => $key, 'product_id' => $product_id ] );
else
{
if ( \S::canAddRedirect( $current_seo_link, $new_seo_link ) )
$mdb -> insert( 'pp_redirects', [ 'from' => $current_seo_link, 'to' => $new_seo_link, 'lang_id' => $key, 'product_id' => $product_id ] );
else
$mdb -> delete( 'pp_redirects', [ 'product_id' => $product_id, 'lang_id' => $key ] );
}
}
}
$mdb -> update( 'pp_shop_products_langs', [
'lang_id' => $key,
'name' => '' !== $name[$key] ? $name[$key] : null,
@@ -1062,7 +1090,7 @@ class ShopProduct
'description' => '' !== $description[$key] ? $description[$key] : null,
'meta_description' => '' !== $meta_description[$key] ? $meta_description[$key] : null,
'meta_keywords' => '' !== $meta_keywords[$key] ? $meta_keywords[$key] : null,
'seo_link' => '' !== \S::seo($seo_link[$key]) ? \S::seo($seo_link[$key]) : null,
'seo_link' => \S::seo( $seo_link[$key] ) != '' ? \S::seo( $seo_link[$key] ) : \S::seo( 'p-' . $product_id . '-' . $name[$key] ),
'copy_from' => '' !== $copy_from[$key] ? $copy_from[$key] : null,
'warehouse_message_zero' => '' !== $warehouse_message_zero[$key] ? $warehouse_message_zero[$key] : null,
'warehouse_message_nonzero' => '' !== $warehouse_message_nonzero[$key] ? $warehouse_message_nonzero[$key] : null,
@@ -1076,7 +1104,9 @@ class ShopProduct
], [
'id' => $translation_id
] );
}
else
{
$mdb -> insert( 'pp_shop_products_langs', [
'product_id' => (int)$product_id,
'lang_id' => $key,
@@ -1097,6 +1127,7 @@ class ShopProduct
'meta_title' => '' !== $meta_title[$key] ? $meta_title[$key] : null,
'xml_name' => '' !== $xml_name[$key] ? $xml_name[$key] : null,
] );
}
}
$not_in = [0];