Files
alrahma_sunday_school_api/app/Models/EmailTemplate.php
T
2026-03-09 16:03:16 -04:00

68 lines
1.6 KiB
PHP
Executable File

<?php
namespace App\Models;
use App\Models\BaseModel;
class EmailTemplate extends BaseModel
{
protected $table = 'email_templates';
// CI model didn't specify timestamps; keep off unless your table has created_at/updated_at.
public $timestamps = false;
protected $fillable = [
'code',
'variant',
'subject',
'body_html',
'is_active',
'updated_by',
'updated_at',
];
protected $casts = [
'is_active' => 'boolean',
'updated_by' => 'integer',
];
public static function getTemplate(string $code, string $variant = 'default'): ?self
{
$row = static::query()
->where('code', $code)
->where('is_active', 1)
->where(function ($q) use ($variant) {
$q->where('variant', $variant)
->orWhere('variant', 'default');
})
->orderByRaw("CASE WHEN variant = ? THEN 0 ELSE 1 END", [$variant])
->first();
return $row;
}
/**
* Equivalent of CI getActiveTemplates():
* is_active=1, orderBy template_key ASC
*/
public static function getActiveTemplates()
{
return static::query()
->where('is_active', 1)
->orderBy('template_key', 'asc')
->get();
}
/**
* Equivalent of CI findByKey():
* template_key = $key AND is_active=1
*/
public static function findByKey(string $key): ?self
{
return static::query()
->where('template_key', $key)
->where('is_active', 1)
->first();
}
}