1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yajra\DataTables\Html; |
4
|
|
|
|
5
|
|
|
use Collective\Html\HtmlBuilder; |
6
|
|
|
use Illuminate\Contracts\Config\Repository; |
7
|
|
|
use Illuminate\Contracts\View\Factory; |
8
|
|
|
use Illuminate\Support\Collection; |
9
|
|
|
use Illuminate\Support\HtmlString; |
10
|
|
|
use Illuminate\Support\Str; |
11
|
|
|
use Illuminate\Support\Traits\Macroable; |
12
|
|
|
use Yajra\DataTables\Html\Columns; |
13
|
|
|
use Yajra\DataTables\Html\Editor\HasEditor; |
14
|
|
|
use Yajra\DataTables\Html\Options\HasOptions; |
15
|
|
|
|
16
|
|
|
class Builder |
17
|
|
|
{ |
18
|
|
|
use Macroable; |
19
|
|
|
use HasOptions; |
20
|
|
|
use HasTable; |
21
|
|
|
use HasEditor; |
22
|
|
|
use Columns\Index; |
23
|
|
|
use Columns\Action; |
24
|
|
|
use Columns\Checkbox; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Collection |
28
|
|
|
*/ |
29
|
|
|
public $collection; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var Repository |
33
|
|
|
*/ |
34
|
|
|
public $config; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var Factory |
38
|
|
|
*/ |
39
|
|
|
public $view; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var HtmlBuilder |
43
|
|
|
*/ |
44
|
|
|
public $html; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
protected $tableAttributes = []; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var string |
53
|
|
|
*/ |
54
|
|
|
protected $template = ''; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var array |
58
|
|
|
*/ |
59
|
|
|
protected $attributes = []; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param Repository $config |
63
|
|
|
* @param Factory $view |
64
|
|
|
* @param HtmlBuilder $html |
65
|
|
|
*/ |
66
|
|
|
public function __construct(Repository $config, Factory $view, HtmlBuilder $html) |
67
|
|
|
{ |
68
|
|
|
$this->config = $config; |
69
|
|
|
$this->view = $view; |
70
|
|
|
$this->html = $html; |
71
|
|
|
$this->collection = new Collection; |
72
|
|
|
$this->tableAttributes = $this->config->get('datatables-html.table', []); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Generate DataTable javascript. |
77
|
|
|
* |
78
|
|
|
* @param null $script |
79
|
|
|
* @param array $attributes |
80
|
|
|
* @return \Illuminate\Support\HtmlString |
81
|
|
|
* @throws \Exception |
82
|
|
|
*/ |
83
|
|
|
public function scripts($script = null, array $attributes = ['type' => 'text/javascript']) |
84
|
|
|
{ |
85
|
|
|
$script = $script ?: $this->generateScripts(); |
86
|
|
|
$attributes = $this->html->attributes($attributes); |
87
|
|
|
|
88
|
|
|
return new HtmlString("<script{$attributes}>{$script}</script>\n"); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get generated raw scripts. |
93
|
|
|
* |
94
|
|
|
* @return \Illuminate\Support\HtmlString |
95
|
|
|
* @throws \Exception |
96
|
|
|
*/ |
97
|
|
|
public function generateScripts() |
98
|
|
|
{ |
99
|
|
|
$parameters = $this->generateJson(); |
100
|
|
|
|
101
|
|
|
return new HtmlString( |
102
|
|
|
sprintf($this->template(), $this->getTableAttribute('id'), $parameters) |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get generated json configuration. |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
public function generateJson() |
112
|
|
|
{ |
113
|
|
|
$args = array_merge( |
114
|
|
|
$this->attributes, [ |
115
|
|
|
'ajax' => $this->ajax, |
116
|
|
|
'columns' => $this->collection->map(function (Column $column) { |
117
|
|
|
$column = $column->toArray(); |
118
|
|
|
unset($column['attributes']); |
119
|
|
|
|
120
|
|
|
return $column; |
121
|
|
|
})->toArray(), |
122
|
|
|
] |
123
|
|
|
); |
124
|
|
|
|
125
|
|
|
return $this->parameterize($args); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Generate DataTables js parameters. |
130
|
|
|
* |
131
|
|
|
* @param array $attributes |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
|
|
public function parameterize($attributes = []) |
135
|
|
|
{ |
136
|
|
|
$parameters = (new Parameters($attributes))->toArray(); |
137
|
|
|
|
138
|
|
|
$values = []; |
139
|
|
|
$replacements = []; |
140
|
|
|
|
141
|
|
|
foreach (array_dot($parameters) as $key => $value) { |
142
|
|
|
if ($this->isCallbackFunction($value, $key)) { |
143
|
|
|
$values[] = trim($value); |
144
|
|
|
array_set($parameters, $key, '%' . $key . '%'); |
145
|
|
|
$replacements[] = '"%' . $key . '%"'; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
$new = []; |
150
|
|
|
foreach ($parameters as $key => $value) { |
151
|
|
|
array_set($new, $key, $value); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$json = json_encode($new); |
155
|
|
|
|
156
|
|
|
$json = str_replace($replacements, $values, $json); |
157
|
|
|
|
158
|
|
|
return $json; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Check if given key & value is a valid callback js function. |
163
|
|
|
* |
164
|
|
|
* @param string $value |
165
|
|
|
* @param string $key |
166
|
|
|
* @return bool |
167
|
|
|
*/ |
168
|
|
|
protected function isCallbackFunction($value, $key) |
169
|
|
|
{ |
170
|
|
|
if (empty($value)) { |
171
|
|
|
return false; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
$callbacks = $this->config->get('datatables-html.callback', ['$', '$.', 'function']); |
175
|
|
|
|
176
|
|
|
return Str::startsWith(trim($value), $callbacks) || Str::contains($key, 'editor'); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Get javascript template to use. |
181
|
|
|
* |
182
|
|
|
* @return string |
183
|
|
|
*/ |
184
|
|
|
protected function template() |
185
|
|
|
{ |
186
|
|
|
$template = $this->template ?: $this->config->get('datatables-html.script', 'datatables::script'); |
187
|
|
|
|
188
|
|
|
return $this->view->make($template, ['editors' => $this->editors])->render(); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Generate DataTable's table html. |
193
|
|
|
* |
194
|
|
|
* @param array $attributes |
195
|
|
|
* @param bool $drawFooter |
196
|
|
|
* @param bool $drawSearch |
197
|
|
|
* @return \Illuminate\Support\HtmlString |
198
|
|
|
*/ |
199
|
|
|
public function table(array $attributes = [], $drawFooter = false, $drawSearch = false) |
200
|
|
|
{ |
201
|
|
|
$this->setTableAttributes($attributes); |
202
|
|
|
|
203
|
|
|
$th = $this->compileTableHeaders(); |
204
|
|
|
$htmlAttr = $this->html->attributes($this->tableAttributes); |
205
|
|
|
|
206
|
|
|
$tableHtml = '<table ' . $htmlAttr . '>'; |
207
|
|
|
$searchHtml = $drawSearch ? '<tr class="search-filter">' . implode('', |
208
|
|
|
$this->compileTableSearchHeaders()) . '</tr>' : ''; |
209
|
|
|
$tableHtml .= '<thead><tr>' . implode('', $th) . '</tr>' . $searchHtml . '</thead>'; |
210
|
|
|
if ($drawFooter) { |
211
|
|
|
$tf = $this->compileTableFooter(); |
212
|
|
|
$tableHtml .= '<tfoot><tr>' . implode('', $tf) . '</tr></tfoot>'; |
213
|
|
|
} |
214
|
|
|
$tableHtml .= '</table>'; |
215
|
|
|
|
216
|
|
|
return new HtmlString($tableHtml); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Configure DataTable's parameters. |
221
|
|
|
* |
222
|
|
|
* @param array $attributes |
223
|
|
|
* @return $this |
224
|
|
|
*/ |
225
|
|
|
public function parameters(array $attributes = []) |
226
|
|
|
{ |
227
|
|
|
$this->attributes = array_merge($this->attributes, $attributes); |
228
|
|
|
|
229
|
|
|
return $this; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Set custom javascript template. |
234
|
|
|
* |
235
|
|
|
* @param string $template |
236
|
|
|
* @return $this |
237
|
|
|
*/ |
238
|
|
|
public function setTemplate($template) |
239
|
|
|
{ |
240
|
|
|
$this->template = $template; |
241
|
|
|
|
242
|
|
|
return $this; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Make a data script to be appended on ajax request of dataTables. |
247
|
|
|
* |
248
|
|
|
* @param array $data |
249
|
|
|
* @return string |
250
|
|
|
*/ |
251
|
|
|
protected function makeDataScript(array $data) |
252
|
|
|
{ |
253
|
|
|
$script = ''; |
254
|
|
|
foreach ($data as $key => $value) { |
255
|
|
|
$script .= PHP_EOL . "data.{$key} = '{$value}';"; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
return $script; |
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..