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/brameda/brameda.nl/app/Komma/WebsiteConfig/WebsiteConfigService.php
<?php
namespace App\Komma\WebsiteConfig;


use App\Helpers\KommaHelpers;
use App\Komma\Documents\Kms\DocumentableInterface;
use App\Komma\Kms\Core\Attributes\Attribute;
use App\Komma\Kms\Core\Attributes\Documents;
use App\Komma\Kms\Core\Attributes\Seperator;
use App\Komma\Kms\Core\Attributes\Title;
use App\Komma\Kms\Core\Sections\SectionService;
use App\Komma\Kms\Core\Sections\SectionTabItem;
use App\Komma\Kms\Core\Tree\NestedSets\Nodes\TreeModel;
use App\Komma\WebsiteConfig\Model\WebsiteConfig;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;

final class WebsiteConfigService extends SectionService
{
    protected $sortable = false;

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

            if(
                !is_a($attribute, Title::class) &&
                !is_a($attribute, Seperator::class) &&
                !is_a($attribute, Documents::class)
            ) {

                $websiteConfigModel = WebsiteConfig::updateOrCreate(['code_name' => $reference],
                    ['value' => $attribute->getValue()]);

            } elseif (is_a($attribute, Documents::class)) {
                $websiteConfigModel = WebsiteConfig::firstOrCreate(['code_name' => $reference]);
                /** @var Documents $attribute */
                $this->documentService->processUploadedDocumentsForModel($websiteConfigModel, $attribute);
            }



        });

        //Return the page
        return $model;
    }

    /**
     * Fills non-product specific attributes. Product 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)
    {
        $sectionTabItems->each(
            function ($sectionTabItem, $key) use ($model) {
                /** @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.");
                $attribute = $sectionTabItem->getAttribute();
                $reference = $attribute->getsValueFromReference();

                if(is_a($attribute, Documents::class)) {
                    $key = KommaHelpers::getShortNameFromClass($sectionTabItem->getAttribute()) . '-' . $sectionTabItem->getAttribute()->getKey()->getValuePart();
                    /** @var $model DocumentableInterface */
                    $websiteConfigModel = WebsiteConfig::where('code_name', '=', $reference)->first();
                    if($websiteConfigModel) {
                        $value = json_encode($websiteConfigModel->documents()->where('key', '=', $key)->get());
                        $attribute->setValue($value);
                    }
                } else {
                    $websiteConfigModel = WebsiteConfig::where('code_name', '=', $reference)->first();
                    if ($websiteConfigModel) {
                        $attribute->setValue($websiteConfigModel->value);
                    }
                }
            }
        );

        return $sectionTabItems;
    }
}