add email sent for event
This commit is contained in:
@@ -12,8 +12,10 @@ use App\Models\EnrollmentModel;
|
||||
use App\Models\InvoiceModel;
|
||||
use App\Models\InvoiceEventModel;
|
||||
use App\Models\ClassSectionModel;
|
||||
use App\Models\ParentModel;
|
||||
use App\Models\PaymentModel;
|
||||
use App\Models\CalendarModel;
|
||||
use App\Services\EmailService;
|
||||
use Config\Database;
|
||||
#use ???
|
||||
use App\Controllers\View\InvoiceController;
|
||||
@@ -32,6 +34,8 @@ class EventController extends ResourceController
|
||||
protected $studentClassModel;
|
||||
protected $classSectionModel;
|
||||
protected $paymentModel;
|
||||
protected $parentModel;
|
||||
protected $emailService;
|
||||
protected $schoolYear;
|
||||
protected $semester;
|
||||
protected $categories;
|
||||
@@ -52,6 +56,8 @@ class EventController extends ResourceController
|
||||
$this->classSectionModel = new ClassSectionModel();
|
||||
$this->paymentModel = new PaymentModel();
|
||||
$this->enrollmentModel = new EnrollmentModel();
|
||||
$this->parentModel = new ParentModel();
|
||||
$this->emailService = new EmailService();
|
||||
|
||||
$this->schoolYear = $this->configModel->getConfig('school_year');
|
||||
$this->semester = $this->configModel->getConfig('semester');
|
||||
@@ -167,7 +173,13 @@ class EventController extends ResourceController
|
||||
}
|
||||
}
|
||||
|
||||
return redirect()->to('/administrator/events')->with('success', 'Event created successfully');
|
||||
$redirect = redirect()->to('/administrator/events')->with('success', 'Event created successfully');
|
||||
if ($eventId && $this->request->getPost('send_email_parent')) {
|
||||
$emailStatus = $this->broadcastEventToParents((int) $eventId);
|
||||
$redirect = $this->applyEventEmailFlash($redirect, $emailStatus);
|
||||
}
|
||||
|
||||
return $redirect;
|
||||
}
|
||||
|
||||
return view('administrator/events/create_event', [
|
||||
@@ -208,7 +220,12 @@ class EventController extends ResourceController
|
||||
]);
|
||||
|
||||
if ($updated) {
|
||||
return redirect()->to('/administrator/events')->with('success', 'Event updated successfully');
|
||||
$redirect = redirect()->to('/administrator/events')->with('success', 'Event updated successfully');
|
||||
if ($this->request->getPost('send_email_parent')) {
|
||||
$emailStatus = $this->broadcastEventToParents((int) $id);
|
||||
$redirect = $this->applyEventEmailFlash($redirect, $emailStatus);
|
||||
}
|
||||
return $redirect;
|
||||
} else {
|
||||
log_message('debug', 'GET detected');
|
||||
return redirect()->back()->with('error', 'Failed to update event');
|
||||
@@ -221,6 +238,106 @@ class EventController extends ResourceController
|
||||
]);
|
||||
}
|
||||
|
||||
private function broadcastEventToParents(int $eventId): array
|
||||
{
|
||||
$event = $this->eventModel->find($eventId);
|
||||
if (!$event) {
|
||||
return ['ok' => false, 'count' => 0, 'message' => 'Event email was skipped because the event could not be loaded.'];
|
||||
}
|
||||
|
||||
$emails = $this->collectParentBroadcastEmails((string) ($event['school_year'] ?? ''));
|
||||
if ($emails === []) {
|
||||
return ['ok' => false, 'count' => 0, 'message' => 'Event email was skipped because no parent email addresses were found.'];
|
||||
}
|
||||
|
||||
$subject = 'School Event: ' . trim((string) ($event['event_name'] ?? 'Upcoming Event'));
|
||||
$innerBody = view('emails/event_broadcast', [
|
||||
'event' => $event,
|
||||
'eventUrl' => base_url('parent/events'),
|
||||
'flyerUrl' => !empty($event['flyer']) ? base_url('uploads/' . ltrim((string) $event['flyer'], '/')) : null,
|
||||
]);
|
||||
$body = view('emails/_wrap_layout', [
|
||||
'title' => $subject,
|
||||
'subject' => $subject,
|
||||
'body_html' => $innerBody,
|
||||
]);
|
||||
|
||||
$ok = $this->emailService->sendCc($emails, $subject, $body, 'general');
|
||||
$batchCount = $this->emailService->getLastCcBatchCount();
|
||||
|
||||
return [
|
||||
'ok' => $ok,
|
||||
'count' => count($emails),
|
||||
'batches' => $batchCount,
|
||||
'message' => $ok
|
||||
? 'Event email was sent in ' . $batchCount . ' CC batch(es) for ' . count($emails) . ' recipient(s).'
|
||||
: 'The event was saved, but sending the parent CC email failed.',
|
||||
];
|
||||
}
|
||||
|
||||
private function collectParentBroadcastEmails(string $schoolYear = ''): array
|
||||
{
|
||||
$emails = [];
|
||||
|
||||
$db = Database::connect();
|
||||
|
||||
$parentRole = $db->table('roles')
|
||||
->select('id')
|
||||
->where('LOWER(name)', 'parent')
|
||||
->get()
|
||||
->getRow();
|
||||
|
||||
if ($parentRole) {
|
||||
$firstParentQuery = $db->table('users')
|
||||
->select('users.email')
|
||||
->join('user_roles', 'user_roles.user_id = users.id')
|
||||
->where('user_roles.role_id', (int) $parentRole->id)
|
||||
->where('users.email IS NOT NULL', null, false)
|
||||
->where('users.email !=', '');
|
||||
|
||||
if ($schoolYear !== '' && $db->fieldExists('school_year', 'users')) {
|
||||
$firstParentQuery->where('users.school_year', $schoolYear);
|
||||
}
|
||||
|
||||
foreach ($firstParentQuery->get()->getResultArray() as $row) {
|
||||
$email = trim((string) ($row['email'] ?? ''));
|
||||
if ($email !== '' && filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
||||
$emails[strtolower($email)] = $email;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$secondParentQuery = $db->table('parents')
|
||||
->select('secondparent_email')
|
||||
->where('secondparent_email IS NOT NULL', null, false)
|
||||
->where('secondparent_email !=', '');
|
||||
|
||||
if ($schoolYear !== '' && $db->fieldExists('school_year', 'parents')) {
|
||||
$secondParentQuery->where('school_year', $schoolYear);
|
||||
}
|
||||
|
||||
foreach ($secondParentQuery->get()->getResultArray() as $row) {
|
||||
$email = trim((string) ($row['secondparent_email'] ?? ''));
|
||||
if ($email !== '' && filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
||||
$emails[strtolower($email)] = $email;
|
||||
}
|
||||
}
|
||||
|
||||
return array_values($emails);
|
||||
}
|
||||
|
||||
private function applyEventEmailFlash($redirect, array $emailStatus)
|
||||
{
|
||||
if (!empty($emailStatus['message'])) {
|
||||
return $redirect->with(
|
||||
($emailStatus['ok'] ?? false) ? 'email_status' : 'email_error',
|
||||
$emailStatus['message']
|
||||
);
|
||||
}
|
||||
|
||||
return $redirect;
|
||||
}
|
||||
|
||||
|
||||
public function delete($id = null)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user