From 1f2942ab6507b620e20efa611a2fc1f692b983d9 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Thu, 22 Aug 2019 17:52:45 +0200 Subject: [PATCH] fix #2961 @2 --- app/Http/Controllers/AjaxController.php | 129 +++++++++++++++++++++++ resources/views/pages/products.blade.php | 5 +- 2 files changed, 132 insertions(+), 2 deletions(-) diff --git a/app/Http/Controllers/AjaxController.php b/app/Http/Controllers/AjaxController.php index 5c8caa9..0f982bb 100644 --- a/app/Http/Controllers/AjaxController.php +++ b/app/Http/Controllers/AjaxController.php @@ -6,12 +6,16 @@ namespace App\Http\Controllers; use App\Models\Page; use App\Models\Product; +use App\Models\ProductType; +use App\Models\Specification; use Cubist\Backpack\app\Http\Controllers\CubistFrontController; use Cubist\Backpack\app\Magic\PageData; use Cubist\Backpack\app\Magic\Search; use Illuminate\Http\Request; +use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\Mail; use Illuminate\Support\Facades\Validator; +use Illuminate\Support\Str; class AjaxController extends CubistFrontController { @@ -179,4 +183,129 @@ class AjaxController extends CubistFrontController { return Search::query($request->q, $request->type ?? null, $request->limit ?? null); } + + public function filtercatalog(Request $request) + { + $product_type = $request->productType; + $filterValues = $request->filter ?? []; + + $res = []; + $type = ProductType::find($product_type); + $rawproducts = Product::where('product_type', $product_type)->where('online', 1)->where('public', 1)->get(); + $products = []; + foreach ($rawproducts as $idx => $rawproduct) { + $products[$idx] = $rawproduct->getPageData(); + } + $locale = App::getLocale(); + + $specifications = $type->filters; + + $allmatches = []; + + $filters = Specification::whereIn('id', $specifications)->get(); + $res['filters'] = []; + foreach ($filters as $filter) { + + $filterValue = null; + if (isset($filterValues[$filter->id])) { + $filterValue = explode(';', $filterValues[$filter->id]); + sort($filterValue); + } + + $specname = 's_' . Str::snake($filter->name); + $data = $filter->getPageData(); + $f = ['label' => $data->label, + 'id' => $data->id, + 'type' => $data->type, + ]; + + $matching = []; + + if ($data->type == 'list') { + $options = []; + $values = []; + foreach ($products as $product) { + $v = $product->get($specname); + if (!$v) { + $v = '-'; + } + if (!isset($values[$v])) { + $values[$v] = 0; + } + $values[$v]++; + if (null === $filterValue) { + $matching[] = $product->id; + } else { + if (in_array($v, $filterValue)) { + $matching[] = $product->id; + } + } + } + foreach ($data->options as $index => $option) { + if (is_scalar($option)) { + $o = $option; + } else { + $o = $option[$locale] ?? $option['fr']; + } + if ($o) { + $nb = $values[$index] ?? 0; + if ($nb > 0) { + $options[] = ['label' => $o, 'value' => $index, 'nb_products' => $nb]; + } + } + } + if (isset($values['-'])) { + $options[] = ['label' => __('Non défini'), 'value' => '-', 'nb_products' => $values['-']]; + } + $f['options'] = $options; + } else if ($data->type == 'numeric' || $data->type == 'range') { + $f['min'] = INF; + $f['max'] = -INF; + $f['unit'] = $data->unit; + + if ($data->type == 'numeric') { + $f['prefix'] = $data['prefix']; + } else { + $f['prefix'] = ''; + } + + foreach ($products as $product) { + $v = $product->get($specname); + if ($data->type == 'range') { + $f['min'] = min($f['min'], $v['first'] ?? INF, $v['second'] ?? INF); + $f['max'] = max($f['max'], $v['first'] ?? -INF, $v['second'] ?? -INF); + if (null !== $filterValue && !($v['first'] == '' && $v['second'] == '')) { + $min = min($v['first'], $v['second']); + $max = max($v['first'], $v['second']); + + if ($min <= $filterValue[1] && $max >= $filterValue[0]) { + $matching[] = $product->id; + } + } + } else { + $f['min'] = min($f['min'], $v); + $f['max'] = min($f['max'], $v); + if (null !== $filterValue) { + if ($v >= $filterValue[0] && $v <= $filterValue[0]) { + $matching[] = $product->id; + } + } + } + if (null === $filterValue && $v != '') { + $matching[] = $product->id; + } + } + } else { + continue; + } + $f['matching'] = ['count' => count($matching), 'hits' => $matching]; + $allmatches[] = $matching; + + $res['filters'][$data->id] = $f; + } + + $intersection = call_user_func_array('array_intersect', $allmatches); + $res['results'] = ['count' => count($intersection), 'hits' => $intersection]; + return $res; + } } diff --git a/resources/views/pages/products.blade.php b/resources/views/pages/products.blade.php index c9de1e8..e493041 100644 --- a/resources/views/pages/products.blade.php +++ b/resources/views/pages/products.blade.php @@ -97,7 +97,7 @@ @php $product_URL = $nav->getHrefById("product/{$product->id}"); @endphp -
+