]> _ Git - pmi.git/commitdiff
fix #2961 @2
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 22 Aug 2019 15:52:45 +0000 (17:52 +0200)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 22 Aug 2019 15:52:45 +0000 (17:52 +0200)
app/Http/Controllers/AjaxController.php
resources/views/pages/products.blade.php

index 5c8caa9ddd56cd0b0d68e0253c8a0b52769c6182..0f982bb280394b8541b54af287ed437854dd07fa 100644 (file)
@@ -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;
+    }
 }
index c9de1e851e2e0f21e2106a8858728dfec0c3f38e..e493041423e1982be3a3581d9d03e425c3ed6975 100644 (file)
@@ -97,7 +97,7 @@
                         @php
                             $product_URL = $nav->getHrefById("product/{$product->id}");
                         @endphp
-                        <div class="product-grid-item bg-grey-200">
+                        <div data-product-id="{{$id}}" class="product-grid-item bg-grey-200">
                             {{-- Image holder --}}
                             <a href="{{ $product_URL }}">
                                 <div class="product-img-holder">
 
                             {{-- Product details --}}
                             <div class="p-4">
-                                <h3><a class="text-navy" href="{{ $product_URL }}">{{ $product->get('reference') }}</a></h3>
+                                <h3><a class="text-navy" href="{{ $product_URL }}">{{ $product->get('reference') }}</a>
+                                </h3>
                                 <div class="product-highlights text-sm">
                                     {{$product->get('name')}}
                                 </div>