src/Services/Platform/KeywordTools.php line 23

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 $doctrineUrlGeneratorInterface $router
  21.         LogTools $logToolsCommonTools $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.                         default:
  129.                             $notFoundKeywords[] = $keyword;
  130.                         break;
  131.                     }
  132.                 }
  133.             }
  134.             else
  135.             {
  136.                 // All hope is lost
  137.                 $notFoundKeywords[] = $keyword;                
  138.             }
  139.         }
  140.         return array(
  141.             'not_found_keywords'    =>    $notFoundKeywords,
  142.             'body'                    =>    $body,
  143.         );
  144.     }
  145.     public function matchKeywords($body$keywords$entity$object)
  146.     {
  147.         $propertyAccessor PropertyAccess::createPropertyAccessor();
  148.         $notFoundKeywords = array();
  149.         foreach ($keywords as $keyword)
  150.         {
  151.             // Plan.io Task #3272 JCAF Notifications
  152.             // This is really ugly, but I have no idea how to do it otherwise
  153.             if ($keyword->getValue() == "%devis.reference%" && $entity->getValue() == "external_devis")
  154.             {
  155.                 $valueToUse $propertyAccessor->getValue($object"ref");
  156.                 $body str_replace($keyword->getValue(), $valueToUse$body);
  157.             }
  158.             else
  159.             {
  160.                 if ($keyword->getEntity() !== null && $keyword->getEntity()->getId() == $entity->getId())
  161.                 {
  162.                     if ($keyword->getMember() !== null)
  163.                     {
  164.                         $member $keyword->getMember();
  165.                         $memberParts explode("."$member);
  166.                         $valueToUse "";
  167.                         if (count($memberParts) == 1)
  168.                         {
  169.                             // Standard : Something like individual.lastname or individual.origin.value
  170.                             $valueToUse $propertyAccessor->getValue($object$keyword->getMember());
  171.                         }
  172.                         else
  173.                         {
  174.                             // <=> individual->getTitle();
  175.                             $stepOne $propertyAccessor->getValue($object$memberParts[0]);
  176.                             if ($stepOne !== null)
  177.                             {
  178.                                 // <=> title->getAbbrv() <=> individual.title.abbrv
  179.                                 $valueToUse $propertyAccessor->getValue($stepOne$memberParts[1]);
  180.                             }
  181.                         }
  182.                         $body str_replace($keyword->getValue(), $valueToUse$body);
  183.                     }
  184.                     else
  185.                     {
  186.                         // Custom stuff like dates, addresses or more complex operations (payment links for example)
  187.                         switch ($keyword->getValue())
  188.                         {
  189.                             case '%mission.jcaf.lien%':
  190.                             {
  191.                                 if ($object instanceof Mission)
  192.                                 {
  193.                                     $id $object->getRemoteJcafId();
  194.                                     if (!empty($id))
  195.                                     {
  196.                                         if ($this->prodRealm)
  197.                                         {
  198.                                             $url "https://jaicaafaire.fr/account/contact/$id/view/";
  199.                                         }
  200.                                         else
  201.                                         {
  202.                                             if ($this->testRealm)
  203.                                             {
  204.                                                 $url "https://test.jaicaafaire.fr/account/contact/$id/view/";
  205.                                             }
  206.                                             else
  207.                                             {
  208.                                                 $url "https://dev.jaicaafaire.fr/app_dev.php/account/contact/$id/view/";
  209.                                             }
  210.                                         }
  211.                                         $code "<a href='$url'>$url</a>";
  212.                                         $body str_replace($keyword->getValue(), $code$body);
  213.                                     }
  214.                                 }
  215.                                 break;
  216.                             }
  217.                             case '%mission.lien%':
  218.                             {
  219.                                 // Get the platform base url
  220.                                 $urlConfig $this->em->getRepository(Config::class)
  221.                                     ->findOneByName(Config::URL);
  222.                                 if ($urlConfig !== null)
  223.                                 {
  224.                                     // This should always be true
  225.                                     $url $this->router->generate("icod_platform_mission_view", array('id' => $object->getId()));
  226.                                     $url $urlConfig->getValue().$url;
  227.                                     $code "<a href='$url'>$url</a>";
  228.                                     $body str_replace($keyword->getValue(), $code$body);
  229.                                 }
  230.                                 break;
  231.                             }
  232.                             case '%application.lien%':
  233.                             {
  234.                                 // Get the platform base url
  235.                                 $urlConfig $this->em->getRepository(Config::class)
  236.                                     ->findOneByName(Config::URL);
  237.                                 if ($urlConfig !== null)
  238.                                 {
  239.                                     // This should always be true
  240.                                     $url $this->router->generate("icod_human_resource_application_view", array('id' => $object->getId()));
  241.                                     $url $urlConfig->getValue().$url;
  242.                                     $code "<a href='$url'>$url</a>";
  243.                                     $body str_replace($keyword->getValue(), $code$body);
  244.                                 }
  245.                                 break;
  246.                             }
  247.                             case '%formulaire_rh.lien%':
  248.                             {
  249.                                 // Get the platform base url
  250.                                 $urlConfig $this->em->getRepository(Config::class)
  251.                                     ->findOneByName(Config::URL);
  252.                                 if ($urlConfig !== null)
  253.                                 {
  254.                                     // This should always be true
  255.                                     $url $this->router->generate("icod_rh_rhform_view", array('id' => $object->getId()));
  256.                                     $url $urlConfig->getValue().$url;
  257.                                     $code "<a href='$url'>$url</a>";
  258.                                     $body str_replace($keyword->getValue(), $code$body);
  259.                                 }
  260.                                 break;
  261.                             }
  262.                             case '%client.adresse%':
  263.                             {
  264.                                 $address $object->getAddress();
  265.                                 if($address !== null)
  266.                                 {
  267.                                     $addressArray $address->transformInArray();
  268.                                     $parts null;
  269.                                     if($addressArray['address'] !== ""){
  270.                                         $parts $parts $addressArray['address'];
  271.                                     }
  272.                                     if($addressArray['complement'] !== ""){
  273.                                         $parts $parts " " $addressArray['complement'];
  274.                                     }
  275.                                     $address $parts " | " $addressArray['postalCode'] .  " - " $addressArray['town'] .", " $addressArray['country'];
  276.                                 }
  277.                                 $body str_replace($keyword->getValue(), $address$body);
  278.                                 break;
  279.                             }
  280.                             case '%task.debut%':
  281.                             {
  282.                                 $date $object->getStartDate();
  283.                                 if($date !== null)
  284.                                 {
  285.                                     $formatedDate $date->format('d/m/Y H:i');
  286.                                     $body str_replace($keyword->getValue(), $formatedDate$body);
  287.                                 }
  288.                                 break;
  289.                             }
  290.                             case '%task.fin%':
  291.                             {
  292.                                 $date $object->getEndDate();
  293.                                 if($date !== null)
  294.                                 {
  295.                                     $formatedDate $date->format('d/m/Y H:i');
  296.                                     $body str_replace($keyword->getValue(), $formatedDate$body);
  297.                                 }
  298.                                 break;
  299.                             }
  300.                             default:
  301.                                 $notFoundKeywords[] = $keyword;
  302.                             break;
  303.                         }
  304.                     }
  305.                 }
  306.                 else
  307.                 {
  308.                     // Custom stuff without matching entities (planning)
  309.                     $specialKeywords = array("%lien.planning.global%""%lien.planning.individuel%");
  310.                     if (in_array($keyword->getValue(), $specialKeywords))
  311.                     {
  312.                         switch ($keyword->getValue())
  313.                         {
  314.                             case '%lien.planning.global%':
  315.                             {
  316.                                 // Get the platform base url
  317.                                 $urlConfig $this->em->getRepository(Config::class)
  318.                                     ->findOneByName(Config::URL);
  319.                                 if ($urlConfig !== null)
  320.                                 {
  321.                                     // This should always be true
  322.                                     $url $this->router->generate("icod_platform_planning_view_global");
  323.                                     $url $urlConfig->getValue().$url;
  324.                                     $code "<a href='$url'>$url</a>";
  325.                                     $body str_replace($keyword->getValue(), $code$body);
  326.                                 }
  327.                                 break;
  328.                             }
  329.                             case '%lien.planning.individuel%':
  330.                             {
  331.                                 // Get the platform base url
  332.                                 $urlConfig $this->em->getRepository(Config::class)
  333.                                     ->findOneByName(Config::URL);
  334.                                 if ($urlConfig !== null)
  335.                                 {
  336.                                     // This should always be true
  337.                                     $url $this->router->generate("icod_platform_planning_view_access");
  338.                                     $url $urlConfig->getValue().$url;
  339.                                     $code "<a href='$url'>$url</a>";
  340.                                     $body str_replace($keyword->getValue(), $code$body);
  341.                                 }
  342.                                 break;
  343.                             }
  344.                         }
  345.                     }
  346.                     else
  347.                     {
  348.                         // All hope is lost
  349.                         $notFoundKeywords[] = $keyword;
  350.                     }
  351.                 }
  352.             }
  353.         }
  354.         return array(
  355.             'not_found_keywords'    =>    $notFoundKeywords,
  356.             'body'                    =>    $body,
  357.         );
  358.     }
  359.     // Plan.io Task #4518
  360.     public function constructBodyForClientAccount($template$client$mission$object)
  361.     {
  362.         // Get body
  363.         $body $template->getBody();
  364.         // Get keywords
  365.         $keywords $template->getKeywords();
  366.         // Match keywords for each entity
  367.         $entityRep $this->em->getRepository(Entity::class);
  368.         // Mission (can be null)
  369.         if ($mission !== null)
  370.         {
  371.             $entity $entityRep->findOneByEntity(Entity::MISSION);
  372.             $result $this->matchKeywordsForClientAccount($body$keywords$entity$mission);
  373.             $body $result['body'];
  374.         }
  375.         
  376.         // Individual
  377.         $entity $entityRep->findOneByEntity(Entity::INDIVIDUAL);        
  378.         if ($client !== null && $client->getIndividual() !== null)
  379.         {
  380.             $result $this->matchKeywordsForClientAccount($body$keywords$entity$client->getIndividual());
  381.             $body $result['body'];
  382.         }
  383.         // Get Entity based on the object
  384.         $entity null;
  385.         // Devis
  386.         if ($object instanceof Devis)
  387.         {
  388.             $entity $entityRep->findOneByEntity(Entity::DEVIS);            
  389.         }
  390.         elseif ($object instanceof Invoice)
  391.         {
  392.             $entity $entityRep->findOneByEntity(Entity::INVOICE);                
  393.         }
  394.         elseif ($object instanceof Task)
  395.         {
  396.             $entity $entityRep->findOneByEntity(Entity::TASK);                
  397.         }
  398.         elseif ($object instanceof IkeaServiceOrder)
  399.         {
  400.             $entity null;
  401.         }
  402.         if ($entity !== null)
  403.         {
  404.             $result $this->matchKeywordsForClientAccount($body$keywords$entity$object);
  405.             $body $result['body'];
  406.         }
  407.         // New line fix
  408.         $body str_replace("\n""<br>"$body);
  409.         return $body;
  410.     }
  411. }