<?php
//----------------------------------------------------------------------
// src/Services/Platform/KeywordTools.php
//----------------------------------------------------------------------
namespace App\Services\Platform;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use App\Entity\Common\Entity;
use App\Entity\Config\Config;
use App\Entity\Ikea\ServiceOrder as IkeaServiceOrder;
use App\Entity\Mission\Mission;
use App\Entity\Planning\Task;
use App\Entity\Platform\Devis\Devis;
use App\Entity\Platform\Invoice\Invoice;
use App\Services\CommonTools;
use App\Services\LogTools;
class KeywordTools
{
public function __construct(ManagerRegistry $doctrine, UrlGeneratorInterface $router,
LogTools $logTools, CommonTools $commonTools,
$testRealm=false, $prodRealm=false)
{
$this->em = $doctrine->getManager();
$this->router = $router;
$this->logTools = $logTools;
$this->commonTools = $commonTools;
$this->testRealm = $testRealm;
$this->prodRealm = $prodRealm;
}
// $template can be EmailTeaplte or EventTemplate
public function handleTemplateForm($template, $entities, $keywords)
{
foreach ($template->getKeywords() as $keyword)
{
$template->removeKeyword($keyword);
}
foreach ($keywords as $keyword)
{
if (strpos($template->getBody(), $keyword->getValue()) !== false)
{
$template->addKeyword($keyword);
}
}
}
// Attention : Email for Client => URL should be Client Platform
public function matchKeywordsForClientAccount($body, $keywords, $entity, $object)
{
$propertyAccessor = PropertyAccess::createPropertyAccessor();
$notFoundKeywords = array();
foreach ($keywords as $keyword)
{
if ($keyword->getEntity() !== null && $keyword->getEntity()->getId() == $entity->getId())
{
if ($keyword->getMember() !== null)
{
$member = $keyword->getMember();
$memberParts = explode(".", $member);
$valueToUse = "";
if (count($memberParts) == 1)
{
// Standard : Something like individual.lastname or individual.origin.value
$valueToUse = $propertyAccessor->getValue($object, $keyword->getMember());
}
else
{
// <=> individual->getTitle();
$stepOne = $propertyAccessor->getValue($object, $memberParts[0]);
if ($stepOne !== null)
{
// <=> title->getAbbrv() <=> individual.title.abbrv
$valueToUse = $propertyAccessor->getValue($stepOne, $memberParts[1]);
}
}
$body = str_replace($keyword->getValue(), $valueToUse, $body);
}
else
{
// Custom stuff like dates, addresses or more complex operations (payment links for example)
switch ($keyword->getValue())
{
case '%mission.lien%':
{
$display = $object->display();
$url = $this->commonTools->craftUrl('client_account_mission_view', array('id' => $object->getId()));
$code = "<a href='$url'>$display</a>";
$body = str_replace($keyword->getValue(), $code, $body);
break;
}
case '%client.adresse%':
{
$address = $object->getAddress();
if($address !== null)
{
$addressArray = $address->transformInArray();
$parts = null;
if($addressArray['address'] !== ""){
$parts = $parts . $addressArray['address'];
}
if($addressArray['complement'] !== ""){
$parts = $parts . " " . $addressArray['complement'];
}
$address = $parts . " | " . $addressArray['postalCode'] . " - " . $addressArray['town'] .", " . $addressArray['country'];
}
$body = str_replace($keyword->getValue(), $address, $body);
break;
}
case '%task.debut%':
{
$date = $object->getStartDate();
if($date !== null)
{
$formatedDate = $date->format('d/m/Y H:i');
$body = str_replace($keyword->getValue(), $formatedDate, $body);
}
break;
}
case '%task.fin%':
{
$date = $object->getEndDate();
if($date !== null)
{
$formatedDate = $date->format('d/m/Y H:i');
$body = str_replace($keyword->getValue(), $formatedDate, $body);
}
break;
}
default:
$notFoundKeywords[] = $keyword;
break;
}
}
}
else
{
// All hope is lost
$notFoundKeywords[] = $keyword;
}
}
return array(
'not_found_keywords' => $notFoundKeywords,
'body' => $body,
);
}
public function matchKeywords($body, $keywords, $entity, $object)
{
$propertyAccessor = PropertyAccess::createPropertyAccessor();
$notFoundKeywords = array();
foreach ($keywords as $keyword)
{
// Plan.io Task #3272 JCAF Notifications
// This is really ugly, but I have no idea how to do it otherwise
if ($keyword->getValue() == "%devis.reference%" && $entity->getValue() == "external_devis")
{
$valueToUse = $propertyAccessor->getValue($object, "ref");
$body = str_replace($keyword->getValue(), $valueToUse, $body);
}
else
{
if ($keyword->getEntity() !== null && $keyword->getEntity()->getId() == $entity->getId())
{
if ($keyword->getMember() !== null)
{
$member = $keyword->getMember();
$memberParts = explode(".", $member);
$valueToUse = "";
if (count($memberParts) == 1)
{
// Standard : Something like individual.lastname or individual.origin.value
$valueToUse = $propertyAccessor->getValue($object, $keyword->getMember());
}
else
{
// <=> individual->getTitle();
$stepOne = $propertyAccessor->getValue($object, $memberParts[0]);
if ($stepOne !== null)
{
// <=> title->getAbbrv() <=> individual.title.abbrv
$valueToUse = $propertyAccessor->getValue($stepOne, $memberParts[1]);
}
}
$body = str_replace($keyword->getValue(), $valueToUse, $body);
}
else
{
// Custom stuff like dates, addresses or more complex operations (payment links for example)
switch ($keyword->getValue())
{
case '%mission.jcaf.lien%':
{
if ($object instanceof Mission)
{
$id = $object->getRemoteJcafId();
if (!empty($id))
{
if ($this->prodRealm)
{
$url = "https://jaicaafaire.fr/account/contact/$id/view/";
}
else
{
if ($this->testRealm)
{
$url = "https://test.jaicaafaire.fr/account/contact/$id/view/";
}
else
{
$url = "https://dev.jaicaafaire.fr/app_dev.php/account/contact/$id/view/";
}
}
$code = "<a href='$url'>$url</a>";
$body = str_replace($keyword->getValue(), $code, $body);
}
}
break;
}
case '%mission.lien%':
{
// Get the platform base url
$urlConfig = $this->em->getRepository(Config::class)
->findOneByName(Config::URL);
if ($urlConfig !== null)
{
// This should always be true
$url = $this->router->generate("icod_platform_mission_view", array('id' => $object->getId()));
$url = $urlConfig->getValue().$url;
$code = "<a href='$url'>$url</a>";
$body = str_replace($keyword->getValue(), $code, $body);
}
break;
}
case '%application.lien%':
{
// Get the platform base url
$urlConfig = $this->em->getRepository(Config::class)
->findOneByName(Config::URL);
if ($urlConfig !== null)
{
// This should always be true
$url = $this->router->generate("icod_human_resource_application_view", array('id' => $object->getId()));
$url = $urlConfig->getValue().$url;
$code = "<a href='$url'>$url</a>";
$body = str_replace($keyword->getValue(), $code, $body);
}
break;
}
case '%formulaire_rh.lien%':
{
// Get the platform base url
$urlConfig = $this->em->getRepository(Config::class)
->findOneByName(Config::URL);
if ($urlConfig !== null)
{
// This should always be true
$url = $this->router->generate("icod_rh_rhform_view", array('id' => $object->getId()));
$url = $urlConfig->getValue().$url;
$code = "<a href='$url'>$url</a>";
$body = str_replace($keyword->getValue(), $code, $body);
}
break;
}
case '%client.adresse%':
{
$address = $object->getAddress();
if($address !== null)
{
$addressArray = $address->transformInArray();
$parts = null;
if($addressArray['address'] !== ""){
$parts = $parts . $addressArray['address'];
}
if($addressArray['complement'] !== ""){
$parts = $parts . " " . $addressArray['complement'];
}
$address = $parts . " | " . $addressArray['postalCode'] . " - " . $addressArray['town'] .", " . $addressArray['country'];
}
$body = str_replace($keyword->getValue(), $address, $body);
break;
}
case '%task.debut%':
{
$date = $object->getStartDate();
if($date !== null)
{
$formatedDate = $date->format('d/m/Y H:i');
$body = str_replace($keyword->getValue(), $formatedDate, $body);
}
break;
}
case '%task.fin%':
{
$date = $object->getEndDate();
if($date !== null)
{
$formatedDate = $date->format('d/m/Y H:i');
$body = str_replace($keyword->getValue(), $formatedDate, $body);
}
break;
}
default:
$notFoundKeywords[] = $keyword;
break;
}
}
}
else
{
// Custom stuff without matching entities (planning)
$specialKeywords = array("%lien.planning.global%", "%lien.planning.individuel%");
if (in_array($keyword->getValue(), $specialKeywords))
{
switch ($keyword->getValue())
{
case '%lien.planning.global%':
{
// Get the platform base url
$urlConfig = $this->em->getRepository(Config::class)
->findOneByName(Config::URL);
if ($urlConfig !== null)
{
// This should always be true
$url = $this->router->generate("icod_platform_planning_view_global");
$url = $urlConfig->getValue().$url;
$code = "<a href='$url'>$url</a>";
$body = str_replace($keyword->getValue(), $code, $body);
}
break;
}
case '%lien.planning.individuel%':
{
// Get the platform base url
$urlConfig = $this->em->getRepository(Config::class)
->findOneByName(Config::URL);
if ($urlConfig !== null)
{
// This should always be true
$url = $this->router->generate("icod_platform_planning_view_access");
$url = $urlConfig->getValue().$url;
$code = "<a href='$url'>$url</a>";
$body = str_replace($keyword->getValue(), $code, $body);
}
break;
}
}
}
else
{
// All hope is lost
$notFoundKeywords[] = $keyword;
}
}
}
}
return array(
'not_found_keywords' => $notFoundKeywords,
'body' => $body,
);
}
// Plan.io Task #4518
public function constructBodyForClientAccount($template, $client, $mission, $object)
{
// Get body
$body = $template->getBody();
// Get keywords
$keywords = $template->getKeywords();
// Match keywords for each entity
$entityRep = $this->em->getRepository(Entity::class);
// Mission (can be null)
if ($mission !== null)
{
$entity = $entityRep->findOneByEntity(Entity::MISSION);
$result = $this->matchKeywordsForClientAccount($body, $keywords, $entity, $mission);
$body = $result['body'];
}
// Individual
$entity = $entityRep->findOneByEntity(Entity::INDIVIDUAL);
if ($client !== null && $client->getIndividual() !== null)
{
$result = $this->matchKeywordsForClientAccount($body, $keywords, $entity, $client->getIndividual());
$body = $result['body'];
}
// Get Entity based on the object
$entity = null;
// Devis
if ($object instanceof Devis)
{
$entity = $entityRep->findOneByEntity(Entity::DEVIS);
}
elseif ($object instanceof Invoice)
{
$entity = $entityRep->findOneByEntity(Entity::INVOICE);
}
elseif ($object instanceof Task)
{
$entity = $entityRep->findOneByEntity(Entity::TASK);
}
elseif ($object instanceof IkeaServiceOrder)
{
$entity = null;
}
if ($entity !== null)
{
$result = $this->matchKeywordsForClientAccount($body, $keywords, $entity, $object);
$body = $result['body'];
}
// New line fix
$body = str_replace("\n", "<br>", $body);
return $body;
}
}