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