fix(automations): always append app name footer to SMS with fallbacks

Resolve display name from name_fa/name/name_en, skip duplicate lines, and support %نام_اپ placeholder in templates.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-08-24 21:57:20 +03:30
co-authored by Cursor
parent f5e66bf951
commit 9bb0fa3a1d
+30 -5
View File
@@ -113,38 +113,63 @@ class AutomationSender
private function ensureAppNameFooter(string $message): string private function ensureAppNameFooter(string $message): string
{ {
$appName = trim($this->config->getString('name_fa')); $appName = $this->resolveAppDisplayName();
$trimmed = rtrim($message); $trimmed = rtrim($message);
if ($appName === '') { if ($appName === '') {
return $trimmed; return $trimmed;
} }
$lines = preg_split('/\R/u', $trimmed) ?: []; if ($this->messageContainsAppName($trimmed, $appName)) {
$last = trim((string) end($lines));
if ($last === $appName) {
return $trimmed; return $trimmed;
} }
return $trimmed === '' ? $appName : ($trimmed."\n".$appName); return $trimmed === '' ? $appName : ($trimmed."\n".$appName);
} }
private function resolveAppDisplayName(): string
{
foreach (['name_fa', 'name', 'name_en'] as $key) {
$value = trim($this->config->getString($key));
if ($value !== '') {
return $value;
}
}
return '';
}
private function messageContainsAppName(string $message, string $appName): bool
{
$lines = preg_split('/\R/u', $message) ?: [];
foreach ($lines as $line) {
if (trim((string) $line) === $appName) {
return true;
}
}
return false;
}
private function render(string $text, Customer $customer): string private function render(string $text, Customer $customer): string
{ {
$name = trim((string) ($customer->name ?: trim(($customer->fname ?? '').' '.($customer->lname ?? '')))); $name = trim((string) ($customer->name ?: trim(($customer->fname ?? '').' '.($customer->lname ?? ''))));
$fname = (string) ($customer->fname ?? ''); $fname = (string) ($customer->fname ?? '');
$mobile = (string) ($customer->mobile ?? ''); $mobile = (string) ($customer->mobile ?? '');
$appName = $this->resolveAppDisplayName();
// سازگاری با قالب قدیمی {{name}} // سازگاری با قالب قدیمی {{name}}
$out = strtr($text, [ $out = strtr($text, [
'{{name}}' => $name, '{{name}}' => $name,
'{{fname}}' => $fname, '{{fname}}' => $fname,
'{{mobile}}' => $mobile, '{{mobile}}' => $mobile,
'{{app_name}}' => $appName,
]); ]);
// الگوی پیامک/تنظیمات: %نام ، ٪نام ، % name ، %name% // الگوی پیامک/تنظیمات: %نام ، ٪نام ، % name ، %name%
// نام_کوچک قبل از نام — وگرنه %نام داخل %نام_کوچک زودتر match می‌شود // نام_کوچک قبل از نام — وگرنه %نام داخل %نام_کوچک زودتر match می‌شود
$out = preg_replace('/[%٪]\s*(?:fname|نام_کوچک|نام‌کوچک)[%٪]?/iu', $fname, $out) ?? $out; $out = preg_replace('/[%٪]\s*(?:fname|نام_کوچک|نام‌کوچک)[%٪]?/iu', $fname, $out) ?? $out;
$out = preg_replace('/[%٪]\s*(?:name|نام)(?![_‌]?کوچک)[%٪]?/iu', $name, $out) ?? $out; $out = preg_replace('/[%٪]\s*(?:app_name|نام_اپ|نام‌اپ)[%٪]?/iu', $appName, $out) ?? $out;
$out = preg_replace('/[%٪]\s*(?:name|نام)(?![_‌]?کوچک)(?![_‌]?اپ)[%٪]?/iu', $name, $out) ?? $out;
$out = preg_replace('/[%٪]\s*(?:mobile|موبایل)[%٪]?/iu', $mobile, $out) ?? $out; $out = preg_replace('/[%٪]\s*(?:mobile|موبایل)[%٪]?/iu', $mobile, $out) ?? $out;
return $out; return $out;