File: D:/HostingSpaces/SBogers95/rentman.io/app/Komma/Pages/PageService.php
<?php
namespace App\Komma\Pages;
use App\Komma\Base\Service;
use App\Komma\CustomerStories\Models\CustomerStory;
use App\Komma\Pages\Models\Page;
class PageService extends Service
{
/**
* Append pages to links
*
* @param $links
*/
public function appendPagesToLinks(&$links)
{
// Find all pages
if (! $pages = $this->site
->pages()
->where('lft', '!=', 1)
->orderBy('lft', 'asc')
->has('translations')
->with('translations', 'translations.route')
->get()
) {
return;
}
// To save an additional request,
// we manually append the (current language) translation to the model
// and remove the inactive / not working pages.
foreach ($pages as $key => $page) {
// Through the query builder we know it has translations
foreach ($page->translations as $translationCollectionKey => $translation) {
// Check that a translation is active and has a slug
if (! $translation->active || empty($translation->slug || empty($translation->route))) {
// Remove translation from collection
$page->translations->forget($translationCollectionKey);
continue;
}
// TODO: setRelation ???
// When it matches the app language (id) append it as it's translation
if (\App::getLanguage()->id == $translation->language_id) {
$page->translation = $translation;
}
}
// Only append page to links if it has a translation
if (empty($page->translation) || ! $page->translation->active || empty($page->translation->route)) {
$links->{'_'.$page->code_name} = (object) [
'node' => $page,
];
continue;
}
$links->{$page->code_name} = (object) [
'name' => $page->translation->name,
'route' => $page->translation->route->alias,
'node' => $page,
];
}
}
/**
* Get page by specific code name
*
* @param $codeName
* @return Page
*/
public function getPageByCodeName($codeName): Page
{
if (! $page = $this->site
->pages()
->where('code_name', '=', $codeName)
->first()) {
\App::abort(404);
}
return $page;
}
public function makeLanguageSwitchForPage($page, $home = null)
{
$languageMenu = (object) [
'withoutFallback' => [],
'withFallback' => [],
];
$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 != '/') {
$route = '/'.$pageTranslation->route->alias;
} else {
$route = $pageTranslation->route->alias;
}
$languageMenu->withoutFallback[$language->iso_2] = $route;
$languageMenu->withFallback[$language->iso_2] = $route;
}
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 != '/') {
$route = '/'.$homeTranslation->route->alias;
} else {
$route = $homeTranslation->route->alias;
}
$languageMenu->withFallback[$language->iso_2] = $route;
continue;
}
}
return $languageMenu;
}
public function extendLanguageMenuWithResource(&$languageMenu, $resource, $home)
{
if (count($languageMenu->withFallback) == 0) {
return;
}
$resourceTranslations = $resource->translations->keyBy('language_id');
$homeTranslations = $home->node->translations->keyBy('language_id');
foreach ($this->site->languages as $language) {
if ($resourceTranslation = $resourceTranslations->get($language->id)) {
// Append the found resource translation to given language menu item if active
if (isset($languageMenu->withFallback[$language->iso_2]) && $resourceTranslation->active) {
// On the customer story the slug isn't on the translation but on the model
if (is_a($resource, CustomerStory::class)) {
$languageMenu->withFallback[$language->iso_2] .= '/'.$resource->slug;
} else {
$languageMenu->withFallback[$language->iso_2] .= '/'.$resourceTranslation->slug;
}
}
if (isset($languageMenu->withoutFallback[$language->iso_2]) && $resourceTranslation->active) {
// On the customer story the slug isn't on the translation but on the model
if (is_a($resource, CustomerStory::class)) {
$languageMenu->withoutFallback[$language->iso_2] .= '/'.$resource->slug;
} else {
$languageMenu->withoutFallback[$language->iso_2] .= '/'.$resourceTranslation->slug;
}
} else {
unset($languageMenu->withoutFallback[$language->iso_2]);
}
continue;
} else {
unset($languageMenu->withoutFallback[$language->iso_2]);
}
if (! isset($languageMenu->withFallback[$language->iso_2])) {
if ($homeTranslation = $homeTranslations->get($language->id)) {
if ($homeTranslation->route->alias != '/') {
$languageMenu->withFallback[$language->iso_2] = '/'.$homeTranslation->route->alias;
} else {
$languageMenu->withFallback[$language->iso_2] = $homeTranslation->route->alias;
}
}
continue;
}
}
}
/**
* 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::get('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);
}
}
}