File: D:/HostingSpaces/brameda/brameda.nl/app/Komma/Pages/Kms/PageService.php
<?php
namespace App\Komma\Pages\Kms;
use App\Helpers\KommaHelpers;
use App\Komma\Documents\Kms\DocumentableInterface;
use App\Komma\Kms\Core\AbstractTranslatableModel;
use App\Komma\Kms\Core\Attributes\Attribute;
use App\Komma\Kms\Core\HasRoutesInterface;
use App\Komma\Kms\Core\Tree\NestedSets\Nodes\TreeModel;
use App\Komma\Kms\Core\Tree\NestedSets\Nodes\TreeModelInterface;
use App\Komma\Kms\Core\Sections\SectionService;
use App\Komma\Kms\Core\Sections\SectionTabItem;
use App\Komma\Globalization\Languages\Models\Language;
use App\Komma\Pages\Models\Page;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
final class PageService extends SectionService
{
protected $sortable = true;
function __construct()
{
$this->forModelName = Page::class;
parent::__construct();
}
/**
* This method will save an model
*
* @param $model Model or null
* @param Collection $sectionTabItems These must be filled with data. This is something you need to do yourself.
*
* @return mixed
*/
public function saveModel(Model $model = null, Collection $sectionTabItems): Model
{
/** @var TreeModel $model */
//Process Page Specific attributes
// Get parent_id of the input
$parentPage = $model->find(1);
// Set default to false
$currentParentId = null;
// Check if model id isset so if it is a new model or not
// if true then get current parent_id
if($model->id) $currentParentId = $model->getParentId();
//Check if parent page is found, model isset and parent page id isn't the current parent id
if($parentPage && $model && $parentPage->id !== $currentParentId)
{
$model->makeLastChildOf($parentPage);
}
$sectionTabItems->each(function($sectionTabItem, $key) use($model) {
/** @var SectionTabItem $sectionTabItem */
$attribute = $sectionTabItem->getAttribute();
$reference = $attribute->getsValueFromReference();
switch ($attribute->getsValueFrom())
{
case Attribute::ValueFromTranslationModel:
if($reference == 'name')
{
/** @var Language $language */
$language = $attribute->getAssociatedLanguage();
$translation = $this->getTranslationModelForModelByLanguage($model, $language);
$translation['slug'] = (str_slug($attribute->getValue()));
//Saving is done by the parent
}
break;
}
});
$model->save(); //Save the page
$model = parent::saveModel($model, $sectionTabItems); //First make sure we have a model and save the attributes in them from the SectionTabItem attributes
$this->siteService->linkModelToCurrentSite($model);
//Return the page
return $model;
}
/**
* Fills non-product specific attributes. Product specific attributes are processed in the parent
*
*
* @param Collection $sectionTabItems A collection containing implementations AbstractSectionTabItem's
* @param Model $model
* @return Collection
*/
public function fillAttributesWithData(Collection $sectionTabItems, Model $model)
{
$filledAttributesCollection = parent::fillAttributesWithData($sectionTabItems, $model);
$sectionTabItems->each(
function ($sectionTabItem, $key) use ($model, $filledAttributesCollection, &$quantityDiscountAttribute, &$quantityPriceAttribute) {
/** @var $sectionTabItem SectionTabItem */
if (!is_a($sectionTabItem->getAttribute(), Attribute::class)) throw new \InvalidArgumentException("One of the attributes in a AbstractSectionTabItem instance is not but must be an child instance of Attribute.");
$attribute = $sectionTabItem->getAttribute();
$valueReference = $sectionTabItem->getAttribute()->getsValueFromReference();
switch ($valueReference) {
case 'site_name':
$site = $this->siteService->getCurrentSite();
$attribute->setValue($site->name);
break;
}
}
);
return $filledAttributesCollection;
}
/**
* This method will remove an TranslatableModelInterface instance
*
* @param $model
* @throws \Exception
*/
public function destroyModel(Model $model)
{
/** @var Model|TreeModelInterface $model */
//delete the images
if(is_a($model, DocumentableInterface::class)) {
/** @var DocumentableInterface $model */
$this->documentService->deleteDocumentsForModel($model);
}
if(is_a($model,AbstractTranslatableModel::class)) {
/** @var AbstractTranslatableModel $model */
foreach ($model->translations()->get() as $translation) {
//Delete the route of the translation
if(is_a($translation, HasRoutesInterface::class)){
/** @var HasRoutesInterface $translation */
$translation->routes()->delete();
}
//Delete the translation
$translation->delete();
}
}
collect($model->findChildren())->each(function(Model $childModel) {
$this->destroyModel($childModel);
});
$model->delete();
}
}