|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin\Display; |
|
4
|
|
|
|
|
5
|
|
|
use Closure; |
|
6
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
8
|
|
|
use InvalidArgumentException; |
|
9
|
|
|
use KodiComponents\Support\HtmlAttributes; |
|
10
|
|
|
use SleepingOwl\Admin\Contracts\Display\ColumnInterface; |
|
11
|
|
|
use SleepingOwl\Admin\Contracts\Display\OrderByClauseInterface; |
|
12
|
|
|
use SleepingOwl\Admin\Contracts\Display\TableHeaderColumnInterface; |
|
13
|
|
|
use SleepingOwl\Admin\Contracts\ModelConfigurationInterface; |
|
14
|
|
|
use SleepingOwl\Admin\Contracts\WithModelInterface; |
|
15
|
|
|
use SleepingOwl\Admin\Display\Column\OrderByClause; |
|
16
|
|
|
use SleepingOwl\Admin\Traits\Assets; |
|
17
|
|
|
use SleepingOwl\Admin\Traits\Renderable; |
|
18
|
|
|
|
|
19
|
|
|
abstract class TableColumn implements ColumnInterface |
|
20
|
|
|
{ |
|
21
|
|
|
use HtmlAttributes, Assets, Renderable; |
|
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var \Closure |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $searchCallback = null; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var \Closure |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $orderCallback = null; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var \Closure |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $filterCallback = null; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var null |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $columMetaClass = null; |
|
42
|
|
|
/** |
|
43
|
|
|
* Column header. |
|
44
|
|
|
* |
|
45
|
|
|
* @var TableHeaderColumnInterface |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $header; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Model instance currently rendering. |
|
51
|
|
|
* |
|
52
|
|
|
* @var Model |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $model; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Column appendant. |
|
58
|
|
|
* |
|
59
|
|
|
* @var ColumnInterface |
|
60
|
|
|
*/ |
|
61
|
|
|
protected $append; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Column width. |
|
65
|
|
|
* |
|
66
|
|
|
* @var string |
|
67
|
|
|
*/ |
|
68
|
|
|
protected $width = null; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @var OrderByClauseInterface |
|
72
|
|
|
*/ |
|
73
|
|
|
protected $orderByClause; |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @var bool |
|
77
|
|
|
*/ |
|
78
|
45 |
|
protected $visible = true; |
|
79
|
|
|
|
|
80
|
45 |
|
/** |
|
81
|
|
|
* @var bool |
|
82
|
45 |
|
*/ |
|
83
|
11 |
|
protected $isSearchable = false; |
|
84
|
11 |
|
|
|
85
|
|
|
/** |
|
86
|
45 |
|
* TableColumn constructor. |
|
87
|
45 |
|
* |
|
88
|
|
|
* @param string|null $label |
|
89
|
|
|
*/ |
|
90
|
|
|
public function __construct($label = null) |
|
91
|
|
|
{ |
|
92
|
2 |
|
$this->header = app(TableHeaderColumnInterface::class); |
|
93
|
|
|
|
|
94
|
2 |
|
if (! is_null($label)) { |
|
95
|
2 |
|
$this->setLabel($label); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$this->initializePackage(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Initialize column. |
|
103
|
|
|
*/ |
|
104
|
|
|
public function initialize() |
|
105
|
|
|
{ |
|
106
|
|
|
$this->includePackage(); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param $columnMetaClass |
|
111
|
|
|
* @return $this |
|
112
|
|
|
*/ |
|
113
|
|
|
public function setMetaData($columnMetaClass) |
|
114
|
|
|
{ |
|
115
|
|
|
$this->columMetaClass = $columnMetaClass; |
|
116
|
|
|
|
|
117
|
|
|
return $this; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @return mixed |
|
122
|
|
|
*/ |
|
123
|
|
|
public function getMetaData() |
|
124
|
|
|
{ |
|
125
|
|
|
return $this->columMetaClass |
|
126
|
|
|
? app()->make($this->columMetaClass) |
|
127
|
|
|
: false; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @param \Closure $callable |
|
132
|
|
|
* @return $this |
|
133
|
|
|
*/ |
|
134
|
|
|
public function setOrderCallback(Closure $callable) |
|
135
|
|
|
{ |
|
136
|
|
|
$this->orderCallback = $callable; |
|
137
|
|
|
|
|
138
|
|
|
return $this->setOrderable($callable); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @param \Closure $callable |
|
143
|
|
|
* @return $this |
|
144
|
|
|
*/ |
|
145
|
|
|
public function setSearchCallback(Closure $callable) |
|
146
|
|
|
{ |
|
147
|
|
|
$this->searchCallback = $callable; |
|
148
|
|
|
|
|
149
|
|
|
return $this; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @param \Closure $callable |
|
154
|
|
|
* @return $this |
|
155
|
|
|
*/ |
|
156
|
|
|
public function setFilterCallback(Closure $callable) |
|
157
|
|
|
{ |
|
158
|
|
|
$this->filterCallback = $callable; |
|
159
|
|
|
|
|
160
|
|
|
return $this; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @return \Closure |
|
165
|
|
|
*/ |
|
166
|
|
|
public function getOrderCallback() |
|
167
|
|
|
{ |
|
168
|
|
|
return $this->orderCallback; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* @return \Closure |
|
173
|
|
|
*/ |
|
174
|
|
|
public function getSearchCallback() |
|
175
|
|
|
{ |
|
176
|
|
|
return $this->searchCallback; |
|
177
|
|
|
} |
|
178
|
24 |
|
|
|
179
|
|
|
/** |
|
180
|
24 |
|
* @return \Closure |
|
181
|
|
|
*/ |
|
182
|
|
|
public function getFilterCallback() |
|
183
|
|
|
{ |
|
184
|
|
|
return $this->filterCallback; |
|
185
|
|
|
} |
|
186
|
1 |
|
|
|
187
|
|
|
/** |
|
188
|
1 |
|
* @return TableHeaderColumnInterface |
|
189
|
|
|
*/ |
|
190
|
|
|
public function getHeader() |
|
191
|
|
|
{ |
|
192
|
|
|
return $this->header; |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
/** |
|
196
|
1 |
|
* @return int|string |
|
197
|
|
|
*/ |
|
198
|
1 |
|
public function getWidth() |
|
199
|
1 |
|
{ |
|
200
|
1 |
|
return $this->width; |
|
201
|
|
|
} |
|
202
|
1 |
|
|
|
203
|
|
|
/** |
|
204
|
1 |
|
* @param int|string $width |
|
205
|
|
|
* |
|
206
|
|
|
* @return $this |
|
207
|
|
|
*/ |
|
208
|
|
|
public function setWidth($width) |
|
209
|
|
|
{ |
|
210
|
16 |
|
if (is_int($width)) { |
|
211
|
|
|
$width = $width.'px'; |
|
212
|
16 |
|
} |
|
213
|
|
|
|
|
214
|
|
|
$this->width = $width; |
|
215
|
|
|
|
|
216
|
|
|
return $this; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
3 |
|
* @param $isSearchable |
|
221
|
|
|
* |
|
222
|
3 |
|
* @return TableColumn |
|
223
|
|
|
*/ |
|
224
|
3 |
|
public function setSearchable($isSearchable) |
|
225
|
|
|
{ |
|
226
|
|
|
$this->isSearchable = $isSearchable; |
|
227
|
|
|
|
|
228
|
|
|
return $this; |
|
229
|
|
|
} |
|
230
|
15 |
|
|
|
231
|
|
|
/** |
|
232
|
15 |
|
* @param bool $visible |
|
233
|
|
|
* @return $this |
|
234
|
|
|
*/ |
|
235
|
|
|
public function setVisible($visible) |
|
236
|
|
|
{ |
|
237
|
|
|
$this->visible = $visible; |
|
238
|
|
|
|
|
239
|
|
|
return $this; |
|
240
|
13 |
|
} |
|
241
|
|
|
|
|
242
|
13 |
|
/** |
|
243
|
13 |
|
* @return ColumnInterface |
|
244
|
|
|
*/ |
|
245
|
13 |
|
public function getAppends() |
|
246
|
1 |
|
{ |
|
247
|
1 |
|
return $this->append; |
|
248
|
|
|
} |
|
249
|
13 |
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* @param ColumnInterface $append |
|
252
|
|
|
* |
|
253
|
|
|
* @return $this |
|
254
|
|
|
*/ |
|
255
|
|
|
public function append(ColumnInterface $append) |
|
256
|
|
|
{ |
|
257
|
|
|
$this->append = $append; |
|
258
|
|
|
|
|
259
|
14 |
|
return $this; |
|
260
|
|
|
} |
|
261
|
14 |
|
|
|
262
|
|
|
/** |
|
263
|
14 |
|
* @return Model $model |
|
264
|
|
|
*/ |
|
265
|
|
|
public function getModel() |
|
266
|
|
|
{ |
|
267
|
|
|
return $this->model; |
|
268
|
|
|
} |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
24 |
|
* @param Model $model |
|
272
|
|
|
* |
|
273
|
24 |
|
* @return $this |
|
274
|
17 |
|
*/ |
|
275
|
17 |
|
public function setModel(Model $model) |
|
276
|
|
|
{ |
|
277
|
24 |
|
$this->model = $model; |
|
278
|
2 |
|
$append = $this->getAppends(); |
|
279
|
|
|
|
|
280
|
|
|
if ($append instanceof WithModelInterface) { |
|
|
|
|
|
|
281
|
22 |
|
$append->setModel($model); |
|
282
|
22 |
|
} |
|
283
|
|
|
|
|
284
|
22 |
|
return $this; |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
/** |
|
288
|
|
|
* Set column header label. |
|
289
|
|
|
* |
|
290
|
4 |
|
* @param string $title |
|
291
|
|
|
* |
|
292
|
4 |
|
* @return $this |
|
293
|
|
|
*/ |
|
294
|
|
|
public function setLabel($title) |
|
295
|
|
|
{ |
|
296
|
|
|
$this->getHeader()->setTitle($title); |
|
297
|
|
|
|
|
298
|
|
|
return $this; |
|
299
|
22 |
|
} |
|
300
|
|
|
|
|
301
|
22 |
|
/** |
|
302
|
|
|
* @param OrderByClauseInterface|bool|string|\Closure $orderable |
|
303
|
|
|
* @return $this |
|
304
|
|
|
*/ |
|
305
|
|
|
public function setOrderable($orderable) |
|
306
|
|
|
{ |
|
307
|
|
|
if ($orderable instanceof Closure || is_string($orderable)) { |
|
308
|
|
|
$orderable = new OrderByClause($orderable); |
|
|
|
|
|
|
309
|
|
|
} |
|
310
|
1 |
|
|
|
311
|
|
|
if ($orderable !== false && ! $orderable instanceof OrderByClauseInterface) { |
|
312
|
1 |
|
throw new InvalidArgumentException('Argument must be instance of SleepingOwl\Admin\Contracts\Display\OrderByClauseInterface interface'); |
|
313
|
|
|
} |
|
314
|
|
|
|
|
315
|
|
|
$this->orderByClause = $orderable; |
|
|
|
|
|
|
316
|
1 |
|
$this->getHeader()->setOrderable($this->isOrderable()); |
|
|
|
|
|
|
317
|
|
|
|
|
318
|
1 |
|
return $this; |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
/** |
|
322
|
|
|
* @return OrderByClauseInterface |
|
323
|
|
|
*/ |
|
324
|
|
|
public function getOrderByClause() |
|
325
|
|
|
{ |
|
326
|
5 |
|
return $this->orderByClause; |
|
327
|
|
|
} |
|
328
|
|
|
|
|
329
|
5 |
|
/** |
|
330
|
5 |
|
* Check if column is orderable. |
|
331
|
5 |
|
* @return bool |
|
332
|
5 |
|
*/ |
|
333
|
|
|
public function isOrderable() |
|
334
|
|
|
{ |
|
335
|
|
|
return $this->orderByClause instanceof OrderByClauseInterface; |
|
336
|
|
|
} |
|
337
|
|
|
|
|
338
|
|
|
/** |
|
339
|
|
|
* Check if column is visible. |
|
340
|
|
|
* @return bool |
|
341
|
|
|
*/ |
|
342
|
|
|
public function isVisible() |
|
343
|
|
|
{ |
|
344
|
|
|
return $this->visible; |
|
345
|
|
|
} |
|
346
|
|
|
|
|
347
|
|
|
/** |
|
348
|
|
|
* Check if column is Searchable. |
|
349
|
|
|
* @return bool |
|
350
|
|
|
*/ |
|
351
|
|
|
public function isSearchable() |
|
352
|
|
|
{ |
|
353
|
|
|
return $this->isSearchable; |
|
354
|
|
|
} |
|
355
|
|
|
|
|
356
|
|
|
/** |
|
357
|
|
|
* @param Builder $query |
|
358
|
|
|
* @param string $direction |
|
359
|
|
|
* @return $this |
|
360
|
|
|
* @deprecated |
|
361
|
|
|
*/ |
|
362
|
|
|
public function orderBy(Builder $query, $direction) |
|
363
|
|
|
{ |
|
364
|
|
|
if (! $this->isOrderable()) { |
|
365
|
|
|
throw new InvalidArgumentException('Argument must be instance of SleepingOwl\Admin\Contracts\Display\OrderByClauseInterface interface'); |
|
366
|
|
|
} |
|
367
|
|
|
|
|
368
|
|
|
$this->orderByClause->modifyQuery($query, $direction); |
|
369
|
|
|
|
|
370
|
|
|
return $this; |
|
371
|
|
|
} |
|
372
|
|
|
|
|
373
|
|
|
/** |
|
374
|
|
|
* Get the instance as an array. |
|
375
|
|
|
* |
|
376
|
|
|
* @return array |
|
377
|
|
|
*/ |
|
378
|
|
|
public function toArray() |
|
379
|
|
|
{ |
|
380
|
|
|
return [ |
|
381
|
|
|
'attributes' => $this->htmlAttributesToString(), |
|
382
|
|
|
'model' => $this->getModel(), |
|
383
|
|
|
'append' => $this->getAppends(), |
|
384
|
|
|
]; |
|
385
|
|
|
} |
|
386
|
|
|
|
|
387
|
|
|
/** |
|
388
|
|
|
* Get related model configuration. |
|
389
|
|
|
* @return ModelConfigurationInterface |
|
390
|
|
|
*/ |
|
391
|
|
|
protected function getModelConfiguration() |
|
392
|
|
|
{ |
|
393
|
|
|
return app('sleeping_owl')->getModel( |
|
|
|
|
|
|
394
|
|
|
$this->getModel() |
|
395
|
|
|
); |
|
396
|
|
|
} |
|
397
|
|
|
} |
|
398
|
|
|
|