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