File: D:/HostingSpaces/SBogers10/veltech.komma.pro/app/Komma/Kms/Core/KmsController.php
<?php
/**
*
*
* @author Komma <info@komma.pro>
* @copyright (c) 2012-2016, Komma
*/
namespace App\Komma\Kms\Core;
use App\Komma\Kms\Core\Sections\Section;
use App\Komma\Kms\Core\Tree\TreeService;
use App\Komma\Sites\SiteServiceInterface;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Routing\Controller;
class KmsController extends Controller
{
Use AuthorizesRequests;
/** @var SiteServiceInterface $siteService */
protected $siteService;
protected $user;
/** @var Section */
protected $section;
/** @var string $forModelName For which model the controller does its job */
protected $forModelName;
/** @var TreeService $treeService */
protected $treeService;
function __construct(Section $section)
{
//Authentication, check if the user is logged in
$this->middleware('auth:kms');
$this->validateForModel();
$this->section = $section;
$this->siteService = app(SiteServiceInterface::class);
$this->treeService = app(TreeService::class);
$this->treeService->setForModelName($this->forModelName);
$this->treeService->setSectionService($this->section->getSectionService());
}
/**
* Validate for model
*/
private function validateForModel()
{
if(!$this->forModelName) throw new \RuntimeException('Make sure you set (override) the $forModelName property in the controller ('.get_class($this).') to the eloquent model the controller does it\'s job for.');
}
/**
* Returns the section this controller works for
*
* @return Section
*/
public function getSection()
{
return $this->section;
}
/**
* Uses the siteService and the Tree service to create a root model if the model uses a tree algorithm
*/
public function createRootTreeModelIfNeeded()
{
$currentSite = $this->siteService->getCurrentSite();
//Also consider the root model as non existent when the site_id of all existing models is not the same as the current site_id.
$constrainingClosure = function ($query) use($currentSite) {
return $query->where('site_id', '=', $currentSite->id);
};
//Extra data to set on the root model when it needed to be created
$extraModelDataForNewRoot = ['site_id' => $currentSite->id];
$this->treeService->makeRootModelForModelnameIfNeeded($this->forModelName, $extraModelDataForNewRoot, $constrainingClosure);
}
}