1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Encore\Admin\Actions; |
4
|
|
|
|
5
|
|
|
use Illuminate\Validation\ValidationException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Response. |
9
|
|
|
* |
10
|
|
|
* @method $this topCenter() |
11
|
|
|
* @method $this topLeft() |
12
|
|
|
* @method $this topRight() |
13
|
|
|
* @method $this bottomLeft() |
14
|
|
|
* @method $this bottomCenter() |
15
|
|
|
* @method $this bottomRight() |
16
|
|
|
* @method $this topFullWidth() |
17
|
|
|
* @method $this bottomFullWidth() |
18
|
|
|
* @method $this timeout($timeout = 5000) |
19
|
|
|
*/ |
20
|
|
|
class Response |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var bool |
24
|
|
|
*/ |
25
|
|
|
public $status = true; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var \Exception |
29
|
|
|
*/ |
30
|
|
|
public $exception; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
public $toastrMethods = [ |
36
|
|
|
'topCenter', 'topLeft', 'topRight', |
37
|
|
|
'bottomLeft', 'bottomCenter', 'bottomRight', |
38
|
|
|
'topFullWidth', 'bottomFullWidth', 'timeout', |
39
|
|
|
]; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var |
43
|
|
|
*/ |
44
|
|
|
protected $plugin; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
protected $then = []; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return $this |
53
|
|
|
*/ |
54
|
|
|
public function toastr() |
55
|
|
|
{ |
56
|
|
|
if (!$this->plugin instanceof Toastr) { |
57
|
|
|
$this->plugin = new Toastr(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return $this |
65
|
|
|
*/ |
66
|
|
|
public function swal() |
67
|
|
|
{ |
68
|
|
|
if (!$this->plugin instanceof SweatAlert2) { |
69
|
|
|
$this->plugin = new SweatAlert2(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return SweatAlert2 |
77
|
|
|
*/ |
78
|
|
|
public function getPlugin() |
79
|
|
|
{ |
80
|
|
|
return $this->plugin; |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $message |
85
|
|
|
* |
86
|
|
|
* @return $this |
87
|
|
|
*/ |
88
|
|
|
public function success(string $message = '') |
89
|
|
|
{ |
90
|
|
|
return $this->show('success', $message); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $message |
95
|
|
|
* |
96
|
|
|
* @return $this |
97
|
|
|
*/ |
98
|
|
|
public function info(string $message = '') |
99
|
|
|
{ |
100
|
|
|
return $this->show('info', $message); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param string $message |
105
|
|
|
* |
106
|
|
|
* @return $this |
107
|
|
|
*/ |
108
|
|
|
public function warning(string $message = '') |
109
|
|
|
{ |
110
|
|
|
return $this->show('warning', $message); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param string $message |
115
|
|
|
* |
116
|
|
|
* @return $this |
117
|
|
|
*/ |
118
|
|
|
public function error(string $message = '') |
119
|
|
|
{ |
120
|
|
|
return $this->show('error', $message); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param string $type |
125
|
|
|
* @param string $title |
126
|
|
|
* |
127
|
|
|
* @return $this |
128
|
|
|
*/ |
129
|
|
|
protected function show($type, $title = '') |
130
|
|
|
{ |
131
|
|
|
$this->getPlugin()->show($type, $title); |
132
|
|
|
|
133
|
|
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Send a redirect response. |
138
|
|
|
* |
139
|
|
|
* @param string $url |
140
|
|
|
* |
141
|
|
|
* @return $this |
142
|
|
|
*/ |
143
|
|
|
public function redirect(string $url) |
144
|
|
|
{ |
145
|
|
|
$this->then = ['action' => 'redirect', 'value' => $url]; |
146
|
|
|
|
147
|
|
|
return $this; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Send a download response. |
152
|
|
|
* |
153
|
|
|
* @param string $url |
154
|
|
|
* |
155
|
|
|
* @return $this |
156
|
|
|
*/ |
157
|
|
|
public function download($url) |
158
|
|
|
{ |
159
|
|
|
$this->then = ['action' => 'download', 'value' => $url]; |
160
|
|
|
|
161
|
|
|
return $this; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Send a refresh response. |
166
|
|
|
* |
167
|
|
|
* @return $this |
168
|
|
|
*/ |
169
|
|
|
public function refresh() |
170
|
|
|
{ |
171
|
|
|
$this->then = ['action' => 'refresh', 'value' => true]; |
172
|
|
|
|
173
|
|
|
return $this; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param \Exception $exception |
178
|
|
|
* |
179
|
|
|
* @return mixed |
180
|
|
|
*/ |
181
|
|
|
public static function withException(\Exception $exception) |
182
|
|
|
{ |
183
|
|
|
$response = new static(); |
184
|
|
|
|
185
|
|
|
$response->status = false; |
186
|
|
|
|
187
|
|
|
if ($exception instanceof ValidationException) { |
188
|
|
|
$message = collect($exception->errors())->flatten()->implode("\n"); |
189
|
|
|
} else { |
190
|
|
|
$message = $exception->getMessage(); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
return $response->toastr()->topCenter()->error($message); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @return \Illuminate\Http\JsonResponse |
198
|
|
|
*/ |
199
|
|
|
public function send() |
200
|
|
|
{ |
201
|
|
|
$data = array_merge( |
202
|
|
|
['status' => $this->status, 'then' => $this->then], |
203
|
|
|
$this->getPlugin()->getOptions() |
204
|
|
|
); |
205
|
|
|
|
206
|
|
|
return response()->json($data); |
|
|
|
|
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @param string $method |
211
|
|
|
* @param array $arguments |
212
|
|
|
* |
213
|
|
|
* @return $this |
214
|
|
|
*/ |
215
|
|
|
public function __call($method, $arguments) |
216
|
|
|
{ |
217
|
|
|
if (in_array($method, $this->toastrMethods)) { |
218
|
|
|
$this->toastr(); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
$this->getPlugin()->{$method}(...$arguments); |
222
|
|
|
|
223
|
|
|
return $this; |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|