File: D:/HostingSpaces/centrum8a/centrum8a.com/app/KommaApp/Pages/PageService.php
<?php
namespace App\KommaApp\Pages;
use App\KommaApp\Pages\Models\Page;
class PageService
{
/**
* Fetch translated page routes
*
* @return object | bool
*/
public function getAllTranslatedPageRoutes()
{
if(!\App::getSite()) return false;
// Find all pages
if( ! $pages = \App::getSite()
->pages()
->where('active',1)
->where('lft', '!=', 1)
->orderBy('lft','asc')
->with('translation')
->with('translation.route')
->get()
) return false;
$routes = [];
// Loop through pages
foreach($pages as $key => $page)
{
if(isset($page->translation) && isset($page->translation->route)){
$routes[$page->code_name] = (object)[
'name' => $page->translation->name,
'route' => $page->translation->route->alias,
'node' => $page
];
}
}
return (object)$routes;
}
/**
* Get page by specific code name
*
* @param $codeName
* @return Page
*/
public function getPageByCodeName($codeName): Page
{
if( ! $page = \App::getSite()
->pages()
->where('code_name','=', $codeName)
->with('translation')
->where('active', 1)
->first()) \App::abort(404);
return $page;
}
/**
* Generate breadcrumb tree of the page
*
* @param Page $page
* @param $links
*/
public function generateBreadcrumbTree(Page &$page, $links){
// Generate the right language root path
$urlString = '/';
if( \App::getSite()->default_language_id !== \App::getLanguage()->id){
$urlString.= \App::getLanguage()->iso_2;
}
// Make the root breadcrumb
$tree = [
(object)[
'name' => \Config::get('site.'.\App::getSite()->slug.'.smallName'),
'url' => $urlString,
'node' => $links->home
]
];
// If we are on the home page the tree is already done so we can return
if($page->code_name === 'home'){
$page->breadcrumbTree = $tree;
return;
}
// Else make the tree through the page models parents
$treeArray = $this->generateTreeArray($page);
//Append the tree in reversed order to the breadcrumb
$treeArray = array_reverse($treeArray);
foreach ($treeArray as $leaf){
// This should generally work, but for fallback we also check it
if(isset($links->{$leaf->code_name})) {
$tree[] = (object)[
'name' => $links->{$leaf->code_name}->name,
'url' => '/' .$links->{$leaf->code_name}->route,
'node' => $links->{$leaf->code_name},
];
}
// Just in case it shouldn't work, load the relation
else{
$leaf = $leaf->load(['translation', 'translation.route']);
$tree[] = (object)[
'name' => $leaf->translation->name,
'url' => $leaf->translation->route->alias,
'node' => $leaf,
];
}
}
// Bind the generated tree to the page
$page->breadcrumbTree = $tree;
}
/**
* Generate tree array by getting the parent till it is the root Page
*
* @param Page $page
* @param array $tree
* @param int $emergencyBreak
* @return array
*/
private function generateTreeArray(Page $page, $tree = [], $emergencyBreak = 0){
if($emergencyBreak >= 10) throw new \RuntimeException('Tree building failed because of emergency break because the index is too deep');
else $emergencyBreak++;
if($page->lft === 1) return $tree;
else{
$tree[] = $page;
return $this->generateTreeArray($page->getParent(), $tree, $emergencyBreak);
}
}
}