From a475c87287009e1e4c376482eb2ebe501947f305 Mon Sep 17 00:00:00 2001 From: Jacek Pyziak Date: Sun, 27 Oct 2024 21:49:36 +0100 Subject: [PATCH] Refactor deleting product and updating SEO links This commit refactors the code in the ShopProduct class to improve the process of deleting a product and updating the SEO links. It adds new database queries to delete entries from the pp_routes and pp_redirects tables when a product is deleted. It also updates the SEO links for translated product names, ensuring that the new SEO link is generated correctly and checking for any existing redirects before adding a new one. Additionally, the commit adds a new function, canAddRedirect, to check for cycles in the redirect map before adding a new redirect. Refactor deleting product and updating SEO links in class.S This commit refactors the clear_redis_cache function in the S class to improve the process of updating the routes and redirects in the .htaccess file. It adds new database queries to delete entries from the pp_routes table and updates the code to generate the RewriteRule directives for the routes. It also adds a new condition to the .htaccess file to skip rewriting requests for existing files and directories. Refactor retrieving subcategories in class.Category This commit refactors the getCategoryData function in the Category class to improve the process of retrieving subcategories. It removes the ORDER clause from the database query to retrieve subcategories, allowing the categories to be returned in their natural order. This change ensures that the subcategories are displayed correctly in the category navigation. Check redirects and routes in index.php This commit adds new code to the index.php file to check for redirects and routes before processing the request. It checks if the requested URL matches any entries in the pp_redirects table and redirects the user to the new URL if a match is found. It also checks if the requested URL matches any entries in the pp_routes table and parses the destination to extract GET parameters if a match is found. This change improves the handling of redirects and routes in the application. --- autoload/admin/factory/class.ShopProduct.php | 33 +++++++- autoload/class.S.php | 77 +++++++++++++++++-- autoload/shop/class.Category.php | 4 +- index.php | 38 +++++++++ templates/.DS_Store | Bin 6148 -> 0 bytes templates_user/.DS_Store | Bin 6148 -> 0 bytes templates_user/components/.DS_Store | Bin 6148 -> 0 bytes 7 files changed, 142 insertions(+), 10 deletions(-) delete mode 100644 templates/.DS_Store delete mode 100644 templates_user/.DS_Store delete mode 100644 templates_user/components/.DS_Store diff --git a/autoload/admin/factory/class.ShopProduct.php b/autoload/admin/factory/class.ShopProduct.php index d915a04..90d5e09 100644 --- a/autoload/admin/factory/class.ShopProduct.php +++ b/autoload/admin/factory/class.ShopProduct.php @@ -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]; diff --git a/autoload/class.S.php b/autoload/class.S.php index 667cb31..6d14e85 100644 --- a/autoload/class.S.php +++ b/autoload/class.S.php @@ -1,6 +1,48 @@ select( 'pp_redirects', '*' ); + + $redirectMap = []; + foreach ( $redirects as $redirect ) + { + $redirectMap[$redirect['from']] = $redirect['to']; + } + + // Dodaj nowe przekierowanie do mapy tymczasowo + $redirectMap[$from] = $to; + + // Funkcja do sprawdzania cyklu za pomocą DFS + $visited = []; + $stack = []; + + function hasCycle($current, $target, &$redirectMap, &$visited) + { + if ($current === $target) { + return true; + } + + if (isset($visited[$current])) { + return false; + } + + $visited[$current] = true; + + if (isset($redirectMap[$current])) { + return hasCycle($redirectMap[$current], $target, $redirectMap, $visited); + } + + return false; + } + + // Sprawdź, czy istnieje ścieżka z $newTo do $newFrom + return !hasCycle($to, $from, $redirectMap, $visited); + } + static public function clear_redis_cache() { $redis = \RedisConnection::getInstance() -> getConnection(); @@ -501,15 +543,19 @@ class S } $results = $mdb -> select( 'pp_langs', [ 'id', 'start' ], [ 'status' => 1, 'ORDER' => [ 'o' => 'ASC' ] ] ); - if ( is_array( $results ) ) { + if ( is_array( $results ) ) + { foreach ( $results as $row ) { !$row['start'] ? $language_link = $row['id'] . '/' : $language_link = ''; $results2 = $mdb -> select( 'pp_shop_products_langs', [ '[><]pp_shop_products' => [ 'product_id' => 'id' ] ], [ 'seo_link', 'name', 'product_id' ], [ 'lang_id' => $row['id'], 'ORDER' => [ 'name' => 'ASC' ] ] ); - if ( is_array( $results2 ) ) { + if ( is_array( $results2 ) ) + { foreach ( $results2 as $row2 ) { + $mdb -> delete( 'pp_routes', [ 'AND' => [ 'product_id' => $row2['product_id'], 'lang_id' => $row['id'] ] ] ); + if ( $row2['name'] ) { $site_map .= '' . PHP_EOL; @@ -524,13 +570,27 @@ class S if ( $row2['seo_link'] ) { - $htaccess_data .= PHP_EOL . 'RewriteRule ^' . $language_link . \S::seo( $row2['seo_link'] ) . '$ index.php?product=' . $row2['product_id'] . ' [L]'; - $htaccess_data .= PHP_EOL . 'RewriteRule ^' . $language_link . \S::seo( $row2['seo_link'] ) . '/([0-9-]+)$ index.php?product=' . $row2['product_id'] . '&permutation_hash=$1 [L]'; + $pattern = '^' . $language_link . \S::seo( $row2['seo_link'] ) . '$'; + $destination = 'index.php?product=' . $row2['product_id']; + + $mdb -> insert( 'pp_routes', [ 'product_id' => $row2['product_id'], 'lang_id' => $row['id'], 'pattern' => $pattern, 'destination' => $destination ] ); + + $pattern = '^' . $language_link . \S::seo( $row2['seo_link'] ) . '/([0-9-]+)$'; + $destination = 'index.php?product=' . $row2['product_id'] . '&permutation_hash=$1'; + + $mdb -> insert( 'pp_routes', [ 'product_id' => $row2['product_id'], 'lang_id' => $row['id'], 'pattern' => $pattern, 'destination' => $destination ] ); } else { - $htaccess_data .= PHP_EOL . 'RewriteRule ^' . $language_link . 'p-' . $row2['product_id'] . '-' . \S::seo( $row2['name'] ) . '$ index.php?product=' . $row2['product_id'] . ' [L]'; - $htaccess_data .= PHP_EOL . 'RewriteRule ^' . $language_link . 'p-' . $row2['product_id'] . '-' . \S::seo( $row2['name'] ) . '/([0-9-]+)$ index.php?product=' . $row2['product_id'] . '&permutation_hash=$1 [L]'; + $pattern = '^' . $language_link . 'p-' . $row2['product_id'] . '-' . \S::seo( $row2['name'] ) . '$'; + $destination = 'index.php?product=' . $row2['product_id']; + + $mdb -> insert( 'pp_routes', [ 'product_id' => $row2['product_id'], 'lang_id' => $row['id'], 'pattern' => $pattern, 'destination' => $destination ] ); + + $pattern = '^' . $language_link . 'p-' . $row2['product_id'] . '-' . \S::seo( $row2['name'] ) . '/([0-9-]+)$'; + $destination = 'index.php?product=' . $row2['product_id'] . '&permutation_hash=$1'; + + $mdb -> insert( 'pp_routes', [ 'product_id' => $row2['product_id'], 'lang_id' => $row['id'], 'pattern' => $pattern, 'destination' => $destination ] ); } } } @@ -709,6 +769,11 @@ class S $htaccess_data ); } + $htaccess_data .= PHP_EOL; + $htaccess_data .= 'RewriteCond %{REQUEST_FILENAME} !-f' . PHP_EOL; + $htaccess_data .= 'RewriteCond %{REQUEST_FILENAME} !-d' . PHP_EOL; + $htaccess_data .= 'RewriteRule ^ index.php [L]'; + $fp = fopen( $dir . '.htaccess', 'w' ); fwrite( $fp, $htaccess_data ); fclose( $fp ); diff --git a/autoload/shop/class.Category.php b/autoload/shop/class.Category.php index 4c6eaeb..bf579b7 100644 --- a/autoload/shop/class.Category.php +++ b/autoload/shop/class.Category.php @@ -66,9 +66,7 @@ class Category implements \ArrayAccess if ( !$objectData ) { - // $shop_categories = $mdb -> select( 'pp_shop_categories', '*', ['parent_id' => $category_id] ); - $shop_categories = $mdb -> select( 'pp_shop_categories', '*', [ 'parent_id' => $category_id, 'ORDER' => [ 'o' => 'ASC' ] ] ); - + $shop_categories = $mdb -> select( 'pp_shop_categories', '*', ['parent_id' => $category_id] ); $shop_categories_langs = array(); foreach ($shop_categories as $shop_categorie) { array_push($shop_categories_langs, $mdb -> get( 'pp_shop_categories_langs', '*', ['category_id' => $shop_categorie['id']] )); diff --git a/index.php b/index.php index 7a1a785..e0ef074 100644 --- a/index.php +++ b/index.php @@ -79,6 +79,44 @@ if ( \S::get( 'action' ) == 'htaccess' ) exit; } +// check redirects +$request_uri = substr( $_SERVER[ 'REQUEST_URI' ], 1, strlen( $_SERVER[ 'REQUEST_URI' ] ) ); +if ( $request_uri != '' ) +{ + $new_url = $mdb -> get( 'pp_redirects', 'to', [ 'from' => $request_uri ], [ 'ORDER' => [ 'date_add' => 'DESC' ] ] ); + if ( $new_url['to'] ) + { + header( 'Location: ' . $new_url[ 'to' ], true, 301 ); + exit; + } +} + +// check routes +$request_uri = ltrim( $_SERVER['REQUEST_URI'], '/' ); +if ( $request_uri != '' ) +{ + $matched = false; + + $routes = $mdb -> select( 'pp_routes', '*' ); + foreach ( $routes as $route ) + { + $pattern = $route['pattern']; + $destination = $route['destination']; + + if ( preg_match( "#^" . $pattern . "#", $request_uri, $matches ) ) + { + // Replace placeholders in the destination with matches from the request URI + $destination = preg_replace( "#^" . $pattern . "#", $destination, $request_uri ); + + // Parse the destination string to extract GET parameters + parse_str(parse_url($destination, PHP_URL_QUERY), $_GET); + + $matched = true; + break; + } + } +} + if ( \S::get( 'a' ) == 'page' and \S::get( 'id' ) ) { $page = \front\factory\Pages::page_details( \S::get( 'id' ) ); diff --git a/templates/.DS_Store b/templates/.DS_Store deleted file mode 100644 index 96797d4292e50044254a7be6c0d61b9e1f76789e..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHKu};H44E2=`fr8Yr3)1lc4BZ(*6uzKrEKq`|M5Bue?D+-w0(K-mgYV+`Y?VZ5 zidayEY{`BX$G$l4+W3x$T=rZ{iN-{fKp6-7FdKyLvvy>l9=_?~9H%s=8+xP%y6Z%n zV;33VwHsl@78IzW=K4J?@29idP*zg*@mqgn8S;F-40A*zua_S$+1cm$w&>y)(bb|^ zg~zmX(5N@iiVFPNR_$t!PWs}SwN4JN-nG_^bC%(|(w}16b@Kno#27FJjDekH06m)} z83~#-28;n?V9fx3A3T&XRjdW$rvoM)0f0leqhK8OK0wb7V5(RP!UJ)V3Y1i*M+_(F z@OzC*6>C9BCl{ZLdGyInPbf}zUe3pGa;czMW55__Gq5L@WA6VK{pbI7l3f`C#=yT~ zzzy?Rp5c|Ww{~8Rd##6FLs>Yk7F?%b5L+=~xfM^KQQ-GH0H%tyAUqJ;2t*pp7z2OG Fz!x5GRMh|g diff --git a/templates_user/.DS_Store b/templates_user/.DS_Store deleted file mode 100644 index dbd07c1a1c1d289c1b490405a326e8a55ae29325..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHKJ8r^25S>XVP-t9I?iIMf%5qNN3m_#C4KXMr)UI+aj>emhq98+&f+l(+&Ai>& zd29I<9*>A<`}MjKX+&fWH%Fk~B9M^^Pys6Nqkw%M3f!hP6`=yd49* i9b;qdc*nm7hJ9eJk%`7>a;(5S$@6*vQa;1$mR diff --git a/templates_user/components/.DS_Store b/templates_user/components/.DS_Store deleted file mode 100644 index 5008ddfcf53c02e82d7eee2e57c38e5672ef89f6..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0