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