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/ZelfVerkopen/zelfverkopen.nl/app/KommaApp/Questions/Kms/QuestionService.php
<?php

namespace App\KommaApp\Questions\Kms;


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

class QuestionService extends SectionService
{
    protected $sortable = true;

    function __construct()
    {
        $this->forModelName = Question::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->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;
            }
        });


        $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


        $sectionTabItems->each(function ($sectionTabItem, $key) use ($model) {
            /** @var SectionTabItem $sectionTabItem */

            $attribute = $sectionTabItem->getAttribute();
            $reference = $attribute->getsValueFromReference();
            switch ($attribute->getsValueFrom()) {
                case 100:
//                    if ($reference == 'how_it_work_steps') {

                        $value = $attribute->getValue();
                        $values = explode(',', $value);

                        if ($reference == 'how_it_work_steps') {

                            \DB::table('block_questions')
                                ->where('question_id', $model->id)
                                ->where('block_code_name', 'LIKE', 'process_%')
                                ->delete();
                        }
                        else if ($reference == 'buy_house_packages') {
                            \DB::table('block_questions')
                                ->where('question_id', $model->id)
                                ->where('block_code_name', 'LIKE', 'buy_process_%')
                                ->delete();
                        }

                        if($value != ""){
                            foreach ($values as $questionBlock){
                                \DB::table('block_questions')
                                    ->insert([
                                        'block_code_name' => $questionBlock,
                                        'question_id' => $model->id
                                    ]);
                            }
                        }

//                    }
                    break;
            }
        });

        //Return the page
        return $model;
    }

    public function fillAttributesWithData(Collection $sectionTabItems, Model $model){

        $filledAttributes = parent::fillAttributesWithData($sectionTabItems, $model);


        $filledAttributes->each(
            function ($sectionTabItem, $key) use ($model, $filledAttributes) {

                if(!is_a($sectionTabItem->getAttribute(), Attribute::class)) throw new \InvalidArgumentException("One of the attributes in a AbstractSectionTabItem instance is not but must be an child instance of Attribute.");

                switch($sectionTabItem->getAttribute()->getsValueFromReference())
                {
                    case 'buy_house_packages':
                    case 'how_it_work_steps':

                        $values = \DB::table('block_questions')
                            ->where('question_id', $model->id)
                            ->get();

                        $valueToProcess = [];
                        foreach ($values as $value){
                            $valueToProcess[] = $value->block_code_name;
                        }

                        $value = implode(',', $valueToProcess);
                        $sectionTabItem->getAttribute()->setValue($value);
                        break;

                }
            }
        );

        return $filledAttributes;

    }

    public function getCategories()
    {

        $categories = [
            Question::Default,
            Question::Method,
            Question::Pricing,
        ];

        $entities = [];

        foreach ($categories as $category) {

            $entities[] = (new SelectOption())
                ->setValue($category)
                ->setHtmlContent(__('kms/questions.categories.'.$category));
        }

        return $entities;
    }

    public function getHowItWorkSteps(){
        return [
            (new SelectOption())
            ->setValue('process_1')
            ->setHtmlContent('Woning aanmelden'),
            (new SelectOption())
            ->setValue('process_2')
            ->setHtmlContent('Taxatie en advertentie'),
            (new SelectOption())
            ->setValue('process_3')
            ->setHtmlContent('Verkoopstrategie en bezichtigingen'),
            (new SelectOption())
            ->setValue('process_4')
            ->setHtmlContent('Onderhandelen en verkopen')
        ];
    }

    public function getBuyHousePackages(){
        return [
            (new SelectOption())
                ->setValue('buy_process_1')
                ->setHtmlContent('Waardeanalyse'),
            (new SelectOption())
                ->setValue('buy_process_2')
                ->setHtmlContent('Aankoopbegeleiding'),
            (new SelectOption())
                ->setValue('buy_process_3')
                ->setHtmlContent('Combinatiepakket Aan- én Verkoop'),
        ];
    }

    public function destroyModel(Model $model)
    {

        $model->pages()->detach();

        parent::destroyModel($model); // TODO: Change the autogenerated stub
    }
}