HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/base.komma.pro/app/Pages/PageService.php
<?php


namespace App\Pages;

use App\Base\Service;
use App\Buttons\Models\Button;
use App\Pages\Models\Page;
use App\Servicepoints\Models\Servicepoint;

final class PageService extends Service
{

    /**
     * @param  int  $id
     * @return \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\HasMany|object|null
     */
    public function getPage(int $id)
    {
        // Find all pages
        if( ! $page = $this->site
            ->pages()
            ->where('active',1)
            ->where('id', $id)
            ->first()
        ) abort(404);

        return $page;
    }

    /**
     * Fetch translated page routes
     *
     * @return object | bool
     */
    public function getAllTranslatedPageRoutes()
    {
        // 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;
    }

    public function setSharableVariables($servicePointId = null, $servicePointButtonId = null, $servicePointHeading = null) {

        // if no servicePoint heading title is set on the page use te value from global config
        if(empty($servicePointHeading)) $servicePointHeading = config('site.global_servicePoint_heading') ?? null;

        // if no servicePointId is set on the page use tHe value from global config
        if(!isset($servicePointId) || $servicePointId == '0') $servicePointId = config('site.global_servicePoint_id') ?? null;
        $servicePoint = Servicepoint::where('id', $servicePointId)->with('translations', 'documents')->first();

        // if no servicePointButtonId is set on the page use te value from global config
        if(!isset($servicePointButtonId) || $servicePointButtonId == '0') $servicePointButtonId = config('site.global_servicePoint_button_id') ?? null;
        $servicePointButton = Button::where('id', $servicePointButtonId)->with('translations')->first();

        view()->share([
            'servicePointHeading' => $servicePointHeading,
            'servicePoint' => $servicePoint,
            'servicePointButton' => $servicePointButton,
        ]);
    }

    /**
     * Generate a sub navigation (children) through the page
     *
     * @param  Page  $page
     * @param $links
     * @return array
     */
    public function getSubNav(Page $page, $links)
    {
        $subNav = [];

        $children = $page->findChildren();
        foreach ($children as $child) $subNav[] = $links->{$child->code_name};

        return $subNav;
    }


    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;
    }

}