src/EventSubscriber/Bloc/BlocSubscriber.php line 51

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber\Bloc;
  3. use App\Application\Sonata\MediaBundle\Entity\Media;
  4. use App\Entity\Formulaire\Contact;
  5. use App\Entity\Formulaire\Conversation;
  6. use App\Entity\Formulaire\Message;
  7. use App\Event\BlocEvent;
  8. use App\Utils\Emails\ContactEmail;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use FOS\UserBundle\Model\UserInterface;
  11. use Symfony\Bundle\SwiftmailerBundle\EventListener\EmailSenderListener;
  12. use Symfony\Component\DependencyInjection\ContainerInterface;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\Form\FormError;
  15. use Symfony\Component\HttpFoundation\File\Exception\FileException;
  16. use Symfony\Component\HttpFoundation\RedirectResponse;
  17. use Psr\Log\LoggerInterface;
  18. use Sonata\MediaBundle\Entity\MediaManager;
  19. class BlocSubscriber implements EventSubscriberInterface
  20. {
  21.     private $em;
  22.     private $contactEmail;
  23.     private $container;
  24.     private $logger;
  25.     private $mediaManager;
  26.     public function __construct(
  27.         EntityManagerInterface $entityManager,
  28.         ContactEmail $contactEmail,
  29.         ContainerInterface $container,
  30.         LoggerInterface $logger,
  31.         MediaManager $mediaManager
  32.     ) {
  33.         $this->em               $entityManager;
  34.         $this->contactEmail     $contactEmail;
  35.         $this->container        $container;
  36.         $this->logger           $logger;
  37.         $this->mediaManager     $mediaManager;
  38.     }
  39.     public static function getSubscribedEvents()
  40.     {
  41.         return array(
  42.             BlocEvent::BLOC_INITIALIZE_FORM => array('onBlocInitializeForm', -10),
  43.         );
  44.     }
  45.     public function onBlocInitializeForm(BlocEvent $event)
  46.     {
  47.         $bloc       $event->getBloc();
  48.         $user       $event->getUser();
  49.         $produit    $event->getProduit();
  50.         $request    $event->getRequest();
  51.         // Override du titre et texte du bloc
  52.         if (
  53.             $bloc->getFormulaire() == Contact::FORM_CSE &&
  54.             $user instanceof UserInterface &&
  55.             false === $user->getGroups()->isEmpty()
  56.         ) {
  57.             $userGroup $user->getGroups()->first();
  58.             if (false === $userGroup->getAfficherFormulaire()) {
  59.                 return;
  60.             }
  61.             $bloc->setTitre($userGroup->getTitreFormulaire());
  62.             $bloc->setTexte($userGroup->getTexteFormulaire());
  63.         }
  64.         $contact = new Contact();
  65.         $contact->setFormulaire($bloc->getFormulaire());
  66.         if ($user instanceof UserInterface) {
  67.             $contact->setUser($user);
  68.         }
  69.         if ($contact->getFormulaire() == Contact::FORM_PRODUIT && !is_null($produit)) {
  70.             $contact->setProduit($produit);
  71.         }
  72.         $className 'App\\Form\\Type\\Formulaire\\'.$bloc->getFormulaire().'Type';
  73.         $form $this->container->get('form.factory')->create($className$contact);
  74.         $form->handleRequest($request);
  75.         if (!$form->isSubmitted()) {
  76.             if ($contact->getFormulaire() == Contact::FORM_PRODUIT && !is_null($produit)) {
  77.                 $contact->setObjet($produit->getTitre());
  78.             }
  79.             if ($user instanceof UserInterface) {
  80.                 $contact->setNom($user->getNom());
  81.                 $contact->setPrenom($user->getPrenom());
  82.                 $contact->setEmail($user->getEmail());
  83.                 if (!is_null($user->getCivilite())) {
  84.                     $contact->setCivilite($user->getCivilite());
  85.                 }
  86.                 if (!is_null($user->getTelephone())) {
  87.                     $contact->setTelephone($user->getTelephone());
  88.                 }
  89.                 if (!is_null($user->getAdresse1())) {
  90.                     $contact->setAdresse($user->getAdresse1());
  91.                 }
  92.                 if (!is_null($user->getCodePostal())) {
  93.                     $contact->setCodePostal($user->getCodePostal());
  94.                 }
  95.                 if (!is_null($user->getVille())) {
  96.                     $contact->setVille($user->getVille());
  97.                 }
  98.             }
  99.             $form->setData($contact);
  100.         }
  101.         if ($form->isSubmitted() && $form->isValid()) {
  102.             if (
  103.                 $contact->getFormulaire() == Contact::FORM_CONTACT ||
  104.                 $contact->getFormulaire() == Contact::FORM_PRODUIT ||
  105.                 $contact->getFormulaire() == Contact::FORM_CSE ||
  106.                 $contact->getFormulaire() == Contact::FORM_MESSAGERIE
  107.             ) {
  108.                 $maxFichier 2;
  109.                 if ($contact->getFormulaire() == Contact::FORM_MESSAGERIE) {
  110.                     $maxFichier 1;
  111.                 }
  112.                 for ($i=1$i<=$maxFichier$i++) {
  113.                     if ($form->has('fichier'.$i)) {
  114.                         $fichier $form->get('fichier'.$i)->getData();
  115.                         if (!empty($fichier)) {
  116.                             try {
  117.                                 $originalFilename pathinfo($fichier->getClientOriginalName(), PATHINFO_FILENAME);
  118.                                 // this is needed to safely include the file name as part of the URL
  119.                                 $safeFilename   transliterator_transliterate('Any-Latin; Latin-ASCII; [^A-Za-z0-9_] remove; Lower()'$originalFilename);
  120.                                 $newFilename    $safeFilename.'-'.uniqid().'.'.$fichier->guessExtension();
  121.                                 $media = new Media();
  122.                                 $media->setBinaryContent($fichier);
  123.                                 $media->setContext('contact');
  124.                                 $media->setProviderName('sonata.media.provider.file');
  125.                                 $media->setEnabled(true);
  126.                                 $media->setName($newFilename);
  127.                                 $this->mediaManager->save($media);
  128.                                 $contact->{"setFichier$i"}($media);
  129.                             }
  130.                             catch (FileException $e) {
  131.                                 $form->get('fichier'.$i)->addError(new FormError('Une erreur est survenue lors de l\'enregistrement de la pièce jointe, veuillez réessayer'));
  132.                             }
  133.                         }
  134.                     }
  135.                 }
  136.             }
  137.             if ($contact->getFormulaire() == Contact::FORM_TOMBOLA) {
  138.                 $participations $this->em->getRepository(Contact::class)->findContacts(
  139.                     false,
  140.                     Contact::FORM_TOMBOLA,
  141.                     $contact->getEmail()
  142.                 );
  143.                 if (== count($participations)) {
  144.                     $this->em->persist($contact);
  145.                     $this->em->flush();
  146.                     $response = new RedirectResponse($request->getPathInfo().'?confirm=true');
  147.                     $event->setResponse($response);
  148.                 }
  149.                 else {
  150.                     $form->addError(new FormError('Vous avez déjà participé à la tombola !'));
  151.                 }
  152.             }
  153.             if ($contact->getFormulaire() != Contact::FORM_TOMBOLA) {
  154.                 if ($contact->getFormulaire() != Contact::FORM_MESSAGERIE) {
  155.                     $this->em->persist($contact);
  156.                     $this->em->flush();
  157.                     $this->contactEmail->alerteContact($contact);
  158.                     (new EmailSenderListener($this->container$this->logger))->onTerminate();
  159.                 }
  160.                 if ($contact->getFormulaire() == Contact::FORM_MESSAGERIE) {
  161.                     $contact->setEmail($user->getEmail());
  162.                     $contact->setCivilite($user->getCivilite());
  163.                     $contact->setNom($user->getNom());
  164.                     $contact->setPrenom($user->getPrenom());
  165.                     $conversations $this->em->getRepository(Conversation::class)
  166.                         ->findConversations(false$contact->getUser()->getId());
  167.                     if (count($conversations)) {
  168.                         $conversation $conversations[0];
  169.                     } else {
  170.                         $conversation = new Conversation();
  171.                         $conversation->setUser($contact->getUser());
  172.                     }
  173.                     $message = new Message();
  174.                     $message->setUser($contact->getUser());
  175.                     $message->setFichier($contact->getFichier1());
  176.                     $message->setObjet($contact->getObjet());
  177.                     $message->setMessage($contact->getMessage());
  178.                     $conversation->setDernierMessage(new \DateTime());
  179.                     $conversation->setNeedReply(true);
  180.                     $conversation->addMessage($message);
  181.                     $this->em->persist($conversation);
  182.                     $this->em->flush();
  183.                     $this->contactEmail->alerteMessage($contactfalse);
  184.                     (new EmailSenderListener($this->container$this->logger))->onTerminate();
  185.                 }
  186.                 $response = new RedirectResponse($request->getPathInfo().'?confirm=true');
  187.                 $event->setResponse($response);
  188.             }
  189.         }
  190.         $bloc->form $form->createView();
  191.     }
  192. }