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