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