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/lc-hydraulics.komma.nl/app/KommaApp/Vacancies/Kms/VacancyService.php
<?php
namespace App\KommaApp\Vacancies\Kms;


use App\KommaApp\Documents\Kms\DocumentableInterface;
use App\KommaApp\Kms\Core\AbstractTranslatableModel;
use App\KommaApp\Kms\Core\Attributes\Attribute;
use App\KommaApp\Kms\Core\NestedSets\Nodes\EloquentNode;
use App\KommaApp\Kms\Core\Sections\AbstractSectionTabItem;
use App\KommaApp\Kms\Core\Sections\SectionService;
use App\KommaApp\Kms\Core\Sections\SectionTabItem;
use App\KommaApp\Languages\Models\Language;
use App\KommaApp\Pages\Models\PageTranslation;
use App\KommaApp\Vacancies\Models\Vacancy;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Facades\DB;

class VacancyService extends SectionService
{
    protected $sortable = false;
    protected $orderBy = 'date';
    protected $orderReverse = true;

    function __construct()
    {
        $this->forModelName = Vacancy::class;

        parent::__construct();
    }

    /**
     * Fills non-productGroup specific attributes. ProductGroup specific attributes are processed in the parent
     *
     *
     * @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);

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

                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.");

//                if(is_a($sectionTabItem->getAttribute(), OnOff::class)) dd($sectionTabItem->getAttribute());
//                if(is_a($sectionTabItem->getAttribute(), Select::class)) dd($sectionTabItem->getAttribute()->getValue());

                $value = null;

                $valueReference = $sectionTabItem->getAttribute()->getsValueFromReference();
                switch($sectionTabItem->getAttribute()->getsValueFrom())
                {
                    //Case 3
                    case Attribute::ValueFromModelHasManyRelation:
                        if(!method_exists($model, $valueReference) === false) throw new \RuntimeException("The value reference method name ('".$valueReference."') does not exist or is not a HasMany relation on the model with class name: ".(get_class($model)).". Please check the attribute configuration in the section.");
                        $data = explode('|', $valueReference);
                        if(count($data) != 2) throw new \InvalidArgumentException("The value reference from attribute '".get_class($sectionTabItem->getAttribute())."' must be a pipe seperated relation|relationAttribute pair. But now it contains more pipes and that is not allowed.");

                        $relationMethod = $data[0];
                        $relationFieldId = $data[1];

                        // We only use this loop to fill the sites.
                        // Because we also have sites that aren't real.
                        if($relationMethod !== 'sites') break;

                        $collection = DB::table('site_vacancies')->where('vacancy_id', $model->id)->get();

                        $value = implode(',', $collection->pluck('site_id')->toArray());
                        break;
                }

                //Finally when we have a value we will set it on the attribute and then process the next one.
                if($value !== null) {
                    $sectionTabItem->getAttribute()->setValue((string)$value);
                }

                $filledAttributes->push($sectionTabItem);
            }
        );

        return $filledAttributes;
    }

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

//        dd($model);

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

            $attribute = $sectionTabItem->getAttribute();

            $reference = $attribute->getsValueFromReference();
            switch ($attribute->getsValueFrom())
            {
                case Attribute::ValueFromTranslationModel;
                    /** @var Language $language */
                    $language = $attribute->getAssociatedLanguage();
                    if(!$model->exists) $model->save();
                    $translation = $this->getOrCreateTranslationModelForModel($model, $language);

                    if($reference == 'name')
                    {
                        $slug = $this->createOrGetUniqueSlug($translation, $attribute->getValue() );
                        $translation['slug'] = $slug;
                        $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

        $translations = $model->translations()->get();
        foreach ($translations as $translation){
            if(empty($translation->name)){
                $translation->delete();
            }
        }

        //Return the page
        return $model;
    }
}