Files
portal-api/app/Models/Template.php
T
2026-09-09 16:56:22 +07:00

110 lines
4.3 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
class Template extends Model
{
use HasFactory;
public const CACHE_PREFIX = 'email_template:';
protected $table = 'template';
protected $fillable = [
'type_id',
'scenario',
'format',
'invoice_template_id',
'name',
'subject',
'message',
'metadata',
'builder_data',
'status',
];
protected $casts = [
'status' => 'integer',
'metadata' => 'array',
'builder_data' => 'array',
];
protected static function booted(): void
{
static::saved(fn (self $template) => $template->forgetCache());
static::deleted(fn (self $template) => $template->forgetCache());
}
public function forgetCache(): void
{
if ($this->scenario) {
Cache::forget(self::CACHE_PREFIX . $this->scenario);
}
}
/** Danh sách variable hợp lệ khai báo trong metadata.variables, key theo variable key. */
public function variables(): array
{
return collect($this->metadata['variables'] ?? [])->keyBy('key')->all();
}
/** 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 = [
'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',
'holiday_notice' => 'Thông báo nghỉ lễ',
'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',
'service_expiry' => 'Dịch vụ sắp hết hạn',
'customer_payment' => 'Thông báo thanh toán khách hàng',
];
public function invoiceTemplate()
{
return $this->belongsTo(self::class, 'invoice_template_id');
}
public function type()
{
return $this->belongsTo(\App\Models\Type::class);
}
}