Skip to content

Commit

Permalink
[FIX] Refactor category and multidomen.
Browse files Browse the repository at this point in the history
  • Loading branch information
Seiger committed May 23, 2024
1 parent fc88da9 commit 3479859
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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) {
Expand Down
56 changes: 46 additions & 10 deletions module/sCommerceModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,24 @@
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;
default :
$query->orderBy($order, $direc);
break;
}

$data['items'] = $query->paginate($perpage);
$data['total'] = sProduct::count();
$data['active'] = sProduct::wherePublished(1)->count();
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand All @@ -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()) {
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
20 changes: 16 additions & 4 deletions src/Controllers/sCommerceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}

/**
Expand Down
27 changes: 23 additions & 4 deletions src/Models/sProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class sProduct extends Model
*
* @var array
*/
protected $appends = ['coverSrc', 'link'];
protected $appends = ['category', 'coverSrc', 'link'];

/**
* Availability constants
Expand Down Expand Up @@ -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
*
Expand All @@ -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 :
Expand Down
5 changes: 3 additions & 2 deletions src/sCommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}

Expand All @@ -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();
}

/**
Expand Down
Loading

0 comments on commit 3479859

Please sign in to comment.