src/Controller/Kernel/MailingController.php line 81

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Kernel;
  3. use App\Entity\Kernel\Mailing;
  4. use App\Entity\Kernel\MailingItem;
  5. use App\Entity\Kernel\ListItem;
  6. use App\Form\Kernel\MailingType;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  12. use App\Service\Kernel\KernelService;
  13. /**
  14.  * @Route("/{_locale}/mailing")
  15.  */
  16. class MailingController extends AbstractController
  17. {
  18.     /**
  19.      * @Route("/", name="mailing_index", methods={"GET"})
  20.      */
  21.     public function index(): Response
  22.     {
  23.         $classname =  explode("\\", get_class($this));
  24.         $arraysize = count($classname);
  25.         $attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
  26.         $this->denyAccessUnlessGranted($attribute);
  27.     
  28.         $mailings = $this->getDoctrine()
  29.             ->getRepository(Mailing::class)
  30.             ->findBy(array('state' => 0));
  31.         return $this->render('kernel/mailing/index.html.twig', [
  32.             'mailings' => $mailings,
  33.         ]);
  34.     }
  35.     
  36.     /**
  37.      * @Route("/showarchived", name="mailing_showarchived", methods={"GET"})
  38.      */
  39.     public function showarchived(): Response
  40.     {
  41.         $classname =  explode("\\", get_class($this));
  42.         $arraysize = count($classname);
  43.         $attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
  44.         $this->denyAccessUnlessGranted($attribute);
  45.     
  46.         $mailings = $this->getDoctrine()
  47.             ->getRepository(Mailing::class)
  48.             ->findBy(array('state' => 1));
  49.         return $this->render('kernel/mailing/archived.html.twig', [
  50.             'mailings' => $mailings,
  51.         ]);
  52.     }
  53.     
  54.     /**
  55.      * @Route("/showdeleted", name="mailing_showdeleted", methods={"GET"})
  56.      */
  57.     public function showdeleted(): Response
  58.     {
  59.         $classname =  explode("\\", get_class($this));
  60.         $arraysize = count($classname);
  61.         $attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
  62.         $this->denyAccessUnlessGranted($attribute);
  63.     
  64.         $mailings = $this->getDoctrine()
  65.             ->getRepository(Mailing::class)
  66.             ->findBy(array('state' => 2));
  67.         return $this->render('kernel/mailing/deleted.html.twig', [
  68.             'mailings' => $mailings,
  69.         ]);
  70.     }
  71.     /**
  72.      * @Route("/new/{type?0}/{step?0}/{entityid?0}", name="mailing_new", methods={"GET","POST"})
  73.      */
  74.     public function new(Request $request, int $type = 0, int $step = 0, int $entityid = 0, KernelService $kernel_service): Response
  75.     {
  76.         $classname =  explode("\\", get_class($this));
  77.         $arraysize = count($classname);
  78.         $attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
  79.         $this->denyAccessUnlessGranted($attribute);
  80.         
  81.         $em = $this->getDoctrine()->getManager();
  82.     
  83.         $entity = null;
  84.         if ($type == 2)
  85.         {
  86.             $entity = $this->getDoctrine()->getManager()->getRepository(\App\Entity\Finance\Lead::class)->findOneById($entityid);
  87.         }
  88.         
  89.         if ($step == 0)
  90.         {
  91.             if (($type == 1) or ($type == 2))
  92.             {
  93.                 $form = $this->createForm(\App\Form\HR\EmployeeSearchType::class, null, 
  94.                     ['action' => $this->generateUrl('mailing_new', 
  95.                             array('type' => $type, 'step' => 1, 'entityid' => $entityid)), 
  96.                         'attr' => ['id' => $this->generateUrl('mailing_new', 
  97.                             array('type' => $type, 'step' => 1, 'entityid' => $entityid))]]);
  98.             }
  99.             
  100.             return $this->render('kernel/mailing/search.html.twig', [
  101.                     'form' => $form->createView(),
  102.                     'type' => $type,
  103.                     'entityid' => $entityid,
  104.                     'entity' => $entity
  105.                 ]);
  106.         }
  107.         
  108.                 
  109.         $items = array();
  110.         if ($step == 1)
  111.         {
  112.             if (($type == 1) or ($type == 2))
  113.             {
  114.                 $requestdata = $request->request->all("employee_search");
  115.                 $qb = $this->getDoctrine()->getManager()->getRepository('App\Entity\HR\Employee');
  116.                 $qb = $qb->createQueryBuilder('t');
  117.                 if (isset($requestdata['otherFunction'])) {
  118.                     $qb->leftJoin('t.otherFunction', 'b');
  119.                 }
  120.                 if (isset($requestdata['competence'])) {
  121.                     $qb->leftJoin('t.competence', 'c');
  122.                 }
  123.                 $qb->andWhere("t.sector = :sector OR :sector = ''");
  124.                 $qb->setParameter("sector", $requestdata['sector']);
  125.                 $qb->andWhere("t.rate <= :rate OR :rate = ''");
  126.                 $qb->setParameter("rate", $requestdata['rate']);
  127.                 $qb->andWhere("t.firstname = :firstname OR :firstname = ''");
  128.                 $qb->setParameter("firstname", $requestdata['firstname']);
  129.                 $qb->andWhere("t.lastname = :lastname OR :lastname = ''");
  130.                 $qb->setParameter("lastname", $requestdata['lastname']);
  131.                 $counter = 1;
  132.                 $addwhere = "";
  133.                 if (isset($requestdata['function'])) {
  134.                     foreach ($requestdata['function'] as $function) {
  135.                         if ($addwhere != "") {
  136.                             $addwhere = "(" . $addwhere . ") OR (t.function = :function" . $counter . ")";
  137.                         } else {
  138.                             $addwhere = $addwhere . "t.function = :function" . $counter;
  139.                         }
  140.                         $qb->setParameter("function" . $counter, $function);
  141.                         $counter++;
  142.                     }
  143.                     if ($addwhere != "") {
  144.                         $qb->andWhere($addwhere);
  145.                     }
  146.                 }
  147.                 $counter = 1;
  148.                 $addwhere = "";
  149.                 if (isset($requestdata['otherFunction'])) {
  150.                     foreach ($requestdata['otherFunction'] as $otherFunction) {
  151.                         if ($addwhere != "") {
  152.                             $addwhere = "(" . $addwhere . ") OR (b.id = :otherFunction" . $counter . ")";
  153.                         } else {
  154.                             $addwhere = $addwhere . "b.id = :otherFunction" . $counter;
  155.                         }
  156.                         $qb->setParameter("otherFunction" . $counter, $otherFunction);
  157.                         $counter++;
  158.                     }
  159.                     if ($addwhere != "") {
  160.                         $qb->andWhere($addwhere);
  161.                     }
  162.                 }
  163.                 /*$counter = 1;
  164.                 $addwhere = "";
  165.                 if (isset($requestdata['competence'])) {
  166.                     foreach ($requestdata['competence'] as $competence) {
  167.                         if ($addwhere != "") {
  168.                             $addwhere = "(" . $addwhere . ") OR (c.id = :competence" . $counter . ")";
  169.                         } else {
  170.                             $addwhere = $addwhere . "c.id = :competence" . $counter;
  171.                         }
  172.                         $qb->setParameter("competence" . $counter, $competence);
  173.                         $counter++;
  174.                     }
  175.                     if ($addwhere != "") {
  176.                         $qb->andWhere($addwhere);
  177.                     }
  178.                 }*/
  179.                 
  180.                 $counter = 1;
  181.             $addwhere = "";
  182.             if (isset($requestdata['region'])) {
  183.                 foreach ($requestdata['region'] as $region) {
  184.                     
  185.                     $regionent = $em->getRepository(ListItem::Class)->find($region);
  186.                     $municipalities = $em->getRepository(ListItem::Class)->findByList1($regionent->getId());
  187.                     foreach ($municipalities as $municipality) {
  188.                         if ($addwhere != "") {
  189.                             $addwhere = "(" . $addwhere . ") OR (t.postcode = :postcode" . $counter . ")";
  190.                         } else {
  191.                             $addwhere = $addwhere . "t.postcode = :postcode" . $counter;
  192.                         }
  193.                         $qb->setParameter("postcode" . $counter, $municipality->getString1());
  194.                         $counter++;
  195.                     }
  196.                 }
  197.                 if ($addwhere != "") {
  198.                     $qb->andWhere($addwhere);
  199.                 }
  200.             }
  201.                 $counter = 1;
  202.                 $addwhere = "";
  203.                 $qb->andWhere("t.availableOn <= :availableOn OR :availableOn = ''");
  204.                 $qb->setParameter("availableOn", implode('-', array_reverse(explode('-', $requestdata['availableOn']))));
  205.                 $qb->andWhere("t.certifications LIKE :certifications1 OR :certifications2 = ''");
  206.                 $qb->setParameter("certifications1", '%' . $requestdata['certifications'] . '%');
  207.                 $qb->setParameter("certifications2", $requestdata['certifications']);
  208.                 $qb->andWhere("t.language = :language OR :language = ''");
  209.                 $qb->setParameter("language", $requestdata['language']);
  210.                 $qb->andWhere("t.yearsExperience >= :yearsExperience OR :yearsExperience = ''");
  211.                 $qb->setParameter("yearsExperience", $requestdata['yearsExperience']);
  212.                 $items2 = $qb->getQuery()->getResult();
  213.             $items = array();
  214.              if (isset($requestdata['competence'])) {
  215.                  foreach ($items2 as $entity)
  216.                 {
  217.                      $addperson = true;
  218.                      $competences = $entity->getCompetence();
  219.                      foreach ($requestdata['competence'] as $competence) {
  220.                          $found = false;
  221.                          foreach($competences as $competence2)
  222.                          {
  223.                              if ($competence == $competence2->getId())
  224.                              {
  225.                                  $found = true;
  226.                              }
  227.                          }
  228.                          if ($found == false)
  229.                          {
  230.                              $addperson = false;
  231.                          }
  232.                     }
  233.                     if ($addperson == true)
  234.                     {
  235.                         $items[] = $entity;
  236.                     }
  237.                  }
  238.              } else {
  239.                  $items = $items2;
  240.              }
  241.                 
  242.                 $entities = array();
  243.             
  244.                 //$before = implode('-', array_reverse(explode('-', $requestdata['latestcontact'])));
  245.                 $before = $requestdata['latestcontact'];
  246.                 if (empty($before) == true)
  247.                 {
  248.                     $entities = $items;
  249.                 }
  250.                 else
  251.                 {
  252.                     //before2 = \DateTime(Y-m-d) TODO
  253.                     foreach ($items as $entity)
  254.                     {
  255.                         $qb = $this->getDoctrine()->getManager()->getRepository('App\Entity\Kernel\\Contact');
  256.                         $qb = $qb->createQueryBuilder('t');
  257.                         $qb->leftJoin('t.employees', 'b');
  258.                         $qb->andWhere("b.id = :employee");
  259.                         $qb->andWhere("t.created > :before");
  260.                         $qb->setParameter("employee", $entity->getId());
  261.                         $qb->setParameter("before", $before );
  262.                         $counter++;
  263.                         $entities3 = $qb->getQuery()->getResult();
  264.                         if ($entities3 == null)
  265.                         {
  266.                             $entities[] = $entity;
  267.                         }
  268.                     }
  269.                 }
  270.                 
  271.                 $items = $entities;
  272.             }
  273.             
  274.             $fromarray = array();
  275.             $from = $kernel_service->getApplicationInfoValue("Kernel.Fromemail", $this->getUser()->getOrganization());
  276.             $fromarray[$from] = $from;
  277.             if (!filter_var($this->getUser()->getEmail(), FILTER_VALIDATE_EMAIL) === false) {
  278.                 $fromarray[$this->getUser()->getFirstname() . " " . $this->getUser()->getLastname() . " <" . $this->getUser()->getEmail() . ">"] = $this->getUser()->getEmail();
  279.             }
  280.             
  281.             $mailing = new Mailing();
  282.             $form = $this->createForm(MailingType::class, $mailing, 
  283.                 ['from' => $fromarray, 'useremail' => $this->getUser()->getEmail(), 'action' => $this->generateUrl('mailing_new', 
  284.                             array('type' => $type, 'step' => 2, 'entityid' => $entityid)), 
  285.                         'attr' => ['id' => $this->generateUrl('mailing_new', 
  286.                             array('type' => $type, 'step' => 2, 'entityid' => $entityid))]]);
  287.             
  288.             $formitems = array();
  289.             foreach($items as $item) {
  290.                 if (($type == 1) or ($type == 2))
  291.                 {
  292.                     $today = new \DateTime();
  293.                     if (($item->getBlacklist() == false) and ($item->getExcludefrommailings() == false)) {
  294.                         if ((is_null($item->getNoEmailUntil())) or ($item->getNoEmailUntil() <= $today)) {
  295.                             if (is_null($item->getEmail()) != true) {
  296.                                 if (filter_var($item->getEmail(), FILTER_VALIDATE_EMAIL)) {
  297.                                     $form->add('consultant' . $item->getId(), CheckboxType::class, array('mapped' => false, 'label' => $item, 'required' => false));
  298.                                 } else {
  299.                                     $form->add('consultant' . $item->getId(), CheckboxType::class, array('mapped' => false, 'label' => $item . " error mail", 'required' => false));
  300.                                 }
  301.                             } else {
  302.                                 $form->add('consultant' . $item->getId(), CheckboxType::class, array('mapped' => false, 'label' => $item . " error mail", 'required' => false));
  303.                             }
  304.                             $formitems[] = $item;
  305.                         }
  306.                     }
  307.                 }
  308.             }
  309.             
  310.             return $this->render('kernel/mailing/new.html.twig', [
  311.                 'mailing' => $mailing,
  312.                 'formitems' => $formitems,
  313.                 'form' => $form->createView(),
  314.                 'type' => $type,
  315.                 'entity' => $entity,
  316.                 'entityid' => $entityid
  317.             ]);
  318.         }
  319.         
  320.         if ($step == 2)
  321.         {
  322.         
  323.             $mailing = new Mailing();
  324.             $form = $this->createForm(MailingType::class, $mailing, 
  325.                     ['action' => $this->generateUrl('mailing_new'), 'attr' => ['id' => $this->generateUrl('mailing_new')]]);
  326.             
  327.             $requestdata = $request->request->all("mailing");
  328.             $mailing->setFromField($requestdata['fromField']);
  329.             $mailing->setBody($requestdata['body']);
  330.             $mailing->setSubject($requestdata['subject']);
  331.             $entityManager = $this->getDoctrine()->getManager();
  332.             $mailing->setState(0);
  333.             if ($type == 2) {
  334.                 $mailing->setLead($entity);
  335.             }
  336.             $entityManager->persist($mailing);
  337.             
  338.             
  339.             $found_it = array();
  340.             foreach($requestdata as $key => $value)
  341.             {
  342.                 if (($type == 1) or ($type == 2))
  343.                 {
  344.                     if (substr($key, 0, 10) == "consultant")
  345.                     {
  346.                         $consultant_id = substr($key, 10);
  347.                         $found_it[] = $consultant_id;
  348.                         $mailingitem = new MailingItem();
  349.                         $mailingitem->setEmployee($entityManager->getRepository(\App\Entity\HR\Employee::class)->find($consultant_id));
  350.                         $mailingitem->setMailing($mailing);
  351.                         $mailingitem->setState(0);
  352.                         $entityManager->persist($mailingitem);
  353.                     }
  354.                 }
  355.             }
  356.             
  357.             $entityManager->flush();
  358.             if ($type == 2) {
  359.                 return $this->redirectToRoute('lead_show', array('id' => $entity->getId()));
  360.             }
  361.             return $this->redirectToRoute('mailing_show', array('id' => $mailing->getId()));
  362.         }
  363.     }
  364.     /**
  365.      * @Route("/{id}", name="mailing_show", methods={"GET"})
  366.      */
  367.     public function show(Mailing $mailing): Response
  368.     {
  369.         $classname =  explode("\\", get_class($this));
  370.         $arraysize = count($classname);
  371.         $attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
  372.         $this->denyAccessUnlessGranted($attribute, $mailing);  
  373.         
  374.         return $this->render('kernel/mailing/show.html.twig', [
  375.             'mailing' => $mailing,
  376.         ]);
  377.     }
  378.     
  379.     /**
  380.      * @Route("/{id}/audit", name="mailing_showaudit", methods={"GET"})
  381.      */
  382.     public function showaudit(Mailing $mailing): Response
  383.     {
  384.         $classname =  explode("\\", get_class($this));
  385.         $arraysize = count($classname);
  386.         $attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
  387.         $this->denyAccessUnlessGranted($attribute, $mailing);  
  388.         
  389.         return $this->render('kernel/mailing/showaudit.html.twig', [
  390.             'mailing' => $mailing,
  391.         ]);
  392.     }
  393.     
  394.     /**
  395.      * @Route("/{id}/archive", name="mailing_archive", methods={"GET"})
  396.      */
  397.     public function archive(Mailing $mailing): Response
  398.     {
  399.         $classname =  explode("\\", get_class($this));
  400.         $arraysize = count($classname);
  401.         $attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
  402.         $this->denyAccessUnlessGranted($attribute, $mailing); 
  403.         
  404.                 
  405.         $entityManager = $this->getDoctrine()->getManager();
  406.         $mailing->setState(1);
  407.         $entityManager->persist($mailing);
  408.         $entityManager->flush();
  409.         
  410.         return $this->redirectToRoute('mailing_show', array('id' => $mailing->getId()));
  411.     }
  412.     
  413.     /**
  414.      * @Route("/{id}/restore", name="mailing_restore", methods={"GET"})
  415.      */
  416.     public function restore(Mailing $mailing): Response
  417.     {
  418.         $classname =  explode("\\", get_class($this));
  419.         $arraysize = count($classname);
  420.         $attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
  421.         $this->denyAccessUnlessGranted($attribute, $mailing); 
  422.         
  423.                 
  424.         $entityManager = $this->getDoctrine()->getManager();
  425.         $mailing->setState(0);
  426.         $entityManager->persist($mailing);
  427.         $entityManager->flush();
  428.         
  429.         return $this->redirectToRoute('mailing_show', array('id' => $mailing->getId()));
  430.     }
  431.     
  432.     /**
  433.      * @Route("/{id}/delete", name="mailing_logicaldelete", methods={"GET"})
  434.      */
  435.     public function logicaldelete(Mailing $mailing): Response
  436.     {
  437.         $classname =  explode("\\", get_class($this));
  438.         $arraysize = count($classname);
  439.         $attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
  440.         $this->denyAccessUnlessGranted($attribute, $mailing); 
  441.         
  442.                 
  443.         $entityManager = $this->getDoctrine()->getManager();
  444.         $mailing->setState(2);
  445.         $entityManager->persist($mailing);
  446.         $entityManager->flush();
  447.         
  448.         return $this->redirectToRoute('mailing_index');
  449.     }
  450.     /**
  451.      * @Route("/{id}/edit", name="mailing_edit", methods={"GET","POST"})
  452.      */
  453.     public function edit(Request $request, Mailing $mailing): Response
  454.     {
  455.         $classname =  explode("\\", get_class($this));
  456.         $arraysize = count($classname);
  457.         $attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
  458.         $this->denyAccessUnlessGranted($attribute, $mailing); 
  459.     
  460.         $form = $this->createForm(MailingType::class, $mailing, 
  461.                 ['action' => $this->generateUrl('mailing_edit', array('id' => $mailing->getId())), 'attr' => ['id' => $this->generateUrl('mailing_edit', array('id' => $mailing->getId()))]]);
  462.         $form->handleRequest($request);
  463.         if ($form->isSubmitted() && $form->isValid()) {
  464.             $this->getDoctrine()->getManager()->flush();
  465.             return $this->redirectToRoute('mailing_show', array('id' => $mailing->getId()));
  466.         }
  467.         return $this->render('kernel/mailing/edit.html.twig', [
  468.             'mailing' => $mailing,
  469.             'form' => $form->createView(),
  470.         ]);
  471.     }
  472.     /**
  473.      * @Route("/{id}", name="mailing_delete", methods={"DELETE"})
  474.      */
  475.     public function delete(Request $request, Mailing $mailing): Response
  476.     {
  477.         $classname =  explode("\\", get_class($this));
  478.         $arraysize = count($classname);
  479.         $attribute = str_replace("Controller", "", $classname[$arraysize-1]) . ":" . str_replace("Action", "", __FUNCTION__);
  480.         $this->denyAccessUnlessGranted($attribute, $mailing); 
  481.     
  482.         if ($this->isCsrfTokenValid('delete'.$mailing->getId(), $request->request->get('_token'))) {
  483.             $entityManager = $this->getDoctrine()->getManager();
  484.             $entityManager->remove($mailing);
  485.             $entityManager->flush();
  486.         }
  487.         return $this->redirectToRoute('mailing_index');
  488.     }
  489. }