1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yajra\DataTables\Html; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
use Illuminate\Support\Fluent; |
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'] : Str::title($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
|
|
|
* Create a computed column that is not searchable/orderable. |
51
|
|
|
* |
52
|
|
|
* @param string $data |
53
|
|
|
* @return Column |
54
|
|
|
*/ |
55
|
|
|
public static function computed($data) |
56
|
|
|
{ |
57
|
|
|
return static::make($data)->orderable(false)->searchable(false); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Set column searchable flag. |
62
|
|
|
* |
63
|
|
|
* @param bool $flag |
64
|
|
|
* @return $this |
65
|
|
|
*/ |
66
|
|
|
public function searchable(bool $flag = true) |
67
|
|
|
{ |
68
|
|
|
$this->attributes['searchable'] = $flag; |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Set column orderable flag. |
75
|
|
|
* |
76
|
|
|
* @param bool $flag |
77
|
|
|
* @return $this |
78
|
|
|
*/ |
79
|
|
|
public function orderable(bool $flag = true) |
80
|
|
|
{ |
81
|
|
|
$this->attributes['orderable'] = $flag; |
82
|
|
|
|
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Make a new column instance. |
88
|
|
|
* |
89
|
|
|
* @param string $data |
90
|
|
|
* @param string $name |
91
|
|
|
* @return Column |
92
|
|
|
*/ |
93
|
|
|
public static function make($data, $name = '') |
94
|
|
|
{ |
95
|
|
|
$attr = [ |
96
|
|
|
'data' => $data, |
97
|
|
|
'name' => $name ?: $data, |
98
|
|
|
]; |
99
|
|
|
|
100
|
|
|
return new static($attr); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Create a checkbox column. |
105
|
|
|
* |
106
|
|
|
* @return Column |
107
|
|
|
*/ |
108
|
|
|
public static function checkbox() |
109
|
|
|
{ |
110
|
|
|
return static::make('') |
111
|
|
|
->content('') |
112
|
|
|
->className('select-checkbox') |
113
|
|
|
->orderable(false) |
114
|
|
|
->searchable(false); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Set column class name. |
119
|
|
|
* |
120
|
|
|
* @param string $class |
121
|
|
|
* @return $this |
122
|
|
|
*/ |
123
|
|
|
public function className($class) |
124
|
|
|
{ |
125
|
|
|
$this->attributes['className'] = $class; |
126
|
|
|
|
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Set column default content. |
132
|
|
|
* |
133
|
|
|
* @param string $value |
134
|
|
|
* @return $this |
135
|
|
|
*/ |
136
|
|
|
public function content($value) |
137
|
|
|
{ |
138
|
|
|
$this->attributes['defaultContent'] = $value; |
139
|
|
|
|
140
|
|
|
return $this; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Set column exportable flag. |
145
|
|
|
* |
146
|
|
|
* @param bool $flag |
147
|
|
|
* @return $this |
148
|
|
|
*/ |
149
|
|
|
public function exportable(bool $flag = true) |
150
|
|
|
{ |
151
|
|
|
$this->attributes['exportable'] = $flag; |
152
|
|
|
|
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Set column printable flag. |
158
|
|
|
* |
159
|
|
|
* @param bool $flag |
160
|
|
|
* @return $this |
161
|
|
|
*/ |
162
|
|
|
public function printable(bool $flag = true) |
163
|
|
|
{ |
164
|
|
|
$this->attributes['printable'] = $flag; |
165
|
|
|
|
166
|
|
|
return $this; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Set column width value. |
171
|
|
|
* |
172
|
|
|
* @param int|string $value |
173
|
|
|
* @return $this |
174
|
|
|
*/ |
175
|
|
|
public function width($value) |
176
|
|
|
{ |
177
|
|
|
$this->attributes['width'] = $value; |
178
|
|
|
|
179
|
|
|
return $this; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Set column title. |
184
|
|
|
* |
185
|
|
|
* @param string $value |
186
|
|
|
* @return $this |
187
|
|
|
*/ |
188
|
|
|
public function title($value) |
189
|
|
|
{ |
190
|
|
|
$this->attributes['title'] = $value; |
191
|
|
|
|
192
|
|
|
return $this; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Set column name. |
197
|
|
|
* |
198
|
|
|
* @param string $value |
199
|
|
|
* @return $this |
200
|
|
|
*/ |
201
|
|
|
public function name($value) |
202
|
|
|
{ |
203
|
|
|
$this->attributes['name'] = $value; |
204
|
|
|
|
205
|
|
|
return $this; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Set column renderer. |
210
|
|
|
* |
211
|
|
|
* @param mixed $value |
212
|
|
|
* @return $this |
213
|
|
|
*/ |
214
|
|
|
public function render($value) |
215
|
|
|
{ |
216
|
|
|
$this->attributes['render'] = $value; |
217
|
|
|
|
218
|
|
|
return $this; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Parse render attribute. |
223
|
|
|
* |
224
|
|
|
* @param mixed $value |
225
|
|
|
* @return string|null |
226
|
|
|
*/ |
227
|
|
|
public function parseRender($value) |
228
|
|
|
{ |
229
|
|
|
/** @var \Illuminate\Contracts\View\Factory $view */ |
230
|
|
|
$view = app('view'); |
231
|
|
|
$parameters = []; |
232
|
|
|
|
233
|
|
|
if (is_array($value)) { |
234
|
|
|
$parameters = array_except($value, 0); |
235
|
|
|
$value = $value[0]; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
if (is_callable($value)) { |
239
|
|
|
return $value($parameters); |
240
|
|
|
} elseif ($this->isBuiltInRenderFunction($value)) { |
241
|
|
|
return $value; |
242
|
|
|
} elseif ($view->exists($value)) { |
243
|
|
|
return $view->make($value)->with($parameters)->render(); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
return $value ? $this->parseRenderAsString($value) : null; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Check if given key & value is a valid datatables built-in renderer function. |
251
|
|
|
* |
252
|
|
|
* @param string $value |
253
|
|
|
* @return bool |
254
|
|
|
*/ |
255
|
|
|
private function isBuiltInRenderFunction($value) |
256
|
|
|
{ |
257
|
|
|
if (empty($value)) { |
258
|
|
|
return false; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
return Str::startsWith(trim($value), ['$.fn.dataTable.render']); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Display render value as is. |
266
|
|
|
* |
267
|
|
|
* @param mixed $value |
268
|
|
|
* @return string |
269
|
|
|
*/ |
270
|
|
|
private function parseRenderAsString($value) |
271
|
|
|
{ |
272
|
|
|
return "function(data,type,full,meta){return $value;}"; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* @return array |
277
|
|
|
*/ |
278
|
|
|
public function toArray() |
279
|
|
|
{ |
280
|
|
|
return array_except($this->attributes, ['printable', 'exportable', 'footer']); |
281
|
|
|
} |
282
|
|
|
} |
283
|
|
|
|