Skip to content

EspoCRM – Open Source CRM Application

Email template processor refactoring

Yurii5a89c8158105
diff --git a/application/Espo/Tools/EmailTemplate/Processor.php b/application/Espo/Tools/EmailTemplate/Processor.php
index 8dc976a6e1..b447c52221 100644
--- a/application/Espo/Tools/EmailTemplate/Processor.php
+++ b/application/Espo/Tools/EmailTemplate/Processor.php
@@ -104,14 +104,18 @@ class Processor
}
}
+ $pairs = [];
+
foreach ($entityHash as $type => $entity) {
$subject = $this->processText(
type: $type,
entity: $entity,
text: $subject,
+ pairs: $pairs,
user: $user,
skipAcl: !$params->applyAcl(),
isHtml: $template->isHtml(),
+
);
}
@@ -120,12 +124,16 @@ class Processor
type: $type,
entity: $entity,
text: $body,
+ pairs: $pairs,
user: $user,
skipAcl: !$params->applyAcl(),
isHtml: $template->isHtml(),
);
}
+ $subject = strtr($subject, $pairs);
+ $body = strtr($body, $pairs);
+
$subject = $this->processPlaceholders($subject, $data);
$body = $this->processPlaceholders($body, $data);
@@ -145,19 +153,25 @@ class Processor
private function processPlaceholders(string $text, Data $data): string
{
+ $pairs = [];
+
foreach ($this->placeholdersProvider->get() as [$key, $placeholder]) {
- $value = $placeholder->get($data);
+ $from = '{' . $key . '}';
- $text = str_replace('{' . $key . '}', $value, $text);
+ $pairs[$from] = $placeholder->get($data);
}
- return $text;
+ return strtr($text, $pairs);
}
+ /**
+ * @param array<string, string> $pairs
+ */
private function processText(
string $type,
Entity $entity,
string $text,
+ array &$pairs,
User $user,
bool $skipLinks = false,
?string $prefixLink = null,
@@ -208,7 +222,13 @@ class Processor
$variableName = "$prefixLink.$attribute";
}
- $text = str_replace("{{$type}.$variableName}", $value, $text);
+ $placeholder = "{{$type}.$variableName}";
+
+ if (array_key_exists($placeholder, $pairs)) {
+ continue;
+ }
+
+ $pairs[$placeholder] = $value;
}
if (!$skipLinks && $entity->hasId()) {
@@ -216,6 +236,7 @@ class Processor
type: $type,
entity: $entity,
text: $text,
+ pairs: $pairs,
user: $user,
skipAcl: $skipAcl,
isHtml: $isHtml,
@@ -225,10 +246,14 @@ class Processor
return $text;
}
+ /**
+ * @param array<string, string> $pairs
+ */
private function processLinks(
string $type,
Entity $entity,
string $text,
+ array &$pairs,
User $user,
bool $skipAcl,
bool $isHtml,
@@ -292,6 +317,7 @@ class Processor
type: $type,
entity: $relatedEntity,
text: $text,
+ pairs: $pairs,
user: $user,
skipLinks: true,
prefixLink: $relation,
diff --git a/tests/integration/Espo/Tools/EmailTemplate/ProcessorTest.php b/tests/integration/Espo/Tools/EmailTemplate/ProcessorTest.php
index a90bd9cd9b..cc5e65291a 100644
--- a/tests/integration/Espo/Tools/EmailTemplate/ProcessorTest.php
+++ b/tests/integration/Espo/Tools/EmailTemplate/ProcessorTest.php
@@ -29,8 +29,12 @@
namespace integration\Espo\Tools\EmailTemplate;
+use DateTimeImmutable;
+use Espo\Core\Binding\Binder;
+use Espo\Core\Binding\BindingProcessor;
use Espo\Core\Field\Link;
use Espo\Core\Field\LinkMultiple;
+use Espo\Core\Utils\DateTime\Clock;
use Espo\Entities\EmailTemplate;
use Espo\Entities\Team;
use Espo\Entities\User;
@@ -148,6 +152,31 @@ class ProcessorTest extends BaseTestCase
public function testProcessAndCleanup(): void
{
+ $this->setConfigParams([
+ 'dateFormat' => 'MM/DD/YYYY',
+ ]);
+
+ $clock = $this->createMock(Clock::class);
+ $clock->method('now')
+ ->willReturn(new DateTimeImmutable('2030-01-02 00:00'));
+
+ $this->setApplication(
+ $this->createApplication(
+ binding: new class ($clock) implements BindingProcessor {
+
+ public function __construct(private Clock $clock) {}
+
+ public function process(Binder $binder): void
+ {
+ $binder->bindInstance(Clock::class, $this->clock);
+ }
+ },
+ reuse: true,
+ )
+ );
+
+ //
+
$em = $this->getEntityManager();
$account = $em->getRDBRepositoryByClass(Account::class)->getNew();
@@ -162,7 +191,7 @@ class ProcessorTest extends BaseTestCase
$template1 = $em->getRDBRepositoryByClass(EmailTemplate::class)->getNew();
$template1->setMultiple([
'subject' => 'Test {Person.firstName} test {Account.name}',
- 'body' => 'Test {Person.name} test {Account.name}.',
+ 'body' => 'Test {Person.name} test {Account.name} {today}.',
]);
$em->saveEntity($template1);
@@ -185,7 +214,7 @@ class ProcessorTest extends BaseTestCase
);
$this->assertEquals(
- 'Test test Account 1.',
+ 'Test test Account 1 01/02/2030.',
$emailData1->getBody(),
);
@@ -199,7 +228,7 @@ class ProcessorTest extends BaseTestCase
);
$this->assertEquals(
- 'Test One test {Account.name}.',
+ 'Test One test {Account.name} 01/02/2030.',
$emailData2->getBody(),
);
}