tinternet.net/core/EventSuscriber/Site/Page/BlockEventSubscriber.php

61 lines
1.7 KiB
PHP
Raw Normal View History

2021-03-22 12:42:48 +01:00
<?php
namespace App\Core\EventSuscriber\Site\Page;
use App\Core\Entity\EntityInterface;
use App\Core\Entity\Site\Page\Block;
use App\Core\Event\EntityManager\EntityManagerEvent;
use App\Core\EventSuscriber\EntityManagerEventSubscriber;
use App\Core\Form\FileUploadHandler;
2021-03-22 16:52:39 +01:00
use App\Core\Entity\Site\Page\FileBlock;
use App\Core\Entity\Site\Page\Page;
2021-03-22 12:42:48 +01:00
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* class BlockEventSubscriber.
*
* @author Simon Vieille <simon@deblan.fr>
*/
class BlockEventSubscriber extends EntityManagerEventSubscriber
{
protected FileUploadHandler $fileUpload;
public function __construct(FileUploadHandler $fileUpload)
{
$this->fileUpload = $fileUpload;
}
public function support(EntityInterface $entity)
{
2021-03-22 16:52:39 +01:00
return $entity instanceof Page;
2021-03-22 12:42:48 +01:00
}
public function onPreUpdate(EntityManagerEvent $event)
{
if (!$this->support($event->getEntity())) {
return;
}
2021-03-22 16:52:39 +01:00
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);
}
);
}
2021-03-22 12:42:48 +01:00
}
2021-03-22 16:52:39 +01:00
}
2021-03-22 12:42:48 +01:00
}
public function onPreCreate(EntityManagerEvent $event)
{
return $this->onPreUpdate($event);
}
}