src/Controller/ClientContactController.php line 735

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by Mediterranean Develup Solutions
  4.  * User: jorge.defreitas@develup.solutions
  5.  * Date: 04/07/2017
  6.  * Time: 10:14
  7.  */
  8. namespace App\Controller;
  9. use App\Entity\Client;
  10. use App\Entity\NewsLetterEmail;
  11. use App\Entity\ClientContact;
  12. use App\Entity\NewsLetterControl;
  13. use App\Entity\SettingsRol;
  14. use App\Entity\User;
  15. use App\Form\ClientContactType;
  16. use App\MDS\CommercialBundle\Entity\CommercialBreakdownVolume;
  17. use App\MDS\CommercialBundle\Entity\CommercialBreakdownVolumeControl;
  18. use Psr\Log\LoggerInterface;
  19. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  20. use Symfony\Component\Routing\Annotation\Route;
  21. use Symfony\Component\HttpFoundation\JsonResponse;
  22. use Symfony\Component\HttpFoundation\Request;
  23. use Symfony\Contracts\Translation\TranslatorInterface;
  24. class ClientContactController extends AbstractController
  25. {
  26.     private $translator;
  27.     public function __construct(TranslatorInterface $translator) {
  28.         $this->translator $translator;
  29.     }
  30.     
  31.     /**
  32.      * @Route("/client/contact/list/{idclient}", name="contact_index")
  33.      */
  34.     public function indexAction($idclientRequest $request)
  35.     {
  36.         $em $this->getDoctrine()->getManager();
  37.         $client $em->getRepository(Client::class)->findById($idclient);
  38.         $contacts $em->getRepository(ClientContact::class)->findBy(
  39.             array('clientId' => $idclient)
  40.         );
  41.         $breakdown null;
  42.         $data_breakdown = array();
  43.         $contacts_data = [];
  44.         foreach ($contacts as $contact) {
  45.             $assigned_user $em->getRepository(User::class)->findOneById($contact->getAssignedAgent());
  46.             $created_user $em->getRepository(User::class)->findOneById($contact->getCreatedId());
  47.             $assigned_user = empty($assigned_user) ? ($em->getRepository(User::class)->findOneById(6)) : $assigned_user;
  48.             $created_user = empty($created_user) ? ($em->getRepository(User::class)->findOneById(6)) : $created_user;
  49.             $contact->setAssignedAgent($assigned_user->getName() . ' ' $assigned_user->getLastName());
  50.             $contact->setCreatedId($created_user->getName() . ' ' $created_user->getLastName());
  51.             $breakdown_Control $em->getRepository(CommercialBreakdownVolumeControl::class)->findBy(
  52.                 array(
  53.                     'clientId' => $contact->getClientId(),
  54.                     'contactId' => $contact->getId(),
  55.                 )
  56.             );
  57.             if (!empty($breakdown_Control)) {
  58.                 foreach ($breakdown_Control as $breakdownControl) {
  59.                     $breakdown $em->getRepository(CommercialBreakdownVolume::class)->findByControlId($breakdownControl->getId());
  60.                     $data_breakdown[] = array(
  61.                         'breakdownControl' => $breakdownControl,
  62.                         'breakdown' => $breakdown,
  63.                     );
  64. //                    d($data_breakdown);
  65.                 }
  66.             }
  67.             $contacts_data[] = array(
  68.                 'contacts' => $contact,
  69.                 'breakdown' => $data_breakdown,
  70.             );
  71.         }
  72.         return $this->render('client/contact/list-contact.html.twig', array(
  73.             'id' => $idclient,
  74.             'clients' => $client,
  75.             'contacts' => $contacts_data
  76.         ));
  77.     }
  78.     /**
  79.      * @Route("/client/contact/add/{idclient}", name="contact_add")
  80.      */
  81.     public function addClientContactAction($idclientRequest $request)
  82.     {
  83.         $contact = new ClientContact();
  84.         $contact->setClientId($idclient);
  85.         $form $this->createClientContactCreateForm($contact);
  86.         return $this->render('client/contact/add-contact.html.twig', array(
  87.             'id' => $idclient,
  88.             'idclient' => $idclient,
  89.             'form' => $form->createView()
  90.         ));
  91.     }
  92.     private function createClientContactCreateForm(ClientContact $entity)
  93.     {
  94.         $form $this->createForm(ClientContactType::class, $entity, array(
  95.             'action' => $this->generateUrl('contact_create'),
  96.             'method' => 'POST'
  97.         ));
  98.         return $form;
  99.     }
  100.     /**
  101.      * @Route("/client/contact/new/{idclient}", name="contact_new")
  102.      */
  103.     public function newClientContactAction($idclientRequest $request)
  104.     {
  105.         $contact = new ClientContact();
  106.         $contact->setClientId($idclient);
  107.         $form $this->NewCreateClientContactCreateForm($contact);
  108.         return $this->render('client/contact/modal/add-modal-contact.html.twig', array('idclient' => $idclient'form' => $form->createView()));
  109.     }
  110.     private function NewCreateClientContactCreateForm(ClientContact $entity)
  111.     {
  112.         $form $this->createForm(ClientContactType::class, $entity, array(
  113.             'action' => $this->generateUrl('contact_create_new'),
  114.             'method' => 'POST'
  115.         ));
  116.         return $form;
  117.     }
  118.     /**
  119.      * @Route("/client/contact/newcreate", name="contact_create_new")
  120.      */
  121.     public function newcreateContactAction(Request $requestLoggerInterface $logger)
  122.     {
  123.         $telephone_data $_POST['contact']['phone'];
  124.         $email_data $_POST['contact']['email'];
  125.         $contact = new ClientContact();
  126.         $form $this->createClientContactCreateForm($contact);
  127.         $form->handleRequest($request);
  128.         $client_agent $form->get('assignedAgent')->getData();
  129.         if (!is_null($client_agent)) {
  130.             $contact->setAssignedAgent($client_agent->getId());
  131.         }
  132.         $verifico_phone $this->getDoctrine()->getRepository(ClientContact::class)->findOneByPhone($telephone_data);
  133.         $verifico_email $this->getDoctrine()->getRepository(ClientContact::class)->findOneByEmail($email_data);
  134.         if (empty($verifico_phone) AND empty($verifico_email)) {
  135.             if ($form->isValid()) {
  136.                 /* Obtengo usuario logueado */
  137.                 $user_logueado $this->get('security.token_storage')->getToken()->getUser();
  138.                 $user_id $user_logueado->getId();
  139.                 $contact->setCreatedId($user_id);
  140.                 $contact->setUpdatedId($user_id);
  141.                 $em $this->getDoctrine()->getManager();
  142. //                $em->persist($contact);
  143. //                $em->flush();
  144.                 /* Gestión de eventos en Log */
  145.                 $user_lastname $user_logueado->getLastname();
  146.                 $user_name $user_logueado->getName();
  147.                 $user_email $user_logueado->getEmail();
  148.                 $user_rol $user_logueado->getRoles();
  149.                 $event_url $request->getPathInfo();
  150.                 $event_complete $user_name ' ' $user_lastname ' - ' $user_email ' - ' $user_rol[0] . ' | ' $event_url;
  151.                 try {
  152.                     $em->persist($contact);
  153.                     $em->flush();
  154.                     $event 'The Client Contact has been created';
  155.                     $successMessage $this->translator->trans($event);
  156.                     $this->addFlash('mensajecontact'$successMessage);
  157.                     $logger->info($event_complete ' | ' $event);
  158.                 } catch (\Exception $e) {
  159.                     $event 'An error occurred: ' $e->getMessage() . ' | transport';
  160.                     /* Para el log */
  161.                     $logger->error($event_complete ' | ' $event);
  162.                     /* Para el usuario */
  163.                     $errorMessage $this->translator->trans($event);
  164.                     $this->addFlash('mensajecontacterror'$errorMessage);
  165.                 }
  166.                 /* Fin Gestión de eventos en Log */
  167.                 return $this->redirectToRoute('client_edit',
  168.                     array(
  169.                         'id' => $contact->getClientId()
  170.                     ));
  171.             } else {
  172.                 $errorMessage $this->translator->trans('Error, some fields are empty');
  173.                 $this->addFlash('mensajecontacterror'$errorMessage);
  174.             }
  175.         } else {
  176.             $errorMessage $this->translator->trans('The Contact of the Client already exists');
  177.             $this->addFlash('mensajecontacterror'$errorMessage);
  178.         }
  179.         return $this->redirectToRoute('contact_add',
  180.             array(
  181.                 'idclient' => $contact->getClientId()
  182.             )
  183.         );
  184.     }
  185.     /**
  186.      * @Route("/client/contact/new2/{idclient}", name="contact_new2")
  187.      */
  188.     public function new2ClientContactAction($idclientRequest $request)
  189.     {
  190.         $contact = new ClientContact();
  191.         $contact->setClientId($idclient);
  192.         $form $this->New2CreateClientContactCreateForm($contact);
  193.         return $this->render('client/contact/modal/add-modal-contact.html.twig', array('idclient' => $idclient'form' => $form->createView()));
  194.     }
  195.     private function New2CreateClientContactCreateForm(ClientContact $entity)
  196.     {
  197.         $form $this->createForm(ClientContactType::class, $entity, array(
  198.             'action' => $this->generateUrl('contact_create_new2'),
  199.             'method' => 'POST'
  200.         ));
  201.         return $form;
  202.     }
  203.     /**
  204.      * @Route("/client/contact/new2create", name="contact_create_new2")
  205.      */
  206.     public function new2createContactAction(Request $requestLoggerInterface $logger)
  207.     {
  208.         $telephone_data $_POST['contact']['phone'];
  209.         $email_data $_POST['contact']['email'];
  210.         $contact = new ClientContact();
  211.         $form $this->createClientContactCreateForm($contact);
  212.         $form->handleRequest($request);
  213.         $client_agent $form->get('assignedAgent')->getData();
  214.         if (!is_null($client_agent)) {
  215.             $contact->setAssignedAgent($client_agent->getId());
  216.         }
  217.         $verifico_phone $this->getDoctrine()->getRepository(ClientContact::class)->findOneByPhone($telephone_data);
  218.         $verifico_email $this->getDoctrine()->getRepository(ClientContact::class)->findOneByEmail($email_data);
  219.         if (empty($verifico_phone) AND empty($verifico_email)) {
  220.             if ($form->isValid()) {
  221.                 /* Obtengo usuario logueado */
  222.                 $user_logueado $this->get('security.token_storage')->getToken()->getUser();
  223.                 $user_id $user_logueado->getId();
  224.                 $contact->setCreatedId($user_id);
  225.                 $contact->setUpdatedId($user_id);
  226.                 $em $this->getDoctrine()->getManager();
  227. //                $em->persist($contact);
  228. //                $em->flush();
  229.                 /* Gestión de eventos en Log */
  230.                 $user_lastname $user_logueado->getLastname();
  231.                 $user_name $user_logueado->getName();
  232.                 $user_email $user_logueado->getEmail();
  233.                 $user_rol $user_logueado->getRoles();
  234.                 $event_url $request->getPathInfo();
  235.                 $event_complete $user_name ' ' $user_lastname ' - ' $user_email ' - ' $user_rol[0] . ' | ' $event_url;
  236.                 try {
  237.                     $em->persist($contact);
  238.                     $em->flush();
  239.                     $event 'The Client Contact has been created';
  240.                     $successMessage $this->translator->trans($event);
  241.                     $this->addFlash('mensajecontact'$successMessage);
  242.                     $logger->info($event_complete ' | ' $event);
  243.                 } catch (\Exception $e) {
  244.                     $event 'An error occurred: ' $e->getMessage() . ' | transport';
  245.                     /* Para el log */
  246.                     $logger->error($event_complete ' | ' $event);
  247.                     /* Para el usuario */
  248.                     $errorMessage $this->translator->trans($event);
  249.                     $this->addFlash('mensajecontacterror'$errorMessage);
  250.                 }
  251.                 /* Fin Gestión de eventos en Log */
  252.                 return $this->redirectToRoute('contact_index',
  253.                     array(
  254.                         'idclient' => $contact->getClientId()
  255.                     ));
  256.             } else {
  257.                 $errorMessage $this->translator->trans('Error, some fields are empty');
  258.                 $this->addFlash('mensajecontacterror'$errorMessage);
  259.             }
  260.         } else {
  261.             $errorMessage $this->translator->trans('The Contact of the Client already exists');
  262.             $this->addFlash('mensajecontacterror'$errorMessage);
  263.         }
  264.         return $this->redirectToRoute('contact_index',
  265.             array(
  266.                 'idclient' => $contact->getClientId()
  267.             )
  268.         );
  269.     }
  270.     /**
  271.      * @Route("/client/contact/create", name="contact_create")
  272.      */
  273.     public function createContactAction(Request $requestLoggerInterface $logger)
  274.     {
  275.         $telephone_data $request->request->get('contact')['phone'];
  276.         $email_data $request->request->get('contact')['email'];
  277.         $contact = new ClientContact();
  278.         $form $this->createClientContactCreateForm($contact);
  279.         $form->handleRequest($request);
  280.         $client_agent $form->get('assignedAgent')->getData();
  281.         if (!is_null($client_agent)) {
  282.             $contact->setAssignedAgent($client_agent->getId());
  283.         }
  284.         $verifico_phone $this->getDoctrine()->getRepository(ClientContact::class)->findOneByPhone($telephone_data);
  285. //        $verifico_email = $this->getDoctrine()->getRepository(ClientContact::class)->findOneByEmail($email_data);
  286. //        if (empty($verifico_phone) AND empty($verifico_email)) {
  287.         if (empty($verifico_phone)) {
  288.             if ($form->isValid()) {
  289.                 $email $request->request->get('contact')['email'];
  290.                 if (isset($request->request->get('suppliercontact')['newsletter'])) {
  291.                     $newsletters $request->request->get('suppliercontact')['newsletter'];
  292.                     $contact->setNewsletter(implode(","$newsletters));
  293.                 } else {
  294.                     $contact->setNewsletter(null);
  295.                 }
  296.                 /* Obtengo usuario logueado */
  297.                 $user_logueado $this->get('security.token_storage')->getToken()->getUser();
  298.                 $user_id $user_logueado->getId();
  299.                 $contact->setCreatedId($user_id);
  300.                 $contact->setUpdatedId($user_id);
  301.                 $em $this->getDoctrine()->getManager();
  302.                 /* Gestión de eventos en Log */
  303.                 $user_lastname $user_logueado->getLastname();
  304.                 $user_name $user_logueado->getName();
  305.                 $user_email $user_logueado->getEmail();
  306.                 $user_rol $user_logueado->getRoles();
  307.                 $event_url $request->getPathInfo();
  308.                 $event_complete $user_name ' ' $user_lastname ' - ' $user_email ' - ' $user_rol[0] . ' | ' $event_url;
  309.                 try {
  310.                     $em->persist($contact);
  311.                     $em->flush();
  312.                     $event 'The Client Contact has been created';
  313.                     $successMessage $this->translator->trans($event);
  314.                     $this->addFlash('mensajecontact'$successMessage);
  315.                     $logger->info($event_complete ' | ' $event);
  316.                 } catch (\Exception $e) {
  317.                     $event 'An error occurred: ' $e->getMessage() . ' | transport';
  318.                     /* Para el log */
  319.                     $logger->error($event_complete ' | ' $event);
  320.                     /* Para el usuario */
  321.                     $errorMessage $this->translator->trans($event);
  322.                     $this->addFlash('mensajecontacterror'$errorMessage);
  323.                 }
  324.                 /* Fin Gestión de eventos en Log */
  325.                 if (isset($request->request->get('suppliercontact')['newsletter'])) {
  326.                     $newsletters $request->request->get('suppliercontact')['newsletter'];
  327.                     foreach ($newsletters as $newsletter) {
  328.                         $correos = array(
  329.                             'newsletterId' => $newsletter,
  330.                             'email' => $email,
  331.                         );
  332.                         $revisar_correo $em->getRepository(NewsLetterEmail::class)->findOneBy($correos);
  333.                         if (empty($revisar_correo)) {
  334.                             $newsletteremail = new NewsLetterEmail();
  335.                             $newsletteremail->setNewsletterId($newsletter);
  336.                             $newsletteremail->setEmail($email);
  337.                             $newsletteremail->setClientId($contact->getClientId());
  338.                             $newsletteremail->setType('Client');
  339.                             $newsletteremail->setCreatedId($user_id);
  340.                             $newsletteremail->setUpdatedId($user_id);
  341.                             $em->persist($newsletteremail);
  342.                             $em->flush();
  343.                         } else {
  344.                             $revisar_correo->setClientId($contact->getClientId());
  345.                             $revisar_correo->setUpdatedId($user_id);
  346.                             $revisar_correo->setType('Client');
  347.                             $em->persist($revisar_correo);
  348.                             $em->flush();
  349.                         }
  350.                     }
  351.                 }
  352.                 return $this->redirectToRoute('contact_index',
  353.                     array(
  354.                         'id' => $contact->getClientId(),
  355.                         'idclient' => $contact->getClientId()
  356.                     ));
  357.             } else {
  358.                 $errorMessage $this->translator->trans('Error, some fields are empty');
  359.                 $this->addFlash('mensajecontacterror'$errorMessage);
  360.             }
  361.         } else {
  362.             $errorMessage $this->translator->trans('The Contact of the Client already exists');
  363.             $this->addFlash('mensajecontacterror'$errorMessage);
  364.         }
  365.         return $this->render('client/contact/add-contact.html.twig', array('id' => $contact->getClientId(), 'idclient' => $contact->getClientId(), 'form' => $form->createView()));
  366.     }
  367.     /**
  368.      * @Route("/client/contact/edited/{id}", name="contact_edit_new")
  369.      */
  370.     public function NeweditContact($id)
  371.     {
  372.         $em $this->getDoctrine()->getManager();
  373.         $contact $em->getRepository(ClientContact::class)->findOneById($id);
  374.         $user $em->getRepository(User::class)->findOneById($contact->getAssignedAgent());
  375.         $contact->setAssignedAgent($user);
  376.         $form $this->NewCreateEditContactForm($contact$id);
  377.         return $this->render('client/contact/modal/edit-modal-contact.html.twig',
  378.             array(
  379.                 'contact' => $contact,
  380.                 'form' => $form->createView()
  381.             )
  382.         );
  383.     }
  384.     private function NewCreateEditContactForm(ClientContact $entity$id)
  385.     {
  386.         $form $this->createForm(ClientContactType::class, $entity,
  387.             array(
  388.                 'action' => $this->generateUrl('contact_update_new',
  389.                     array(
  390.                         'id' => $id
  391.                     )
  392.                 ), 'method' => 'PUT'));
  393.         return $form;
  394.     }
  395.     /**
  396.      * @Route("/client/contact/updated/{id}", name="contact_update_new", methods={"POST"})
  397.      */
  398.     public function NewUpdateAction($idRequest $requestLoggerInterface $logger)
  399.     {
  400.         $em $this->getDoctrine()->getManager();
  401.         $contact $em->getRepository(ClientContact::class)->findOneById($id);
  402.         $form $this->NewCreateEditContactForm($contact$id);
  403.         $form->handleRequest($request);
  404.         $assignedAgent $form->get('assignedAgent')->getData();
  405.         if (!is_null($assignedAgent)) {
  406.             $contact->setAssignedAgent($assignedAgent->getId());
  407.         }
  408.         if ($form->isSubmitted() && $form->isValid()) {
  409.             /* Obtengo usuario logueado */
  410.             $user_logueado $this->get('security.token_storage')->getToken()->getUser();
  411.             $user_id $user_logueado->getId();
  412.             $contact->getUpdatedId($user_id);
  413.             $em $this->getDoctrine()->getManager();
  414. //            $em->persist($contact);
  415. //            $em->flush();
  416.             /* Gestión de eventos en Log */
  417.             $user_lastname $user_logueado->getLastname();
  418.             $user_name $user_logueado->getName();
  419.             $user_email $user_logueado->getEmail();
  420.             $user_rol $user_logueado->getRoles();
  421.             $event_url $request->getPathInfo();
  422.             $event_complete $user_name ' ' $user_lastname ' - ' $user_email ' - ' $user_rol[0] . ' | ' $event_url;
  423.             try {
  424.                 $em->persist($contact);
  425.                 $em->flush();
  426.                 $event 'The Client Contact has been modified.';
  427.                 $successMessage $this->translator->trans($event);
  428.                 $this->addFlash('mensajecontact'$successMessage);
  429.                 $logger->info($event_complete ' | ' $event);
  430.             } catch (\Exception $e) {
  431.                 $event 'An error occurred: ' $e->getMessage() . ' | transport';
  432.                 /* Para el log */
  433.                 $logger->error($event_complete ' | ' $event);
  434.                 /* Para el usuario */
  435.                 $errorMessage $this->translator->trans($event);
  436.                 $this->addFlash('mensajecontacterror'$errorMessage);
  437.             }
  438.             /* Fin Gestión de eventos en Log */
  439.             return $this->redirectToRoute('contact_index',
  440.                 array(
  441.                     'idclient' => $contact->getClientId()
  442.                 )
  443.             );
  444.         }
  445.         return $this->render('client/contact/list-contact.html.twig',
  446.             array(
  447.                 'idclient' => $contact->getClientId()
  448.             )
  449.         );
  450.     }
  451.     /**
  452.      * @Route("/client/contact/edit/{id}", name="contact_edit")
  453.      */
  454.     public function editContact($id)
  455.     {
  456.         $em $this->getDoctrine()->getManager();
  457.         $contact $em->getRepository(ClientContact::class)->findOneById($id);
  458.         $user $em->getRepository(User::class)->findOneById($contact->getAssignedAgent());
  459.         $contact->setAssignedAgent($user);
  460.         $newsletters explode(","$contact->getNewsletter());
  461.         $newsletterscontrol $em->getRepository(NewsLetterControl::class)->findById($newsletters);
  462.         $contact->setNewsletter($newsletterscontrol);
  463.         $form $this->createEditContactForm($contact$id);
  464.         return $this->render('client/contact/edit-contact.html.twig',
  465.             array(
  466.                 'id' => $id,
  467.                 'contact' => $contact,
  468.                 'form' => $form->createView()
  469.             )
  470.         );
  471.     }
  472.     private function createEditContactForm(ClientContact $entity$id)
  473.     {
  474.         $form $this->createForm(ClientContactType::class, $entity,
  475.             array(
  476.                 'action' => $this->generateUrl('contact_update',
  477.                     array(
  478.                         'id' => $id
  479.                     )
  480.                 ), 'method' => 'PUT'));
  481.         return $form;
  482.     }
  483.     /**
  484.      * @Route("/client/contact/update/{id}", name="contact_update", methods={"POST", "PUT"})
  485.      */
  486.     public function updateAction($idRequest $requestLoggerInterface $logger)
  487.     {
  488.         $em $this->getDoctrine()->getManager();
  489.         $contact $em->getRepository(ClientContact::class)->findOneById($id);
  490.         $contact->setNewsletter(null);
  491.         $assignedAgent $contact->getAssignedAgent();
  492.         $form $this->createEditContactForm($contact$id);
  493.         $form->handleRequest($request);
  494.         /* Obtengo usuario logueado */
  495.         $user_logueado $this->get('security.token_storage')->getToken()->getUser();
  496.         $user_role $user_logueado->getRole();
  497.         $user_userrol $user_logueado->getUserrol();
  498.         /* VERIFICAR EL weight DEL USUARIO */
  499.         $accesoNivel $em->getRepository(SettingsRol::class)->findOneById($user_userrol);
  500.         $nivelAcceso $accesoNivel->getWeight();
  501.         if ($user_role == "ROLE_ADMIN" || $nivelAcceso <= '2') {
  502.             $assignedAgent $form->get('assignedAgent')->getData();
  503.             if (!is_null($assignedAgent)) {
  504.                 $contact->setAssignedAgent($assignedAgent->getId());
  505.             }
  506.         } else {
  507.             $contact->setAssignedAgent($assignedAgent);
  508.         }
  509.         if ($form->isSubmitted() && $form->isValid()) {
  510.             $user_id $user_logueado->getId();
  511. //            d($user_id);
  512. //            exit();
  513.             $contact->setUpdatedId($user_id);
  514.             if (isset($request->request->get('contact')['newsletter'])) {
  515.                 $newsletters $request->request->get('contact')['newsletter'];
  516.                 $contact->setNewsletter(implode(","$newsletters));
  517.             } else {
  518.                 $contact->setNewsletter(null);
  519.             }
  520.             $email $request->request->get('contact')['email'];
  521.             if (isset($request->request->get('contact')['newsletter'])) {
  522.                 $newsletters $request->request->get('contact')['newsletter'];
  523.                 foreach ($newsletters as $newsletter) {
  524.                     $correos = array(
  525.                         'newsletterId' => $newsletter,
  526.                         'email' => $email
  527.                     );
  528.                     $revisar_correo $em->getRepository(NewsLetterEmail::class)->findOneBy($correos);
  529.                     if (empty($revisar_correo)) {
  530.                         $newsletteremail = new NewsLetterEmail();
  531.                         $newsletteremail->setNewsletterId($newsletter);
  532.                         $newsletteremail->setEmail($email);
  533.                         $newsletteremail->setClientId($contact->getClientId());
  534.                         $newsletteremail->setType('Client');
  535.                         $newsletteremail->setCreatedId($user_id);
  536.                         $newsletteremail->setUpdatedId($user_id);
  537.                         $em->persist($newsletteremail);
  538.                         $em->flush();
  539.                     } else {
  540.                         $revisar_correo->setClientId($contact->getClientId());
  541.                         $revisar_correo->setUpdatedId($user_id);
  542.                         $revisar_correo->setType('Client');
  543.                         $em->persist($revisar_correo);
  544.                         $em->flush();
  545.                     }
  546.                 }
  547.             }
  548.             /* Gestión de eventos en Log */
  549.             $user_lastname $user_logueado->getLastname();
  550.             $user_name $user_logueado->getName();
  551.             $user_email $user_logueado->getEmail();
  552.             $user_rol $user_logueado->getRoles();
  553.             $event_url $request->getPathInfo();
  554.             $event_complete $user_name ' ' $user_lastname ' - ' $user_email ' - ' $user_rol[0] . ' | ' $event_url;
  555.             try {
  556.                 $em->persist($contact);
  557.                 $em->flush();
  558.                 $event 'The Client Contact has been modified.';
  559.                 $successMessage $this->translator->trans($event);
  560.                 $this->addFlash('mensajecontact'$successMessage);
  561.                 $logger->info($event_complete ' | ' $event);
  562.             } catch (\Exception $e) {
  563.                 $event 'An error occurred: ' $e->getMessage();
  564.                 /* Para el log */
  565.                 $logger->error($event_complete ' | ' $event);
  566.                 /* Para el usuario */
  567.                 $errorMessage $this->translator->trans($event);
  568.                 $this->addFlash('mensajecontacterror'$errorMessage);
  569.             }
  570.             /* Fin Gestión de eventos en Log */
  571.             return $this->redirectToRoute('contact_index',
  572.                 array(
  573.                     'idclient' => $contact->getClientId()
  574.                 )
  575.             );
  576.         }
  577.         return $this->render('client/contact/edit-contact.html.twig',
  578.             array(
  579.                 'id' => $contact->getClientId(),
  580.                 'idclient' => $contact->getClientId(),
  581.                 'contact' => $contact,
  582.                 'form' => $form->createView()
  583.             )
  584.         );
  585.     }
  586.     /**
  587.      * @Route("/client/contact/listdesplegable", name="get_contact_select")
  588.      */
  589.     public function contactSelectAction(Request $request)
  590.     {
  591.         $idclient $_POST['client'];
  592.         $em $this->getDoctrine()->getManager();
  593.         $contacts $em->getRepository(ClientContact::class)->findByClientId($idclient);
  594.         if (!empty($contacts)) {
  595.             $datos = array();
  596.             foreach ($contacts as $contact) {
  597.                 $datos[] = array(
  598.                     "idcliente" => $contact->getClientId(),
  599.                     "id" => $contact->getId(),
  600.                     "name" => $contact->getName(),
  601.                     "lastname" => $contact->getLastname(),
  602.                     "department" => $contact->getDepartment(),
  603.                 );
  604.             }
  605.         } else {
  606.             $datos[] = array(
  607.                 "idcliente" => '',
  608.                 "id" => '',
  609.                 "name" => $this->translator->trans('Nonexistent contact'),
  610.                 "lastname" => '',
  611.                 "department" => $this->translator->trans('Please add contact'),
  612.             );
  613.         }
  614.         $return = array(
  615.             'contacts' => $datos,
  616.         );
  617.         $response = new JsonResponse($return);
  618.         return $response;
  619.     }
  620.     /**
  621.      * @Route("/client/contact/add-image", name="client_contact_image")
  622.      */
  623.     public function addClientContactImage()
  624.     {
  625.         $dataa $_POST['image'];
  626.         list($type$data) = explode(';'$dataa);
  627.         list(, $data) = explode(','$data);
  628.         $data base64_decode($data);
  629.         $imageName time() . '.png';
  630.         $ruta "assets/images/clients/contact/";
  631.         if (!file_exists($ruta)) {
  632.             mkdir($ruta0777true);
  633.         }
  634.         $file fopen($ruta $imageName"wb");
  635.         fwrite($file$data);
  636.         fclose($file);
  637.         $return = array(
  638.             'base64' => $dataa,
  639.             'nombre_archivo' => $ruta $imageName,
  640.         );
  641.         $response = new JsonResponse($return);
  642.         return $response;
  643.     }
  644. }