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['orderable'] = isset($attributes['orderable']) ? $attributes['orderable'] : true; |
27
|
|
|
$attributes['searchable'] = isset($attributes['searchable']) ? $attributes['searchable'] : true; |
28
|
|
|
$attributes['exportable'] = isset($attributes['exportable']) ? $attributes['exportable'] : true; |
29
|
|
|
$attributes['printable'] = isset($attributes['printable']) ? $attributes['printable'] : true; |
30
|
|
|
$attributes['footer'] = isset($attributes['footer']) ? $attributes['footer'] : ''; |
31
|
|
|
$attributes['attributes'] = isset($attributes['attributes']) ? $attributes['attributes'] : []; |
32
|
|
|
|
33
|
|
|
// Allow methods override attribute value |
34
|
|
|
foreach ($attributes as $attribute => $value) { |
35
|
|
|
$method = 'parse' . ucfirst(strtolower($attribute)); |
36
|
|
|
if (method_exists($this, $method)) { |
37
|
|
|
$attributes[$attribute] = $this->$method($value); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if (! isset($attributes['name']) && isset($attributes['data'])) { |
42
|
|
|
$attributes['name'] = $attributes['data']; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
parent::__construct($attributes); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Parse render attribute. |
50
|
|
|
* |
51
|
|
|
* @param mixed $value |
52
|
|
|
* @return string|null |
53
|
|
|
*/ |
54
|
|
|
public function parseRender($value) |
55
|
|
|
{ |
56
|
|
|
/** @var \Illuminate\Contracts\View\Factory $view */ |
57
|
|
|
$view = app('view'); |
58
|
|
|
$parameters = []; |
59
|
|
|
|
60
|
|
|
if (is_array($value)) { |
61
|
|
|
$parameters = array_except($value, 0); |
62
|
|
|
$value = $value[0]; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if (is_callable($value)) { |
66
|
|
|
return $value($parameters); |
67
|
|
|
} elseif ($this->isBuiltInRenderFunction($value)) { |
68
|
|
|
return $value; |
69
|
|
|
} elseif ($view->exists($value)) { |
70
|
|
|
return $view->make($value)->with($parameters)->render(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return $value ? $this->parseRenderAsString($value) : null; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return array |
78
|
|
|
*/ |
79
|
|
|
public function toArray() |
80
|
|
|
{ |
81
|
|
|
return array_except($this->attributes, ['printable', 'exportable', 'footer']); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Display render value as is. |
86
|
|
|
* |
87
|
|
|
* @param mixed $value |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
private function parseRenderAsString($value) |
91
|
|
|
{ |
92
|
|
|
return "function(data,type,full,meta){return $value;}"; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Check if given key & value is a valid datatables built-in renderer function. |
97
|
|
|
* |
98
|
|
|
* @param string $value |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
|
|
private function isBuiltInRenderFunction($value) |
102
|
|
|
{ |
103
|
|
|
if (empty($value)) { |
104
|
|
|
return false; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return Str::startsWith(trim($value), ['$.fn.dataTable.render']); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|