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/csb.komma.pro/app/Products/Kms/ProductController.php
<?php

namespace App\Products\Kms;

/**
 * @author      Komma <info@komma.pro>
 * @copyright   (c) 2012-2016, Komma
 */

use App\Attributes\Attribute;
use App\Attributes\ComponentArea;
use App\Components\ComponentArea\ComponentAreaService;
use App\Products\Models\Product;
use App\Products\Models\ProductTranslation;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
use Komma\KMS\Core\AbstractTranslatableModel;
use Komma\KMS\Core\AbstractTranslationModel;
use Komma\KMS\Core\SectionController;

final class ProductController extends SectionController
{

    /**
     * @var ComponentAreaService
     */
    private $componentAreaService;

    protected $slug = "products";
    protected $sortable = true;
    protected $classModelName = Product::class;
    protected $forTranslationModelName = ProductTranslation::class;

    function __construct()
    {
        $productSection = new ProductSection($this->slug);
        parent::__construct($productSection);
        $this->modelService = new ProductModelService();
        $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);

        $this->forEachComponentArea($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);

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

        return $attributesByValueFrom;
    }

    /**
     * @param AbstractTranslatableModel $model
     * @param Collection $componentAreaAttributes
     * @param string $do
     */
    protected function forEachComponentArea(AbstractTranslatableModel $model, Collection $componentAreaAttributes, \Closure $do)
    {
        if($this->forTranslationModelName && $componentAreaAttributes->count() > 0) {
            if($componentAreaAttributes) {
                $componentAreaAttributes->each(function (Attribute $attribute) use ($model, $do) {
                    if (($language = $attribute->getAssociatedLanguage()) && $model) {
                        $translationModel = $this->translationService->getTranslationModelForModelByLanguage($model, $language);
                        if ($translationModel) $do->call($this, $translationModel, $attribute);
                    }
                });
            }
        }
    }
}