<?php
namespace App\Form\EventListener;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Doctrine\ORM\EntityRepository;
use App\Entity\Reports;
class AddCompaniaFieldSubscriber implements EventSubscriberInterface
{
/* private $factory;
public function __construct(FormFactoryInterface $factory)
{
$this->factory = $factory;
}*/
public static function getSubscribedEvents()
{
return [
FormEvents::PRE_SET_DATA => 'onPreSetData',
FormEvents::PRE_SUBMIT => 'onPreSubmit',
];
}
private function addCompaniaForm($form, $estado) //$province)
{
$form->add($this->factory->createNamed('compania','entity', null, array(
'class' =>Reports::class,
'empty_value' => 'Compania',
'auto_initialize' => false,
'mapped' => false,
'property' => 'fkcompania',
'query_builder' => function (EntityRepository $repository) use ($estado){//($province) {
$qb = $repository->createQueryBuilder('c')
//->innerJoin('c.estado', 'estado')
;
//if ($province instanceof Provincia) {
if ($estado instanceof Reports) {
$qb->where('c.estado = :estado')
->setParameter('estado', $estado);
}/* else {
$qb->where('provincia.provincia = :provincia')
->setParameter('provincia', null);
}*/
return $qb;
}
)));
}
public function preSetData(FormEvent $event)
{
$data = $event->getData();
$form = $event->getForm();
if (null === $data) {
return;
}
$estado = ($data->getSiniestroId()) ? $data->getSiniestroId()->getEstado() : null ;
$this->addCompaniaForm($form, $estado);
}
public function preBind(FormEvent $event)
{
$data = $event->getData();
$form = $event->getForm();
if (null === $data) {
return;
}
$province = array_key_exists('fkprovincia', $data) ? $data['fkprovincia'] : null;
$this->addCityForm($form, $province);
}
}