HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/blijegasten/blijegasten.be/app/Komma/Shop/Categories/Kms/CategoryModelService.php
<?php
namespace App\Komma\Shop\Categories\Kms;


use App\Helpers\KommaHelpers;
use App\Komma\Kms\Core\ModelService;
use App\Komma\Kms\Core\Tree\NestedSets\Nodes\TreeModelInterface;
use App\Komma\Kms\Core\Tree\TreeService;
use App\Komma\Shop\Categories\Models\Category;
use Illuminate\Support\Str;

class CategoryModelService extends ModelService
{
    protected $sortable = true;

    function __construct()
    {
        $this->setModelClassName(Category::class);
        parent::__construct();
    }

    /**
     * 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;
    }
}