Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 109 additions & 3 deletions Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,127 @@ protected function joins(object $Query): object

/**
* Create a Notification
*
* @param string $name
* @param int $assignedTo
* @param string $title
* @param string $content
* @param string|null $link
* @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 .= '<p>This is an automated <strong>'.$category.'</strong> notification.</p>';
$body .= '<p>'.$content.'</p>';
if($link){
$body .= '<p style="text-align:center;margin-top: 40px;margin-bottom:40px;">';
$body .= '<a href="'.$REQUEST->getHostAddress().$link.'" target="_blank" style="margin-left: 6px; margin-right: 6px; text-decoration:none; background-color: #528fb3;color: #fff;font-size: 24px;padding: 20px 40px;text-align: center;margin: 20px 20px;border-radius: 8px;">View Details</a>';
$body .= '</p>';
}
$body .= '<p>If you did not expect this message, please ignore it.</p>';

// 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,<br>".$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;
}
}
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.0.4
v0.0.5
2 changes: 1 addition & 1 deletion info.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down