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:
@@ -113,38 +113,63 @@ class AutomationSender
|
||||
|
||||
private function ensureAppNameFooter(string $message): string
|
||||
{
|
||||
$appName = trim($this->config->getString('name_fa'));
|
||||
$appName = $this->resolveAppDisplayName();
|
||||
$trimmed = rtrim($message);
|
||||
if ($appName === '') {
|
||||
return $trimmed;
|
||||
}
|
||||
|
||||
$lines = preg_split('/\R/u', $trimmed) ?: [];
|
||||
$last = trim((string) end($lines));
|
||||
if ($last === $appName) {
|
||||
if ($this->messageContainsAppName($trimmed, $appName)) {
|
||||
return $trimmed;
|
||||
}
|
||||
|
||||
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
|
||||
{
|
||||
$name = trim((string) ($customer->name ?: trim(($customer->fname ?? '').' '.($customer->lname ?? ''))));
|
||||
$fname = (string) ($customer->fname ?? '');
|
||||
$mobile = (string) ($customer->mobile ?? '');
|
||||
$appName = $this->resolveAppDisplayName();
|
||||
|
||||
// سازگاری با قالب قدیمی {{name}}
|
||||
$out = strtr($text, [
|
||||
'{{name}}' => $name,
|
||||
'{{fname}}' => $fname,
|
||||
'{{mobile}}' => $mobile,
|
||||
'{{app_name}}' => $appName,
|
||||
]);
|
||||
|
||||
// الگوی پیامک/تنظیمات: %نام ، ٪نام ، % name ، %name%
|
||||
// نام_کوچک قبل از نام — وگرنه %نام داخل %نام_کوچک زودتر match میشود
|
||||
$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;
|
||||
|
||||
return $out;
|
||||
|
||||
Reference in New Issue
Block a user