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/slenders.komma.pro/app/Komma/Components/ComponentType/Types/Brands.php
<?php


namespace App\Komma\Components\ComponentType\Types;

use App\Komma\Components\Component\ViewComponent;
use App\Komma\Components\ComponentType\ComponentTypes;
use App\Komma\Kms\Core\Attributes\Attribute;
use App\Komma\Kms\Core\Attributes\Documents;
use App\Komma\Kms\Core\Attributes\Models\ImageProperty;
use App\Komma\Kms\Core\Attributes\OnOff;
use App\Komma\Kms\Core\Attributes\TextArea;
use App\Komma\Kms\Core\Attributes\TextField;

class Brands extends AbstractComponentType
{

    const POSSIBLE_AMOUNT = 10;

    public function __construct()
    {
        //Set an id that corresponds to the ComponentTypes enum
        $this->id = ComponentTypes::BRANDS;

        //Set icon and name
        $this->name = 'brands';

        parent::__construct();

        $this->attributes->push((new TextField('Titel'))
            ->setPlaceholderText('Ex. Usp')
            ->mapValueFrom(Attribute::ValueFromComponent, 'title')
        );

        $this->attributes->push((new TextField('Ondertitel'))
            ->setPlaceholderText('Ex. Slenders Wooncomfort is dealer van ')
            ->mapValueFrom(Attribute::ValueFromComponent, 'subtitle')
        );

        for($i = 1; $i <= self::POSSIBLE_AMOUNT; $i++) {

            $this->attributes->push((new Documents())
                ->setLabelText('Logo')
                ->onlyAllowImages()
                ->setSmallDragAndDropArea()
                ->setMaxDocuments(1)
                ->setSubFolder('component_uploads')
                ->setImageProperties([
                    (new ImageProperty())->setName('small')->setCropMethod(ImageProperty::Resize)->setHeight(200)->setWidth(200),
                ])
                ->mapValueFrom(Attribute::ValueFromComponent, 'image_' . $i) //Needed to sustain Attribute integrity.
                ->setDataAttribute('tab', $i ));
        }
    }

    public function prepare(ViewComponent $viewComponent)
    {
        $logos = collect();

        for($i = 1; $i <= self::POSSIBLE_AMOUNT; $i++) {

            $image = $viewComponent->{'image_'.$i};

            if(!empty($image) && $image->isNotEmpty()) {

                $usp = (object)[
                    'id' => $i,
                    'logo' => $image->first()
                ];

                $logos->push($usp);
            }

            unset($viewComponent->{'image_'.$i});
        }

        $viewComponent->logos = $logos;
    }
}