File: D:/HostingSpaces/SBogers10/umans.komma.pro/app/Komma/Pages/PageService.php
<?php
namespace Komma\Pages;
use Carbon\Carbon;
use Komma\Blocks\BlockService;
use Komma\Images\ImageService;
use Komma\Pages\Models\Page;
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)
->with('translation')
->with('blocks.translation')
->with('images')
->where('active', 1)
->first()) \App::abort(404, 'Page not found');
if($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($page->images->count() != 0) $page->images = $this->imageService->orderImagesOnLanguage($page->images);
$this->fillPage($page);
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;
if(in_array($page->code_name, ['exterior'])) $page->content = $this->blockService->getBlocksWhere('type', 'home');
else $page->content = $this->blockService->getBlocksWhere('type', $page->code_name);
}
/**
* 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];
}
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;
}
public function getPagesByTree($lft = 1, $rgt = 2){
return Page::with('translation')
->with('translation.route')
->where('pages.active', '=', 1)
->where('pages.code_name', '!=', 'null')
->where('pages.lft', '>', $lft)
->where('pages.lft', '<', $rgt)
->orderBy('pages.lft')
->get();
}
/**
* 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];
}
}