src/Services/Platform/KeywordTools.php line 30

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/Services/Platform/KeywordTools.php
  4. //----------------------------------------------------------------------
  5. namespace App\Services\Platform;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use Symfony\Component\PropertyAccess\PropertyAccess;
  8. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  9. use App\Entity\Common\Entity;
  10. use App\Entity\Config\Config;
  11. use App\Entity\Ikea\ServiceOrder as IkeaServiceOrder;
  12. use App\Entity\Mission\Mission;
  13. use App\Entity\Planning\Task;
  14. use App\Entity\Platform\Devis\Devis;
  15. use App\Entity\Platform\Invoice\Invoice;
  16. use App\Services\CommonTools;
  17. use App\Services\LogTools;
  18. class KeywordTools
  19. {
  20.     public function __construct(ManagerRegistry $doctrine, UrlGeneratorInterface $router, 
  21.         LogTools $logTools, CommonTools $commonTools,
  22.         $testRealm=false, $prodRealm=false)
  23.     {
  24.         $this->em = $doctrine->getManager();
  25.         $this->router = $router;
  26.         $this->logTools = $logTools;
  27.         $this->commonTools = $commonTools;
  28.         $this->testRealm = $testRealm;
  29.         $this->prodRealm = $prodRealm;
  30.     }
  31.     // $template can be EmailTeaplte or EventTemplate
  32.     public function handleTemplateForm($template, $entities, $keywords)
  33.     {
  34.         foreach ($template->getKeywords() as $keyword)
  35.         {
  36.             $template->removeKeyword($keyword);
  37.         }
  38.         foreach ($keywords as $keyword)
  39.         {
  40.             if (strpos($template->getBody(), $keyword->getValue()) !== false)
  41.             {
  42.                 $template->addKeyword($keyword);
  43.             }
  44.         }
  45.     }
  46.     // Attention : Email for Client => URL should be Client Platform
  47.     public function matchKeywordsForClientAccount($body, $keywords, $entity, $object)
  48.     {
  49.         $propertyAccessor = PropertyAccess::createPropertyAccessor();
  50.         $notFoundKeywords = array();
  51.         foreach ($keywords as $keyword)
  52.         {
  53.             if ($keyword->getEntity() !== null && $keyword->getEntity()->getId() == $entity->getId())
  54.             {
  55.                 if ($keyword->getMember() !== null)
  56.                 {
  57.                     $member = $keyword->getMember();
  58.                     $memberParts = explode(".", $member);
  59.                     $valueToUse = "";
  60.                     if (count($memberParts) == 1)
  61.                     {
  62.                         // Standard : Something like individual.lastname or individual.origin.value
  63.                         $valueToUse = $propertyAccessor->getValue($object, $keyword->getMember());
  64.                     }
  65.                     else
  66.                     {
  67.                         // <=> individual->getTitle();
  68.                         $stepOne = $propertyAccessor->getValue($object, $memberParts[0]);
  69.                         if ($stepOne !== null)
  70.                         {
  71.                             // <=> title->getAbbrv() <=> individual.title.abbrv
  72.                             $valueToUse = $propertyAccessor->getValue($stepOne, $memberParts[1]);
  73.                         }
  74.                     }
  75.                     $body = str_replace($keyword->getValue(), $valueToUse, $body);
  76.                 }
  77.                 else
  78.                 {
  79.                     // Custom stuff like dates, addresses or more complex operations (payment links for example)
  80.                     switch ($keyword->getValue())
  81.                     {
  82.                         case '%mission.lien%':
  83.                         {
  84.                             $display = $object->display();
  85.                             $url = $this->commonTools->craftUrl('client_account_mission_view', array('id' => $object->getId()));
  86.                             $code = "<a href='$url'>$display</a>";
  87.                             $body = str_replace($keyword->getValue(), $code, $body);
  88.                             break;
  89.                         }
  90.                         case '%client.adresse%':
  91.                         {
  92.                             $address = $object->getAddress();
  93.                             if($address !== null)
  94.                             {
  95.                                 $addressArray = $address->transformInArray();
  96.                                 $parts = null;
  97.                                 if($addressArray['address'] !== ""){
  98.                                     $parts = $parts . $addressArray['address'];
  99.                                 }
  100.                                 if($addressArray['complement'] !== ""){
  101.                                     $parts = $parts . " " . $addressArray['complement'];
  102.                                 }
  103.                                 $address = $parts . " | " . $addressArray['postalCode'] .  " - " . $addressArray['town'] .", " . $addressArray['country'];
  104.                             }
  105.                             $body = str_replace($keyword->getValue(), $address, $body);
  106.                             break;
  107.                         }
  108.                         case '%task.debut%':
  109.                         {
  110.                             $date = $object->getStartDate();
  111.                             if($date !== null)
  112.                             {
  113.                                 $formatedDate = $date->format('d/m/Y H:i');
  114.                                 $body = str_replace($keyword->getValue(), $formatedDate, $body);
  115.                             }
  116.                             break;
  117.                         }
  118.                         case '%task.fin%':
  119.                         {
  120.                             $date = $object->getEndDate();
  121.                             if($date !== null)
  122.                             {
  123.                                 $formatedDate = $date->format('d/m/Y H:i');
  124.                                 $body = str_replace($keyword->getValue(), $formatedDate, $body);
  125.                             }
  126.                             break;
  127.                         }
  128.                         case '%ikea_service_order.date_intervention%':
  129.                         {
  130.                             if ($object instanceof IkeaServiceOrder)
  131.                             {                        
  132.                                 $date = $object->getInterventionDate();
  133.                                 if ($date !== null)
  134.                                 {
  135.                                     $formatedDate = $date->format('d/m/Y H:i');
  136.                                     $body = str_replace($keyword->getValue(), $formatedDate, $body);
  137.                                 }
  138.                             }
  139.                             break;
  140.                         }
  141.                         default:
  142.                             $notFoundKeywords[] = $keyword;
  143.                         break;
  144.                     }
  145.                 }
  146.             }
  147.             else
  148.             {
  149.                 // All hope is lost
  150.                 $notFoundKeywords[] = $keyword;                
  151.             }
  152.         }
  153.         return array(
  154.             'not_found_keywords'    =>    $notFoundKeywords,
  155.             'body'                    =>    $body,
  156.         );
  157.     }
  158.     public function matchKeywords($body, $keywords, $entity, $object)
  159.     {
  160.         $propertyAccessor = PropertyAccess::createPropertyAccessor();
  161.         $notFoundKeywords = array();
  162.         foreach ($keywords as $keyword)
  163.         {
  164.             // Plan.io Task #3272 JCAF Notifications
  165.             // This is really ugly, but I have no idea how to do it otherwise
  166.             if ($keyword->getValue() == "%devis.reference%" && $entity->getValue() == "external_devis")
  167.             {
  168.                 $valueToUse = $propertyAccessor->getValue($object, "ref");
  169.                 $body = str_replace($keyword->getValue(), $valueToUse, $body);
  170.             }
  171.             else
  172.             {
  173.                 if ($keyword->getEntity() !== null && $keyword->getEntity()->getId() == $entity->getId())
  174.                 {
  175.                     if ($keyword->getMember() !== null)
  176.                     {
  177.                         $member = $keyword->getMember();
  178.                         $memberParts = explode(".", $member);
  179.                         $valueToUse = "";
  180.                         if (count($memberParts) == 1)
  181.                         {
  182.                             // Standard : Something like individual.lastname or individual.origin.value
  183.                             $valueToUse = $propertyAccessor->getValue($object, $keyword->getMember());
  184.                         }
  185.                         else
  186.                         {
  187.                             // <=> individual->getTitle();
  188.                             $stepOne = $propertyAccessor->getValue($object, $memberParts[0]);
  189.                             if ($stepOne !== null)
  190.                             {
  191.                                 // <=> title->getAbbrv() <=> individual.title.abbrv
  192.                                 $valueToUse = $propertyAccessor->getValue($stepOne, $memberParts[1]);
  193.                             }
  194.                         }
  195.                         $body = str_replace($keyword->getValue(), $valueToUse, $body);
  196.                     }
  197.                     else
  198.                     {
  199.                         // Custom stuff like dates, addresses or more complex operations (payment links for example)
  200.                         switch ($keyword->getValue())
  201.                         {
  202.                             case '%mission.jcaf.lien%':
  203.                             {
  204.                                 if ($object instanceof Mission)
  205.                                 {
  206.                                     $id = $object->getRemoteJcafId();
  207.                                     if (!empty($id))
  208.                                     {
  209.                                         if ($this->prodRealm)
  210.                                         {
  211.                                             $url = "https://jaicaafaire.fr/account/contact/$id/view/";
  212.                                         }
  213.                                         else
  214.                                         {
  215.                                             if ($this->testRealm)
  216.                                             {
  217.                                                 $url = "https://test.jaicaafaire.fr/account/contact/$id/view/";
  218.                                             }
  219.                                             else
  220.                                             {
  221.                                                 $url = "https://dev.jaicaafaire.fr/app_dev.php/account/contact/$id/view/";
  222.                                             }
  223.                                         }
  224.                                         $code = "<a href='$url'>$url</a>";
  225.                                         $body = str_replace($keyword->getValue(), $code, $body);
  226.                                     }
  227.                                 }
  228.                                 break;
  229.                             }
  230.                             case '%mission.lien%':
  231.                             {
  232.                                 // Get the platform base url
  233.                                 $urlConfig = $this->em->getRepository(Config::class)
  234.                                     ->findOneByName(Config::URL);
  235.                                 if ($urlConfig !== null)
  236.                                 {
  237.                                     // This should always be true
  238.                                     $url = $this->router->generate("icod_platform_mission_view", array('id' => $object->getId()));
  239.                                     $url = $urlConfig->getValue().$url;
  240.                                     $code = "<a href='$url'>$url</a>";
  241.                                     $body = str_replace($keyword->getValue(), $code, $body);
  242.                                 }
  243.                                 break;
  244.                             }
  245.                             case '%application.lien%':
  246.                             {
  247.                                 // Get the platform base url
  248.                                 $urlConfig = $this->em->getRepository(Config::class)
  249.                                     ->findOneByName(Config::URL);
  250.                                 if ($urlConfig !== null)
  251.                                 {
  252.                                     // This should always be true
  253.                                     $url = $this->router->generate("icod_human_resource_application_view", array('id' => $object->getId()));
  254.                                     $url = $urlConfig->getValue().$url;
  255.                                     $code = "<a href='$url'>$url</a>";
  256.                                     $body = str_replace($keyword->getValue(), $code, $body);
  257.                                 }
  258.                                 break;
  259.                             }
  260.                             case '%formulaire_rh.lien%':
  261.                             {
  262.                                 // Get the platform base url
  263.                                 $urlConfig = $this->em->getRepository(Config::class)
  264.                                     ->findOneByName(Config::URL);
  265.                                 if ($urlConfig !== null)
  266.                                 {
  267.                                     // This should always be true
  268.                                     $url = $this->router->generate("icod_rh_rhform_view", array('id' => $object->getId()));
  269.                                     $url = $urlConfig->getValue().$url;
  270.                                     $code = "<a href='$url'>$url</a>";
  271.                                     $body = str_replace($keyword->getValue(), $code, $body);
  272.                                 }
  273.                                 break;
  274.                             }
  275.                             case '%client.adresse%':
  276.                             {
  277.                                 $address = $object->getAddress();
  278.                                 if($address !== null)
  279.                                 {
  280.                                     $addressArray = $address->transformInArray();
  281.                                     $parts = null;
  282.                                     if($addressArray['address'] !== ""){
  283.                                         $parts = $parts . $addressArray['address'];
  284.                                     }
  285.                                     if($addressArray['complement'] !== ""){
  286.                                         $parts = $parts . " " . $addressArray['complement'];
  287.                                     }
  288.                                     $address = $parts . " | " . $addressArray['postalCode'] .  " - " . $addressArray['town'] .", " . $addressArray['country'];
  289.                                 }
  290.                                 $body = str_replace($keyword->getValue(), $address, $body);
  291.                                 break;
  292.                             }
  293.                             case '%task.debut%':
  294.                             {
  295.                                 $date = $object->getStartDate();
  296.                                 if($date !== null)
  297.                                 {
  298.                                     $formatedDate = $date->format('d/m/Y H:i');
  299.                                     $body = str_replace($keyword->getValue(), $formatedDate, $body);
  300.                                 }
  301.                                 break;
  302.                             }
  303.                             case '%task.fin%':
  304.                             {
  305.                                 $date = $object->getEndDate();
  306.                                 if($date !== null)
  307.                                 {
  308.                                     $formatedDate = $date->format('d/m/Y H:i');
  309.                                     $body = str_replace($keyword->getValue(), $formatedDate, $body);
  310.                                 }
  311.                                 break;
  312.                             }
  313.                             case '%ikea_service_order.date_intervention%':
  314.                             {
  315.                                 if ($object instanceof IkeaServiceOrder)
  316.                                 {                        
  317.                                     $date = $object->getInterventionDate();
  318.                                     if ($date !== null)
  319.                                     {
  320.                                         $formatedDate = $date->format('d/m/Y H:i');
  321.     
  322.                                         $body = str_replace($keyword->getValue(), $formatedDate, $body);
  323.                                     }
  324.                                 }
  325.                                 break;
  326.                             }
  327.                             default:
  328.                                 $notFoundKeywords[] = $keyword;
  329.                             break;
  330.                         }
  331.                     }
  332.                 }
  333.                 else
  334.                 {
  335.                     // Custom stuff without matching entities (planning)
  336.                     $specialKeywords = array("%lien.planning.global%", "%lien.planning.individuel%");
  337.                     if (in_array($keyword->getValue(), $specialKeywords))
  338.                     {
  339.                         switch ($keyword->getValue())
  340.                         {
  341.                             case '%lien.planning.global%':
  342.                             {
  343.                                 // Get the platform base url
  344.                                 $urlConfig = $this->em->getRepository(Config::class)
  345.                                     ->findOneByName(Config::URL);
  346.                                 if ($urlConfig !== null)
  347.                                 {
  348.                                     // This should always be true
  349.                                     $url = $this->router->generate("icod_platform_planning_view_global");
  350.                                     $url = $urlConfig->getValue().$url;
  351.                                     $code = "<a href='$url'>$url</a>";
  352.                                     $body = str_replace($keyword->getValue(), $code, $body);
  353.                                 }
  354.                                 break;
  355.                             }
  356.                             case '%lien.planning.individuel%':
  357.                             {
  358.                                 // Get the platform base url
  359.                                 $urlConfig = $this->em->getRepository(Config::class)
  360.                                     ->findOneByName(Config::URL);
  361.                                 if ($urlConfig !== null)
  362.                                 {
  363.                                     // This should always be true
  364.                                     $url = $this->router->generate("icod_platform_planning_view_access");
  365.                                     $url = $urlConfig->getValue().$url;
  366.                                     $code = "<a href='$url'>$url</a>";
  367.                                     $body = str_replace($keyword->getValue(), $code, $body);
  368.                                 }
  369.                                 break;
  370.                             }
  371.                         }
  372.                     }
  373.                     else
  374.                     {
  375.                         // All hope is lost
  376.                         $notFoundKeywords[] = $keyword;
  377.                     }
  378.                 }
  379.             }
  380.         }
  381.         return array(
  382.             'not_found_keywords'    =>    $notFoundKeywords,
  383.             'body'                    =>    $body,
  384.         );
  385.     }
  386.     // Plan.io Task #4518
  387.     public function constructBodyForClientAccount($template, $client, $mission, $object)
  388.     {
  389.         // Get body
  390.         $body = $template->getBody();
  391.         // Get keywords
  392.         $keywords = $template->getKeywords();
  393.         // Match keywords for each entity
  394.         $entityRep = $this->em->getRepository(Entity::class);
  395.         // Mission (can be null)
  396.         if ($mission !== null)
  397.         {
  398.             $entity = $entityRep->findOneByEntity(Entity::MISSION);
  399.             $result = $this->matchKeywordsForClientAccount($body, $keywords, $entity, $mission);
  400.             $body = $result['body'];
  401.         }
  402.         
  403.         // Individual
  404.         $entity = $entityRep->findOneByEntity(Entity::INDIVIDUAL);        
  405.         if ($client !== null && $client->getIndividual() !== null)
  406.         {
  407.             $result = $this->matchKeywordsForClientAccount($body, $keywords, $entity, $client->getIndividual());
  408.             $body = $result['body'];
  409.         }
  410.         // Get Entity based on the object
  411.         $entity = null;
  412.         // Devis
  413.         if ($object instanceof Devis)
  414.         {
  415.             $entity = $entityRep->findOneByEntity(Entity::DEVIS);            
  416.         }
  417.         elseif ($object instanceof Invoice)
  418.         {
  419.             $entity = $entityRep->findOneByEntity(Entity::INVOICE);                
  420.         }
  421.         elseif ($object instanceof Task)
  422.         {
  423.             $entity = $entityRep->findOneByEntity(Entity::TASK);                
  424.         }
  425.         elseif ($object instanceof IkeaServiceOrder)
  426.         {
  427.             $entity = $entityRep->findOneByEntity(Entity::IKEA_SERVICE_ORDER);            
  428.         }
  429.         if ($entity !== null)
  430.         {
  431.             $result = $this->matchKeywordsForClientAccount($body, $keywords, $entity, $object);
  432.             $body = $result['body'];
  433.         }
  434.         // New line fix
  435.         $body = str_replace("\n", "<br>", $body);
  436.         return $body;
  437.     }
  438. }