From: Stephen Cameron Date: Mon, 2 May 2022 16:12:09 +0000 (+0200) Subject: Support category pages at any depth + code refactor. Done #5270 @2 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=8a801b434e789cbad7add9514d568d1c3ffdfbdb;p=pmi.git Support category pages at any depth + code refactor. Done #5270 @2 --- diff --git a/app/Templates/CategoryListing.php b/app/Templates/CategoryListing.php index 6e1890f..4cb256f 100644 --- a/app/Templates/CategoryListing.php +++ b/app/Templates/CategoryListing.php @@ -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); + } }