File: D:/HostingSpaces/SBogers10/firetech.komma.pro/app/KommaApp/Shop/Categories/Kms/CategoryService.php
<?php
namespace App\KommaApp\Shop\Categories\Kms;
use App\KommaApp\Kms\Core\Attributes\Attribute;
use App\KommaApp\Kms\Core\Kms;
use App\KommaApp\Kms\Core\KmsInterface;
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\Shop\Categories\Models\Category;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
class CategoryService extends SectionService implements CategoryServiceInterface
{
protected $sortable = true;
/** @var Kms $kms */
protected $kms;
function __construct()
{
$this->forModelName = Category::class;
$this->kms = \App::make(KmsInterface::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
$sectionTabItems->each(function($sectionTabItem, $key) use($model) {
/** @var SectionTabItem $sectionTabItem */
$model->site_id = 1; //we need to set this altough it has not value. It is needed for the inner workings of the tree algorithm
$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
}
break;
case Attribute::ValueFromTranslationModel;
if($reference == 'name')
{
/** @var Language $language */
$language = $attribute->getAssociatedLanguage();
if(!$model->exists) $model->save();
$translation = $this->getOrCreateTranslationModelForModel($model, $language);
$translation['slug'] = (str_slug($attribute->getValue()));
$translation->save();
}
break;
}
});
$model->save(); //Save the category
$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;
}
/**
* This method will create the root model if necessary.
* This TranslatableModelInterface database entry is used as a base for the lft and rgt.
*
* @param $siteId
* @return null
*/
protected function makeRootModelIfNotExists($siteId = null)
{
if($this->forModelName::where(['lft' => 1])->first()) return null;
$newRoot = new $this->forModelName(['active' => 1, 'site_id' => '1', 'tree' => '1']);
return $newRoot;
}
/**
* Makes the AbstractTranslatableModel a child from the categories by id if it isn't a child yet
*
* @param CategorizableInterface $model
* @param string $categoryIds comma seperated like this 1,4,2,5
*/
public function makeChildOfCategoriesIfNotAlready(CategorizableInterface $model, string $categoryIds):void
{
if(!$model->id) return;
if($categoryIds !== "") {
$categoryIds = array_unique(explode(',', $categoryIds));
} else {
$categoryIds = [];
}
$categories = $this->forModelName::whereIn('id', $categoryIds)->get();
if(!$categories) return;
$modelShortClassName = $this->kms->getShortNameFromClass($model, true);
$relationName = str_plural($modelShortClassName);
if(!method_exists((new $this->forModelName), $relationName)) {
throw new \RuntimeException('The class "'.$this->forModelName.'" must have, but did not have a MorphToMany relation method called "'.$relationName.'". Without it we cannot make the "'.get_class($model).'" a child of "'.$this->forModelName.'"');
}
$model->categories()->sync($categoryIds);
}
/**
* Returns the category ids as a comma separated string for the CategorizableInterface implementation
*
* @param CategorizableInterface $model
* @return string Category ids, comma separated
*/
public function getCategoryIdsForModel(CategorizableInterface $model): ?string
{
if(!$model->id) return null;
$idsCollection = $model->categories()->get(['category_id'])->map(function(Category $category ) {
return $category->category_id;
});
$idString = implode(',', $idsCollection->toArray());
if($idString == "") return null;
return $idString;
}
}