-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotification.php
More file actions
116 lines (114 loc) · 4.63 KB
/
Notification.php
File metadata and controls
116 lines (114 loc) · 4.63 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
<?php
/*
Used to create banner notifications.
*/
class Notification {
/*
* Types of notifications:
* ERROR something went wrong, and you need to know.
* SUCCESS something went right, and I want you to know.
* WARNING You're flirting with danger, there.
* INFO something happened, and I just feel like you might want to know.
*/
private $error, $success, $warning, $info = null;
private $defaultError = 'Error: Something went wrong.';
private $defaultWarning = 'warning: An issue arose, but we don\'t know what.';
private $defaultInfo = 'info: Something happened, and we figure you should know.';
private $defaultSuccess = 'success: something good happened.';
public function __construct(){}
// public function __construct(String $type, String $message){
// $this->addNote($type, $message);
// }
/*
add a single note. multiple notes of the same type will be appended to the same notification box, and will start on a new line. If you don't want to write a message, then just omit the message, and the default will be used.
addNote('success', 'Success: You are now logged in.');
addNote('success'); // will display a default message for the given type.
*/
public function addNote(String $type,String $message = ''){
$type = trim(strtolower($type));
$message = trim(strtolower($message));
switch($type){
case 'error':
//provide a default type of message if there is none.
if(isset($this->error)){
$this->error .= '<div class="error"><span class="closebtn" onclick="this.parentElement.style.display=\'none\';">×</span>' . $message . '</div>';
}else{
$this->error = '<div class="error"><span class="closebtn" onclick="this.parentElement.style.display=\'none\';">×</span>' . $message . '</div>';
}
break;
case 'warning':
//provide a default type of message if there is none.
if(isset($this->warning)){
$this->warning .= '<div class="warning"><span class="closebtn" onclick="this.parentElement.style.display=\'none\';">×</span>' . $message . '</div>';
}else{
$this->warning = '<div class="warning"><span class="closebtn" onclick="this.parentElement.style.display=\'none\';">×</span>' . $message . '</div>';
}
break;
case 'info':
//provide a default type of message if there is none.
if(isset($this->info)){
$this->info .= '<div class="info"><span class="closebtn" onclick="this.parentElement.style.display=\'none\';">×</span>' . $message . '</div>';
}else{
$this->info = '<div class="info"><span class="closebtn" onclick="this.parentElement.style.display=\'none\';">×</span>' . $message . '</div>';
}
break;
case 'success':
//provide a default type of message if there is none.
if(isset($this->success)){
$this->success .= '<div class="success"><span class="closebtn" onclick="this.parentElement.style.display=\'none\';">×</span>' . $message . '</div>';
}else{
$this->success = '<div class="success"><span class="closebtn" onclick="this.parentElement.style.display=\'none\';">×</span>' . $message . '</div>';
}
break;
default:
throw new Exception('Invalid notification type.');
//log the error using whatever means you so desire.
$this->error = 'ERROR: Something happened, and the notification type wasn\'t set correctly. Here is the message: ' . $message;
}
}
//use this anytime you want to display any notes that MAY have come up.
public function notify(){
if (isset($this->error)) {
echo $this->error;
$this->error = null;
unset($this->error);
}
if (isset($this->success)) {
echo $this->success;
$this->success = null;
unset($this->success);
} if (isset($this->warning)) {
echo $this->warning;
$this->warning = null;
unset($this->warning);
} if (isset($this->info)) {
echo $this->info;
$this->info = null;
unset($this->info);
}
}
public function getDefaultError(){
return $this->defaultError;
}
public function getDefaultWarning(){
return $this->defaultWarning;
}
public function getDefaultInfo(){
return $this->defaultInfo;
}
public function getDefaultSuccess(){
return $this->defaultSuccess;
}
public function setDefaultError(String $message): void{
$this->defaultError = $message;
}
public function setDefaultWarning(String $message): void{
$this->defaultWarning = $message;
}
public function setDefaultInfo(String $message): void{
$this->defaultInfo = $message;
}
public function setDefaultSuccess(String $message): void{
$this->defaultSuccess = $message;
}
}