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


namespace App\Components\ComponentType\Types;

use App\Buttons\Kms\ButtonService;
use App\Buttons\Models\Button;
use App\Components\Component\ViewComponent;
use App\Components\ComponentType\ComponentTypes;
use Komma\KMS\Core\Attributes\Attribute;
use Komma\KMS\Core\Attributes\Documents;
use Komma\KMS\Core\Attributes\Models\ImageProperty;
use Komma\KMS\Core\Attributes\MultiSelect;
use Komma\KMS\Core\Attributes\OnOff;
use Komma\KMS\Core\Attributes\Seperator;
use Komma\KMS\Core\Attributes\TextArea;
use Komma\KMS\Core\Attributes\TextField;
use Komma\KMS\Core\Attributes\Title;

class ContentSlider extends AbstractComponentType
{

    const POSSIBLE_AMOUNT = 4;

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

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

        parent::__construct();

        $buttonsService = \App::make(ButtonService::class);
        $selectOptions = $buttonsService->getOptionsForSelect(false, true);

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

            $this->attributes->push((new TextField(__('kms/attributes/components.name_tab')))
                ->mapValueFrom(Attribute::ValueFromComponent, 'label_' . ($i + 1))
                ->setDataAttribute('tab', ($i + 1))
            );

            $this->attributes->push((new Seperator())
                ->setDataAttribute('tab', ($i + 1)));
            $this->attributes->push((new Title(__('kms/attributes/components.text')))
                ->setDataAttribute('tab', ($i + 1)));

            $this->attributes->push((new TextField(__('kms/attributes/components.title')))
                ->mapValueFrom(Attribute::ValueFromComponent, 'header_' . ($i + 1))
                ->setDataAttribute('tab', ($i + 1))
            );
            $this->attributes->push((new TextArea())
                ->enableTinymceEditor()
                ->setLabelText(__('kms/attributes/components.text'))
                ->mapValueFrom(Attribute::ValueFromComponent, 'text_' . ($i + 1))
                ->setDataAttribute('tab', ($i + 1))
            );
            $this->attributes->push((new MultiSelect())
                ->setLabelText(__('KMS::kms/global.button'))
                ->setItems($selectOptions->toArray())
                ->setMaxItemsToSelect(1)
                ->canBeLinkedWith(Button::class)
                ->mapValueFrom(Attribute::ValueFromItself, 'buttons_' . ($i + 1))
                ->setDataAttribute('tab', ($i + 1))
            );

            $this->attributes->push((new Seperator())
                ->setDataAttribute('tab', ($i + 1)));
            $this->attributes->push((new Title(__('kms/attributes/components.image')))
                ->setDataAttribute('tab', ($i + 1)));

            $this->attributes->push((new Documents())
                ->onlyAllowImages()
                ->setSubFolder('component_uploads')
                ->setMaxDocuments(1)
                ->setImageProperties([
                    (new ImageProperty())->setName('medium')->setCropMethod(ImageProperty::Resize)->setWidth(724),
                    (new ImageProperty())->setName('small')->setCropMethod(ImageProperty::Resize)->setWidth(359),
                ])
                ->setSmallDragAndDropArea()
                ->mapValueFrom(Attribute::ValueFromComponent, 'image_' . ($i + 1))
                ->setDataAttribute('tab', ($i + 1))
            );

            $this->attributes->push((new Seperator())
                ->setDataAttribute('tab', ($i + 1)));
            $this->attributes->push((new Title(__('kms/attributes/components.options')))
                ->setDataAttribute('tab', ($i + 1)));

            $this->attributes->push((new OnOff())
                ->setLabelText(__('kms/attributes/components.swap_text_image'))
                ->mapValueFrom(Attribute::ValueFromComponent, 'reversed_' . ($i + 1))
                ->setDataAttribute('tab', ($i + 1))
            );
        }

    }

    public function prepare(ViewComponent $viewComponent)
    {
        $tabs = collect();
        for($i = 1; $i <= self::POSSIBLE_AMOUNT; $i++) {
            $label = $viewComponent->{'label_'.$i};
            $header = $viewComponent->{'header_'.$i};
            $text = $viewComponent->{'text_'.$i};
            $buttons = $viewComponent->{'buttons_'.$i};
            $image = $viewComponent->{'image_'.$i};
            $reversed = $viewComponent->{'reversed_'.$i};
            if(!empty($label) || !empty($header) || !empty($text) || !empty($buttons) || !empty($image) || !empty($reversed)) {
                $tab = (object)[
                    'id' => $i,
                    'label' => $label,
                    'header' => $header,
                    'text' => $text,
                    'buttons' => $buttons,
                    'image' => $image,
                    'reversed' => $reversed
                ];
                $tabs->push($tab);
            }
            unset($viewComponent->{'label_'.$i});
            unset($viewComponent->{'header_'.$i});
            unset($viewComponent->{'text_'.$i});
            unset($viewComponent->{'buttons_'.$i});
            unset($viewComponent->{'image_'.$i});
            unset($viewComponent->{'reversed_'.$i});
        }
        $viewComponent->tabs = $tabs;
    }
}