Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event pre_update triggered twice #2005

Closed
jakobeissler opened this issue Jan 16, 2018 · 3 comments
Closed

Event pre_update triggered twice #2005

jakobeissler opened this issue Jan 16, 2018 · 3 comments

Comments

@jakobeissler
Copy link

jakobeissler commented Jan 16, 2018

Hello everyone.

I've created an event subscriber that sends an email every time certain entity is updated but the event is triggered twice and i cant figure out why.

EasyAdminBundle version: 1.17.9

services.yml

app.easy_admin.aprobar_publicacion:
        class: AppBundle\EventListener\AprobarPublicacionSubscriber
        arguments: ['@mailer','@service_container','@twig']
        tags:
            - { name: kernel.event_subscriber, event: easy_admin.pre_update, method: preUpdate }

AprobarPublicacionSubscriber.php

<?php
namespace AppBundle\EventListener;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
use AppBundle\Entity\VestidoDeNovia;
use AppBundle\Entity\PlantillaEmail;

class AprobarPublicacionSubscriber implements EventSubscriberInterface {

    protected $mailer;
    protected $container;
    protected $twig;

    public static function getSubscribedEvents() {
        return array(
            'easy_admin.pre_update' => array('preUpdate'),
        );
    }

    public function __construct(\Swift_Mailer $mailer, \Symfony\Component\DependencyInjection\ContainerInterface $container, \Twig_Environment $twig) {

        $this->mailer = $mailer;
        $this->container = $container;
        $this->twig = $twig;
    }

    public function preUpdate(GenericEvent $event) {

        $entity = $event->getSubject();

        if (!($entity instanceof VestidoDeNovia)) {
            return;
        }
        
        $uow = $event->getArgument('em')->getUnitOfWork();
        $uow->computeChangeSets();
        $changeset = $uow->getEntityChangeSet($entity);

        if (array_key_exists('estado', $changeset)) {
            
            $estadoActual = $changeset['estado'][0]->getId();

            if ($estadoActual != 2 and $entity->getEstado()->getId() == 2) {

                $plantillaEmail = new PlantillaEmail();
                $plantillaEmail = $event->getArgument('em')->getRepository(PlantillaEmail::class)->find(6);

                $message = (new \Swift_Message($plantillaEmail->getAsunto()))
                        ->setFrom($this->container->getParameter('direccion_mail_sistema'))
                        ->setTo($entity->getUsuario()->getEmail())
                        ->setBody(
                        $this->twig->render(
                                'AppBundle::Emails/publicacion_exitosa.html.twig', array(
                            'titulo' => $plantillaEmail->getAsunto(),
                            'mensaje' => $plantillaEmail->getMensaje(),
                                )
                        ), 'text/html'
                        )
                ;
                $this->mailer->send($message);
            }
        }
    }

}

What am i missing?

Thank you.

@javiereguiluz
Copy link
Collaborator

This is the expected behavior ... but it didn't happen in previous versions because of a bug. You can edit information in two ways: via a form and via Ajax. The form is the regular workflow and is handled by editAction() and the Ajax edit is only for the boolean toggles of the list view. This is handled by the updateEntityProperty() method. So, maybe someone edit some data and then clicked on a toggle or viceversa.

If this is correct, you can remove the toggle from the list view or you can detect if the request is via Ajax and not execute the logic in that case. You can inject the RequestStack service to get the current request and execute the isXmlHttpRequest() method.

@jakobeissler
Copy link
Author

@javiereguiluz thank you for answering. I tried your solution but it still is triggered twice. I've injected RequestStack and checked isXmlHttpRequest(), and in both cases it is not an ajax call. It happens every time i update a form (no ajax).

@javiereguiluz
Copy link
Collaborator

I'm closing this issue because we're starting a new phase in the history of this bundle (see #2059). We've moved it into a new GitHub organization and we need to start from scratch: no past issues, no pending pull requests, etc.

I understand if you are angry or disappointed by this, but we really need to "reset" everything in order to reignite the development of this bundle.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants