WIP
This commit is contained in:
@@ -32,6 +32,20 @@ if (! function_exists('translatable')) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Translate
|
||||
*/
|
||||
function tr(string $text, string $locale = 'tk'): string
|
||||
{
|
||||
$text = json_decode($text);
|
||||
|
||||
if ($text) {
|
||||
$text->{$locale} ?? '';
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
if (! function_exists('removeWhiteSpace')) {
|
||||
/**
|
||||
* Remove white sapce from string
|
||||
|
||||
@@ -46,7 +46,7 @@ class FilterController extends Controller
|
||||
}
|
||||
|
||||
if ($this->shouldFilterByBrand()) {
|
||||
return $this->filterByCategoryResource(Brand::find($this->request->brand_id));
|
||||
return $this->categoriesForBrand($this->request->brand_id);
|
||||
}
|
||||
|
||||
if ($this->shouldFilterByChannel()) {
|
||||
@@ -59,55 +59,46 @@ class FilterController extends Controller
|
||||
private function brands()
|
||||
{
|
||||
if ($this->shouldFilterByBrand()) {
|
||||
return Brand::where('id', $this->request->brand_id)->get(['id', 'name']);
|
||||
return Brand::query()->where('id', $this->request->brand_id)->get(['id', 'name']);
|
||||
}
|
||||
|
||||
if ($this->shouldFilterByCategory()) {
|
||||
$brands = Category::find($this->request->category_id)->products()
|
||||
->where('products.is_visible', true)
|
||||
->where('products.parent_id', null)
|
||||
->where('products.stock', '>', 0)
|
||||
->distinct('products.brand_id')
|
||||
->pluck('products.brand_id');
|
||||
$categoryId = (int) $this->request->category_id;
|
||||
|
||||
return Brand::whereIntegerInRaw('id', $brands)->get(['id', 'name']);
|
||||
return DB::table('brands')
|
||||
->select('brands.id', 'brands.name')
|
||||
->join('products', 'products.brand_id', '=', 'brands.id')
|
||||
->join('product_has_relations', 'products.id', '=', 'product_has_relations.product_id')
|
||||
->where('product_has_relations.productable_type', 'category')
|
||||
->where('product_has_relations.productable_id', $categoryId)
|
||||
->where('products.is_visible', true)
|
||||
->whereNull('products.parent_id')
|
||||
->where('products.stock', '>', 0)
|
||||
->groupBy('brands.id', 'brands.name', 'brands.sort_order')
|
||||
->orderBy('brands.sort_order')
|
||||
->get();
|
||||
}
|
||||
|
||||
if ($this->shouldFilterByCollection()) {
|
||||
$brands = Collection::find($this->request->collection_id)->products()
|
||||
->where('products.is_visible', true)
|
||||
->where('products.parent_id', null)
|
||||
->where('products.stock', '>', 0)
|
||||
->distinct('products.brand_id')
|
||||
->pluck('products.brand_id');
|
||||
$collectionId = (int) $this->request->collection_id;
|
||||
|
||||
return Brand::whereIntegerInRaw('id', $brands)->get(['id', 'name']);
|
||||
return DB::table('brands')
|
||||
->select('brands.id', 'brands.name')
|
||||
->join('products', 'products.brand_id', '=', 'brands.id')
|
||||
->join('product_has_relations', 'products.id', '=', 'product_has_relations.product_id')
|
||||
->where('product_has_relations.productable_type', 'collection')
|
||||
->where('product_has_relations.productable_id', $collectionId)
|
||||
->where('products.is_visible', true)
|
||||
->whereNull('products.parent_id')
|
||||
->where('products.stock', '>', 0)
|
||||
->groupBy('brands.id', 'brands.name', 'brands.sort_order')
|
||||
->orderBy('brands.sort_order')
|
||||
->get();
|
||||
}
|
||||
|
||||
return Brand::query()->where('is_visible', true)->ordered()->get(['id', 'name']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter by category
|
||||
*/
|
||||
private function filterByCategoryResource($resource)
|
||||
{
|
||||
$products = $resource->products()
|
||||
->where('products.is_visible', true)
|
||||
->where('products.parent_id', null)
|
||||
->where('products.stock', '>', 0)
|
||||
->distinct('products.id')
|
||||
->pluck('products.id');
|
||||
|
||||
return Category::where('is_visible', true)
|
||||
->ordered()
|
||||
->join('product_has_relations', 'categories.id', '=', 'product_has_relations.productable_id')
|
||||
->where('product_has_relations.productable_type', '=', 'category')
|
||||
->whereIntegerInRaw('product_has_relations.product_id', $products)
|
||||
->get(['categories.id', 'categories.parent_id', 'categories.name'])
|
||||
->unique('categories.id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Categories for a resource
|
||||
*/
|
||||
@@ -133,7 +124,33 @@ class FilterController extends Controller
|
||||
->map(fn ($category) => [
|
||||
'id' => $category->id,
|
||||
'parent_id' => $category->parent_id,
|
||||
'name' => $category->name,
|
||||
'name' => tr($category->name),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Brands
|
||||
*/
|
||||
private function categoriesForBrand(int $id)
|
||||
{
|
||||
return DB::table('categories')
|
||||
->select('categories.id', 'categories.parent_id', 'categories.name')
|
||||
->join('product_has_relations', function ($join) {
|
||||
$join->on('categories.id', '=', 'product_has_relations.productable_id')
|
||||
->where('product_has_relations.productable_type', 'category');
|
||||
})
|
||||
->join('products', 'product_has_relations.product_id', '=', 'products.id')
|
||||
->where('products.brand_id', $id)
|
||||
->where('products.is_visible', true)
|
||||
->whereNull('products.parent_id')
|
||||
->where('products.stock', '>', 0)
|
||||
->where('categories.is_visible', true)
|
||||
->distinct()
|
||||
->get()
|
||||
->map(fn ($category) => [
|
||||
'id' => $category->id,
|
||||
'parent_id' => $category->parent_id,
|
||||
'name' => tr($category->name),
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user