File: D:/HostingSpaces/SBogers10/boldt.komma.pro/app/Komma/Pages/PageService.php
<?php
namespace App\Komma\Pages;
use App\Komma\Base\Service;
use App\Komma\Pages\Models\Page;
final class PageService extends Service
{
/**
* Fetch translated page routes
*
* @return object | bool
*/
public function getAllTranslatedPageRoutes()
{
if(!$this->site) return false;
// Find all pages
if( ! $pages = $this->site
->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 = $this->site
->pages()
->where('code_name','=', $codeName)
->with('translation')
->where('active', 1)
->first()) \App::abort(404);
return $page;
}
public function makeLanguageSwitchForPage($page, $home = null)
{
$languageMenu = [];
$pageTranslations = $page->node->translations->keyBy('language_id');
if(!empty($home)) $homeTranslations = $home->node->translations->keyBy('language_id');
foreach ($this->site->languages as $language)
{
if($pageTranslation = $pageTranslations->get($language->id))
{
if(isset($pageTranslation->route)){
if($pageTranslation->route->alias != '/') $languageMenu[$language->iso_2] = $pageTranslation->route->alias;
else $languageMenu[$language->iso_2] = $pageTranslation->route->alias;
}
continue;
}
// If page hasn't a translation grab the home route (should always exists
if(!empty($home) && $homeTranslation = $homeTranslations->get($language->id))
{
if($homeTranslation->route->alias != '/') $languageMenu[$language->iso_2] = $homeTranslation->route->alias;
else $languageMenu[$language->iso_2] = $homeTranslation->route->alias;
continue;
}
}
return $languageMenu;
}
/**
* 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( $this->site->default_language_id !== \App::getLanguage()->id){
$urlString.= \App::getLanguage()->iso_2;
}
// Make the root breadcrumb
$tree = [
(object)[
'name' => config('site.'.$this->site->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);
}
}
}