<?php
namespace App\EventSubscriber;
use Algolia\SearchBundle\SearchService;
use App\Entity\Clonar;
use App\Entity\Inventario;
use App\Entity\POSAdmin;
use App\Entity\Producto;
use App\Service\QI;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use EasyCorp\Bundle\EasyAdminBundle\Event\EasyAdminEvents;
use GPBMetadata\Google\Type\PostalAddress;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Kreait\Firebase\Factory;
use Kreait\Firebase\ServiceAccount;
use Symfony\Component\EventDispatcher\GenericEvent;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
class EasyAdminSubscriber implements EventSubscriberInterface
{
protected $em;
protected $qi;
protected $rs;
private $ts;
protected $searchService;
public function __construct(EntityManagerInterface $em, QI $qi, RequestStack $request_stack, TokenStorageInterface $ts,SearchService $searchService)
{
$this->em = $em;
$this->qi = $qi;
$this->rs = $request_stack;
$this->ts = $ts;
$this->searchService = $searchService;
}
public static function getSubscribedEvents()
{
return array(
EasyAdminEvents::POST_UPDATE => array('onPostUpdate'),
EasyAdminEvents::PRE_PERSIST => array('onPrePersist'),
EasyAdminEvents::PRE_UPDATE=> array('onPreUpdate'),
EasyAdminEvents::POST_PERSIST => array('onPostUpdate'),
EasyAdminEvents::PRE_REMOVE => array('onPreRemove'),
EasyAdminEvents::POST_REMOVE => array('onPostRemove'),
);
}
function to_camel_case($str, $capitalise_first_char = false) {
if($capitalise_first_char) {
$str[0] = strtoupper($str[0]);
}
$func = create_function('$c', 'return strtoupper($c[1]);');
return preg_replace_callback('/_([a-z])/', $func, $str);
}
public function onPostUpdate(GenericEvent $event)
{
$entity = $event->getSubject();
$this->qi->saveFire($entity);
$this->rs->getCurrentRequest()->getSession()->set('accion0','entre');
if($entity instanceof Producto){
$this->rs->getCurrentRequest()->getSession()->set('accion1','entre');
/* @var $user POSAdmin */
$user = $this->ts->getToken()->getUser();
if(method_exists($user,'getTienda') && $user->getTienda()){
$inventario = $this->em->getRepository('App:Inventario')->findOneBy(array('producto'=>$entity,'tienda'=>$user->getTienda()));
if (!$inventario){
$inventario= new Inventario();
$inventario->setProducto($entity);
$inventario->setTienda($user->getTienda());
$inventario->setPrecio($entity->getPrecio());
$inventario->setCantidad(1);
}else{
$inventario->setPrecio($entity->getPrecio());
$inventario->setCantidad(1);
}
$this->em->persist($inventario);
$this->em->flush();
$this->qi->saveFire($inventario);
}
$this->searchService->index($this->em, $entity);
if(count($entity->getHijos())>0){
foreach ($entity->getHijos() as $hijo){
// dd(count($entity->getHijos()));
// $hijo->setRecomendado($entity->getRecomendado());
// //$hijo->setDisponible($entity->getDisponible());
// $hijo->setNombre($entity->getNombre());
// $hijo->setVisible($entity->getVisible());
// $hijo->setPrecio($entity->getPrecio());
// $hijo->setPrecioSinDescuento($entity->getPrecioSinDescuento());
// $hijo->setPreciousd($entity->getPreciousd());
// $hijo->setPrecioSinDescuentousd($entity->getPrecioSinDescuentousd());
// $hijo->setPrecioeuro($entity->getPrecioeuro());
// $hijo->setPrecioSinDescuentoeuro($entity->getPrecioSinDescuentoeuro());
// $hijo->setPreciomex($entity->getPreciomex());
// $hijo->setPrecioSinDescuentomxn($entity->getPrecioSinDescuentomxn());
// $hijo->setResumenEs($entity->getResumenEs());
// $hijo->setResumenEn($entity->getResumenEn());
// $hijo->setSilueta($entity->getSilueta());
// $hijo->setColeccion($entity->getColeccion());
// $hijo->setCampaign($entity->getCampaign());
// $hijo->setImagen($entity->getImagen());
$hijo->setSubcategoria($entity->getSubcategoria());
// foreach ($entity->getGuiaTallas() as $guia){
// $hijo->addGuiaTalla($guia);
// }
foreach ($entity->getCategorias() as $categoria){
$hijo->addCategoria($categoria);
}
// foreach ($entity->getSubcategoria() as $subcategoria){
// $hijo->setSubcategoria($subcategoria);
// }
// foreach ($entity->getImagenProductos() as $imagen){
// $hijo->addImagenProducto($imagen);
// }
// foreach ($entity->getRecomendados() as $recomendado){
// $hijo->addRecomendado($recomendado);
// }
//$hijo->setVerdeid($entity->getVerdeid());
$this->em->persist($hijo);
$this->em->flush();
}
}
}
if($entity instanceof Clonar){
/* @var $entity Clonar */
$fuente = $entity->getFuente()->getId();
$destino = $entity->getDestino()->getId();
$this->qi->clonarInventario($fuente, $destino);
$this->qi->popularCategorias($entity->getDestino());
}
}
public function onPostPersist(GenericEvent $event)
{
$entity = $event->getSubject();
if($entity instanceof Producto){
$this->searchService->index($this->em, $entity);
}
$this->qi->saveFire($entity);
}
public function onPrePersist(GenericEvent $event)
{
$entity = $event->getSubject();
/*$firekey = $entity->getId();
$entity->setKeyFirebase($firekey);*/
$event['entity'] = $entity;
}
public function onPreUpdate(GenericEvent $event)
{
$entity = $event->getSubject();
/*
if($entity->getKeyFirebase() == "") {
$firekey = $entity->getId();
$entity->setKeyFirebase($firekey);
}
*/
$event['entity'] = $entity;
}
public function onPreRemove(GenericEvent $event)
{
$entity = $event->getSubject();
$session = $this->rs->getCurrentRequest()->getSession();
$session->set('ent_id',$entity->getId());
$session->set('ent_tabla',get_class($entity));
//$this->qi->removeFire($entity);
//dump($entity);
//die();
}
public function onPostRemove(GenericEvent $event)
{
$entity = $event->getSubject();
$session = $this->rs->getCurrentRequest()->getSession();
$id = $session->get('ent_id');
$tabla = $session->get('ent_tabla');
$tabla = explode(':',$tabla);
$tabla = end($tabla);
$tabla = explode("\\",get_class($entity));
$tabla = end($tabla);
if($entity instanceof Producto) {
$this->searchService->remove($this->em, $entity);
}
//$this->qi->removeFireBack($tabla,$id);
//dump($entity);
//die();
}
}