File: D:/HostingSpaces/SBogers10/zuiderbos.komma.pro/app/Komma/Pages/PageService.php
<?php
namespace Komma\Pages;
use Komma\Blocks\BlockService;
use Komma\Images\ImageService;
use Komma\Pages\Models\Page;
use Komma\Pages\Models\PageTranslation;
class PageService
{
protected $imageService;
protected $blockService;
public function __construct(ImageService $imageService, BlockService $blockService)
{
$this->imageService = $imageService;
$this->blockService = $blockService;
}
/**
* Get specific page content
* with this id and current language
*
* @param $id
* @return mixed
*/
public function getPageContent($id)
{
if (! $page = Page::where('id', '=', $id)
->where('active', 1)
->first()) {
\App::abort(404, 'Page not found');
}
if (isset($page->images) && $page->images->count() != 0) {
$page->images = $this->imageService->orderImagesOnLanguage($page->images);
}
$this->fillPage($page);
return $page;
}
/**
* Get page by specific code name
*
* @param $codeName
* @return mixed
*/
public function getPageByCodeName($codeName)
{
if (! $page = Page::where('code_name', '=', $codeName)
->with('blocks.translation')
->with('translation')
->where('active', 1)
->first()) {
\App::abort(404, 'Page not found');
}
if (isset($page->images) && $page->images->count() != 0) {
$page->images = $this->imageService->orderImagesOnLanguage($page->images);
}
$this->fillPage($page);
return $page;
}
/**
* Get page by route name
*
* @param $route
* @return mixed
*/
public function getPageByRoute($route)
{
// Get route by URI
if (! $route = \DB::table('routes')
->where('route', '=', $route)
->first()
) {
\App::abort(404, 'Page not found');
}
$route = PageTranslation::where('id', $route->routable_id)->first();
$page = $route->page;
$page = $this->getPageContent($page->id);
return $page;
}
/**
* Load the Route in other languages
* and the block where type is code_name
*
* @param Page $page
*/
public function fillPage(Page &$page)
{
//$page->routeInOtherLanguages = $this->getOtherLanguageRoutes($page->id)->allTranslations;
//$page->content = $this->blockService->getBlocksWhere('type', $page->code_name);
$page->content = $this->blockService->getContent($page->code_name);
$page->depth = $page->getDepth();
}
/**
* Get Route and name of all pages
*
*
* @return mixed
*/
public function getAllRoutes()
{
// Get pages with translations and routes
$url = Page::with('translation')
->with('translation.route')
->where('pages.active', '=', 1)
->where('pages.code_name', '!=', 'null')
->get();
$linksArray = [];
foreach ($url as $link) {
if (! isset($link->translation)) {
continue;
}
$route = $link->translation->route->route;
if ($route == '/') {
$route = '';
}
$linksArray[$link->code_name] = (object) ['route' => $route,
'name' => $link->translation->name,
'code_name' => $link->code_name,
'page'=> $link, ];
}
return (object) $linksArray;
}
/**
* Get associate array based upon codeName (order)
* Mostly used for header and footer menu
*
* @param array $codesNames
* @return array
*/
public function getPagesRoutesIn($codesNames = [])
{
$pages = $this->getAllRoutes();
$pageRoutes = [];
foreach ($codesNames as $codeName) {
if (! isset($pages->$codeName)) {
continue;
}
$pageRoutes[] = (object) [
'name' => $pages->$codeName->name,
'route' => $pages->$codeName->route,
'code_name' => $pages->$codeName->code_name,
];
}
return $pageRoutes;
}
/**
* Get all translations of an page
* based upon this page id
*
* @param $page_id
* @return mixed
*/
public function getOtherLanguageRoutes($page_id)
{
return Page::where('id', '=', $page_id)
->with('allTranslations')
->with('allTranslations.route')
->first();
}
public function getMetaData($code_name = '')
{
$page = Page::where('code_name', $code_name)
->with('translation')
->first();
return (object) ['meta_title' => $page->translation->meta_title, 'meta_description' => $page->translation->meta_description];
}
/**
* Get the highest parent of the given page
*
* @param Page $page
*/
public function getRootParent(Page $page)
{
$parent = $page->getParent();
// If this parent isn't on the root of the branch
// load his parent till depth is 1 (first layer of the tree)
if ($parent->getDepth() > 1) {
$parent = $this->getRootParent($parent);
}
return $parent;
}
public function getPageTree()
{
if (! $page = Page::where('id', 1)
->first()) {
\App::abort(404, 'Page not found');
}
return $page;
}
}