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
|
|
|
$theme = (isset($notification['theme']) ? $notification['theme'] : 'metroui'); |
75
|
|
|
$timeout = (isset($notification['timeout']) ? $notification['timeout'] : 2000); |
76
|
|
|
$text = (isset($notification['text']) ? str_replace("'", "\\'", htmlentities($notification['text'])) : null); |
77
|
|
|
$message = (isset($notification['message']) ? $notification['message'] : ''); |
|
|
|
|
78
|
|
|
$type = (isset($notification['type']) ? $notification['type'] : 'info'); |
79
|
|
|
$layout = (isset($notification['layout']) ? $notification['layout'] : 'topRight'); |
80
|
|
|
|
81
|
|
|
$output = " |
82
|
|
|
$(function () { |
83
|
|
|
new Noty({ |
84
|
|
|
theme: '".$theme."', |
85
|
|
|
timeout: '".$timeout."', |
86
|
|
|
type: '".$type."', |
87
|
|
|
layout: '".$layout."', |
88
|
|
|
text: '".$text."' |
89
|
|
|
}).show(); |
90
|
|
|
}); |
91
|
|
|
"; |
92
|
|
|
|
93
|
|
|
return $output; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Add a notification |
98
|
|
|
* |
99
|
|
|
* @param string $type Could be error, info, success, or warning. |
100
|
|
|
* @param string $text The notification's message |
101
|
|
|
* @param string $title The notification's title |
|
|
|
|
102
|
|
|
* @param array $options |
103
|
|
|
* @param bool $onlyNextRequest if true(default), se the notification in session only for the next request |
104
|
|
|
* |
105
|
|
|
*/ |
106
|
|
|
public function add($theme, $timeout, $type, $layout, $text, array $options = [], bool $onlyNextRequest=true) |
107
|
|
|
{ |
108
|
|
|
if ($type=='') { |
109
|
|
|
$type = 'info'; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$this->notifications[] = [ |
113
|
|
|
'theme' => $theme, |
114
|
|
|
'timeout' => $timeout, |
115
|
|
|
'type' => $type, |
116
|
|
|
'layout' => $layout, |
117
|
|
|
'text' => $text, |
118
|
|
|
'options' => $options |
119
|
|
|
]; |
120
|
|
|
|
121
|
|
|
if($onlyNextRequest){ |
122
|
|
|
$this->session->flash('laravel::notifications', $this->notifications); |
123
|
|
|
}else{ |
124
|
|
|
$this->session->put('laravel::notifications', $this->notifications); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Shortcut for adding an info notification |
130
|
|
|
* |
131
|
|
|
* @param string $message The notification's message |
|
|
|
|
132
|
|
|
* @param string $title The notification's title |
|
|
|
|
133
|
|
|
* @param array $options |
134
|
|
|
*/ |
135
|
|
|
public function info($text, array $options = []) |
|
|
|
|
136
|
|
|
{ |
137
|
|
|
$this->add('metroui', 2000, 'info', 'topRight', $text, array(), true); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Shortcut for adding an error notification |
142
|
|
|
* |
143
|
|
|
* @param string $message The notification's message |
|
|
|
|
144
|
|
|
* @param string $title The notification's title |
|
|
|
|
145
|
|
|
* @param array $options |
146
|
|
|
*/ |
147
|
|
|
public function error($text, array $options = []) |
|
|
|
|
148
|
|
|
{ |
149
|
|
|
$this->add('metroui', 2000, 'error', 'topRight', $text, array(), true); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Shortcut for adding a warning notification |
154
|
|
|
* |
155
|
|
|
* @param string $message The notification's message |
|
|
|
|
156
|
|
|
* @param string $title The notification's title |
|
|
|
|
157
|
|
|
* @param array $options |
158
|
|
|
*/ |
159
|
|
|
public function warning($text, array $options = []) |
|
|
|
|
160
|
|
|
{ |
161
|
|
|
$this->add('metroui', 2000, 'warning', 'topRight', $text, array(), true); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Shortcut for adding a success notification |
166
|
|
|
* |
167
|
|
|
* @param string $message The notification's message |
|
|
|
|
168
|
|
|
* @param string $title The notification's title |
|
|
|
|
169
|
|
|
* @param array $options |
170
|
|
|
*/ |
171
|
|
|
public function success($text, array $options = []) |
|
|
|
|
172
|
|
|
{ |
173
|
|
|
$this->add('metroui', 2000, 'success', 'topRight', $text, array(), true); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Clear all notifications |
179
|
|
|
* @param bool $withSession if true (default) clean notifications in session too. |
180
|
|
|
*/ |
181
|
|
|
public function clear(bool $withSession = true) |
182
|
|
|
{ |
183
|
|
|
$this->notifications = []; |
184
|
|
|
|
185
|
|
|
if($withSession){ |
186
|
|
|
$this->session->forget('laravel::notifications'); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.