]> _ Git - pmi.git/commitdiff
Support category pages at any depth + code refactor. Done #5270 @2
authorStephen Cameron <stephen@cubedesigners.com>
Mon, 2 May 2022 16:12:09 +0000 (18:12 +0200)
committerStephen Cameron <stephen@cubedesigners.com>
Mon, 2 May 2022 16:12:09 +0000 (18:12 +0200)
app/Templates/CategoryListing.php

index 6e1890f4a863bb84bf601c024620fc7d0468a7ca..4cb256f1470c770d6f804f7f6d573d94210e5416 100644 (file)
@@ -42,30 +42,35 @@ class CategoryListing extends Base
     {
         parent::setData($data);
 
-        // Get all the child pages of this page that use the template 'category'.
+        // Get all *nested* child pages that use the template 'category'.
         // These will be used to populate the grid of sub-categories
-        $current_page_ID = $data['page']->id;
-        $categories = Page::with('media')
-            ->where('parent_id', $current_page_ID)
-            ->where('template', 'category')
-            ->orderBy('lft')
-            ->get();
-
-        $data['categories'] = [];
-
-        foreach ($categories as $category) {
-            if (!$category->getAttribute('status')) {
-                continue;
-            }
-            if (!in_array(App::getVariant(), $category->getAttribute('variant'))) {
-                continue;
-            }
-            $data['categories'][$category->id] = $category->getPageData();
-        }
+        $data['categories'] = $this->getChildPages($data['page']->id);
 
         // Get related applications, if any
         if (isset($data['page']->related_applications) && count($data['page']->related_applications) > 0) {
             $data['applications'] = PageData::fromEntities(Application::whereVariant()->whereIn('id', $data['page']->related_applications)->get());
         }
     }
+
+    public function getChildPages($parent_ID) {
+
+        // Find the lft and rgt values for the parent so that we can fetch all nested children in the tree
+        $parent = Page::select(['lft', 'rgt'])->where('id', $parent_ID)->first();
+
+        if (!$parent) {
+            return [];
+        }
+
+        $locale = App::getLocale();
+
+        $pages = Page::with('media')
+            ->whereVariant()
+            ->where("status->$locale", 1)
+            ->where('template', 'category')
+            ->whereBetween('lft', [$parent['lft'], $parent['rgt']]) // Gets nested children at any depth
+            ->orderBy('lft')
+            ->get();
+
+        return PageData::fromEntities($pages);
+    }
 }