From 45f8489ca41745b4b704c792f792344fbecb35da Mon Sep 17 00:00:00 2001 From: Louis Ouellet Date: Wed, 22 Oct 2025 16:04:15 -0400 Subject: [PATCH 1/2] General: Version bumped to v0.0.5 --- VERSION | 2 +- info.cfg | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/VERSION b/VERSION index 7df503e..5c314e5 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -v0.0.4 +v0.0.5 diff --git a/info.cfg b/info.cfg index 73bb68b..0c09b06 100644 --- a/info.cfg +++ b/info.cfg @@ -5,7 +5,7 @@ "author": "LaswitchTech", "email": "support@laswitchtech.com", "date": "2025-10-22", - "version": "v0.0.4", + "version": "v0.0.5", "tags": "notifications", "description": "Manage notifications.", "repository": "https://github.com/LaswitchTech/core-plugin-notifications", From 4dafb9af47e0d56cc55b28862a2518935cf761d8 Mon Sep 17 00:00:00 2001 From: Louis Ouellet Date: Fri, 28 Nov 2025 10:09:30 -0500 Subject: [PATCH 2/2] General: Implemented notify. --- Model.php | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 109 insertions(+), 3 deletions(-) diff --git a/Model.php b/Model.php index d777483..9c91137 100644 --- a/Model.php +++ b/Model.php @@ -33,6 +33,8 @@ protected function joins(object $Query): object /** * Create a Notification + * + * @param string $name * @param int $assignedTo * @param string $title * @param string $content @@ -40,14 +42,118 @@ protected function joins(object $Query): object * @param string $category * @return int */ - public function notify(int $assignedTo, string $title, string $content, string $link = null, string $category = "System"): int + public function notify(string $name, int $assignedTo, string $title, string $content, ?string $link = null, string $category = "System"): int { - return $this->create([ + // Import Global Variables + global $CONFIG, $REQUEST, $SMTP, $AUTH; + + // Initialize Status + $status = 0; + + // Import the Users Model + require_once $CONFIG->root() . '/lib/plugins/users/Model.php'; + + // Initialize the Users Model + $Model = new UsersModel(); + + // Retrieve the Assigned User + $user = $Model->fetch($assignedTo); + + // Retrieve the Notification Settings + $settings = $user['settings']['notifications'] ?? []; + + // Check if the notification settings exist + if (!isset($settings[$name])) { + $settings[$name] = [ + 'email' => false, + 'sms' => false, + 'push' => true + ]; + $Model->update($assignedTo, [ + 'settings' => json_encode(array_merge($user['settings'], [ + 'notifications' => $settings + ])) + ]); + } + + // Allowed Notifications + $allowed = $settings[$name] ?? []; + + // Set the Notification + $notification = [ 'assignedTo' => $assignedTo, 'title' => $title, 'content' => $content, 'link' => $link, 'category' => $category, - ]); + ]; + + // Check if we can create the notification + if(in_array('push', $allowed) && $allowed['push']){ + + // Created Notification + if($this->create($notification)){ + $status += 1; + } + } + + // Check if we can send the notification via email + if(in_array('email', $allowed) && $allowed['email']){ + + // Connect to the smtp server + $SMTP->connect(); + + // Check if the smtp server is connected + if($SMTP->isConnected()){ + + // Authenticate to the SMTP Server + $SMTP->authenticate(); + + // Check if the SMTP Server is authenticated + if($SMTP->isAuthenticated()){ + + // Write the email + $body = ''; + $body .= '

This is an automated '.$category.' notification.

'; + $body .= '

'.$content.'

'; + if($link){ + $body .= '

'; + $body .= 'View Details'; + $body .= '

'; + } + $body .= '

If you did not expect this message, please ignore it.

'; + + // Create a new message + $eml = $SMTP->message() + ->to($user['username']) + ->from($AUTH->user()->organization()->email ?? $CONFIG->get('smtp','username')) + ->subject($category.' - '.$title) + ->body($body) + ->var('greetings', "Sincerely,
".$AUTH->user()->organization()->name."'s Team"); + + // Send the message + $eml->send(); + + // Check if the message was sent + if($eml->status()){ + + // Save the message + $eml->save(); + + // Update status + $status += 1; + } + } + } + } + + // Check if we can send the notification via sms + if(in_array('sms', $allowed) && $allowed['sms']){ + // Send SMS Notification + // (SMS sending logic would go here) + } + + // Return status + return $status; } }