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