File: D:/HostingSpaces/SBogers10/otium.komma.nl/app/Components/Types/Data.php
<?php
namespace App\Components\Types;
use App\Components\ComponentTypes;
use Komma\KMS\Components\Component\ViewComponent;
use Komma\KMS\Components\ComponentType\Types\AbstractComponentType;
use Komma\KMS\Core\Attributes\Documents;
use Komma\KMS\Core\Attributes\Models\ImageProperty;
use Komma\KMS\Core\Attributes\Seperator;
use Komma\KMS\Core\Attributes\TextField;
use Komma\KMS\Core\Attributes\Title;
class Data extends AbstractComponentType
{
protected int $id = ComponentTypes::DATA;
protected string $name = 'data';
// Number of tabs that can be filled
const POSSIBLE_AMOUNT = 10;
public function defineAttributesAndTabs()
{
$this->addItems([
(new Title())
->setLabelText('Afbeeldingen'),
(new Documents())
->setReference('image_left')
->setLabelText('Afbeelding 1')
->onlyAllowImages()
->setSmallDragAndDropArea()
->setMaxDocuments(1)
->setSubFolder('data')
->setImageProperties([
(new ImageProperty())->setName('small')->setCropMethod(ImageProperty::Fit)->setWidth(500)->setHeight(500),
(new ImageProperty())->setName('medium')->setCropMethod(ImageProperty::Fit)->setWidth(720)->setHeight(414),
(new ImageProperty())->setName('large')->setCropMethod(ImageProperty::Fit)->setWidth(1440)->setHeight(828),
]),
(new Documents())
->setReference('image_right')
->setLabelText('Afbeelding 2')
->onlyAllowImages()
->setSmallDragAndDropArea()
->setMaxDocuments(1)
->setSubFolder('data')
->setImageProperties([
(new ImageProperty())->setName('small')->setCropMethod(ImageProperty::Fit)->setWidth(500)->setHeight(500),
(new ImageProperty())->setName('medium')->setCropMethod(ImageProperty::Fit)->setWidth(720)->setHeight(414),
(new ImageProperty())->setName('large')->setCropMethod(ImageProperty::Fit)->setWidth(1440)->setHeight(828),
]),
(new Seperator()),
(new Title())
->setLabelText('Data'),
]);
// Make the tabs
for($i = 0; $i < self::POSSIBLE_AMOUNT; $i++) {
$this->addItems([
(new TextField())
->setReference('key_' . ($i + 1))
->setLabelText('Label')
->setDataAttribute('tab', ($i + 1)),
(new TextField())
->setReference('value_' . ($i + 1))
->setLabelText('Waarde')
->setDataAttribute('tab', ($i + 1))
]);
}
}
public function prepare(ViewComponent $viewComponent)
{
$tabs = collect();
for($i = 1; $i <= self::POSSIBLE_AMOUNT; $i++) {
$key = $viewComponent->{'key_'.$i};
$value = $viewComponent->{'value_'.$i};
if(!empty($key) || !empty($value)) {
$tab = (object)[
'id' => $i,
'key' => $key,
'value' => $value,
];
$tabs->push($tab);
}
unset($viewComponent->{'key_'.$i});
unset($viewComponent->{'value_'.$i});
}
$viewComponent->tabs = $tabs;
}
}