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/azenio.komma.pro/app/Base/SectionControllerWithComponentAreas.php
<?php declare(strict_types=1);


namespace App\Base;


use App\Attributes\Attribute;
use App\Attributes\ComponentArea;
use App\Components\ComponentArea\ComponentAreaService;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Komma\KMS\Core\AbstractTranslationModel;
use Komma\KMS\Core\SectionController;
use Komma\KMS\Core\Sections\Section;

abstract class SectionControllerWithComponentAreas extends SectionController
{
    /**
     * @var ComponentAreaService
     */
    protected $componentAreaService;

    function __construct(Section $section)
    {
        parent::__construct($section);
        $this->componentAreaService = new ComponentAreaService();
    }

    /**
     * @param Model $model
     * @param Collection|null $attributesByValueFrom
     * @return Model
     */
    protected function save(Model $model, Collection $attributesByValueFrom = null): Model
    {
        $model = parent::save($model, $attributesByValueFrom);

        if($this->forTranslationModelName) {
            $this->executeCallbackForModelsTranslationsMatchingAttributeLanguage($model,
                $attributesByValueFrom->get(Attribute::ValueFromComponentArea),
                function (AbstractTranslationModel $translation, ComponentArea $attribute) use ($model) {
                    //If the translation model does not exist, and the component area is not empty, we need to save the translation.
                    if (!$translation->exists && $attribute->getValue() !== '[]') {
                        $translation->translatable()->associate($model);
                        $translation->save();
                    }
                    $this->componentAreaService->saveAttribute($translation, $attribute);
                }
            );
        }

        return $model;
    }

    /**
     * A function that MUST delegate to services to accomplish loading.
     * It MUST NOT act like a service.
     *
     * @param Model $model
     * @param Collection $attributesByValueFrom Keys must be ValueFrom integers. Values must be Attributes that have the ValueFrom integers
     * @return Collection The same collection as you've passed in. Only "filled".
     */
    protected function load(Model $model, Collection $attributesByValueFrom = null): Collection
    {
        $attributesByValueFrom = parent::load($model, $attributesByValueFrom);

        if($this->forTranslationModelName) {
            $this->executeCallbackForModelsTranslationsMatchingAttributeLanguage($model,
                $attributesByValueFrom->get(Attribute::ValueFromComponentArea),
                function (AbstractTranslationModel $translation, ComponentArea $attribute) {
                    $this->componentAreaService->loadAttribute($translation, $attribute);
                }
            );
        }

        return $attributesByValueFrom;
    }

    /**
     * This method is called when a item will be deleted
     *
     * @param Model $model
     * @return mixed
     * @throws \Exception
     */
    public function destroy(Model $model)
    {
        $this->authorize('destroy', $model);
        $this->componentAreaService->destroyForModel($model);
        if($this->forTranslationModelName) {
            $model->translations()->get()->each(function (AbstractTranslationModel $translationModel) {
                $this->componentAreaService->destroyForModel($translationModel);
            });
        }
        return parent::destroy($model);
    }
}