1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yajra\DataTables\Html; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Fluent; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @property string data |
10
|
|
|
* @property string name |
11
|
|
|
* @property string orderable |
12
|
|
|
* @property string searchable |
13
|
|
|
* @property string printable |
14
|
|
|
* @property string exportable |
15
|
|
|
* @property string footer |
16
|
|
|
* @property array attributes |
17
|
|
|
* @see https://datatables.net/reference/option/ for possible columns option |
18
|
|
|
*/ |
19
|
|
|
class Column extends Fluent |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @param array $attributes |
23
|
|
|
*/ |
24
|
|
|
public function __construct($attributes = []) |
25
|
|
|
{ |
26
|
|
|
$attributes['title'] = isset($attributes['title']) ? $attributes['title'] : self::titleFormat($attributes['data']); |
27
|
|
|
$attributes['orderable'] = isset($attributes['orderable']) ? $attributes['orderable'] : true; |
28
|
|
|
$attributes['searchable'] = isset($attributes['searchable']) ? $attributes['searchable'] : true; |
29
|
|
|
$attributes['exportable'] = isset($attributes['exportable']) ? $attributes['exportable'] : true; |
30
|
|
|
$attributes['printable'] = isset($attributes['printable']) ? $attributes['printable'] : true; |
31
|
|
|
$attributes['footer'] = isset($attributes['footer']) ? $attributes['footer'] : ''; |
32
|
|
|
$attributes['attributes'] = isset($attributes['attributes']) ? $attributes['attributes'] : []; |
33
|
|
|
|
34
|
|
|
// Allow methods override attribute value |
35
|
|
|
foreach ($attributes as $attribute => $value) { |
36
|
|
|
$method = 'parse' . ucfirst(strtolower($attribute)); |
37
|
|
|
if (method_exists($this, $method)) { |
38
|
|
|
$attributes[$attribute] = $this->$method($value); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if (! isset($attributes['name']) && isset($attributes['data'])) { |
43
|
|
|
$attributes['name'] = $attributes['data']; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
parent::__construct($attributes); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Format string to title case. |
51
|
|
|
* |
52
|
|
|
* @param string $value |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
public static function titleFormat($value) |
56
|
|
|
{ |
57
|
|
|
return Str::title(str_replace('_', ' ', $value)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Create a computed column that is not searchable/orderable. |
62
|
|
|
* |
63
|
|
|
* @param string $data |
64
|
|
|
* @param string $title |
65
|
|
|
* @return Column |
66
|
|
|
*/ |
67
|
|
|
public static function computed($data, $title = '') |
68
|
|
|
{ |
69
|
|
|
if (empty($title)) { |
70
|
|
|
$title = self::titleFormat($data); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return static::make($data)->title($title)->orderable(false)->searchable(false); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Set column searchable flag. |
78
|
|
|
* |
79
|
|
|
* @param bool $flag |
80
|
|
|
* @return $this |
81
|
|
|
*/ |
82
|
|
|
public function searchable(bool $flag = true) |
83
|
|
|
{ |
84
|
|
|
$this->attributes['searchable'] = $flag; |
85
|
|
|
|
86
|
|
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Set column orderable flag. |
91
|
|
|
* |
92
|
|
|
* @param bool $flag |
93
|
|
|
* @return $this |
94
|
|
|
*/ |
95
|
|
|
public function orderable(bool $flag = true) |
96
|
|
|
{ |
97
|
|
|
$this->attributes['orderable'] = $flag; |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Set column title. |
104
|
|
|
* |
105
|
|
|
* @param string $value |
106
|
|
|
* @return $this |
107
|
|
|
*/ |
108
|
|
|
public function title($value) |
109
|
|
|
{ |
110
|
|
|
$this->attributes['title'] = $value; |
111
|
|
|
|
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Make a new column instance. |
117
|
|
|
* |
118
|
|
|
* @param string $data |
119
|
|
|
* @param string $name |
120
|
|
|
* @return Column |
121
|
|
|
*/ |
122
|
|
|
public static function make($data, $name = '') |
123
|
|
|
{ |
124
|
|
|
$attr = [ |
125
|
|
|
'data' => $data, |
126
|
|
|
'name' => $name ?: $data, |
127
|
|
|
]; |
128
|
|
|
|
129
|
|
|
return new static($attr); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Create a checkbox column. |
134
|
|
|
* |
135
|
|
|
* @param string $title |
136
|
|
|
* @return Column |
137
|
|
|
*/ |
138
|
|
|
public static function checkbox($title = '') |
139
|
|
|
{ |
140
|
|
|
return static::make('') |
141
|
|
|
->content('') |
142
|
|
|
->title($title) |
143
|
|
|
->className('select-checkbox') |
144
|
|
|
->orderable(false) |
145
|
|
|
->searchable(false); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Set column class name. |
150
|
|
|
* |
151
|
|
|
* @param string $class |
152
|
|
|
* @return $this |
153
|
|
|
*/ |
154
|
|
|
public function className($class) |
155
|
|
|
{ |
156
|
|
|
$this->attributes['className'] = $class; |
157
|
|
|
|
158
|
|
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Set column default content. |
163
|
|
|
* |
164
|
|
|
* @param string $value |
165
|
|
|
* @return $this |
166
|
|
|
*/ |
167
|
|
|
public function content($value) |
168
|
|
|
{ |
169
|
|
|
$this->attributes['defaultContent'] = $value; |
170
|
|
|
|
171
|
|
|
return $this; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Set column visible flag. |
176
|
|
|
* |
177
|
|
|
* @param bool $flag |
178
|
|
|
* @return $this |
179
|
|
|
*/ |
180
|
|
|
public function visible(bool $flag = true) |
181
|
|
|
{ |
182
|
|
|
$this->attributes['visible'] = $flag; |
183
|
|
|
|
184
|
|
|
return $this; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Append a class name to field. |
189
|
|
|
* |
190
|
|
|
* @param string $class |
191
|
|
|
* @return $this |
192
|
|
|
*/ |
193
|
|
|
public function addClass($class) |
194
|
|
|
{ |
195
|
|
|
if (! isset($this->attributes['className'])) { |
196
|
|
|
$this->attributes['className'] = $class; |
197
|
|
|
} else { |
198
|
|
|
$this->attributes['className'] .= " $class"; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return $this; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Set column exportable flag. |
206
|
|
|
* |
207
|
|
|
* @param bool $flag |
208
|
|
|
* @return $this |
209
|
|
|
*/ |
210
|
|
|
public function exportable(bool $flag = true) |
211
|
|
|
{ |
212
|
|
|
$this->attributes['exportable'] = $flag; |
213
|
|
|
|
214
|
|
|
return $this; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Set column printable flag. |
219
|
|
|
* |
220
|
|
|
* @param bool $flag |
221
|
|
|
* @return $this |
222
|
|
|
*/ |
223
|
|
|
public function printable(bool $flag = true) |
224
|
|
|
{ |
225
|
|
|
$this->attributes['printable'] = $flag; |
226
|
|
|
|
227
|
|
|
return $this; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Set column width value. |
232
|
|
|
* |
233
|
|
|
* @param int|string $value |
234
|
|
|
* @return $this |
235
|
|
|
*/ |
236
|
|
|
public function width($value) |
237
|
|
|
{ |
238
|
|
|
$this->attributes['width'] = $value; |
239
|
|
|
|
240
|
|
|
return $this; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Set column name. |
245
|
|
|
* |
246
|
|
|
* @param string $value |
247
|
|
|
* @return $this |
248
|
|
|
*/ |
249
|
|
|
public function name($value) |
250
|
|
|
{ |
251
|
|
|
$this->attributes['name'] = $value; |
252
|
|
|
|
253
|
|
|
return $this; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Set column renderer. |
258
|
|
|
* |
259
|
|
|
* @param mixed $value |
260
|
|
|
* @return $this |
261
|
|
|
*/ |
262
|
|
|
public function render($value) |
263
|
|
|
{ |
264
|
|
|
$this->attributes['render'] = $value; |
265
|
|
|
|
266
|
|
|
return $this; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Parse render attribute. |
271
|
|
|
* |
272
|
|
|
* @param mixed $value |
273
|
|
|
* @return string|null |
274
|
|
|
*/ |
275
|
|
|
public function parseRender($value) |
276
|
|
|
{ |
277
|
|
|
/** @var \Illuminate\Contracts\View\Factory $view */ |
278
|
|
|
$view = app('view'); |
279
|
|
|
$parameters = []; |
280
|
|
|
|
281
|
|
|
if (is_array($value)) { |
282
|
|
|
$parameters = array_except($value, 0); |
283
|
|
|
$value = $value[0]; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
if (is_callable($value)) { |
287
|
|
|
return $value($parameters); |
288
|
|
|
} elseif ($this->isBuiltInRenderFunction($value)) { |
289
|
|
|
return $value; |
290
|
|
|
} elseif ($view->exists($value)) { |
291
|
|
|
return $view->make($value)->with($parameters)->render(); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
return $value ? $this->parseRenderAsString($value) : null; |
295
|
|
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Check if given key & value is a valid datatables built-in renderer function. |
299
|
|
|
* |
300
|
|
|
* @param string $value |
301
|
|
|
* @return bool |
302
|
|
|
*/ |
303
|
|
|
private function isBuiltInRenderFunction($value) |
304
|
|
|
{ |
305
|
|
|
if (empty($value)) { |
306
|
|
|
return false; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
return Str::startsWith(trim($value), ['$.fn.dataTable.render']); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Display render value as is. |
314
|
|
|
* |
315
|
|
|
* @param mixed $value |
316
|
|
|
* @return string |
317
|
|
|
*/ |
318
|
|
|
private function parseRenderAsString($value) |
319
|
|
|
{ |
320
|
|
|
return "function(data,type,full,meta){return $value;}"; |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* @return array |
325
|
|
|
*/ |
326
|
|
|
public function toArray() |
327
|
|
|
{ |
328
|
|
|
return array_except($this->attributes, ['printable', 'exportable', 'footer']); |
329
|
|
|
} |
330
|
|
|
} |
331
|
|
|
|