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