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/topswtw.komma.pro/app/KommaApp/Shop/Pages/PageService.php
<?php


namespace KommaApp\Shop\Pages;

use Illuminate\Support\Facades\URL;
use KommaApp\Shop\Menus\SidebarMenu;
use KommaApp\Shop\Tree\Tree;
use KommaApp\Shop\Tree\TreeNode;
use KommaApp\Shop\Tree\TreeService;
use KommaApp\Shop\Checkout\Shipping\Models\ShippingCost;
use KommaApp\Shop\Routes\Route;

class PageService extends TreeService
{
    protected $pageEntities = [];

    protected $pageRepository;

    protected $sidebarMenu;

    /**
     * @param PageRepository $pageRepository
     * @param Tree           $tree
     */
    public function __construct(PageRepository $pageRepository, SidebarMenu $sidebarMenu)
    {
        $this->pageRepository = $pageRepository;
        $this->sidebarMenu = $sidebarMenu;
    }

    public function boot()
    {
        $pages = $this->pageRepository->getCodeNameRoutes();

        foreach ($pages as $page) {
            $this->pageEntities[$page->codeName] = new TreeNode([
                'name' => $page->name,
                'route' => $page->route
            ]);
        }
    }

    public function page($codeName)
    {
        if (empty($this->pageEntities)) $this->boot();
        if (isset($this->pageEntities[$codeName])) {
            return $this->pageEntities[$codeName];
        }
        return new TreeNode([]);
    }

    public function getPageByShopAndLang($codeName, $shopId, $languageId)
    {

        $route = Route::select('routes.*')
            ->leftJoin('page_translations', 'page_translations.id', '=', 'routes.routable_id')
            ->leftJoin('pages', 'pages.id', '=', 'page_translations.page_id')
            ->where('pages.code_name', '=', $codeName)
            ->where('routes.routable_type', '=', 'Komma\Kms\Pages\Models\PageTranslation')
            ->where('routes.shop_id', '=', $shopId)
            ->where('pages.active', 1)
            ->where('page_translations.language_id', '=', $languageId)
            ->first();

        if ($route) return $route->route;

        return NULL;


    }

    public function getTreeNodes($withIndex = true)
    {
        $nodes = $this->pageRepository->getTreeNodes();
        if (!$withIndex) unset($nodes['index']);
        return $nodes;
    }

    public function getImagesByPageId($pageId)
    {
        return $this->pageRepository->getImagesByPageId($pageId);
    }

    public function renderDefaultPage($pageId)
    {
        $node = $this->getNodeById($pageId);
        $rootNode = $this->getRootNode($node);
        $sidebarMenu = $this->sidebarMenu->create($rootNode);
        $images = $this->getImagesByPageId($pageId);


        $view = \View::make( viewPrefix() . 'pages.default')
            ->withEntity($node)
            ->withRoot($rootNode)
            ->withMenu($sidebarMenu)
            ->withImages($images);

        //Check if the call to action is free_shipping
        if ($node->call_to_action == 'free_shipping') {
            //yes get the shippingCosts
            $shipping_costs = ShippingCost::where('country_id', \Shop::getCountryId())->first();
            $view = $view->with('ShippingCosts', $shipping_costs);
        }

        return $view;
    }

    public function getBackRoute($back_route)
    {
        //We are going to check if the session searh_query exist
        if (\Session::has('search_query')) {
            //if so check if the privous page has ?q={searchquery}
            //encode the string en prepare for regex
            $query = preg_quote(urlencode(\Session::pull('search_query')));
            $pattern = '#\?q=' . str_replace(' ', '\+', $query) . '#';
            if (preg_match($pattern, Url::previous())) {
                //if true return previous
                return url::previous();
            }
        }

        return $back_route;
    }
}