|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace RaysTech\StarterKit\Supports; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Traits\Macroable; |
|
6
|
|
|
use RaysTech\StarterKit\Supports\SessionStore; |
|
7
|
|
|
|
|
8
|
|
|
class FlashToast |
|
9
|
|
|
{ |
|
10
|
|
|
use Macroable; |
|
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* The session store. |
|
13
|
|
|
* |
|
14
|
|
|
* @var SessionStore |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $session; |
|
17
|
|
|
/** |
|
18
|
|
|
* The messages collection. |
|
19
|
|
|
* |
|
20
|
|
|
* @var \Illuminate\Support\Collection |
|
21
|
|
|
*/ |
|
22
|
|
|
public $messages; |
|
23
|
|
|
/** |
|
24
|
|
|
* Create a new FlashNotifier instance. |
|
25
|
|
|
* |
|
26
|
|
|
* @param SessionStore $session |
|
27
|
|
|
*/ |
|
28
|
|
|
function __construct(SessionStore $session) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->session = $session; |
|
31
|
|
|
$this->messages = collect(); |
|
32
|
|
|
} |
|
33
|
|
|
/** |
|
34
|
|
|
* Flash an information message. |
|
35
|
|
|
* |
|
36
|
|
|
* @param string|null $message |
|
37
|
|
|
* @return $this |
|
38
|
|
|
*/ |
|
39
|
|
|
public function info($message = null) |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->message($message, 'info'); |
|
42
|
|
|
} |
|
43
|
|
|
/** |
|
44
|
|
|
* Flash a success message. |
|
45
|
|
|
* |
|
46
|
|
|
* @param string|null $message |
|
47
|
|
|
* @return $this |
|
48
|
|
|
*/ |
|
49
|
|
|
public function success($message = null) |
|
50
|
|
|
{ |
|
51
|
|
|
return $this->message($message, 'success'); |
|
52
|
|
|
} |
|
53
|
|
|
/** |
|
54
|
|
|
* Flash an error message. |
|
55
|
|
|
* |
|
56
|
|
|
* @param string|null $message |
|
57
|
|
|
* @return $this |
|
58
|
|
|
*/ |
|
59
|
|
|
public function error($message = null) |
|
60
|
|
|
{ |
|
61
|
|
|
return $this->message($message, 'danger'); |
|
62
|
|
|
} |
|
63
|
|
|
/** |
|
64
|
|
|
* Flash a warning message. |
|
65
|
|
|
* |
|
66
|
|
|
* @param string|null $message |
|
67
|
|
|
* @return $this |
|
68
|
|
|
*/ |
|
69
|
|
|
public function warning($message = null) |
|
70
|
|
|
{ |
|
71
|
|
|
return $this->message($message, 'warning'); |
|
72
|
|
|
} |
|
73
|
|
|
/** |
|
74
|
|
|
* Flash a general message. |
|
75
|
|
|
* |
|
76
|
|
|
* @param string|null $message |
|
77
|
|
|
* @param string|null $level |
|
78
|
|
|
* @return $this |
|
79
|
|
|
*/ |
|
80
|
|
|
public function message($message = null, $level = null) |
|
81
|
|
|
{ |
|
82
|
|
|
// If no message was provided, we should update |
|
83
|
|
|
// the most recently added message. |
|
84
|
|
|
if (! $message) { |
|
85
|
|
|
return $this->updateLastMessage(compact('level')); |
|
86
|
|
|
} |
|
87
|
|
|
if (! $message instanceof Message) { |
|
|
|
|
|
|
88
|
|
|
$message = new Message(compact('message', 'level')); |
|
89
|
|
|
} |
|
90
|
|
|
$this->messages->push($message); |
|
91
|
|
|
return $this->flash(); |
|
92
|
|
|
} |
|
93
|
|
|
/** |
|
94
|
|
|
* Modify the most recently added message. |
|
95
|
|
|
* |
|
96
|
|
|
* @param array $overrides |
|
97
|
|
|
* @return $this |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function updateLastMessage($overrides = []) |
|
100
|
|
|
{ |
|
101
|
|
|
$this->messages->last()->update($overrides); |
|
102
|
|
|
return $this; |
|
103
|
|
|
} |
|
104
|
|
|
/** |
|
105
|
|
|
* Flash an overlay modal. |
|
106
|
|
|
* |
|
107
|
|
|
* @param string|null $message |
|
108
|
|
|
* @param string $title |
|
109
|
|
|
* @return $this |
|
110
|
|
|
*/ |
|
111
|
|
|
public function overlay($message = null, $title = 'Notice') |
|
112
|
|
|
{ |
|
113
|
|
|
if (! $message) { |
|
114
|
|
|
return $this->updateLastMessage(['title' => $title, 'overlay' => true]); |
|
115
|
|
|
} |
|
116
|
|
|
return $this->message( |
|
117
|
|
|
new OverlayMessage(compact('title', 'message')) |
|
|
|
|
|
|
118
|
|
|
); |
|
119
|
|
|
} |
|
120
|
|
|
/** |
|
121
|
|
|
* Add an "important" flash to the session. |
|
122
|
|
|
* |
|
123
|
|
|
* @return $this |
|
124
|
|
|
*/ |
|
125
|
|
|
public function important() |
|
126
|
|
|
{ |
|
127
|
|
|
return $this->updateLastMessage(['important' => true]); |
|
128
|
|
|
} |
|
129
|
|
|
/** |
|
130
|
|
|
* Clear all registered messages. |
|
131
|
|
|
* |
|
132
|
|
|
* @return $this |
|
133
|
|
|
*/ |
|
134
|
|
|
public function clear() |
|
135
|
|
|
{ |
|
136
|
|
|
$this->messages = collect(); |
|
137
|
|
|
return $this; |
|
138
|
|
|
} |
|
139
|
|
|
/** |
|
140
|
|
|
* Flash all messages to the session. |
|
141
|
|
|
*/ |
|
142
|
|
|
protected function flash() |
|
143
|
|
|
{ |
|
144
|
|
|
$this->session->flash('flash_notification', $this->messages); |
|
|
|
|
|
|
145
|
|
|
return $this; |
|
146
|
|
|
} |
|
147
|
|
|
} |