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/SBogers10/honger.komma.pro/app/KommaApp/Cases/Kms/CaseService.php
<?php
namespace App\KommaApp\Cases\Kms;


use App\KommaApp\Kms\Core\AbstractTranslatableModel;
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\Cases\Models\CaseModel;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;

class CaseService extends SectionService
{
    protected $sortable = true;

    function __construct()
    {
        $this->forModelName = CaseModel::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 */

            $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->makeFirstChildOf($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 page

        $model = parent::saveModel($model, $sectionTabItems); //First make sure we have a model and save the attributes in them from the SectionTabItem attributes

        // Remove the cache
        Cache::forget('cases');

        //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);

        return $filledAttributes;
    }

    /**
     * Uses a model that must have a translations method, creates a new translation for it if it does not exists and associates it with the language given.
     * Returns the translation
     *
     * @param AbstractTranslatableModel $model
     * @param Language $language
     * @return Model The translation
     */
    public function getOrCreateTranslationModelForModel(AbstractTranslatableModel $model, Language $language): Model{

        $translation = $this->forTranslationModel::where([
            ['case_id','=', $model->id],
            ['language_id','=',$language->id],
        ])->first();
        if($translation) return $translation;

        //Translation does not exist. Create it and associate a language with it and save it in the model.

        $translation = new $this->forTranslationModel;

        $translation->language()->associate($language);
        $model->translations()->save($translation);
        return $translation;
    }

    public function getCasesForSelect()
    {
        $entities = [];

        $cases = CaseModel::where('active', 1)
            ->where('lft', '!=', 1)
            ->where('code_name', '!=', '')
            ->get();

        foreach ($cases as $case) {
            $entities[] = (new SelectOption())
                ->setValue($case->code_name)
                ->setContent($case->code_name)
                ->setHtmlContent($case->code_name);
        }

        return $entities;
    }
}