1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Encore\Admin\Layout; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Encore\Admin\Facades\Admin; |
7
|
|
|
use Illuminate\Contracts\Support\Renderable; |
8
|
|
|
use Illuminate\Support\Arr; |
9
|
|
|
|
10
|
|
|
class Content implements Renderable |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Content title. |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $title = ' '; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Content description. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $description = ' '; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Page breadcrumb. |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $breadcrumb = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var Row[] |
35
|
|
|
*/ |
36
|
|
|
protected $rows = []; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
protected $view; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Content constructor. |
45
|
|
|
* |
46
|
|
|
* @param Closure|null $callback |
47
|
|
|
*/ |
48
|
|
|
public function __construct(\Closure $callback = null) |
49
|
|
|
{ |
50
|
|
|
if ($callback instanceof Closure) { |
51
|
|
|
$callback($this); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Alias of method `title`. |
57
|
|
|
* |
58
|
|
|
* @param string $header |
59
|
|
|
* |
60
|
|
|
* @return $this |
61
|
|
|
*/ |
62
|
|
|
public function header($header = '') |
63
|
|
|
{ |
64
|
|
|
return $this->title($header); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $title |
69
|
|
|
* |
70
|
|
|
* @return $this |
71
|
|
|
*/ |
72
|
|
|
public function title($title) |
73
|
|
|
{ |
74
|
|
|
$this->title = $title; |
75
|
|
|
|
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Set description of content. |
81
|
|
|
* |
82
|
|
|
* @param string $description |
83
|
|
|
* |
84
|
|
|
* @return $this |
85
|
|
|
*/ |
86
|
|
|
public function description($description = '') |
87
|
|
|
{ |
88
|
|
|
$this->description = $description; |
89
|
|
|
|
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Set breadcrumb of content. |
95
|
|
|
* |
96
|
|
|
* @param array ...$breadcrumb |
97
|
|
|
* |
98
|
|
|
* @return $this |
99
|
|
|
*/ |
100
|
|
|
public function breadcrumb(...$breadcrumb) |
101
|
|
|
{ |
102
|
|
|
$this->validateBreadcrumb($breadcrumb); |
103
|
|
|
|
104
|
|
|
$this->breadcrumb = (array) $breadcrumb; |
105
|
|
|
|
106
|
|
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Validate content breadcrumb. |
111
|
|
|
* |
112
|
|
|
* @param array $breadcrumb |
113
|
|
|
* |
114
|
|
|
* @throws \Exception |
115
|
|
|
* |
116
|
|
|
* @return bool |
117
|
|
|
*/ |
118
|
|
|
protected function validateBreadcrumb(array $breadcrumb) |
119
|
|
|
{ |
120
|
|
|
foreach ($breadcrumb as $item) { |
121
|
|
|
if (!is_array($item) || !Arr::has($item, 'text')) { |
122
|
|
|
throw new \Exception('Breadcrumb format error!'); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return true; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Alias of method row. |
131
|
|
|
* |
132
|
|
|
* @param mixed $content |
133
|
|
|
* |
134
|
|
|
* @return $this |
135
|
|
|
*/ |
136
|
|
|
public function body($content) |
137
|
|
|
{ |
138
|
|
|
return $this->row($content); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Add one row for content body. |
143
|
|
|
* |
144
|
|
|
* @param $content |
145
|
|
|
* |
146
|
|
|
* @return $this |
147
|
|
|
*/ |
148
|
|
|
public function row($content) |
149
|
|
|
{ |
150
|
|
|
if ($content instanceof Closure) { |
151
|
|
|
$row = new Row(); |
152
|
|
|
call_user_func($content, $row); |
153
|
|
|
$this->addRow($row); |
154
|
|
|
} else { |
155
|
|
|
$this->addRow(new Row($content)); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Render giving view as content body. |
163
|
|
|
* |
164
|
|
|
* @param string $view |
165
|
|
|
* @param array $data |
166
|
|
|
* |
167
|
|
|
* @return $this |
168
|
|
|
*/ |
169
|
|
|
public function view($view, $data = []) |
170
|
|
|
{ |
171
|
|
|
$this->view = compact('view', 'data'); |
172
|
|
|
|
173
|
|
|
return $this; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @param string $view |
178
|
|
|
* @param array $data |
179
|
|
|
*/ |
180
|
|
|
public function component($view, $data = []) |
181
|
|
|
{ |
182
|
|
|
return $this->body(Admin::component($view, $data)); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @param $var |
187
|
|
|
* |
188
|
|
|
* @return $this |
189
|
|
|
*/ |
190
|
|
|
public function dump($var) |
|
|
|
|
191
|
|
|
{ |
192
|
|
|
return $this->row(admin_dump(...func_get_args())); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Add Row. |
197
|
|
|
* |
198
|
|
|
* @param Row $row |
199
|
|
|
*/ |
200
|
|
|
protected function addRow(Row $row) |
201
|
|
|
{ |
202
|
|
|
$this->rows[] = $row; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Build html of content. |
207
|
|
|
* |
208
|
|
|
* @return string |
209
|
|
|
*/ |
210
|
|
|
public function build() |
211
|
|
|
{ |
212
|
|
|
ob_start(); |
213
|
|
|
|
214
|
|
|
foreach ($this->rows as $row) { |
215
|
|
|
$row->build(); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
$contents = ob_get_contents(); |
219
|
|
|
|
220
|
|
|
ob_end_clean(); |
221
|
|
|
|
222
|
|
|
return $contents; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Set success message for content. |
227
|
|
|
* |
228
|
|
|
* @param string $title |
229
|
|
|
* @param string $message |
230
|
|
|
* |
231
|
|
|
* @return $this |
232
|
|
|
*/ |
233
|
|
|
public function withSuccess($title = '', $message = '') |
234
|
|
|
{ |
235
|
|
|
admin_success($title, $message); |
236
|
|
|
|
237
|
|
|
return $this; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Set error message for content. |
242
|
|
|
* |
243
|
|
|
* @param string $title |
244
|
|
|
* @param string $message |
245
|
|
|
* |
246
|
|
|
* @return $this |
247
|
|
|
*/ |
248
|
|
|
public function withError($title = '', $message = '') |
249
|
|
|
{ |
250
|
|
|
admin_error($title, $message); |
251
|
|
|
|
252
|
|
|
return $this; |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Set warning message for content. |
257
|
|
|
* |
258
|
|
|
* @param string $title |
259
|
|
|
* @param string $message |
260
|
|
|
* |
261
|
|
|
* @return $this |
262
|
|
|
*/ |
263
|
|
|
public function withWarning($title = '', $message = '') |
264
|
|
|
{ |
265
|
|
|
admin_warning($title, $message); |
266
|
|
|
|
267
|
|
|
return $this; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Set info message for content. |
272
|
|
|
* |
273
|
|
|
* @param string $title |
274
|
|
|
* @param string $message |
275
|
|
|
* |
276
|
|
|
* @return $this |
277
|
|
|
*/ |
278
|
|
|
public function withInfo($title = '', $message = '') |
279
|
|
|
{ |
280
|
|
|
admin_info($title, $message); |
281
|
|
|
|
282
|
|
|
return $this; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* @return array |
287
|
|
|
*/ |
288
|
|
|
protected function getUserData() |
289
|
|
|
{ |
290
|
|
|
if (!$user = Admin::user()) { |
291
|
|
|
return []; |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
return Arr::only($user->toArray(), ['id', 'username', 'email', 'name', 'avatar']); |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Render this content. |
299
|
|
|
* |
300
|
|
|
* @return string |
301
|
|
|
*/ |
302
|
|
|
public function render() |
303
|
|
|
{ |
304
|
|
|
$items = [ |
305
|
|
|
'header' => $this->title, |
306
|
|
|
'description' => $this->description, |
307
|
|
|
'breadcrumb' => $this->breadcrumb, |
308
|
|
|
'_content_' => $this->build(), |
309
|
|
|
'_view_' => $this->view, |
310
|
|
|
'_user_' => $this->getUserData(), |
311
|
|
|
]; |
312
|
|
|
|
313
|
|
|
return view('admin::content', $items)->render(); |
|
|
|
|
314
|
|
|
} |
315
|
|
|
} |
316
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.