-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEndpoint.php
More file actions
120 lines (98 loc) · 4.26 KB
/
Endpoint.php
File metadata and controls
120 lines (98 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
// Import additionnal class into the global namespace
use \LaswitchTech\Core\Base\BaseEndpoint;
class NotificationsEndpoint extends BaseEndpoint {
/**
* Constructor
*/
public function __construct()
{
// Call the parent constructor
parent::__construct();
// Initialize the Endpoint
$this->init('notifications');
// Set Properties
$this->required = ['assignedTo','title','content'];
// Set Access
$this->Public = false;
// Set Properties
switch($this->Request->getNamespace()){
case "/notifications/notify":
$this->Level = 2;
break;
case "/notifications/dismiss":
$this->Level = 3;
break;
}
}
/**
* Create a Notification
*/
public function notifyAction(): array
{
// Set the default message
$message = ["status" => 200, "message" => "OK", "data" => []];
// Retrieve the notifications's id and new stage
$assignedTo = $this->Request->getParams('REQUEST','assignedTo') ?? $this->Auth->user()->id;
$title = $this->Request->getParams('REQUEST','title');
$content = $this->Request->getParams('REQUEST','content');
$link = $this->Request->getParams('REQUEST','link');
$category = $this->Request->getParams('REQUEST','category');
// Check if the parameter exists
if(empty($assignedTo) || is_null($assignedTo)){
$message = ["status" => 400, "message" => "Bad Request", "data" => "The 'assignedTo' parameter is required."];
}
if(empty($title) || is_null($title)){
$message = ["status" => 400, "message" => "Bad Request", "data" => "The 'title' parameter is required."];
}
if(empty($content) || is_null($content)){
$message = ["status" => 400, "message" => "Bad Request", "data" => "The 'content' parameter is required."];
}
// Check if we can proceed
if($message['status'] == 200){
// Check the request method
if($this->Request->getMethod() == "POST"){
// Create the notification
if($this->Model->{$this->name}->notify((int)$assignedTo, (string)$title, (string)$content, (string)$link, (string)$category) > 0){
$message['data'] = "The user has been notified successfully.";
} else {
$message = ["status" => 500, "message" => "Internal Server Error", "data" => "An error occurred while creating the notification."];
}
} else {
$message = ["status" => 405, "message" => "Method Not Allowed", "data" => "The method is not allowed for the requested URL."];
}
}
// Return the message
return $message;
}
/**
* Dismiss a Notification
*/
public function dismissAction(): array
{
// Set the default message
$message = ["status" => 200, "message" => "OK", "data" => []];
// Retrieve the notification's id
$notificationId = $this->Request->getParams('REQUEST','notificationId');
// Check if the parameter exists
if(empty($notificationId) || is_null($notificationId) || (int)$notificationId <= 0){
$message = ["status" => 400, "message" => "Bad Request", "data" => "The 'notificationId' parameter is required."];
}
// Check if we can proceed
if($message['status'] == 200){
// Check the request method
if($this->Request->getMethod() == "POST"){
// Dismiss the notification
if($this->Model->{$this->name}->update((int)$notificationId,['isDismissed' => 1]) > 0){
$message['data'] = "The notification has been dismissed successfully.";
} else {
$message = ["status" => 500, "message" => "Internal Server Error", "data" => "An error occurred while dismissing the notification."];
}
} else {
$message = ["status" => 405, "message" => "Method Not Allowed", "data" => "The method is not allowed for the requested URL."];
}
}
// Return the message
return $message;
}
}