This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Yajra\DataTables\Html; |
||
4 | |||
5 | use Illuminate\Support\Arr; |
||
6 | use Illuminate\Support\Str; |
||
7 | use Illuminate\Support\Fluent; |
||
8 | use Yajra\DataTables\Html\Options\Plugins\SearchPanes; |
||
9 | |||
10 | /** |
||
11 | * @property string data |
||
12 | * @property string name |
||
13 | * @property string orderable |
||
14 | * @property string searchable |
||
15 | * @property string printable |
||
16 | * @property string exportable |
||
17 | * @property string footer |
||
18 | * @property array attributes |
||
19 | * @see https://datatables.net/reference/option/#columns |
||
20 | */ |
||
21 | class Column extends Fluent |
||
22 | { |
||
23 | use SearchPanes; |
||
24 | |||
25 | /** |
||
26 | * @param array $attributes |
||
27 | */ |
||
28 | public function __construct($attributes = []) |
||
29 | { |
||
30 | $attributes['title'] = isset($attributes['title']) ? $attributes['title'] : self::titleFormat($attributes['data']); |
||
31 | $attributes['orderable'] = isset($attributes['orderable']) ? $attributes['orderable'] : true; |
||
32 | $attributes['searchable'] = isset($attributes['searchable']) ? $attributes['searchable'] : true; |
||
33 | $attributes['exportable'] = isset($attributes['exportable']) ? $attributes['exportable'] : true; |
||
34 | $attributes['printable'] = isset($attributes['printable']) ? $attributes['printable'] : true; |
||
35 | $attributes['footer'] = isset($attributes['footer']) ? $attributes['footer'] : ''; |
||
36 | $attributes['attributes'] = isset($attributes['attributes']) ? $attributes['attributes'] : []; |
||
37 | |||
38 | // Allow methods override attribute value |
||
39 | foreach ($attributes as $attribute => $value) { |
||
40 | $method = 'parse' . ucfirst(strtolower($attribute)); |
||
41 | if (! is_null($value) && method_exists($this, $method)) { |
||
42 | $attributes[$attribute] = $this->$method($value); |
||
43 | } |
||
44 | } |
||
45 | |||
46 | if (! isset($attributes['name']) && isset($attributes['data'])) { |
||
47 | $attributes['name'] = $attributes['data']; |
||
48 | } |
||
49 | |||
50 | parent::__construct($attributes); |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * Format string to title case. |
||
55 | * |
||
56 | * @param string $value |
||
57 | * @return string |
||
58 | */ |
||
59 | public static function titleFormat($value) |
||
60 | { |
||
61 | return Str::title(str_replace('_', ' ', $value)); |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Create a computed column that is not searchable/orderable. |
||
66 | * |
||
67 | * @param string $data |
||
68 | * @param string|null $title |
||
69 | * @return Column |
||
70 | */ |
||
71 | public static function computed($data, $title = null) |
||
72 | { |
||
73 | if (is_null($title)) { |
||
74 | $title = self::titleFormat($data); |
||
75 | } |
||
76 | |||
77 | return static::make($data)->title($title)->orderable(false)->searchable(false); |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * Set column searchable flag. |
||
82 | * |
||
83 | * @param bool $flag |
||
84 | * @return $this |
||
85 | * @see https://datatables.net/reference/option/columns.searchable |
||
86 | */ |
||
87 | public function searchable(bool $flag = true) |
||
88 | { |
||
89 | $this->attributes['searchable'] = $flag; |
||
90 | |||
91 | return $this; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Set column orderable flag. |
||
96 | * |
||
97 | * @param bool $flag |
||
98 | * @return $this |
||
99 | * @see https://datatables.net/reference/option/columns.orderable |
||
100 | */ |
||
101 | public function orderable(bool $flag = true) |
||
102 | { |
||
103 | $this->attributes['orderable'] = $flag; |
||
104 | |||
105 | return $this; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Set column responsive priority. |
||
110 | * |
||
111 | * @param int|string $value |
||
112 | * @return $this |
||
113 | * @see https://datatables.net/reference/option/columns.responsivePriority |
||
114 | */ |
||
115 | public function responsivePriority($value) |
||
116 | { |
||
117 | $this->attributes['responsivePriority'] = $value; |
||
118 | |||
119 | return $this; |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * Set column title. |
||
124 | * |
||
125 | * @param string $value |
||
126 | * @return $this |
||
127 | * @see https://datatables.net/reference/option/columns.title |
||
128 | */ |
||
129 | public function title($value) |
||
130 | { |
||
131 | $this->attributes['title'] = $value; |
||
132 | |||
133 | return $this; |
||
134 | } |
||
135 | |||
136 | /** |
||
137 | * Make a new column instance. |
||
138 | * |
||
139 | * @param string $data |
||
140 | * @param string $name |
||
141 | * @return Column |
||
142 | */ |
||
143 | public static function make($data, $name = '') |
||
144 | { |
||
145 | $attr = [ |
||
146 | 'data' => $data, |
||
147 | 'name' => $name ?: $data, |
||
148 | ]; |
||
149 | |||
150 | return new static($attr); |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * Make a new formatted column instance. |
||
155 | * |
||
156 | * @param string $name |
||
157 | * @return Column |
||
158 | */ |
||
159 | public static function formatted($name) |
||
160 | { |
||
161 | $attr = [ |
||
162 | 'data' => $name, |
||
163 | 'name' => $name, |
||
164 | 'title' => self::titleFormat($name), |
||
165 | 'render' => 'full.'.$name.'_formatted', |
||
166 | ]; |
||
167 | |||
168 | return new static($attr); |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * Create a checkbox column. |
||
173 | * |
||
174 | * @param string $title |
||
175 | * @return Column |
||
176 | */ |
||
177 | public static function checkbox($title = '') |
||
178 | { |
||
179 | return static::make('') |
||
180 | ->content('') |
||
181 | ->title($title) |
||
182 | ->className('select-checkbox') |
||
183 | ->orderable(false) |
||
184 | ->exportable(false) |
||
185 | ->searchable(false); |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Set column class name. |
||
190 | * |
||
191 | * @param string $class |
||
192 | * @return $this |
||
193 | * @see https://datatables.net/reference/option/columns.className |
||
194 | */ |
||
195 | public function className($class) |
||
196 | { |
||
197 | $this->attributes['className'] = $class; |
||
198 | |||
199 | return $this; |
||
200 | } |
||
201 | |||
202 | /** |
||
203 | * Set column default content. |
||
204 | * |
||
205 | * @param string $value |
||
206 | * @return $this |
||
207 | * @see https://datatables.net/reference/option/columns.defaultContent |
||
208 | */ |
||
209 | public function content($value) |
||
210 | { |
||
211 | $this->attributes['defaultContent'] = $value; |
||
212 | |||
213 | return $this; |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * Set column visible flag. |
||
218 | * |
||
219 | * @param bool $flag |
||
220 | * @return $this |
||
221 | * @see https://datatables.net/reference/option/columns.visible |
||
222 | */ |
||
223 | public function visible(bool $flag = true) |
||
224 | { |
||
225 | $this->attributes['visible'] = $flag; |
||
226 | |||
227 | return $this; |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Set column hidden state. |
||
232 | * |
||
233 | * @return $this |
||
234 | * @see https://datatables.net/reference/option/columns.visible |
||
235 | */ |
||
236 | public function hidden() |
||
237 | { |
||
238 | return $this->visible(false); |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * Append a class name to field. |
||
243 | * |
||
244 | * @param string $class |
||
245 | * @return $this |
||
246 | */ |
||
247 | View Code Duplication | public function addClass($class) |
|
0 ignored issues
–
show
|
|||
248 | { |
||
249 | if (! isset($this->attributes['className'])) { |
||
250 | $this->attributes['className'] = $class; |
||
251 | } else { |
||
252 | $this->attributes['className'] .= " $class"; |
||
253 | } |
||
254 | |||
255 | return $this; |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * Set column exportable flag. |
||
260 | * |
||
261 | * @param bool $flag |
||
262 | * @return $this |
||
263 | */ |
||
264 | public function exportable(bool $flag = true) |
||
265 | { |
||
266 | $this->attributes['exportable'] = $flag; |
||
267 | |||
268 | return $this; |
||
269 | } |
||
270 | |||
271 | /** |
||
272 | * Set column printable flag. |
||
273 | * |
||
274 | * @param bool $flag |
||
275 | * @return $this |
||
276 | */ |
||
277 | public function printable(bool $flag = true) |
||
278 | { |
||
279 | $this->attributes['printable'] = $flag; |
||
280 | |||
281 | return $this; |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * Set column width value. |
||
286 | * |
||
287 | * @param int|string $value |
||
288 | * @return $this |
||
289 | * @see https://datatables.net/reference/option/columns.width |
||
290 | */ |
||
291 | public function width($value) |
||
292 | { |
||
293 | $this->attributes['width'] = $value; |
||
294 | |||
295 | return $this; |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * Set column data option value. |
||
300 | * |
||
301 | * @param string $value |
||
302 | * @return $this |
||
303 | * @see https://datatables.net/reference/option/columns.data |
||
304 | */ |
||
305 | public function data($value) |
||
306 | { |
||
307 | $this->attributes['data'] = $value; |
||
308 | |||
309 | return $this; |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * Set column name option value. |
||
314 | * |
||
315 | * @param string $value |
||
316 | * @return $this |
||
317 | * @see https://datatables.net/reference/option/columns.name |
||
318 | */ |
||
319 | public function name($value) |
||
320 | { |
||
321 | $this->attributes['name'] = $value; |
||
322 | |||
323 | return $this; |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * Set column edit field option value. |
||
328 | * |
||
329 | * @param string $value |
||
330 | * @return $this |
||
331 | * @see https://datatables.net/reference/option/columns.editField |
||
332 | */ |
||
333 | public function editField($value) |
||
334 | { |
||
335 | $this->attributes['editField'] = $value; |
||
336 | |||
337 | return $this; |
||
338 | } |
||
339 | |||
340 | /** |
||
341 | * Set column orderData option value. |
||
342 | * |
||
343 | * @param mixed $value |
||
344 | * @return $this |
||
345 | * @see https://datatables.net/reference/option/columns.orderData |
||
346 | */ |
||
347 | public function orderData($value) |
||
348 | { |
||
349 | $this->attributes['orderData'] = $value; |
||
350 | |||
351 | return $this; |
||
352 | } |
||
353 | |||
354 | /** |
||
355 | * Set column orderDataType option value. |
||
356 | * |
||
357 | * @param mixed $value |
||
358 | * @return $this |
||
359 | * @see https://datatables.net/reference/option/columns.orderDataType |
||
360 | */ |
||
361 | public function orderDataType($value) |
||
362 | { |
||
363 | $this->attributes['orderDataType'] = $value; |
||
364 | |||
365 | return $this; |
||
366 | } |
||
367 | |||
368 | /** |
||
369 | * Set column orderSequence option value. |
||
370 | * |
||
371 | * @param mixed $value |
||
372 | * @return $this |
||
373 | * @see https://datatables.net/reference/option/columns.orderSequence |
||
374 | */ |
||
375 | public function orderSequence($value) |
||
376 | { |
||
377 | $this->attributes['orderSequence'] = $value; |
||
378 | |||
379 | return $this; |
||
380 | } |
||
381 | |||
382 | /** |
||
383 | * Set column cellType option value. |
||
384 | * |
||
385 | * @param mixed $value |
||
386 | * @return $this |
||
387 | * @see https://datatables.net/reference/option/columns.cellType |
||
388 | */ |
||
389 | public function cellType($value) |
||
390 | { |
||
391 | $this->attributes['cellType'] = $value; |
||
392 | |||
393 | return $this; |
||
394 | } |
||
395 | |||
396 | /** |
||
397 | * Set column type option value. |
||
398 | * |
||
399 | * @param mixed $value |
||
400 | * @return $this |
||
401 | * @see https://datatables.net/reference/option/columns.type |
||
402 | */ |
||
403 | public function type($value) |
||
404 | { |
||
405 | $this->attributes['type'] = $value; |
||
406 | |||
407 | return $this; |
||
408 | } |
||
409 | |||
410 | /** |
||
411 | * Set column contentPadding option value. |
||
412 | * |
||
413 | * @param mixed $value |
||
414 | * @return $this |
||
415 | * @see https://datatables.net/reference/option/columns.contentPadding |
||
416 | */ |
||
417 | public function contentPadding($value) |
||
418 | { |
||
419 | $this->attributes['contentPadding'] = $value; |
||
420 | |||
421 | return $this; |
||
422 | } |
||
423 | |||
424 | /** |
||
425 | * Set column createdCell option value. |
||
426 | * |
||
427 | * @param mixed $value |
||
428 | * @return $this |
||
429 | * @see https://datatables.net/reference/option/columns.createdCell |
||
430 | */ |
||
431 | public function createdCell($value) |
||
432 | { |
||
433 | $this->attributes['createdCell'] = $value; |
||
434 | |||
435 | return $this; |
||
436 | } |
||
437 | |||
438 | /** |
||
439 | * Use the js renderer "$.fn.dataTable.render.". |
||
440 | * |
||
441 | * @param mixed $value |
||
442 | * @param mixed ...$params |
||
443 | * @return $this |
||
444 | * @see https://datatables.net/reference/option/columns.render |
||
445 | */ |
||
446 | public function renderJs($value, ...$params) |
||
447 | { |
||
448 | if ($params) { |
||
0 ignored issues
–
show
The expression
$params of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent. Consider making the comparison explicit by using ![]() |
|||
449 | $value .= '('; |
||
450 | foreach ($params as $param) { |
||
451 | $value .= "'{$param}',"; |
||
452 | } |
||
453 | $value = mb_substr($value, 0, -1); |
||
454 | $value .= ')'; |
||
455 | } |
||
456 | |||
457 | $renderer = '$.fn.dataTable.render.' . $value; |
||
458 | |||
459 | return $this->render($renderer); |
||
460 | } |
||
461 | |||
462 | /** |
||
463 | * Set column renderer. |
||
464 | * |
||
465 | * @param mixed $value |
||
466 | * @return $this |
||
467 | * @see https://datatables.net/reference/option/columns.render |
||
468 | */ |
||
469 | public function render($value) |
||
470 | { |
||
471 | $this->attributes['render'] = $this->parseRender($value); |
||
472 | |||
473 | return $this; |
||
474 | } |
||
475 | |||
476 | /** |
||
477 | * Set column renderer with give raw value. |
||
478 | * |
||
479 | * @param mixed $value |
||
480 | * @return $this |
||
481 | * @see https://datatables.net/reference/option/columns.render |
||
482 | */ |
||
483 | public function renderRaw($value) |
||
484 | { |
||
485 | $this->attributes['render'] = $value; |
||
486 | |||
487 | return $this; |
||
488 | } |
||
489 | |||
490 | /** |
||
491 | * Parse render attribute. |
||
492 | * |
||
493 | * @param mixed $value |
||
494 | * @return string|null |
||
495 | */ |
||
496 | public function parseRender($value) |
||
497 | { |
||
498 | /** @var \Illuminate\Contracts\View\Factory $view */ |
||
499 | $view = app('view'); |
||
500 | $parameters = []; |
||
501 | |||
502 | if (is_array($value)) { |
||
503 | $parameters = Arr::except($value, 0); |
||
504 | $value = $value[0]; |
||
505 | } |
||
506 | |||
507 | if (is_callable($value)) { |
||
508 | return $value($parameters); |
||
509 | } elseif ($this->isBuiltInRenderFunction($value)) { |
||
510 | return $value; |
||
511 | } elseif ($view->exists($value)) { |
||
512 | return $view->make($value)->with($parameters)->render(); |
||
513 | } |
||
514 | |||
515 | return $value ? $this->parseRenderAsString($value) : null; |
||
516 | } |
||
517 | |||
518 | /** |
||
519 | * Check if given key & value is a valid datatables built-in renderer function. |
||
520 | * |
||
521 | * @param string $value |
||
522 | * @return bool |
||
523 | */ |
||
524 | private function isBuiltInRenderFunction($value) |
||
525 | { |
||
526 | if (empty($value)) { |
||
527 | return false; |
||
528 | } |
||
529 | |||
530 | return Str::startsWith(trim($value), ['$.fn.dataTable.render', '[']); |
||
531 | } |
||
532 | |||
533 | /** |
||
534 | * Display render value as is. |
||
535 | * |
||
536 | * @param mixed $value |
||
537 | * @return string |
||
538 | */ |
||
539 | private function parseRenderAsString($value) |
||
540 | { |
||
541 | return "function(data,type,full,meta){return $value;}"; |
||
542 | } |
||
543 | |||
544 | /** |
||
545 | * Set column footer. |
||
546 | * |
||
547 | * @param mixed $value |
||
548 | * @return $this |
||
549 | */ |
||
550 | public function footer($value) |
||
551 | { |
||
552 | $this->attributes['footer'] = $value; |
||
553 | |||
554 | return $this; |
||
555 | } |
||
556 | |||
557 | /** |
||
558 | * Set custom html title instead defult label. |
||
559 | * |
||
560 | * @param mixed $value |
||
561 | * @return $this |
||
562 | */ |
||
563 | public function titleAttr($value) |
||
564 | { |
||
565 | $this->attributes['titleAttr'] = $value; |
||
566 | |||
567 | return $this; |
||
568 | } |
||
569 | |||
570 | /** |
||
571 | * @return array |
||
572 | */ |
||
573 | public function toArray() |
||
574 | { |
||
575 | return Arr::except($this->attributes, [ |
||
576 | 'printable', |
||
577 | 'exportable', |
||
578 | 'footer', |
||
579 | ]); |
||
580 | } |
||
581 | } |
||
582 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.