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/ste.komma.pro/app/Components/ComponentType/Types/TextUsp.php
<?php


namespace App\Components\ComponentType\Types;


use App\Components\Component\ViewComponent;
use App\Components\ComponentType\ComponentTypes;
use Komma\KMS\Core\Attributes\Attribute;
use Komma\KMS\Core\Attributes\OnOff;
use Komma\KMS\Core\Attributes\TextArea;
use Komma\KMS\Core\Attributes\TextField;

class TextUsp extends AbstractComponentType
{
    //Set an id that corresponds to the ComponentTypes enum
    protected $id = ComponentTypes::TEXT_USP;

    //Set icon and name
    protected $name = 'text-usp';

    // Number of tabs that can be filled
    const POSSIBLE_AMOUNT = 5;

    public function __construct()
    {
        parent::__construct();

        $this->attributes->push((new TextArea())
            ->enableTinymceEditor()
            ->mapValueFrom(Attribute::ValueFromComponent, 'text')
        );

        // Make the tabs
        for($i = 0; $i < self::POSSIBLE_AMOUNT; $i++) {

            $this->attributes->push(
                (new TextField('USP ' . ($i + 1)))
                    ->mapValueFrom(Attribute::ValueFromComponent,'label_' . ($i + 1))
                    ->setDataAttribute('tab', ($i + 1)),
            );
        }

        $this->attributes->push((new OnOff())
            ->setLabelText('Omwisselen tekst / USP')
            ->mapValueFrom(Attribute::ValueFromComponent, 'reversed')
        );
    }

    public function prepare(ViewComponent $viewComponent)
    {
        $tabs = collect();
        for($i = 1; $i <= self::POSSIBLE_AMOUNT; $i++) {
            $label = $viewComponent->{'label_'.$i};

            if(!empty($label)) {
                $tab = (object)[
                    'id' => $i,
                    'label' => $label,
                ];
                $tabs->push($tab);
            }
            unset($viewComponent->{'label_'.$i});
        }
        $viewComponent->usps = $tabs;
    }
}