Files
alrahma_sunday_school_api/app/Models/EmailTemplate.php
T
root 3fde161f29
API CI/CD / Validate (composer + pint) (push) Successful in 3m6s
API CI/CD / Test (PHPUnit) (push) Failing after 6m55s
API CI/CD / Build frontend assets (push) Successful in 1m2s
API CI/CD / Security audit (push) Failing after 50s
API CI/CD / Deploy to shared hosting (PHP) (push) Has been skipped
fix failed tests
2026-07-07 04:12:02 -04:00

68 lines
1.6 KiB
PHP

<?php
namespace App\Models;
class EmailTemplate extends BaseModel
{
protected $table = 'email_templates';
// legacy model didn't specify timestamps; keep off unless your table has created_at/updated_at.
public $timestamps = true;
protected $fillable = [
'template_key',
'code',
'variant',
'name',
'subject',
'body',
'body_html',
'is_active',
'updated_by',
];
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 legacy 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 legacy 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();
}
}