You've already forked Atomcms-edit
52 lines
1.1 KiB
PHP
Executable File
52 lines
1.1 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class EmailTemplate extends Model
|
|
{
|
|
protected $table = 'email_templates';
|
|
|
|
protected $fillable = [
|
|
'name',
|
|
'subject',
|
|
'body',
|
|
'variables',
|
|
'is_active',
|
|
];
|
|
|
|
protected $casts = [
|
|
'is_active' => 'boolean',
|
|
];
|
|
|
|
public static function getTemplate(string $name): ?self
|
|
{
|
|
return static::where('name', $name)
|
|
->where('is_active', true)
|
|
->first();
|
|
}
|
|
|
|
public static function render(string $name, array $variables): ?array
|
|
{
|
|
$template = static::getTemplate($name);
|
|
|
|
if (! $template) {
|
|
return null;
|
|
}
|
|
|
|
$subject = $template->subject;
|
|
$body = $template->body;
|
|
|
|
foreach ($variables as $key => $value) {
|
|
$subject = str_replace('{{' . $key . '}}', $value, $subject);
|
|
$body = str_replace('{{' . $key . '}}', $value, $body);
|
|
}
|
|
|
|
return [
|
|
'subject' => $subject,
|
|
'body' => $body,
|
|
];
|
|
}
|
|
}
|