<?php
namespace App\EventListener;
use Sogec\BOBundle\Event\CRUDEvents;
use Sogec\BOBundle\Event\ExportListEvent;
use Symfony\Component\EventDispatcher\Event;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
/**
* Class ExportParticipationSubscriber.
*/
class ExportParticipationSubscriber implements EventSubscriberInterface
{
/**
* @var SessionInterface
*/
private $session;
/**
* ExportParticipationSubscriber constructor.
*
* @param SessionInterface $session
*/
public function __construct(SessionInterface $session)
{
$this->session = $session;
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return [
CRUDEvents::EXPORT_INITIALIZE => [
['isFilterEmpty', 2],
],
];
}
/**
* @param ExportListEvent $event
*/
public function isFilterEmpty(Event $event)
{
if ('participation_filter' !== $event->getForm()->getName()) {
return;
}
$tErrors = [];
$data = unserialize($this->session->get('filter_list_'.$event->getForm()->getName(), 'a:0:{}'));
if (empty($data)) {
$tErrors[] = 'Veillez filtrer la liste avant de faire un export';
$event->setExportErrors($tErrors);
$event->stopPropagation();
}
}
/**
* @param ExportListEvent $event
*/
public function isParticipationExportable(Event $event)
{
if ('participation_filter' !== $event->getForm()->getName()) {
return;
}
$tErrors = [];
$data = unserialize($this->session->get('filter_list_'.$event->getForm()->getName(), 'a:0:{}'));
// il faut filtrer
if (('participation_filter' === $event->getForm()->getName())) {
if (isset($data['operation']) && 1 !== count($data['operation'])) {
$tErrors[] = 'Veillez séléctionner seulement une operation';
$event->setExportErrors($tErrors);
}
}
}
}