]> _ Git - pmi.git/commitdiff
Fix #4893 @4
authorStephen Cameron <stephen@cubedesigners.com>
Mon, 7 Feb 2022 18:31:06 +0000 (19:31 +0100)
committerStephen Cameron <stephen@cubedesigners.com>
Mon, 7 Feb 2022 18:31:06 +0000 (19:31 +0100)
app/Http/Controllers/ApplicationController.php
app/Templates/Category.php
app/Templates/Products.php [new file with mode: 0644]

index 005c9e9e9bbe3c83560732701e7c981d7a61b00f..7dd38d446af2171cc10a9880a4dac42c0de40b83 100644 (file)
@@ -18,7 +18,7 @@ class ApplicationController extends CubistFrontController
             $this->_404();
         }
         $this->data['page'] = $application->getPageData();
-        $this->data['related'] = PageData::fromEntities(Product::with('media')->whereIn('id', $application->related)->get());
+        $this->data['related'] = PageData::fromEntities(Product::with('media')->whereVariant()->whereIn('id', $application->related)->get());
         return view('pages.application', $this->data);
     }
 }
index 1e28c42d4532bea3c760469c16c49679e5a386b7..a867097b85853b2ef7d7cee565be3e547d11d127 100644 (file)
@@ -5,7 +5,7 @@ namespace App\Templates;
 
 use App\Models\Product;
 use App\Models\ProductType;
-use Cubist\Backpack\app\Magic\Menu\PageItem;
+use Cubist\Backpack\app\Magic\PageData;
 
 class Category extends Base
 {
@@ -67,77 +67,11 @@ class Category extends Base
             ->orderBy('reference')
             ->get();
 
-        $data['products'] = [];
-        foreach ($products as $item) {
-            $data['products'][$item->id] = $item->getPageData();
-        }
+        $data['products'] = PageData::fromEntities($products);
 
         // Get available filters
         $filters = Product::getFilteredProducts($cat);
         $data['filters'] = $filters ? $filters['filters'] : []; // To be used by Vue component
         $data['filter_results'] = $filters ? $filters['results'] : [];
     }
-
-    /**
-     * @param \Cubist\Backpack\app\Magic\Menu\Item $menu
-     */
-    public function setMenuChildren($menu)
-    {
-        $filter = $menu->getPageData()->get('filter', '');
-        if($filter!==''){
-            return;
-        }
-
-        $products = self::_getProducts($menu->getVariant(), $menu->getLocale());
-        $data = $menu->getPageData();
-        $cat = $data->get('category');
-        $root = $menu->getRoot();
-
-        foreach ($products as $product) {
-            if ($product->product_type != $cat) {
-                continue;
-            }
-            if (!$product->online) {
-                continue;
-            }
-
-            $product->setLocale('fr');
-            $slug_fr=$product->getPageData()->get('slug');
-            $product->setLocale($menu->getLocale());
-
-            $canonical = $href = $product->getSlugOrTitleAttribute();
-            if ($root->findOneByHref($canonical) !== null) {
-                continue;
-            }
-
-            $detail = new PageItem();
-            $detail->setLocale($menu->getLocale());
-            $detail->initFromEntity($product);
-            $detail->setTitle($product->name);
-            $detail->setCanonical($canonical);
-            $detail->setHref($href);
-            $detail->setId('product/' . $product->id);
-            $detail->setController(['controller' => 'ProductController', 'action' => 'productDetails', 'params' => ['id' => $product->id]]);
-            $detail->hideInAllMenus();
-            $detail->addUrlAliases([$slug_fr]);
-            $menu->addChild($detail);
-        }
-    }
-
-    /**
-     * @param $variant
-     * @param $locale
-     * @return Product[]
-     */
-    public static function _getProducts($variant, $locale)
-    {
-
-        if (!isset(self::$_products[$variant])) {
-            self::$_products[$variant] = [];
-        }
-        if (!isset(self::$_products[$variant][$locale])) {
-            self::$_products[$variant][$locale] = Product::whereVariant($variant)->get();
-        }
-        return self::$_products[$variant][$locale];
-    }
 }
diff --git a/app/Templates/Products.php b/app/Templates/Products.php
new file mode 100644 (file)
index 0000000..3de3ebe
--- /dev/null
@@ -0,0 +1,79 @@
+<?php
+
+
+namespace App\Templates;
+
+use App\Models\Product;
+use Cubist\Backpack\app\Magic\Menu\PageItem;
+
+/*
+ * This template serves as a top level placeholder for the product pages.
+ * It is mainly responsible for generating all the product URLs (previously
+ * the Category template did this, but it was very inefficient and hard to
+ * debug with some products being missed...)
+*/
+class Products extends Base
+{
+    protected $_virtual = true;
+    protected $_navigable = false;
+    protected static $_products = [];
+
+    public function getName()
+    {
+        return 'Produits (Master Placeholder)';
+    }
+
+    /**
+     * @param \Cubist\Backpack\app\Magic\Menu\Item $menu
+     */
+    public function setMenuChildren($menu)
+    {
+        $products = self::_getProducts($menu->getVariant(), $menu->getLocale());
+
+        $root = $menu->getRoot();
+
+        foreach ($products as $product) {
+
+            $product->setLocale('fr');
+            $slug_fr = $product->getPageData()->get('slug');
+            $product->setLocale($menu->getLocale());
+
+            $canonical = $href = $product->getSlugOrTitleAttribute();
+            if ($root->findOneByHref($canonical) !== null) {
+                continue;
+            }
+
+            $detail = new PageItem();
+            $detail->setLocale($menu->getLocale());
+            $detail->initFromEntity($product);
+            $detail->setTitle($product->name);
+            $detail->setCanonical($canonical);
+            $detail->setHref($href);
+            $detail->setId('product/' . $product->id);
+            $detail->setController(['controller' => 'ProductController', 'action' => 'productDetails', 'params' => ['id' => $product->id]]);
+            $detail->hideInAllMenus();
+            $detail->addUrlAliases([$slug_fr]);
+            $menu->addChild($detail);
+        }
+    }
+
+    /**
+     * @param $variant
+     * @param $locale
+     * @return Product[]
+     */
+    public static function _getProducts($variant, $locale)
+    {
+
+        if (!isset(self::$_products[$variant])) {
+            self::$_products[$variant] = [];
+        }
+        if (!isset(self::$_products[$variant][$locale])) {
+            self::$_products[$variant][$locale] = Product::whereVariant($variant)
+                ->where('online', 1)
+                ->where('public', 1)
+                ->get();
+        }
+        return self::$_products[$variant][$locale];
+    }
+}