File: D:/HostingSpaces/SBogers10/slenders.komma.pro/app/Komma/Components/ComponentType/Types/Usp.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 Usp extends AbstractComponentType
{
const POSSIBLE_AMOUNT = 3;
public function __construct()
{
//Set an id that corresponds to the ComponentTypes enum
$this->id = ComponentTypes::USP;
//Set icon and name
$this->name = 'usp';
parent::__construct();
$this->attributes->push((new TextField('Titel'))
->setPlaceholderText('Ex. Usp')
->mapValueFrom(Attribute::ValueFromComponent, 'title')
);
$this->attributes->push((new OnOff())
->setLabelText("Dark color")
->mapValueFrom(Attribute::ValueFromComponent, 'dark_color')
);
for($i = 1; $i <= self::POSSIBLE_AMOUNT; $i++) {
$this->attributes->push((new TextField('Usp'))
->setPlaceholderText('Ex. Meer aandacht')
->setDataAttribute('tab', $i)
->mapValueFrom(Attribute::ValueFromComponent, 'title_' . $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('Impression')
->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)
{
$usps = collect();
for($i = 1; $i <= self::POSSIBLE_AMOUNT; $i++) {
$name = $viewComponent->{'title_'.$i};
$description = $viewComponent->{'description_'.$i};
$image = $viewComponent->{'image_'.$i};
if(!empty($title) || !empty($description)) {
$usp = (object)[
'id' => $i,
'name' => $name,
'description' => $description,
'image' => $image
];
$usps->push($usp);
}
unset($viewComponent->{'title_'.$i});
unset($viewComponent->{'description_'.$i});
unset($viewComponent->{'image_'.$i});
}
$viewComponent->usps = $usps;
}
}