From 34798597ff313927ccadf954277da1d5083eb096 Mon Sep 17 00:00:00 2001 From: Serhii Korneliuk Date: Thu, 23 May 2024 19:48:58 +0300 Subject: [PATCH] [FIX] Refactor category and multidomen. --- ...2_27_190234_ecommerce_structure_tables.php | 2 +- module/sCommerceModule.php | 56 +++++++-- src/Controllers/sCommerceController.php | 20 ++- src/Models/sProduct.php | 27 +++- src/sCommerce.php | 5 +- views/productTab.blade.php | 115 ++++++++++++++---- views/productsTab.blade.php | 21 +++- 7 files changed, 200 insertions(+), 46 deletions(-) diff --git a/database/migrations/2023_12_27_190234_ecommerce_structure_tables.php b/database/migrations/2023_12_27_190234_ecommerce_structure_tables.php index 78942f8..2084037 100644 --- a/database/migrations/2023_12_27_190234_ecommerce_structure_tables.php +++ b/database/migrations/2023_12_27_190234_ecommerce_structure_tables.php @@ -65,7 +65,6 @@ public function up(): void $table->id('id'); $table->unsignedTinyInteger('published')->default(0)->index()->comment('0-Unpublished|1-Published'); $table->unsignedTinyInteger('availability')->default(0)->index()->comment('0-Not available|1-In stock|2-On order'); - $table->unsignedInteger('category')->default(0)->index()->comment('Resource ID as Category'); $table->string('sku')->index()->comment('It is the SKU Product code'); $table->string('alias', 512)->index()->comment('It using for generate url'); $table->unsignedInteger('position')->default(0)->index()->comment('Position the product in list'); @@ -106,6 +105,7 @@ public function up(): void Schema::create('s_product_category', function (Blueprint $table) { $table->foreignId('product')->comment('Product ID')->constrained('s_products')->cascadeOnDelete(); $table->unsignedInteger('category')->default(0)->index()->comment('Resource ID as Category'); + $table->string('scope')->index()->default(''); }); Schema::create('s_product_attribute_values', function (Blueprint $table) { diff --git a/module/sCommerceModule.php b/module/sCommerceModule.php index 2abe9bc..4903163 100644 --- a/module/sCommerceModule.php +++ b/module/sCommerceModule.php @@ -57,7 +57,16 @@ case "category": $query->addSelect( '*', - DB::Raw('(select `' . DB::getTablePrefix() . 'site_content`.`pagetitle` from `' . DB::getTablePrefix() . 'site_content` where `' . DB::getTablePrefix() . 'site_content`.`id` = `' . DB::getTablePrefix() . 's_products`.`category`) as cat') + DB::Raw( + '(select `' . DB::getTablePrefix() . 'site_content`.`pagetitle` + from `' . DB::getTablePrefix() . 'site_content` + where `' . DB::getTablePrefix() . 'site_content`.`id` = ( + select `' . DB::getTablePrefix() . 's_product_category`.`category` + from `' . DB::getTablePrefix() . 's_product_category` + where `' . DB::getTablePrefix() . 's_product_category`.`product` = `' . DB::getTablePrefix() . 's_products`.`id` + limit 1) + ) as cat' + ) ); $query->orderBy('cat', $direc); break; @@ -65,7 +74,7 @@ $query->orderBy($order, $direc); break; } - + $data['items'] = $query->paginate($perpage); $data['total'] = sProduct::count(); $data['active'] = sProduct::wherePublished(1)->count(); @@ -78,9 +87,12 @@ $product = sCommerce::getProduct($requestId); $categoryParentsIds = [0]; - if ($product->category) { - $categoryParentsIds = $sCommerceController->categoryParentsIds($product->category); + if ($product->categories) { + foreach ($product->categories as $category) { + $categoryParentsIds = array_merge($categoryParentsIds, $sCommerceController->categoryParentsIds($category->id)); + } } + $attributes = sAttribute::whereHas('categories', function ($q) use ($categoryParentsIds) { $q->whereIn('category', $categoryParentsIds); })->get(); @@ -128,7 +140,6 @@ $product->published = (int)request()->input('published', 0); $product->availability = (int)request()->input('availability', 0); - $product->category = (int)request()->input('parent', sCommerce::config('basic.catalog_root', evo()->getConfig('site_start', 1))); $product->sku = request()->input('sku', ''); $product->alias = $sCommerceController->validateAlias($alias, (int)$product->id); $product->position = (int)request()->input('position', 0); @@ -146,7 +157,17 @@ $product->type = $type; $product->save(); - $categories = array_merge((array)request()->input('categories', []), [$product->category]); + $categories = (array)request()->input('categories', []); + if (evo()->getConfig('check_sMultisite', false)) { + foreach(Seiger\sMultisite\Models\sMultisite::all() as $domain) { + $parent = (int)request()->input('parent_' . $domain->key, 0); + if ($parent > 0) { + $categories[$parent] = ['scope' => 'primary_' . $domain->key]; + } + } + } else { + $categories[(int)request()->input('parent', sCommerce::config('basic.catalog_root', evo()->getConfig('site_start', 1)))] = ['scope' => 'primary']; + } $product->categories()->sync($categories); if (!$product->texts->count()) { @@ -206,7 +227,13 @@ $requestId = (int)request()->input('i', 0); $product = sCommerce::getProduct($requestId); - $categoryParentsIds = $sCommerceController->categoryParentsIds($product->category); + $categoryParentsIds = [0]; + if ($product->categories) { + foreach ($product->categories as $category) { + $categoryParentsIds = array_merge($categoryParentsIds, $sCommerceController->categoryParentsIds($category->id)); + } + } + $attributes = sAttribute::lang($sCommerceController->langDefault())->whereHas('categories', function ($q) use ($categoryParentsIds) { $q->whereIn('category', $categoryParentsIds); })->orderBy('position')->get(); @@ -231,7 +258,14 @@ if ($product) { $product->attrValues()->detach(); - $categoryParentsIds = $sCommerceController->categoryParentsIds($product->category); + + $categoryParentsIds = [0]; + if ($product->categories) { + foreach ($product->categories as $category) { + $categoryParentsIds = array_merge($categoryParentsIds, $sCommerceController->categoryParentsIds($category->id)); + } + } + $attributes = sAttribute::lang($sCommerceController->langDefault())->whereHas('categories', function ($q) use ($categoryParentsIds) { $q->whereIn('category', $categoryParentsIds); })->get(); @@ -354,8 +388,10 @@ $product = sCommerce::getProduct($content->product ?? 0); $categoryParentsIds = [0]; - if ($product->category) { - $categoryParentsIds = $sCommerceController->categoryParentsIds($product->category); + if ($product->categories) { + foreach ($product->categories as $category) { + $categoryParentsIds = array_merge($categoryParentsIds, $sCommerceController->categoryParentsIds($category->id)); + } } $attributes = sAttribute::whereHas('categories', function ($q) use ($categoryParentsIds) { $q->whereIn('category', $categoryParentsIds); diff --git a/src/Controllers/sCommerceController.php b/src/Controllers/sCommerceController.php index b5b300b..182f8ab 100644 --- a/src/Controllers/sCommerceController.php +++ b/src/Controllers/sCommerceController.php @@ -2,6 +2,7 @@ use EvolutionCMS\Models\SiteContent; use Illuminate\Support\Facades\Cache; +use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log; use Illuminate\Support\Str; use Psr\Container\ContainerExceptionInterface; @@ -24,15 +25,26 @@ class sCommerceController public function setProductsListing(): void { $productsListing = []; - $products = sProduct::select('id', 'alias', 'category')->wherePublished(1)->get(); + $categories = []; + $products = sProduct::select('id', 'alias')->wherePublished(1)->get(); if ($products) { + $scopes = DB::table('s_product_category')->where('scope', 'LIKE', 'primary%')->get(); + foreach ($scopes as $scope) { + $categories[$scope->product][] = trim(str_replace('primary', '', $scope->scope), '_'); + } foreach ($products as $product) { - $link = str_replace(MODX_SITE_URL, '', $product->link); - $productsListing[trim($link, '/')] = $product->id; + if (isset($categories[$product->id])) { + foreach ($categories[$product->id] as $category) { + $link = str_replace(MODX_SITE_URL, '', $product->getLinkAttribute($category)); + $productsListing[$category][trim($link, '/')] = $product->id; + } + } } } evo()->clearCache('full'); - Cache::forever('productsListing', $productsListing); + foreach ($productsListing as $key => $array) { + Cache::forever('productsListing' . $key, $array); + } } /** diff --git a/src/Models/sProduct.php b/src/Models/sProduct.php index 40e983e..06db038 100644 --- a/src/Models/sProduct.php +++ b/src/Models/sProduct.php @@ -29,7 +29,7 @@ class sProduct extends Model * * @var array */ - protected $appends = ['coverSrc', 'link']; + protected $appends = ['category', 'coverSrc', 'link']; /** * Availability constants @@ -180,6 +180,24 @@ public function attrValues() ->orderBy('position'); } + /** + * Retrieves the category attribute for the product. + * + * @param string|null $key The key for the site scope. If null, the default site key is used. + * @return int|null The category ID of the product or the catalog root ID. + */ + public function getCategoryAttribute($key = null): int + { + if (evo()->getConfig('check_sMultisite', false)) { + $key = $key ?? evo()->setConfig('site_key', 'default'); + $category = $this->categories()->whereScope('primary_' . $key)->first()->id ?? null; + } else { + $category = $this->categories()->whereScope('primary')->first()->id ?? null; + } + $cateroot = sCommerce::config('basic.catalog_root', evo()->getConfig('site_start', 1)); + return $category ?? $cateroot; + } + /** * Get the link attribute for the current object * @@ -190,14 +208,15 @@ public function attrValues() * * @return string The URL of the object. */ - public function getLinkAttribute(): string + public function getLinkAttribute($key = ''): string { switch (sCommerce::config('product.link_rule', 'root')) { case "catalog" : - $base_url = UrlProcessor::makeUrl(sCommerce::config('basic.catalog_root', evo()->getConfig('site_start', 1))); + $category = sCommerce::config('basic.catalog_root' . $key, evo()->getConfig('site_start', 1)); + $base_url = UrlProcessor::makeUrl($category); break; case "category" : - $category = (int)$this->category ?: sCommerce::config('basic.catalog_root', evo()->getConfig('site_start', 1)); + $category = (int)$this->getCategoryAttribute(trim($key) ? $key : null) ?: sCommerce::config('basic.catalog_root' . $key, evo()->getConfig('site_start', 1)); $base_url = UrlProcessor::makeUrl($category); break; default : diff --git a/src/sCommerce.php b/src/sCommerce.php index 39ff153..5edb781 100644 --- a/src/sCommerce.php +++ b/src/sCommerce.php @@ -7,6 +7,7 @@ use Illuminate\Support\Str; use Seiger\sCommerce\Controllers\sCommerceController; use Seiger\sCommerce\Models\sAttribute; +use Seiger\sCommerce\Models\sCategory; use Seiger\sCommerce\Models\sProduct; use Seiger\sCommerce\Models\sProductTranslate; @@ -59,7 +60,7 @@ public function getProductByAlias(string $alias): object public function getTreeActiveCategories(int $category, int $dept = 10): object { $sCommerceController = new sCommerceController(); - $object = SiteContent::find($category); + $object = sCategory::find($category); return $sCommerceController->listSubCategories($object, $dept); } @@ -86,7 +87,7 @@ public function getCategoryProducts(int $category = null, string $lang = null, i $categories = array_merge([$category], $sCommerceController->listAllActiveSubCategories($category, $dept)); $productIds = DB::table('s_product_category')->select(['product'])->whereIn('category', $categories)->get()->pluck('product')->toArray(); - return sProduct::lang($lang)->whereIn('category', $categories)->orWhereIn('id', $productIds)->active()->get(); + return sProduct::lang($lang)->WhereIn('id', $productIds)->active()->get(); } /** diff --git a/views/productTab.blade.php b/views/productTab.blade.php index 2a65996..7a6d279 100644 --- a/views/productTab.blade.php +++ b/views/productTab.blade.php @@ -81,33 +81,68 @@ @endif -
-
-
- - + @if (evo()->getConfig('check_sMultisite', false)) + + + @foreach(Seiger\sMultisite\Models\sMultisite::all() as $domain) +
+
+
+ + +
+
+
+ @php($parentlookup = false) + @if(($item->categories()->whereScope('primary_' . $domain->key)->first()->id ?? 0) == 0) + @php($parentname = __('global.disabled')) + @else + @php($parentlookup = ($item->getCategoryAttribute($domain->key) ?? sCommerce::config('basic.catalog_root', evo()->getConfig('site_start', 1)))) + @endif + @if($parentlookup !== false && is_numeric($parentlookup)) + @php($parentname = \EvolutionCMS\Models\SiteContent::withTrashed()->select('pagetitle')->find($parentlookup)->pagetitle) + @if(!$parentname) + @php(evo()->webAlertAndQuit(__('global.error_no_parent'))) + @endif + @endif + + {{$parentlookup}} ({{entities($parentname)}}) + + +
+
+
-
-
- @php($parentlookup = false) - @if(($item->category ?? sCommerce::config('basic.catalog_root', 0)) == 0) - @php($parentname = evo()->getConfig('site_name')) - @else - @php($parentlookup = ($item->category ?? sCommerce::config('basic.catalog_root', evo()->getConfig('site_start', 1)))) - @endif - @if($parentlookup !== false && is_numeric($parentlookup)) - @php($parentname = \EvolutionCMS\Models\SiteContent::withTrashed()->select('pagetitle')->find($parentlookup)->pagetitle) - @if(!$parentname) - @php(evo()->webAlertAndQuit($_lang["error_no_parent"])) + @endforeach + @else +
+
+
+ + +
+
+
+ @php($parentlookup = false) + @if(($item->category ?? sCommerce::config('basic.catalog_root', 0)) == 0) + @php($parentname = evo()->getConfig('site_name')) + @else + @php($parentlookup = ($item->category ?? sCommerce::config('basic.catalog_root', evo()->getConfig('site_start', 1)))) @endif - @endif - - {{$parentlookup}} ({{entities($parentname)}}) - + @if($parentlookup !== false && is_numeric($parentlookup)) + @php($parentname = \EvolutionCMS\Models\SiteContent::withTrashed()->select('pagetitle')->find($parentlookup)->pagetitle) + @if(!$parentname) + @php(evo()->webAlertAndQuit($_lang["error_no_parent"])) + @endif + @endif + + {{$parentlookup}} ({{entities($parentname)}}) + +
-
+ @endif @if(sCommerce::config('product.show_field_categories', 1) == 1)
@@ -193,4 +228,40 @@
+ @endpush diff --git a/views/productsTab.blade.php b/views/productsTab.blade.php index aea4ae9..4be935c 100644 --- a/views/productsTab.blade.php +++ b/views/productsTab.blade.php @@ -1,5 +1,8 @@ @php $order = request()->has('order') ? request()->input('order') : 'id'; + if (evo()->getConfig('check_sMultisite', false)) { + $domains = \Seiger\sMultisite\Models\sMultisite::all(); + } @endphp
@@ -81,8 +84,8 @@ class="form-control rounded-left scom-input seiger__search" @endif @if (evo()->getConfig('check_sMultisite', false) && sCommerce::config('products.show_field_websites', 1) == 1) - - + + @endif @if (sCommerce::config('products.show_field_visibility', 1) == 1) @@ -138,13 +141,25 @@ class="form-control rounded-left scom-input seiger__search" @if($item->category > 1) {{$resources[$item->category]}} + @elseif(evo()->getConfig('check_sMultisite', false)) + @php($categories = $item->categories()->where('scope', 'LIKE', 'primary%')->get()) + @foreach($categories as $category) + {{$category->pagetitle}} + @endforeach @else {{evo()->getConfig('site_name')}} @endif @endif @if (evo()->getConfig('check_sMultisite', false) && sCommerce::config('products.show_field_websites', 1) == 1) - {{$item->websites}} + + @php($categories = $item->categories()->withPivot(['scope'])->wherePivot('scope', 'LIKE', 'primary%')->get()) + @foreach($categories as $category) + @php($scope = str_replace('primary_', '', $category->pivot->scope)) + @php($website = $domains->where('key', $scope)->first()) + {{$website->site_name}} + @endforeach + @endif @if (sCommerce::config('products.show_field_visibility', 1) == 1)