1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Padosoft\Laravel\Notification\Notifier; |
4
|
|
|
|
5
|
|
|
use Illuminate\Session\SessionManager; |
6
|
|
|
|
7
|
|
|
class Notifier |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Added notifications |
12
|
|
|
* |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
protected $notifications = []; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Added notifications |
19
|
|
|
* |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $options = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Illuminate Session |
26
|
|
|
* |
27
|
|
|
* @var \Illuminate\Session\SessionManager |
28
|
|
|
*/ |
29
|
|
|
protected $session; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Constructor |
33
|
|
|
* |
34
|
|
|
* @param \Illuminate\Session\SessionManager $session |
35
|
|
|
* |
36
|
|
|
* @internal param \Illuminate\Session\SessionManager $session |
37
|
|
|
*/ |
38
|
|
|
public function __construct(SessionManager $session) |
39
|
|
|
{ |
40
|
|
|
$this->session = $session; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Render all the notifications' script to insert into script tag |
45
|
|
|
* |
46
|
|
|
* @return string |
47
|
|
|
* |
48
|
|
|
*/ |
49
|
|
|
public function render() : string |
50
|
|
|
{ |
51
|
|
|
$notifications = $this->session->get('laravel::notifications'); |
52
|
|
|
if (!$notifications) { |
53
|
|
|
$notifications = []; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$output = ''; |
57
|
|
|
|
58
|
|
|
foreach ($notifications as $notification) { |
59
|
|
|
|
60
|
|
|
$output .= $this->renderNotification($notification); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $output; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Render the script of given notification to insert into script tag |
68
|
|
|
* |
69
|
|
|
* @param $notification |
70
|
|
|
* @return string |
71
|
|
|
*/ |
72
|
|
|
public function renderNotification($notification): string |
73
|
|
|
{ |
74
|
|
|
$title = (isset($notification['title']) ? str_replace("'", "\\'", |
75
|
|
|
htmlentities($notification['title'])) : null) ; |
76
|
|
|
$message = $notification['message']; |
77
|
|
|
$type = $notification['type']; |
78
|
|
|
|
79
|
|
|
$output = " |
80
|
|
|
$(function () { |
81
|
|
|
new Notifier({ |
82
|
|
|
title: '" .$title. "', |
83
|
|
|
text: '" . $message . "', |
84
|
|
|
type: '" . $type . "' |
85
|
|
|
}); |
86
|
|
|
});"; |
87
|
|
|
|
88
|
|
|
return $output; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Add a notification |
93
|
|
|
* |
94
|
|
|
* @param string $type Could be error, info, success, or warning. |
95
|
|
|
* @param string $message The notification's message |
96
|
|
|
* @param string $title The notification's title |
97
|
|
|
* @param array $options |
98
|
|
|
* @param bool $onlyNextRequest if true(default), se the notification in session only for the next request |
99
|
|
|
* |
100
|
|
|
*/ |
101
|
|
|
public function add($type, $message, $title = null, array $options = [], bool $onlyNextRequest=true) |
102
|
|
|
{ |
103
|
|
|
if ($type=='') { |
104
|
|
|
$type = 'info'; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$this->notifications[] = [ |
108
|
|
|
'type' => $type, |
109
|
|
|
'title' => $title, |
110
|
|
|
'message' => $message, |
111
|
|
|
'options' => $options |
112
|
|
|
]; |
113
|
|
|
|
114
|
|
|
if($onlyNextRequest){ |
115
|
|
|
$this->session->flash('laravel::notifications', $this->notifications); |
116
|
|
|
}else{ |
117
|
|
|
$this->session->put('laravel::notifications', $this->notifications); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Shortcut for adding an info notification |
123
|
|
|
* |
124
|
|
|
* @param string $message The notification's message |
125
|
|
|
* @param string $title The notification's title |
126
|
|
|
* @param array $options |
127
|
|
|
*/ |
128
|
|
|
public function info($message, $title = null, array $options = []) |
129
|
|
|
{ |
130
|
|
|
$this->add('info', $message, $title, $options); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Shortcut for adding an error notification |
135
|
|
|
* |
136
|
|
|
* @param string $message The notification's message |
137
|
|
|
* @param string $title The notification's title |
138
|
|
|
* @param array $options |
139
|
|
|
*/ |
140
|
|
|
public function error($message, $title = null, array $options = []) |
141
|
|
|
{ |
142
|
|
|
$this->add('error', $message, $title, $options); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Shortcut for adding a warning notification |
147
|
|
|
* |
148
|
|
|
* @param string $message The notification's message |
149
|
|
|
* @param string $title The notification's title |
150
|
|
|
* @param array $options |
151
|
|
|
*/ |
152
|
|
|
public function warning($message, $title = null, array $options = []) |
153
|
|
|
{ |
154
|
|
|
$this->add('warning', $message, $title, $options); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Shortcut for adding a success notification |
159
|
|
|
* |
160
|
|
|
* @param string $message The notification's message |
161
|
|
|
* @param string $title The notification's title |
162
|
|
|
* @param array $options |
163
|
|
|
*/ |
164
|
|
|
public function success($message, $title = null, array $options = []) |
165
|
|
|
{ |
166
|
|
|
$this->add('success', $message, $title, $options); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Clear all notifications |
171
|
|
|
* @param bool $withSession if true (default) clean notifications in session too. |
172
|
|
|
*/ |
173
|
|
|
public function clear(bool $withSession = true) |
174
|
|
|
{ |
175
|
|
|
$this->notifications = []; |
176
|
|
|
|
177
|
|
|
if($withSession){ |
178
|
|
|
$this->session->forget('laravel::notifications'); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|