|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Yajra\DataTables\Html\Editor; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
|
6
|
|
|
use Illuminate\Support\Str; |
|
7
|
|
|
use Illuminate\Support\Fluent; |
|
8
|
|
|
use Yajra\DataTables\Utilities\Helper; |
|
9
|
|
|
use Yajra\DataTables\Html\Editor\Fields\Field; |
|
10
|
|
|
|
|
11
|
|
|
class Editor extends Fluent |
|
12
|
|
|
{ |
|
13
|
|
|
use HasEvents; |
|
14
|
|
|
const DISPLAY_LIGHTBOX = 'lightbox'; |
|
15
|
|
|
const DISPLAY_ENVELOPE = 'envelope'; |
|
16
|
|
|
const DISPLAY_BOOTSTRAP = 'bootstrap'; |
|
17
|
|
|
const DISPLAY_FOUNDATION = 'foundation'; |
|
18
|
|
|
const DISPLAY_JQUERYUI = 'jqueryui'; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Editor constructor. |
|
22
|
|
|
* |
|
23
|
|
|
* @param string $instance |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct($instance = 'editor') |
|
26
|
|
|
{ |
|
27
|
|
|
$attributes['instance'] = $instance; |
|
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
parent::__construct($attributes); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Make new Editor instance. |
|
34
|
|
|
* |
|
35
|
|
|
* @param string $instance |
|
36
|
|
|
* @return Editor |
|
37
|
|
|
*/ |
|
38
|
|
|
public static function make($instance = 'editor') |
|
39
|
|
|
{ |
|
40
|
|
|
return new static($instance); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Append raw scripts. |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $scripts |
|
47
|
|
|
* @return Editor |
|
48
|
|
|
*/ |
|
49
|
|
|
public function scripts($scripts) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->attributes['scripts'] = $scripts; |
|
52
|
|
|
|
|
53
|
|
|
return $this; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Set Editor's variable name / instance. |
|
58
|
|
|
* |
|
59
|
|
|
* @param $instance |
|
60
|
|
|
* @return $this |
|
61
|
|
|
*/ |
|
62
|
|
|
public function instance($instance) |
|
63
|
|
|
{ |
|
64
|
|
|
$this->attributes['instance'] = $instance; |
|
65
|
|
|
|
|
66
|
|
|
return $this; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Set Editor's ajax parameter. |
|
71
|
|
|
* |
|
72
|
|
|
* @param string|array $ajax |
|
73
|
|
|
* @return $this |
|
74
|
|
|
* @see https://editor.datatables.net/reference/option/ajax |
|
75
|
|
|
*/ |
|
76
|
|
|
public function ajax($ajax) |
|
77
|
|
|
{ |
|
78
|
|
|
$this->attributes['ajax'] = $ajax; |
|
79
|
|
|
|
|
80
|
|
|
return $this; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Set Editor's table source. |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $table |
|
87
|
|
|
* @return $this |
|
88
|
|
|
* @see https://editor.datatables.net/reference/option/table |
|
89
|
|
|
*/ |
|
90
|
|
|
public function table($table) |
|
91
|
|
|
{ |
|
92
|
|
|
$this->attributes['table'] = $table; |
|
93
|
|
|
|
|
94
|
|
|
return $this; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Set Editor's idSrc option. |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $idSrc |
|
101
|
|
|
* @return $this |
|
102
|
|
|
* @see https://editor.datatables.net/reference/option/idSrc |
|
103
|
|
|
*/ |
|
104
|
|
|
public function idSrc($idSrc = 'DT_RowId') |
|
105
|
|
|
{ |
|
106
|
|
|
$this->attributes['idSrc'] = $idSrc; |
|
107
|
|
|
|
|
108
|
|
|
return $this; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Set Editor's display option. |
|
113
|
|
|
* |
|
114
|
|
|
* @param string $display |
|
115
|
|
|
* @return $this |
|
116
|
|
|
* @see https://editor.datatables.net/reference/option/display |
|
117
|
|
|
*/ |
|
118
|
|
|
public function display($display) |
|
119
|
|
|
{ |
|
120
|
|
|
$this->attributes['display'] = $display; |
|
121
|
|
|
|
|
122
|
|
|
return $this; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Set Editor's fields. |
|
127
|
|
|
* |
|
128
|
|
|
* @param array $fields |
|
129
|
|
|
* @return $this |
|
130
|
|
|
* @see https://editor.datatables.net/reference/option/fields |
|
131
|
|
|
*/ |
|
132
|
|
|
public function fields(array $fields) |
|
133
|
|
|
{ |
|
134
|
|
|
$this->attributes['fields'] = $fields; |
|
135
|
|
|
|
|
136
|
|
|
return $this; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Set Editor's formOptions. |
|
141
|
|
|
* |
|
142
|
|
|
* @param mixed $formOptions |
|
143
|
|
|
* @return $this |
|
144
|
|
|
* @see https://editor.datatables.net/reference/option/formOptions |
|
145
|
|
|
* @see https://editor.datatables.net/reference/type/form-options |
|
146
|
|
|
*/ |
|
147
|
|
|
public function formOptions(array $formOptions) |
|
148
|
|
|
{ |
|
149
|
|
|
$this->attributes['formOptions'] = $formOptions; |
|
150
|
|
|
|
|
151
|
|
|
return $this; |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Set Editor's bubble formOptions. |
|
156
|
|
|
* |
|
157
|
|
|
* @param mixed $formOptions |
|
158
|
|
|
* @return $this |
|
159
|
|
|
* @see https://editor.datatables.net/reference/option/formOptions.bubble |
|
160
|
|
|
*/ |
|
161
|
|
|
public function formOptionsBubble(array $formOptions) |
|
162
|
|
|
{ |
|
163
|
|
|
$this->attributes['formOptions']['bubble'] = Helper::castToArray($formOptions);; |
|
164
|
|
|
|
|
165
|
|
|
return $this; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Set Editor's inline formOptions. |
|
170
|
|
|
* |
|
171
|
|
|
* @param mixed $formOptions |
|
172
|
|
|
* @return $this |
|
173
|
|
|
* @see https://editor.datatables.net/reference/option/formOptions.inline |
|
174
|
|
|
*/ |
|
175
|
|
|
public function formOptionsInline($formOptions) |
|
176
|
|
|
{ |
|
177
|
|
|
$this->attributes['formOptions']['inline'] = Helper::castToArray($formOptions); |
|
178
|
|
|
|
|
179
|
|
|
return $this; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Set Editor's main formOptions. |
|
184
|
|
|
* |
|
185
|
|
|
* @param mixed $formOptions |
|
186
|
|
|
* @return $this |
|
187
|
|
|
* @see https://editor.datatables.net/reference/option/formOptions.main |
|
188
|
|
|
*/ |
|
189
|
|
|
public function formOptionsMain($formOptions) |
|
190
|
|
|
{ |
|
191
|
|
|
$this->attributes['formOptions']['main'] = Helper::castToArray($formOptions); |
|
192
|
|
|
|
|
193
|
|
|
return $this; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* Set Editor's language. |
|
198
|
|
|
* |
|
199
|
|
|
* @param array $language |
|
200
|
|
|
* @return $this |
|
201
|
|
|
* @see https://editor.datatables.net/reference/option/i18n |
|
202
|
|
|
*/ |
|
203
|
|
|
public function language(array $language) |
|
204
|
|
|
{ |
|
205
|
|
|
$this->attributes['i18n'] = $language; |
|
206
|
|
|
|
|
207
|
|
|
return $this; |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Set Editor's template. |
|
212
|
|
|
* |
|
213
|
|
|
* @param string $template |
|
214
|
|
|
* @return $this |
|
215
|
|
|
* @see https://editor.datatables.net/reference/option/template |
|
216
|
|
|
*/ |
|
217
|
|
|
public function template($template) |
|
218
|
|
|
{ |
|
219
|
|
|
$this->attributes['template'] = $template; |
|
220
|
|
|
|
|
221
|
|
|
return $this; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* Convert the fluent instance to an array. |
|
226
|
|
|
* |
|
227
|
|
|
* @return array |
|
228
|
|
|
*/ |
|
229
|
|
|
public function toArray() |
|
230
|
|
|
{ |
|
231
|
|
|
$array = parent::toArray(); |
|
232
|
|
|
|
|
233
|
|
|
unset($array['events']); |
|
234
|
|
|
|
|
235
|
|
|
foreach (Arr::get($array, 'fields', []) as $key => &$field) { |
|
|
|
|
|
|
236
|
|
|
if ($field instanceof Field) { |
|
237
|
|
|
Arr::set($array['fields'], $key, $field->toArray()); |
|
238
|
|
|
} |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
return $array; |
|
242
|
|
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* Convert the fluent instance to JSON. |
|
246
|
|
|
* |
|
247
|
|
|
* @param int $options |
|
248
|
|
|
* @return string |
|
249
|
|
|
*/ |
|
250
|
|
|
public function toJson($options = 0) |
|
251
|
|
|
{ |
|
252
|
|
|
$parameters = $this->jsonSerialize(); |
|
253
|
|
|
|
|
254
|
|
|
unset($parameters['events']); |
|
255
|
|
|
|
|
256
|
|
|
$values = []; |
|
257
|
|
|
$replacements = []; |
|
258
|
|
|
|
|
259
|
|
|
foreach (Arr::dot($parameters) as $key => $value) { |
|
260
|
|
|
if ($key === 'table') { |
|
261
|
|
|
Arr::set($parameters, $key, '#' . $value); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
View Code Duplication |
if ($this->isCallbackFunction($value, $key)) { |
|
|
|
|
|
|
265
|
|
|
$values[] = trim($value); |
|
266
|
|
|
Arr::set($parameters, $key, '%' . $key . '%'); |
|
267
|
|
|
$replacements[] = '"%' . $key . '%"'; |
|
268
|
|
|
} |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
$new = []; |
|
272
|
|
|
foreach ($parameters as $key => $value) { |
|
273
|
|
|
Arr::set($new, $key, $value); |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
$json = json_encode($new, $options); |
|
277
|
|
|
|
|
278
|
|
|
$json = str_replace($replacements, $values, $json); |
|
279
|
|
|
|
|
280
|
|
|
return $json; |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* Check if given key & value is a valid callback js function. |
|
285
|
|
|
* |
|
286
|
|
|
* @param string $value |
|
287
|
|
|
* @param string $key |
|
288
|
|
|
* @return bool |
|
289
|
|
|
*/ |
|
290
|
|
|
protected function isCallbackFunction($value, $key) |
|
291
|
|
|
{ |
|
292
|
|
|
if (empty($value) || is_object($value) || is_array($value)) { |
|
293
|
|
|
return false; |
|
294
|
|
|
} |
|
295
|
|
|
|
|
296
|
|
|
$callbacks = config('datatables-html.callback', ['$', '$.', 'function']); |
|
297
|
|
|
|
|
298
|
|
|
return Str::startsWith(trim($value), $callbacks) || Str::contains($key, ['editor', 'minDate', 'maxDate']); |
|
299
|
|
|
} |
|
300
|
|
|
} |
|
301
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.