src/EventSubscriber/EasyAdminSubscriber.php line 59

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Clonar;
  4. use App\Entity\Inventario;
  5. use App\Entity\POSAdmin;
  6. use App\Entity\Producto;
  7. use App\Service\QI;
  8. use Doctrine\ORM\EntityManager;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents;
  11. use GPBMetadata\Google\Type\PostalAddress;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Kreait\Firebase\Factory;
  14. use Kreait\Firebase\ServiceAccount;
  15. use Symfony\Component\EventDispatcher\GenericEvent;
  16. use Symfony\Component\HttpFoundation\RequestStack;
  17. use Symfony\Component\PropertyAccess\PropertyAccess;
  18. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  19. use Symfony\Component\Serializer\Encoder\JsonEncoder;
  20. use Symfony\Component\Serializer\Encoder\XmlEncoder;
  21. use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
  22. use Symfony\Component\Serializer\Serializer;
  23. class EasyAdminSubscriber implements EventSubscriberInterface
  24. {
  25.     protected $em;
  26.     protected $qi;
  27.     protected $rs;
  28.     private $ts;
  29.     public function __construct(EntityManagerInterface $emQI $qiRequestStack $request_stackTokenStorageInterface $ts)
  30.     {
  31.         $this->em $em;
  32.         $this->qi $qi;
  33.         $this->rs $request_stack;
  34.         $this->ts $ts;
  35.     }
  36.     public static function getSubscribedEvents()
  37.     {
  38.         return array(
  39.             EasyAdminEvents::POST_UPDATE => array('onPostUpdate'),
  40.             EasyAdminEvents::PRE_PERSIST => array('onPrePersist'),
  41.             EasyAdminEvents::PRE_UPDATE=> array('onPreUpdate'),
  42.             EasyAdminEvents::POST_PERSIST => array('onPostUpdate'),
  43.             EasyAdminEvents::PRE_REMOVE => array('onPreRemove'),
  44.             EasyAdminEvents::POST_REMOVE => array('onPostRemove'),
  45.         );
  46.     }
  47.     function to_camel_case($str$capitalise_first_char false) {
  48.         if($capitalise_first_char) {
  49.             $str[0] = strtoupper($str[0]);
  50.         }
  51.         $func create_function('$c''return strtoupper($c[1]);');
  52.         return preg_replace_callback('/_([a-z])/'$func$str);
  53.     }
  54.     public function onPostUpdate(GenericEvent $event)
  55.     {
  56.         $entity $event->getSubject();
  57.         $this->qi->saveFire($entity);
  58.         $this->rs->getCurrentRequest()->getSession()->set('accion0','entre');
  59.         if($entity instanceof Producto){
  60.             $this->rs->getCurrentRequest()->getSession()->set('accion1','entre');
  61.             /* @var $user POSAdmin */
  62.             $user $this->ts->getToken()->getUser();
  63.             if(method_exists($user,'getTienda') && $user->getTienda()){
  64.                 $inventario $this->em->getRepository('App:Inventario')->findOneBy(array('producto'=>$entity,'tienda'=>$user->getTienda()));
  65.                 if (!$inventario){
  66.                     $inventario= new Inventario();
  67.                     $inventario->setProducto($entity);
  68.                     $inventario->setTienda($user->getTienda());
  69.                     $inventario->setPrecio($entity->getPrecio());
  70.                     $inventario->setCantidad(1);
  71.                 }else{
  72.                     $inventario->setPrecio($entity->getPrecio());
  73.                     $inventario->setCantidad(1);
  74.                 }
  75.                 $this->em->persist($inventario);
  76.                 $this->em->flush();
  77.                 $this->qi->saveFire($inventario);
  78.             }
  79.         }
  80.         if($entity instanceof Clonar){
  81.             /* @var $entity Clonar */
  82.             $fuente $entity->getFuente()->getId();
  83.             $destino $entity->getDestino()->getId();
  84.             $this->qi->clonarInventario($fuente$destino);
  85.             $this->qi->popularCategorias($entity->getDestino());
  86.         }
  87.     }
  88.     public function onPostPersist(GenericEvent $event)
  89.     {
  90.         $entity $event->getSubject();
  91.         $this->qi->saveFire($entity);
  92.     }
  93.     public function onPrePersist(GenericEvent $event)
  94.     {
  95.         $entity $event->getSubject();
  96.         /*$firekey = $entity->getId();
  97.         $entity->setKeyFirebase($firekey);*/
  98.         $event['entity'] = $entity;
  99.     }
  100.     public function onPreUpdate(GenericEvent $event)
  101.     {
  102.         $entity $event->getSubject();
  103.         /*
  104.         if($entity->getKeyFirebase() == "") {
  105.             $firekey = $entity->getId();
  106.             $entity->setKeyFirebase($firekey);
  107.         }
  108.         */
  109.         $event['entity'] = $entity;
  110.     }
  111.     public function onPreRemove(GenericEvent $event)
  112.     {
  113.         $entity $event->getSubject();
  114.         $session $this->rs->getCurrentRequest()->getSession();
  115.         $session->set('ent_id',$entity->getId());
  116.         $session->set('ent_tabla',get_class($entity));
  117.         //$this->qi->removeFire($entity);
  118.         //dump($entity);
  119.         //die();
  120.     }
  121.     public function onPostRemove(GenericEvent $event)
  122.     {
  123.         $entity $event->getSubject();
  124.         $session $this->rs->getCurrentRequest()->getSession();
  125.         $id $session->get('ent_id');
  126.         $tabla $session->get('ent_tabla');
  127.         $tabla explode(':',$tabla);
  128.         $tabla end($tabla);
  129.         $tabla explode("\\",get_class($entity));
  130.         $tabla end($tabla);
  131.         //$this->qi->removeFireBack($tabla,$id);
  132.         //dump($entity);
  133.         //die();
  134.     }
  135. }