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/netwerkbrabant/netwerkbrabant.nl/app/KommaApp/Questions/Kms/QuestionService.php
<?php
namespace App\KommaApp\Questions\Kms;

use App\KommaApp\Kms\Core\Attributes\Attribute;
use App\KommaApp\Kms\Core\NestedSets\Nodes\TreeModel;
use App\KommaApp\Kms\Core\Sections\SectionService;
use App\KommaApp\Kms\Core\Sections\SectionTabItem;
use App\KommaApp\Languages\Models\Language;
use App\KommaApp\Questions\Models\Question;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;

class QuestionService extends SectionService
{
    function __construct()
    {
        $this->forModelName = Question::class;

        parent::__construct();
    }

    /**
     * This method will save a 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 TreeModel $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:

                    $rootQuestion = Question::query()
                        ->where('lft', 1)
                        ->first();

                    $model->makeLastChildOf($rootQuestion);

                    break;
                case Attribute::ValueFromTranslationModel:
                    if(in_array($reference, ['name', 'description']))
                    {
                        /** @var Language $language */
                        $language = $attribute->getAssociatedLanguage();
                        $translation = $this->getTranslationModelForModelByLanguage($model, $language);
                        $translation['slug'] = $this->createOrGetUniqueSlug($translation, $attribute->getValue());
                        //Saving is done by the parent
                    }
                    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
        $this->siteService->linkModelToCurrentSite($model);


        //Return the question
        return $model;
    }
}