This commit is contained in:
baodev0
2026-10-05 09:30:24 +07:00
parent a1aeb88061
commit 1544629a1f
53 changed files with 1292 additions and 963 deletions
+32 -42
View File
@@ -3,6 +3,8 @@
namespace App\Models;
use App\Support\EmailTemplateShell;
use App\Support\MailGlobals;
use App\Support\MailPlaceholders;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
@@ -57,54 +59,42 @@ class Template extends Model
return collect($this->metadata['variables'] ?? [])->keyBy('key')->all();
}
/**
* @param array<int, array{key: string, label?: string|null, example?: string|null}> $overrides
*/
public function syncVariables(array $overrides = []): void
{
$declared = $this->variables();
$used = MailPlaceholders::extract($this->subject, $this->message);
$overrides = collect($overrides)->keyBy('key');
$variables = collect(array_unique([...array_keys($declared), ...$used]))
->reject(fn (string $key) => ($declared[$key]['custom'] ?? false) && !in_array($key, $used, true))
->map(function (string $key) use ($declared, $overrides) {
$variable = $declared[$key] ?? ['key' => $key, 'label' => MailGlobals::LABELS[$key] ?? $key, 'type' => 'string', 'custom' => true];
$override = $overrides->get($key, []);
if (filled($override['label'] ?? null)) {
$variable['label'] = trim($override['label']);
}
if (array_key_exists('example', $override)) {
$variable['example'] = (string) $override['example'];
}
return $variable;
})
->values()
->all();
$this->metadata = [...($this->metadata ?? []), 'variables' => $variables];
}
/** Mail sender config khai báo trong metadata.mail (from_email, from_name, reply_to, reply_to_name, cc, bcc). */
public function mailConfig(): array
{
return $this->metadata['mail'] ?? [];
}
public const SCENARIOS = [
'layout' => 'Bố cục Header/Footer (mặc định)',
'vm_credentials' => 'Khởi tạo dịch vụ',
'renewal_reminder' => 'Nhắc gia hạn',
'news_notification' => 'Thông báo tin tức',
'invoice_pdf' => 'Hoá đơn điện tử',
'vm_suspended' => 'VPS bị tạm ngưng',
'domain_order_received' => 'Đơn đăng ký tên miền đã ghi nhận',
'hosting_account_created' => 'Hosting đã khởi tạo',
'manual_deposit_status' => 'Trạng thái nạp tiền thủ công',
'order_payment_success' => 'Thanh toán đơn hàng thành công',
'password_reset_admin' => 'Admin cấp lại mật khẩu',
'service_renewal_reminder'=> 'Nhắc gia hạn dịch vụ',
'snapshot_action' => 'Thao tác Snapshot VPS',
'ticket_created' => 'Ticket hỗ trợ mới',
'verify_account_welcome' => 'Mã xác thực tài khoản',
'vm_creation_failed' => 'Khởi tạo VPS thất bại',
'vm_password_changed' => 'Đổi mật khẩu VPS',
'vm_rebuild_failed' => 'Rebuild VPS thất bại',
'domain_order_approved' => 'Tên miền được duyệt',
'domain_order_rejected' => 'Tên miền bị từ chối',
'order_created' => 'Xác nhận đơn hàng',
'admin_order_notification' => '[Admin] Đơn hàng mới',
'admin_domain_order_new' => '[Admin] Đơn đăng ký tên miền mới',
'admin_hosting_account_created' => '[Admin] Hosting account mới',
'admin_manual_deposit_pending' => '[Admin] Yêu cầu nạp tiền chờ duyệt',
'admin_ticket_notification' => '[Admin] Ticket hỗ trợ mới',
'admin_trial_registration_request' => '[Admin] Yêu cầu dùng thử VPS',
'customer_deposit_success' => 'Nạp tiền thành công',
'customer_welcome' => 'Chào mừng khách hàng mới',
'vm_port_change' => 'Đổi port Remote VPS',
'vm_password_reset_success' => 'Đặt lại mật khẩu VPS thành công',
'customer_signup_verify' => 'Xác thực đăng ký tài khoản',
'hosting_information' => 'Thông tin truy cập Hosting',
'customer_password_reset' => 'Khôi phục mật khẩu khách hàng',
'customer_password_reset_link' => 'Link đặt lại mật khẩu khách hàng',
'service_expiry' => 'Dịch vụ sắp hết hạn',
'customer_payment' => 'Thông báo thanh toán khách hàng',
'invoice_payment_reminder' => 'Nhắc thanh toán hoá đơn',
'invoice_notification' => 'Thông báo hoá đơn / gia hạn cước',
];
public function invoiceTemplate()
{
return $this->belongsTo(self::class, 'invoice_template_id');