File: D:/HostingSpaces/SBogers10/ijzerenman.komma.pro/app/Custom/Controllers/BaseController.php
<?php
namespace Komma\Controllers;
use Illuminate\Routing\Controller;
use Komma\Pages\PageRepository;
class BaseController extends Controller
{
/**
* @var \Komma\Pages\PageRepository
*/
protected $currentPageEntity;
public function __construct()
{
\View::share('komma',
strpos(\Request::getHttpHost(), 'komma') !== false ||
strpos(\Request::getHttpHost(), 'local') !== false);
}
/**
* @param $param
* @param $pageEntity
* @param null $method
* @return mixed
*/
public function routeProcessor($data, $param, $method = null)
{
\Current::set($data);
$this->setGlobalNavigation();
/*
|--------------------------------------------------------------------------
| Check for method
|--------------------------------------------------------------------------
*/
if($method != null) return $this->{$method}();
/*
|--------------------------------------------------------------------------
| If no method specified and no param, go to index
|--------------------------------------------------------------------------
*/
if(empty($param) || $param == '/') return $this->index();
/*
|--------------------------------------------------------------------------
| If a param is found, check if the param is and existing method
|--------------------------------------------------------------------------
*/
// Get urls
$urls = explode('?',$param);
$urlsWithoutGet = $urls[0];
$urls = explode('/', $urlsWithoutGet);
// First url is the method
$method = camel_case($urls[0]);
// Get the parameters
unset($urls[0]);
$arguments = array_values($urls);
// Call a dynamic method with the arguments
if(method_exists($this, $method))
{
return call_user_func_array([$this, $method], $arguments);
}
/*
|--------------------------------------------------------------------------
| If nothing found, trow a 404 exception
|--------------------------------------------------------------------------
*/
return \App::abort(404);
}
/**
* @param $pageEntity
* @return mixed
*/
public function renderView($view, $entity)
{
/*
|--------------------------------------------------------------------------
| Return normal view
|--------------------------------------------------------------------------
*/
if ( ! \Request::ajax()) return $view;
/*
|--------------------------------------------------------------------------
| Return data as json
|--------------------------------------------------------------------------
*/
return \Response::json([
'view' => $view,
'title' => $entity->name,
'name' => $entity->codeName()
]);
}
/**
* Load global navigation in view
*/
public function setGlobalNavigation()
{
// Get root
$pageRepository = new PageRepository();
$root = $pageRepository->getRoot();
// Return immediate children of root
$globals = $pageRepository->getImmediateChildren($root->id);
// Current Global id
isset(\Current::parents()[0]->id) ?
$currentGlobalId = \Current::parents()[0]->id :
$currentGlobalId = null;
$currentSubPages = null;
// Go through each global page
foreach($globals as $key => $global)
{
// Add sub pages for this global to his children
$globals[$key]->children = $pageRepository->getImmediateChildren($global->id);
if($currentGlobalId == $global->id)
{
$currentSubPages = $globals[$key]->children;
}
// Add a name for display
$globals[$key]->displayName = str_replace('Strand','<span class="grey">Strand</span>',$global->name);
// Home route always links to the root
if( $global->codeName == 'home') $globals[$key]->route = '';
}
// Share the data with whatever view is loaded
\View::share(['globals' => $globals ]);
\View::share(['subPages' => $currentSubPages ]);
}
}