File: D:/HostingSpaces/SBogers10/shop.komma.nl/app/Categories/Kms/CategoryModelService.php
<?php
namespace App\Categories\Kms;
use Illuminate\Database\Eloquent\Builder;
use Komma\KMS\Core\Attributes\Models\SelectOption;
use Komma\KMS\Core\ModelService;
use Komma\KMS\Core\Tree\NestedSets\Nodes\TreeModelInterface;
use App\Categories\Models\Category;
use Illuminate\Support\Str;
use Komma\KMS\Helpers\KommaHelpers;
class CategoryModelService extends ModelService
{
protected $sortable = true;
function __construct()
{
parent::__construct();
$this->setModelClassName(Category::class);
$this->treeService = new CategoryTreeService();
}
/**
* Make the category a child of the root category OR just save it if it already is a child of the root category
*
* @param $model
* @return TreeModelInterface
*/
private function makeChildOfRootOrSave(TreeModelInterface $model)
{
$rootModel = $this->modelClassName::where('lft', '=', 1)->first();
if(!$rootModel) throw new \RuntimeException('No root model for '.$this->modelClassName.'. Make sure it exists');
$model->makeLastChildOf($rootModel);
return $model;
}
/**
* Makes the CategorizableInterface 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->modelClassName::whereIn('id', $categoryIds)->get();
if(!$categories) return;
$modelShortClassName = KommaHelpers::getShortNameFromClass($model, true);
$relationName = Str::plural($modelShortClassName);
if(!method_exists((new $this->modelClassName), $relationName)) {
throw new \RuntimeException('The class "'.$this->modelClassName.'" 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->modelClassName.'"');
}
$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;
}
public function modelsForCurrentSite()
{
$language = app()->getLanguage();
$currentSite = $this->siteService->getCurrentSite();
return $this->models()->whereHas('translations', function(Builder $query) use($language) {
$query->where('language_id', '=', $language->id);
});
}
public function namesById() {
if(!request()->ajax()) return [];
return $this->getOptionsForSelectAsTree()->mapWithKeys(function(SelectOption $option) {
return [$option->getValue() => $option->getContent()];
});
}
}