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
{
{
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;
+ }
}