File: D:/HostingSpaces/groendesignkoencox/groendesign-koencox.be/app/Pages/Kms/PageController.php
<?php
namespace App\Pages\Kms;
/**
* @author Komma <info@komma.pro>
* @copyright (c) 2012-2016, Komma
*/
use App\Attributes\Attribute;
use App\Attributes\ComponentArea;
use App\Base\SectionControllerWithComponentAreas;
use App\Components\ComponentArea\ComponentAreaService;
use App\Pages\Models\PageTranslation;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Komma\KMS\Core\AbstractTranslatableModel;
use Komma\KMS\Core\AbstractTranslationModel;
use Komma\KMS\Core\SectionController;
use App\Pages\Models\Page;
use Komma\KMS\Core\Tree\NestedSets\Nodes\AbstractTranslatableTreeModel;
final class PageController extends SectionControllerWithComponentAreas
{
protected $sortable = true;
protected $slug = "pages";
protected $classModelName = Page::class;
protected $forTranslationModelName = PageTranslation::class;
/**
* @var ComponentAreaService
*/
/**
* @var PageRouteService
*/
private $routeService;
/**
* Constructor
*/
public function __construct()
{
$pageSection = new PageSection($this->slug);
parent::__construct($pageSection);
$this->routeService = new PageRouteService();
$this->modelService = new PageModelService();
}
/**
* @param Model $model
* @param Collection|null $attributesByValueFrom
* @return Model
*/
protected function save(Model $model, Collection $attributesByValueFrom = null): Model
{
/** @var AbstractTranslatableTreeModel $model */
if(!$model->exists) {
$rootModel = $this->classModelName::allRoot()->first();
$model->makeLastChildOf($rootModel);
}
$model = parent::save($model, $attributesByValueFrom);
$this->routeService->createOrUpdateRoutesForModelsTranslationsIfChanged($model);
return $model;
}
/**
* A function that MUST delegate to services to accomplish loading.
* It MUST NOT act like a service.
*
* @param Model $model
* @param Collection $attributesByValueFrom Keys must be ValueFrom integers. Values must be Attributes that have the ValueFrom integers
* @return Collection The same collection as you've passed in. Only "filled".
*/
protected function load(Model $model, Collection $attributesByValueFrom = null): Collection
{
$attributesByValueFrom = parent::load($model, $attributesByValueFrom);
$this->forEachComponentArea($model, $attributesByValueFrom->get(Attribute::ValueFromComponentArea), function(AbstractTranslationModel $translation, ComponentArea $attribute) {
$this->componentAreaService->loadAttribute($translation, $attribute);
});
return $attributesByValueFrom;
}
/**
* @param AbstractTranslatableModel $model
* @param Collection $componentAreaAttributes
* @param string $do
*/
protected function forEachComponentArea(AbstractTranslatableModel $model, Collection $componentAreaAttributes, \Closure $do)
{
if($this->forTranslationModelName && $componentAreaAttributes->count() > 0) {
if($componentAreaAttributes) {
$componentAreaAttributes->each(function (Attribute $attribute) use ($model, $do) {
if (($language = $attribute->getAssociatedLanguage()) && $model) {
$translationModel = $this->translationService->getTranslationModelForModelByLanguage($model, $language);
if ($translationModel) $do->call($this, $translationModel, $attribute);
}
});
}
}
}
/**
* This method is called when a item will be deleted
*
* @param Model $model
* @return mixed
* @throws \Exception
*/
public function destroy(Model $model)
{
$this->authorize('destroy', $model);
$this->routeService->destroyForModel($model);
return parent::destroy($model);
}
}