2022-03-13 19:32:32 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Core\EventSubscriber\Site\Page;
|
|
|
|
|
|
|
|
use App\Core\Entity\EntityInterface;
|
|
|
|
use App\Core\Entity\Site\Page\FileBlock;
|
|
|
|
use App\Core\Entity\Site\Page\Page;
|
|
|
|
use App\Core\Event\EntityManager\EntityManagerEvent;
|
|
|
|
use App\Core\EventSubscriber\EntityManagerEventSubscriber;
|
|
|
|
use App\Core\Form\FileUploadHandler;
|
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* class BlockEventSubscriber.
|
|
|
|
*
|
|
|
|
* @author Simon Vieille <simon@deblan.fr>
|
|
|
|
*/
|
|
|
|
class BlockEventSubscriber extends EntityManagerEventSubscriber
|
|
|
|
{
|
2023-10-20 09:44:18 +02:00
|
|
|
public function __construct(protected FileUploadHandler $fileUpload)
|
2022-03-13 19:32:32 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-04-25 16:32:05 +02:00
|
|
|
public function supports(EntityInterface $entity): bool
|
2022-03-13 19:32:32 +01:00
|
|
|
{
|
|
|
|
return $entity instanceof Page;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onPreUpdate(EntityManagerEvent $event)
|
|
|
|
{
|
2022-04-25 16:32:05 +02:00
|
|
|
if (!$this->supports($event->getEntity())) {
|
2022-03-13 19:32:32 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($event->getEntity()->getBlocks() as $block) {
|
|
|
|
if ($block instanceof FileBlock) {
|
|
|
|
if ($block->getValue() instanceof UploadedFile) {
|
|
|
|
$directory = 'uploads/page/block';
|
|
|
|
|
|
|
|
$this->fileUpload->handleForm(
|
|
|
|
$block->getValue(),
|
|
|
|
$directory,
|
|
|
|
function ($filename) use ($block, $directory) {
|
|
|
|
$block->setValue($directory.'/'.$filename);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function onPreCreate(EntityManagerEvent $event)
|
|
|
|
{
|
|
|
|
return $this->onPreUpdate($event);
|
|
|
|
}
|
|
|
|
}
|