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/SBogers95/rentman.io/app/Komma/Dynamic/ComponentType/Types/SideNavSlider.php
<?php

namespace App\Komma\Dynamic\ComponentType\Types;

use App\Komma\Dynamic\Component\ViewComponent;
use App\Komma\Dynamic\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\Select;
use App\Komma\Kms\Core\Attributes\TextArea;
use App\Komma\Kms\Core\Attributes\TextField;

class SideNavSlider extends AbstractComponentType
{
    public function __construct()
    {
        //Set an id that corresponds to the ComponentTypes enum
        $this->id = ComponentTypes::SIDENAV_SLIDER;
        $this->tabCount = 10;

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

        parent::__construct();

        $this->attributes->push((new TextField('Label'))
            ->setPlaceholderText('Label')
            ->mapValueFrom(Attribute::ValueFromItself, 'label'));

        $this->attributes->push((new TextField('Title'))
            ->setPlaceholderText('Title')
            ->mapValueFrom(Attribute::ValueFromItself, 'title'));


        for ($i = 1; $i <= $this->tabCount; $i++) {

            $this->attributes->push((new Select())
                ->setItems($this->getIconOptions())
                ->setLabelText('Tab icon')
                ->mapValueFrom(Attribute::ValueFromItself, 'tab_icon_'.$i)
                ->setDataAttribute('tab', $i)
            );

            $this->attributes->push((new TextField('Tab label'))
                ->mapValueFrom(Attribute::ValueFromItself, 'tab_label_'.$i)//Needed to sustain Attribute integrity.
                ->setPlaceholderText('Ex. Projects & schedules')
                ->setDataAttribute('tab', $i)
            );

            $this->attributes->push((new Documents())
                ->setLabelText('image')
                ->onlyAllowImages()
                ->setSubFolder('component_uploads')
                ->setMaxDocuments(1)
                ->setImageProperties([
                    (new ImageProperty())->setName('original')->setCropMethod(ImageProperty::Resize)->setWidth(1200),
                    (new ImageProperty())->setName('large')->setCropMethod(ImageProperty::Fit)->setWidth(800)->setHeight(480),
                    (new ImageProperty())->setName('medium')->setCropMethod(ImageProperty::Fit)->setWidth(600)->setHeight(360),
                    (new ImageProperty())->setName('small')->setCropMethod(ImageProperty::Fit)->setWidth(375)->setHeight(225),
                ])
                ->setSmallDragAndDropArea()
                ->setDataAttribute('tab', $i)
                ->mapValueFrom(Attribute::ValueFromItself, 'tab_image_'.$i));

            $this->attributes->push((new TextArea())
                ->setLabelText('Description')
                ->enableTinymceEditorOnClick()
                ->setPlaceholderText('Ex. Lorum ipsum')
                ->mapValueFrom(Attribute::ValueFromItself, 'tab_description_'.$i)//Needed to sustain Attribute integrity.
                ->setDataAttribute('tab', $i));


            $this->addButton('Button '.$i, $i);
        }
    }

    public function prepare(ViewComponent $component)
    {

        $sidenavsliderComponent = (object) [
            'items' => collect(),
        ];

        $whileCounter = 1;
        do {
            // Break if a variable the first value of the to grab items isn't found
            if (! isset($component->{'tab_label_'.$whileCounter}) || $component->{'tab_label_'.$whileCounter} == '') {
                // If zero continue just continue, because sometimes it starts from 1
                if ($whileCounter == 0) {
                    $whileCounter++;
                    continue;
                } else {
                    break;
                }
            }

            // Grab the desired attribute from the component
            $tabItem = (object) [
                'icon' => $component->{'tab_icon_'.$whileCounter},
                'label' => $component->{'tab_label_'.$whileCounter},
                'image' => $component->{'tab_image_'.$whileCounter},
                'description' => $component->{'tab_description_'.$whileCounter},
                'button_label' => $component->{'button_'.$whileCounter.'_label'},
                'button_link' => $component->{'button_'.$whileCounter.'_link'},
            ];

            $sidenavsliderComponent->items->push($tabItem);
            $whileCounter++;

            // Safety break at 50
        } while ($whileCounter <= $this->tabCount);

        unset($component->tab_icon_1);
        unset($component->tab_label_1);
        unset($component->tab_image_1);
        unset($component->tab_description_1);
        unset($component->button_1_label);
        unset($component->button_1_link);
        unset($component->tab_icon_2);
        unset($component->tab_label_2);
        unset($component->tab_image_2);
        unset($component->tab_description_2);
        unset($component->button_2_label);
        unset($component->button_2_link);
        unset($component->tab_icon_3);
        unset($component->tab_label_3);
        unset($component->tab_image_3);
        unset($component->tab_description_3);
        unset($component->button_3_label);
        unset($component->button_3_link);
        unset($component->tab_icon_4);
        unset($component->tab_label_4);
        unset($component->tab_image_4);
        unset($component->tab_description_4);
        unset($component->button_4_label);
        unset($component->button_4_link);
        unset($component->tab_icon_5);
        unset($component->tab_label_5);
        unset($component->tab_image_5);
        unset($component->tab_description_5);
        unset($component->button_5_label);
        unset($component->button_5_link);
        unset($component->tab_icon_6);
        unset($component->tab_label_6);
        unset($component->tab_image_6);
        unset($component->tab_description_6);
        unset($component->button_6_label);
        unset($component->button_6_link);

        $component->tabs = $sidenavsliderComponent;

    }

}