Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mail notification blocks #1693

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 47 additions & 2 deletions Core/Lib/Email/MailNotifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ class MailNotifier
public static function getText(string $text, array $params): string
{
foreach ($params as $key => $value) {
$text = str_replace('{' . $key . '}', $value, $text);
if (is_string($value)) {
$text = str_replace('{' . $key . '}', $value, $text);
}
}

return $text;
Expand Down Expand Up @@ -81,6 +83,7 @@ public static function send(string $notificationName, string $email, string $nam
$newMail->to($email, $name);
$newMail->title = static::getText($notification->subject, $params);
$newMail->text = static::getText($notification->body, $params);
static::replaceTextToBlock($newMail, $params);

foreach ($mainBlocks as $block) {
$newMail->addMainBlock($block);
Expand All @@ -96,4 +99,46 @@ public static function send(string $notificationName, string $email, string $nam

return $newMail->send();
}
}

protected static function replaceTextToBlock(DinNewMail &$newMail, array $params): void
{
// si no hay parámetros o texto, no hacemos nada
if (empty($params) || empty($newMail->text)) {
return;
}

// obtenemos las coincidencias de {block1}, {block2}, ... sobre el texto
preg_match_all('/{block(\d+)}/', $newMail->text, $matches);

// si no hay coincidencias, no hacemos nada
if (empty($matches[1])) {
return;
}

// obtenemos el texto hasta el primer bloque, y entre los bloques
// para añadir un TextBlock con el texto encontrado
$text = $newMail->text;
$newMail->text = '';
$lastPos = 0;

// recorremos los bloques encontrados
foreach ($matches[1] as $index => $blockIndex) {
$substr = substr($text, $lastPos, strpos($text, '{block' . $blockIndex . '}') - $lastPos);
$lastPos = strpos($text, '{block' . $blockIndex . '}') + strlen('{block' . $blockIndex . '}');
if (empty($substr) && isset($params['block' . $blockIndex]) && $params['block' . $blockIndex] instanceof BaseBlock) {
$newMail->addMainBlock($params['block' . $blockIndex]);
continue;
}

$newMail->addMainBlock(new TextBlock($substr));
if (isset($params['block' . $blockIndex]) && $params['block' . $blockIndex] instanceof BaseBlock) {
$newMail->addMainBlock($params['block' . $blockIndex]);
}
}

$substr = substr($text, $lastPos);
if (false === empty($substr)) {
$newMail->addMainBlock(new TextBlock($substr));
}
}
}