This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user