File: D:/HostingSpaces/slenders/slenders.nl/app/Komma/Components/ComponentType/Types/ClientReviews.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\OnOff;
use App\Komma\Kms\Core\Attributes\TextArea;
use App\Komma\Kms\Core\Attributes\TextField;
class ClientReviews extends AbstractComponentType
{
const POSSIBLE_AMOUNT = 5;
public function __construct()
{
//Set an id that corresponds to the ComponentTypes enum
$this->id = ComponentTypes::CLIENT_REVIEWS;
//Set icon and name
$this->name = 'client-reviews';
parent::__construct();
$this->attributes->push((new TextField('Titel'))
->setPlaceholderText('Ex. Klanten aan het woord')
->mapValueFrom(Attribute::ValueFromComponent, 'title')
);
for($i = 1; $i <= self::POSSIBLE_AMOUNT; $i++) {
$this->attributes->push((new TextField('Naam'))
->setPlaceholderText('Ex. John Doe')
->setDataAttribute('tab', $i)
->mapValueFrom(Attribute::ValueFromComponent, 'name_' . $i)//Needed to sustain Attribute integrity.
);
$this->attributes->push((new TextArea())
->setLabelText('Omschrijving')
->setPlaceholderText('Ex. Aangezien echte aandacht daar voor mij...')
->setDataAttribute('tab', $i)
->mapValueFrom(Attribute::ValueFromComponent, 'description_' . $i)//Needed to sustain Attribute integrity.
);
$this->attributes->push((new Documents())
->setLabelText('Afbeelding')
->onlyAllowImages()
->setSmallDragAndDropArea()
->setMaxDocuments(1)
->setSubFolder('component_uploads')
->mapValueFrom(Attribute::ValueFromComponent, 'image_' . $i) //Needed to sustain Attribute integrity.
->setDataAttribute('tab', $i ));
}
}
public function prepare(ViewComponent $viewComponent)
{
$reviews = collect();
for($i = 1; $i <= self::POSSIBLE_AMOUNT; $i++) {
$name = $viewComponent->{'name_'.$i};
$description = $viewComponent->{'description_'.$i};
$image = $viewComponent->{'image_'.$i};
if(!empty($title) || !empty($description)) {
$review = (object)[
'id' => $i,
'name' => $name,
'description' => $description,
'image' => $image
];
$reviews->push($review);
}
unset($viewComponent->{'name_'.$i});
unset($viewComponent->{'description_'.$i});
unset($viewComponent->{'image_'.$i});
}
$viewComponent->reviews = $reviews;
}
}