File: D:/HostingSpaces/SBogers10/honger.komma.pro/app/KommaApp/Posts/Kms/PostService.php
<?php
namespace App\KommaApp\Posts\Kms;
use App\KommaApp\Kms\Core\Attributes\Attribute;
use App\KommaApp\Kms\Core\NestedSets\Nodes\EloquentNode;
use App\KommaApp\Kms\Core\Sections\SectionService;
use App\KommaApp\Kms\Core\Sections\SectionTabItem;
use App\KommaApp\Languages\Models\Language;
use App\KommaApp\Pages\Models\PageTranslation;
use App\KommaApp\Posts\Models\Post;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
class PostService extends SectionService
{
protected $sortable = false;
protected $orderBy = 'date';
protected $orderReverse = true;
function __construct()
{
$this->forModelName = Post::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 EloquentNode $model */
//Process Page Specific attributes
// dd($model);
$sectionTabItems->each(function($sectionTabItem, $key) use($model) {
/** @var SectionTabItem $sectionTabItem */
$attribute = $sectionTabItem->getAttribute();
$reference = $attribute->getsValueFromReference();
switch ($attribute->getsValueFrom())
{
case Attribute::ValueFromModel:
if($reference == 'parent_id') {
// Get parent_id of the input
$parentId = $attribute->getValue();
$parentPage = $model->find($parentId);
// 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);
}
$attribute->mapValueFrom(Attribute::ValueFromItself, ''); //unset parent_id it so it won't get saved directly on the model in the parent_id field. The makeLastChildOf call above does save the parent id via lft rgt
} elseif($reference == 'code_name') {
}
break;
case Attribute::ValueFromTranslationModel;
/** @var Language $language */
$language = $attribute->getAssociatedLanguage();
if(!$model->exists) $model->save();
$translation = $this->getOrCreateTranslationModelForModel($model, $language);
if($reference == 'name')
{
$slug = $this->createOrGetUniqueSlug($translation, $attribute->getValue() );
$translation['slug'] = $slug;
$translation->save();
}
break;
case Attribute::ValueFromItself;
// Qnd: Custom for author
if($reference == 'author_id')
{
/** @var Post $model */
$model->author()->associate($attribute->getValue());
$model->save();
}
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
//Return the page
return $model;
}
/**
* Fills attributes with data from a model in this way:
*
* 1. First it looks if it needs to get the value from a translationModel.
* If so, gets it, fills the attribute and fills the next attribute if any
* 2. Then it looks if it needs to get the value from it's model.
* If so, gets it, fills the attribute and fills the next attribute if any
* 4. Then it looks if it needs to get the value from the images associated with it.
* If so, gets it, fills the attribute and fills the next attribute if any
* Please notice that if cases 1, 2 and 3 where true the value you may have set with setValue is overwritten.
*
* @param Collection $sectionTabItems A collection containing implementations AbstractSectionTabItem's
* @param Model $model
* @return Collection
*/
public function fillAttributesWithData(Collection $sectionTabItems, Model $model)
{
$filledAttributes = parent::fillAttributesWithData($sectionTabItems,$model);
$sectionTabItems->each(
function (SectionTabItem $sectionTabItem, $key) use ($model, $filledAttributes) {
$value = null;
$valueReference = $sectionTabItem->getAttribute()->getsValueFromReference();
switch ($sectionTabItem->getAttribute()->getsValueFrom()) {
//Case 1. Check if the translation of the model has a value.
case Attribute::ValueFromItself:
if($valueReference == 'author_id')
{
$author = $model->author()->first();
if($author) $sectionTabItem->getAttribute()->setValue($author->id);
}
break;
}
//Finally when we have a value we will set it on the attribute and then process the next one.
if ($value !== null) {
$sectionTabItem->getAttribute()->setValue((string)$value);
}
$filledAttributes->push($sectionTabItem);
}
);
return $filledAttributes;
}
}