File: D:/HostingSpaces/SBogers70/inter-mill.com/app/Komma/SimplePages/SimplePageService.php
<?php
namespace Komma\SimplePages;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use Komma\Blocks\BlockService;
use Komma\Images\ImageService;
use Komma\SimplePages\Models\SimplePage;
class SimplePageService {
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 getSimplePageContent($id){
if(!$page = SimplePage::where('id','=',$id)
->with('translation')
->with('images')
->where('active', 1)
->first()) \App::abort(404, 'Page not found');
$this->fillSimplePage($page);
return $page;
}
/**
* Get page by specific code name
*
* @param $codeName
* @return mixed
*/
public function getSimplePageByCodeName($codeName){
if(!$page = SimplePage::where('code_name','=', $codeName)
->with('translation')
->where('active', 1)
->first()) \App::abort(404, 'Page not found');
$this->fillSimplePage($page);
return $page;
}
/**
* Load the Route in other languages
* and the block where type is code_name
*
* @param Page $page
*/
public function fillSimplePage(SimplePage &$page){
$page->routeInOtherLanguages = $this->getOtherLanguageRoutes($page->id)->allTranslations;
$page->content = $this->blockService->getBlocksWhere('type', 'global');
}
/**
* Get Route and name of all pages
*
*
* @return mixed
*/
public function getAllRoutes(){
// Get pages with translations and routes
$url = SimplePage::with('translation')
->with('translation.route')
->where('active', '=', 1)
->where('lft', '!=', '1')
->get();
$linksArray = [];
foreach ($url as $link){
if(!isset($link->translation)) continue;
$route = $link->translation->route->route;
if($route == '/') $route = '';
$simpleRoute = (object)['route' => $route, 'name' => $link->translation->name];
if(isset($link->code_name) && $link->code_name != ''){
$simpleRoute->code_name = $link->code_name;
$linksArray[$link->code_name] = $simpleRoute;
}
else{
$linksArray[$link->id] = $simpleRoute;
}
}
return (object)$linksArray;
}
/**
* Get associate array based upon codeName (order)
* Mostly used for header and footer menu
*
* @param array $codesNames
* @return array
*/
public function getSimplePagesRoutesIn($codesNames = [], Collection $pages = null){
if( ! isset($pages)) $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 SimplePage::where('id', '=', $page_id)
->with('allTranslations')
->with('allTranslations.route')
->first();
}
}