|
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 Noty({ |
|
82
|
|
|
theme: 'metroui', |
|
83
|
|
|
timeout: 2000, |
|
84
|
|
|
type: 'info', |
|
85
|
|
|
layout: 'topRight', |
|
86
|
|
|
text: 'Azione effettuata' |
|
87
|
|
|
}).show(); |
|
88
|
|
|
});"; |
|
89
|
|
|
/* |
|
|
|
|
|
|
90
|
|
|
new Notifier({ |
|
91
|
|
|
title: '" .$title. "', |
|
92
|
|
|
text: '" . $message . "', |
|
93
|
|
|
type: '" . $type . "' |
|
94
|
|
|
}); |
|
95
|
|
|
*/ |
|
96
|
|
|
return $output; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Add a notification |
|
101
|
|
|
* |
|
102
|
|
|
* @param string $type Could be error, info, success, or warning. |
|
103
|
|
|
* @param string $message The notification's message |
|
104
|
|
|
* @param string $title The notification's title |
|
105
|
|
|
* @param array $options |
|
106
|
|
|
* @param bool $onlyNextRequest if true(default), se the notification in session only for the next request |
|
107
|
|
|
* |
|
108
|
|
|
*/ |
|
109
|
|
|
public function add($type, $message, $title = null, array $options = [], bool $onlyNextRequest=true) |
|
110
|
|
|
{ |
|
111
|
|
|
if ($type=='') { |
|
112
|
|
|
$type = 'info'; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
$this->notifications[] = [ |
|
116
|
|
|
'type' => $type, |
|
117
|
|
|
'title' => $title, |
|
118
|
|
|
'message' => $message, |
|
119
|
|
|
'options' => $options |
|
120
|
|
|
]; |
|
121
|
|
|
|
|
122
|
|
|
if($onlyNextRequest){ |
|
123
|
|
|
$this->session->flash('laravel::notifications', $this->notifications); |
|
124
|
|
|
}else{ |
|
125
|
|
|
$this->session->put('laravel::notifications', $this->notifications); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Shortcut for adding an info notification |
|
131
|
|
|
* |
|
132
|
|
|
* @param string $message The notification's message |
|
133
|
|
|
* @param string $title The notification's title |
|
134
|
|
|
* @param array $options |
|
135
|
|
|
*/ |
|
136
|
|
|
public function info($message, $title = null, array $options = []) |
|
137
|
|
|
{ |
|
138
|
|
|
$this->add('info', $message, $title, $options); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Shortcut for adding an error notification |
|
143
|
|
|
* |
|
144
|
|
|
* @param string $message The notification's message |
|
145
|
|
|
* @param string $title The notification's title |
|
146
|
|
|
* @param array $options |
|
147
|
|
|
*/ |
|
148
|
|
|
public function error($message, $title = null, array $options = []) |
|
149
|
|
|
{ |
|
150
|
|
|
$this->add('error', $message, $title, $options); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Shortcut for adding a warning notification |
|
155
|
|
|
* |
|
156
|
|
|
* @param string $message The notification's message |
|
157
|
|
|
* @param string $title The notification's title |
|
158
|
|
|
* @param array $options |
|
159
|
|
|
*/ |
|
160
|
|
|
public function warning($message, $title = null, array $options = []) |
|
161
|
|
|
{ |
|
162
|
|
|
$this->add('warning', $message, $title, $options); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Shortcut for adding a success notification |
|
167
|
|
|
* |
|
168
|
|
|
* @param string $message The notification's message |
|
169
|
|
|
* @param string $title The notification's title |
|
170
|
|
|
* @param array $options |
|
171
|
|
|
*/ |
|
172
|
|
|
public function success($message, $title = null, array $options = []) |
|
173
|
|
|
{ |
|
174
|
|
|
$this->add('success', $message, $title, $options); |
|
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
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.