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/netwerkbrabant.komma.pro/app/KommaApp/Regions/Kms/RegionService.php
<?php

namespace App\KommaApp\Regions\Kms;


use App\KommaApp\Kms\Core\Attributes\Attribute;
use App\KommaApp\Kms\Core\Attributes\Models\SelectOptionInterface;
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\Regions\Models\Region;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection as BaseCollection;


class RegionService extends SectionService
{
    protected $sortable = false;
    protected $orderBy = 'name';
    protected $orderReverse = false;

    function __construct()
    {
        $this->forModelName = Region::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 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:
                    if ($reference == 'name') {
                        /** @var Language $language */
                        $model->slug = $this->createOrGetUniqueSlug($model, $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

        //Return the page
        return $model;
    }


    /**
     * Get the region options for select
     *
     * @param bool $allowNullableSelectOption
     * @param bool $withCompanyFilter
     * @return BaseCollection
     */
    public function getOptionsForSelect($allowNullableSelectOption = false, $withCompanyFilter = false): BaseCollection
    {
        $selectOptions = collect();


        if ($allowNullableSelectOption) {
            $selectOption = (\App::make(SelectOptionInterface::class))
                ->setContent(__('kms/global.none'))
                ->setHtmlContent(__('kms/global.none'))
                ->setValue(null);
            $selectOptions->push($selectOption);
        }

        /** @var $sidebarListItems SidebarListItem[] */
        $sidebarListItems = $this->getModelsForSideBar();

        $regionQuery = Region::whereNotNull('name');
        if (!$withCompanyFilter) {
            $regionQuery->where('company_filter_only', 0);
        }
        $regions = $regionQuery->get();


        foreach ($regions as $region) {

            /** @var SelectOptionInterface $selectOption */
            $selectOption = (\App::make(SelectOptionInterface::class))
                ->setContent($region->name)
                ->setHtmlContent($region->name)
                ->setValue($region->id);

            $selectOptions->push($selectOption);
        }
        return $selectOptions;

    }
}