Files
alrahma_sunday_school/app/Database/Migrations/2026-07-30-100000_CreateRegistrationOpeningEmailTemplate.php
root 81a72a4c59
Tests / PHPUnit (push) Failing after 34s
fix school year and related issues
2026-07-30 00:48:54 -04:00

68 lines
2.0 KiB
PHP

<?php
namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
class CreateRegistrationOpeningEmailTemplate extends Migration
{
public function up(): void
{
if (! $this->db->tableExists('email_templates')) {
return;
}
$fields = $this->db->getFieldNames('email_templates');
$keyField = in_array('code', $fields, true) ? 'code' : 'template_key';
$bodyField = in_array('body_html', $fields, true) ? 'body_html' : 'body';
$nameField = in_array('name', $fields, true) ? 'name' : null;
$exists = $this->db->table('email_templates')
->where($keyField, 'registration_opening')
->countAllResults() > 0;
if ($exists) {
return;
}
$data = [
$keyField => 'registration_opening',
'subject' => 'Registration is now open for {{school_year}}',
$bodyField => $this->bodyHtml(),
'is_active' => 1,
];
if ($nameField !== null) {
$data[$nameField] = 'Registration Opening';
}
$this->db->table('email_templates')->insert($data);
}
public function down(): void
{
if (! $this->db->tableExists('email_templates')) {
return;
}
$fields = $this->db->getFieldNames('email_templates');
$keyField = in_array('code', $fields, true) ? 'code' : 'template_key';
$this->db->table('email_templates')
->where($keyField, 'registration_opening')
->delete();
}
private function bodyHtml(): string
{
return <<<'HTML'
<p>Dear {{name}},</p>
<p>Registration is now open for the {{school_year}} school year.</p>
<p>Please complete your registration through the school portal. The registration deadline is {{registration_deadline}}.</p>
<p><a href="{{registration_url}}">Start registration</a></p>
<p>If you already have an account, you can also log in here: <a href="{{login_url}}">{{login_url}}</a></p>
<p>Regards,<br>{{school_name}}</p>
HTML;
}
}