src/EventSubscriber/EasyAdminSubscriber.php line 173

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Algolia\SearchBundle\SearchService;
  4. use App\Entity\Clonar;
  5. use App\Entity\Inventario;
  6. use App\Entity\POSAdmin;
  7. use App\Entity\Producto;
  8. use App\Service\QI;
  9. use Doctrine\ORM\EntityManager;
  10. use Doctrine\ORM\EntityManagerInterface;
  11. use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents;
  12. use GPBMetadata\Google\Type\PostalAddress;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Kreait\Firebase\Factory;
  15. use Kreait\Firebase\ServiceAccount;
  16. use Symfony\Component\EventDispatcher\GenericEvent;
  17. use Symfony\Component\HttpFoundation\RequestStack;
  18. use Symfony\Component\PropertyAccess\PropertyAccess;
  19. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  20. use Symfony\Component\Serializer\Encoder\JsonEncoder;
  21. use Symfony\Component\Serializer\Encoder\XmlEncoder;
  22. use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
  23. use Symfony\Component\Serializer\Serializer;
  24. class EasyAdminSubscriber implements EventSubscriberInterface
  25. {
  26.     protected $em;
  27.     protected $qi;
  28.     protected $rs;
  29.     private $ts;
  30.     protected $searchService;
  31.     public function __construct(EntityManagerInterface $emQI $qiRequestStack $request_stackTokenStorageInterface $ts,SearchService $searchService)
  32.     {
  33.         $this->em $em;
  34.         $this->qi $qi;
  35.         $this->rs $request_stack;
  36.         $this->ts $ts;
  37.         $this->searchService $searchService;
  38.     }
  39.     public static function getSubscribedEvents()
  40.     {
  41.         return array(
  42.             EasyAdminEvents::POST_UPDATE => array('onPostUpdate'),
  43.             EasyAdminEvents::PRE_PERSIST => array('onPrePersist'),
  44.             EasyAdminEvents::PRE_UPDATE=> array('onPreUpdate'),
  45.             EasyAdminEvents::POST_PERSIST => array('onPostUpdate'),
  46.             EasyAdminEvents::PRE_REMOVE => array('onPreRemove'),
  47.             EasyAdminEvents::POST_REMOVE => array('onPostRemove'),
  48.         );
  49.     }
  50.     function to_camel_case($str$capitalise_first_char false) {
  51.         if($capitalise_first_char) {
  52.             $str[0] = strtoupper($str[0]);
  53.         }
  54.         $func create_function('$c''return strtoupper($c[1]);');
  55.         return preg_replace_callback('/_([a-z])/'$func$str);
  56.     }
  57.     public function onPostUpdate(GenericEvent $event)
  58.     {
  59.         $entity $event->getSubject();
  60.         $this->qi->saveFire($entity);
  61.         $this->rs->getCurrentRequest()->getSession()->set('accion0','entre');
  62.         if($entity instanceof Producto){
  63.             $this->rs->getCurrentRequest()->getSession()->set('accion1','entre');
  64.             /* @var $user POSAdmin */
  65.             $user $this->ts->getToken()->getUser();
  66.             if(method_exists($user,'getTienda') && $user->getTienda()){
  67.                 $inventario $this->em->getRepository('App:Inventario')->findOneBy(array('producto'=>$entity,'tienda'=>$user->getTienda()));
  68.                 if (!$inventario){
  69.                     $inventario= new Inventario();
  70.                     $inventario->setProducto($entity);
  71.                     $inventario->setTienda($user->getTienda());
  72.                     $inventario->setPrecio($entity->getPrecio());
  73.                     $inventario->setCantidad(1);
  74.                 }else{
  75.                     $inventario->setPrecio($entity->getPrecio());
  76.                     $inventario->setCantidad(1);
  77.                 }
  78.                 $this->em->persist($inventario);
  79.                 $this->em->flush();
  80.                 $this->qi->saveFire($inventario);
  81.             }
  82.             $this->searchService->index($this->em$entity);
  83.             if(count($entity->getHijos())>0){
  84.                 
  85.                 
  86.                 foreach ($entity->getHijos() as $hijo){
  87.                     // dd(count($entity->getHijos()));
  88.                     // $hijo->setRecomendado($entity->getRecomendado());
  89.                     // //$hijo->setDisponible($entity->getDisponible());
  90.                     // $hijo->setNombre($entity->getNombre());
  91.                     // $hijo->setVisible($entity->getVisible());
  92.                     // $hijo->setPrecio($entity->getPrecio());
  93.                     // $hijo->setPrecioSinDescuento($entity->getPrecioSinDescuento());
  94.                     // $hijo->setPreciousd($entity->getPreciousd());
  95.                     // $hijo->setPrecioSinDescuentousd($entity->getPrecioSinDescuentousd());
  96.                     // $hijo->setPrecioeuro($entity->getPrecioeuro());
  97.                     // $hijo->setPrecioSinDescuentoeuro($entity->getPrecioSinDescuentoeuro());
  98.                     // $hijo->setPreciomex($entity->getPreciomex());
  99.                     // $hijo->setPrecioSinDescuentomxn($entity->getPrecioSinDescuentomxn());
  100.                     // $hijo->setResumenEs($entity->getResumenEs());
  101.                     // $hijo->setResumenEn($entity->getResumenEn());
  102.                     // $hijo->setSilueta($entity->getSilueta());
  103.                     // $hijo->setColeccion($entity->getColeccion());
  104.                     // $hijo->setCampaign($entity->getCampaign());
  105.                     // $hijo->setImagen($entity->getImagen());
  106.                     $hijo->setSubcategoria($entity->getSubcategoria());
  107.                     // foreach ($entity->getGuiaTallas() as $guia){
  108.                     //     $hijo->addGuiaTalla($guia);
  109.                     // }
  110.                     foreach ($entity->getCategorias() as $categoria){
  111.                         $hijo->addCategoria($categoria);
  112.                     }
  113.                     // foreach ($entity->getSubcategoria() as $subcategoria){
  114.                     //     $hijo->setSubcategoria($subcategoria);
  115.                     // }
  116.                     // foreach ($entity->getImagenProductos() as $imagen){
  117.                     //     $hijo->addImagenProducto($imagen);
  118.                     // }
  119.                     // foreach ($entity->getRecomendados() as $recomendado){
  120.                     //     $hijo->addRecomendado($recomendado);
  121.                     // }
  122.                     //$hijo->setVerdeid($entity->getVerdeid());
  123.                     $this->em->persist($hijo);
  124.                     $this->em->flush();
  125.                 }
  126.             }
  127.         }
  128.         if($entity instanceof Clonar){
  129.             /* @var $entity Clonar */
  130.             $fuente $entity->getFuente()->getId();
  131.             $destino $entity->getDestino()->getId();
  132.             $this->qi->clonarInventario($fuente$destino);
  133.             $this->qi->popularCategorias($entity->getDestino());
  134.         }
  135.     }
  136.     public function onPostPersist(GenericEvent $event)
  137.     {
  138.         $entity $event->getSubject();
  139.         if($entity instanceof Producto){
  140.             $this->searchService->index($this->em$entity);
  141.         }
  142.         $this->qi->saveFire($entity);
  143.     }
  144.     public function onPrePersist(GenericEvent $event)
  145.     {
  146.         $entity $event->getSubject();
  147.         /*$firekey = $entity->getId();
  148.         $entity->setKeyFirebase($firekey);*/
  149.         $event['entity'] = $entity;
  150.     }
  151.     public function onPreUpdate(GenericEvent $event)
  152.     {
  153.         $entity $event->getSubject();
  154.         /*
  155.         if($entity->getKeyFirebase() == "") {
  156.             $firekey = $entity->getId();
  157.             $entity->setKeyFirebase($firekey);
  158.         }
  159.         */
  160.         $event['entity'] = $entity;
  161.     }
  162.     public function onPreRemove(GenericEvent $event)
  163.     {
  164.         $entity $event->getSubject();
  165.         $session $this->rs->getCurrentRequest()->getSession();
  166.         $session->set('ent_id',$entity->getId());
  167.         $session->set('ent_tabla',get_class($entity));
  168.         //$this->qi->removeFire($entity);
  169.         //dump($entity);
  170.         //die();
  171.     }
  172.     public function onPostRemove(GenericEvent $event)
  173.     {
  174.         $entity $event->getSubject();
  175.         $session $this->rs->getCurrentRequest()->getSession();
  176.         $id $session->get('ent_id');
  177.         $tabla $session->get('ent_tabla');
  178.         $tabla explode(':',$tabla);
  179.         $tabla end($tabla);
  180.         $tabla explode("\\",get_class($entity));
  181.         $tabla end($tabla);
  182.         if($entity instanceof Producto) {
  183.             $this->searchService->remove($this->em$entity);
  184.         }
  185.         //$this->qi->removeFireBack($tabla,$id);
  186.         //dump($entity);
  187.         //die();
  188.     }
  189. }