File: D:/HostingSpaces/SBogers10/honger.komma.pro/app/KommaApp/Categories/Kms/CategoryService.php
<?php
namespace App\KommaApp\Categories\Kms;
use App\KommaApp\Kms\Core\Attributes\Attribute;
use App\KommaApp\Kms\Core\Attributes\Models\SelectOption;
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\Categories\Models\Category;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
class CategoryService extends SectionService
{
protected $sortable = false;
protected $orderReverse = true;
function __construct()
{
$this->forModelName = Category::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 Specific attributes
$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();
$parentModel = $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($parentModel && $model && $parentModel->id !== $currentParentId)
{
$model->makeLastChildOf($parentModel);
}
$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;
}
});
$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;
}
/**
* @param null $languageId
* @param int|null $excludeId
* @return array
*/
public function getOptionsForSelect($languageId = null, $excludeId = null, $allowNull = true)
{
$entities = [];
$categories = Category::with('translation')->get();
foreach($categories as $category){
$entities[] = (new SelectOption())
->setValue($category->id)
->setContent($category->id)
->setHtmlContent($category->translation->name);
}
return $entities;
}
}