1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Encore\Admin\Form; |
4
|
|
|
|
5
|
|
|
use Encore\Admin\Admin; |
6
|
|
|
use Encore\Admin\Form; |
7
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
8
|
|
|
use Illuminate\Contracts\Support\Renderable; |
9
|
|
|
use Illuminate\Support\Arr; |
10
|
|
|
use Illuminate\Support\Facades\Validator; |
11
|
|
|
use Illuminate\Support\Traits\Macroable; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class Field. |
15
|
|
|
*/ |
16
|
|
|
class Field implements Renderable |
17
|
|
|
{ |
18
|
|
|
use Macroable; |
19
|
|
|
|
20
|
|
|
const FILE_DELETE_FLAG = '_file_del_'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Element id. |
24
|
|
|
* |
25
|
|
|
* @var array|string |
26
|
|
|
*/ |
27
|
|
|
protected $id; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Element value. |
31
|
|
|
* |
32
|
|
|
* @var mixed |
33
|
|
|
*/ |
34
|
|
|
protected $value; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Field original value. |
38
|
|
|
* |
39
|
|
|
* @var mixed |
40
|
|
|
*/ |
41
|
|
|
protected $original; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Field default value. |
45
|
|
|
* |
46
|
|
|
* @var mixed |
47
|
|
|
*/ |
48
|
|
|
protected $default; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Element label. |
52
|
|
|
* |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
protected $label = ''; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Column name. |
59
|
|
|
* |
60
|
|
|
* @var string|array |
61
|
|
|
*/ |
62
|
|
|
protected $column = ''; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Form element name. |
66
|
|
|
* |
67
|
|
|
* @var string |
68
|
|
|
*/ |
69
|
|
|
protected $elementName = []; |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Form element classes. |
73
|
|
|
* |
74
|
|
|
* @var array |
75
|
|
|
*/ |
76
|
|
|
protected $elementClass = []; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Variables of elements. |
80
|
|
|
* |
81
|
|
|
* @var array |
82
|
|
|
*/ |
83
|
|
|
protected $variables = []; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Options for specify elements. |
87
|
|
|
* |
88
|
|
|
* @var array |
89
|
|
|
*/ |
90
|
|
|
protected $options = []; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Validation rules. |
94
|
|
|
* |
95
|
|
|
* @var string|\Closure |
96
|
|
|
*/ |
97
|
|
|
protected $rules = ''; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @var callable |
101
|
|
|
*/ |
102
|
|
|
protected $validator; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Validation messages. |
106
|
|
|
* |
107
|
|
|
* @var array |
108
|
|
|
*/ |
109
|
|
|
protected $validationMessages = []; |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Css required by this field. |
113
|
|
|
* |
114
|
|
|
* @var array |
115
|
|
|
*/ |
116
|
|
|
protected static $css = []; |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Js required by this field. |
120
|
|
|
* |
121
|
|
|
* @var array |
122
|
|
|
*/ |
123
|
|
|
protected static $js = []; |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Script for field. |
127
|
|
|
* |
128
|
|
|
* @var string |
129
|
|
|
*/ |
130
|
|
|
protected $script = ''; |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Element attributes. |
134
|
|
|
* |
135
|
|
|
* @var array |
136
|
|
|
*/ |
137
|
|
|
protected $attributes = []; |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Parent form. |
141
|
|
|
* |
142
|
|
|
* @var Form |
143
|
|
|
*/ |
144
|
|
|
protected $form = null; |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* View for field to render. |
148
|
|
|
* |
149
|
|
|
* @var string |
150
|
|
|
*/ |
151
|
|
|
protected $view = ''; |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Help block. |
155
|
|
|
* |
156
|
|
|
* @var array |
157
|
|
|
*/ |
158
|
|
|
protected $help = []; |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Key for errors. |
162
|
|
|
* |
163
|
|
|
* @var mixed |
164
|
|
|
*/ |
165
|
|
|
protected $errorKey; |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Placeholder for this field. |
169
|
|
|
* |
170
|
|
|
* @var string|array |
171
|
|
|
*/ |
172
|
|
|
protected $placeholder; |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Width for label and field. |
176
|
|
|
* |
177
|
|
|
* @var array |
178
|
|
|
*/ |
179
|
|
|
protected $width = [ |
180
|
|
|
'label' => 2, |
181
|
|
|
'field' => 8, |
182
|
|
|
]; |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* If the form horizontal layout. |
186
|
|
|
* |
187
|
|
|
* @var bool |
188
|
|
|
*/ |
189
|
|
|
protected $horizontal = true; |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* column data format. |
193
|
|
|
* |
194
|
|
|
* @var Closure |
195
|
|
|
*/ |
196
|
|
|
protected $customFormat = null; |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* @var bool |
200
|
|
|
*/ |
201
|
|
|
protected $display = true; |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @var array |
205
|
|
|
*/ |
206
|
|
|
protected $labelClass = []; |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Field constructor. |
210
|
|
|
* |
211
|
|
|
* @param $column |
212
|
|
|
* @param array $arguments |
213
|
|
|
*/ |
214
|
|
|
public function __construct($column, $arguments = []) |
215
|
|
|
{ |
216
|
|
|
$this->column = $column; |
217
|
|
|
$this->label = $this->formatLabel($arguments); |
218
|
|
|
$this->id = $this->formatId($column); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* Get assets required by this field. |
223
|
|
|
* |
224
|
|
|
* @return array |
225
|
|
|
*/ |
226
|
|
|
public static function getAssets() |
227
|
|
|
{ |
228
|
|
|
return [ |
229
|
|
|
'css' => static::$css, |
230
|
|
|
'js' => static::$js, |
231
|
|
|
]; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Format the field element id. |
236
|
|
|
* |
237
|
|
|
* @param string|array $column |
238
|
|
|
* |
239
|
|
|
* @return string|array |
240
|
|
|
*/ |
241
|
|
|
protected function formatId($column) |
242
|
|
|
{ |
243
|
|
|
return str_replace('.', '_', $column); |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Format the label value. |
248
|
|
|
* |
249
|
|
|
* @param array $arguments |
250
|
|
|
* |
251
|
|
|
* @return string |
252
|
|
|
*/ |
253
|
|
|
protected function formatLabel($arguments = []) |
254
|
|
|
{ |
255
|
|
|
$column = is_array($this->column) ? current($this->column) : $this->column; |
256
|
|
|
|
257
|
|
|
$label = isset($arguments[0]) ? $arguments[0] : ucfirst($column); |
258
|
|
|
|
259
|
|
|
return str_replace(['.', '_'], ' ', $label); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Format the name of the field. |
264
|
|
|
* |
265
|
|
|
* @param string $column |
266
|
|
|
* |
267
|
|
|
* @return array|mixed|string |
268
|
|
|
*/ |
269
|
|
|
protected function formatName($column) |
270
|
|
|
{ |
271
|
|
|
if (is_string($column)) { |
272
|
|
|
$name = explode('.', $column); |
273
|
|
|
|
274
|
|
|
if (count($name) == 1) { |
275
|
|
|
return $name[0]; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
$html = array_shift($name); |
279
|
|
|
foreach ($name as $piece) { |
280
|
|
|
$html .= "[$piece]"; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
return $html; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
if (is_array($this->column)) { |
287
|
|
|
$names = []; |
288
|
|
|
foreach ($this->column as $key => $name) { |
289
|
|
|
$names[$key] = $this->formatName($name); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
return $names; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
return ''; |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Set form element name. |
300
|
|
|
* |
301
|
|
|
* @param string $name |
302
|
|
|
* |
303
|
|
|
* @return $this |
304
|
|
|
* |
305
|
|
|
* @author Edwin Hui |
306
|
|
|
*/ |
307
|
|
|
public function setElementName($name) |
308
|
|
|
{ |
309
|
|
|
$this->elementName = $name; |
310
|
|
|
|
311
|
|
|
return $this; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* Fill data to the field. |
316
|
|
|
* |
317
|
|
|
* @param array $data |
318
|
|
|
* |
319
|
|
|
* @return void |
320
|
|
|
*/ |
321
|
|
|
public function fill($data) |
322
|
|
|
{ |
323
|
|
|
// Field value is already setted. |
324
|
|
|
// if (!is_null($this->value)) { |
325
|
|
|
// return; |
326
|
|
|
// } |
327
|
|
|
|
328
|
|
|
if (is_array($this->column)) { |
329
|
|
|
foreach ($this->column as $key => $column) { |
330
|
|
|
$this->value[$key] = array_get($data, $column); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
return; |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
$this->value = array_get($data, $this->column); |
337
|
|
|
if (isset($this->customFormat) && $this->customFormat instanceof \Closure) { |
338
|
|
|
$this->value = call_user_func($this->customFormat, $this->value); |
339
|
|
|
} |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* custom format form column data when edit. |
344
|
|
|
* |
345
|
|
|
* @param Closure $call |
346
|
|
|
* |
347
|
|
|
* @return [null] |
|
|
|
|
348
|
|
|
*/ |
349
|
|
|
public function customFormat(\Closure $call) |
350
|
|
|
{ |
351
|
|
|
$this->customFormat = $call; |
|
|
|
|
352
|
|
|
} |
353
|
|
|
|
354
|
|
|
/** |
355
|
|
|
* Set original value to the field. |
356
|
|
|
* |
357
|
|
|
* @param array $data |
358
|
|
|
* |
359
|
|
|
* @return void |
360
|
|
|
*/ |
361
|
|
|
public function setOriginal($data) |
362
|
|
|
{ |
363
|
|
|
if (is_array($this->column)) { |
364
|
|
|
foreach ($this->column as $key => $column) { |
365
|
|
|
$this->original[$key] = array_get($data, $column); |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
return; |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
$this->original = array_get($data, $this->column); |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/** |
375
|
|
|
* @param Form $form |
376
|
|
|
* |
377
|
|
|
* @return $this |
378
|
|
|
*/ |
379
|
|
|
public function setForm(Form $form = null) |
380
|
|
|
{ |
381
|
|
|
$this->form = $form; |
382
|
|
|
|
383
|
|
|
return $this; |
384
|
|
|
} |
385
|
|
|
|
386
|
|
|
/** |
387
|
|
|
* Set width for field and label. |
388
|
|
|
* |
389
|
|
|
* @param int $field |
390
|
|
|
* @param int $label |
391
|
|
|
* |
392
|
|
|
* @return $this |
393
|
|
|
*/ |
394
|
|
|
public function setWidth($field = 8, $label = 2) |
395
|
|
|
{ |
396
|
|
|
$this->width = [ |
397
|
|
|
'label' => $label, |
398
|
|
|
'field' => $field, |
399
|
|
|
]; |
400
|
|
|
|
401
|
|
|
return $this; |
402
|
|
|
} |
403
|
|
|
|
404
|
|
|
/** |
405
|
|
|
* Set the field options. |
406
|
|
|
* |
407
|
|
|
* @param array $options |
408
|
|
|
* |
409
|
|
|
* @return $this |
410
|
|
|
*/ |
411
|
|
View Code Duplication |
public function options($options = []) |
|
|
|
|
412
|
|
|
{ |
413
|
|
|
if ($options instanceof Arrayable) { |
414
|
|
|
$options = $options->toArray(); |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
$this->options = array_merge($this->options, $options); |
418
|
|
|
|
419
|
|
|
return $this; |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
/** |
423
|
|
|
* Get or set rules. |
424
|
|
|
* |
425
|
|
|
* @param null $rules |
426
|
|
|
* @param array $messages |
427
|
|
|
* |
428
|
|
|
* @return $this |
429
|
|
|
*/ |
430
|
|
|
public function rules($rules = null, $messages = []) |
431
|
|
|
{ |
432
|
|
|
if ($rules instanceof \Closure) { |
433
|
|
|
$this->rules = $rules; |
434
|
|
|
} |
435
|
|
|
|
436
|
|
|
if (is_array($rules)) { |
437
|
|
|
$thisRuleArr = array_filter(explode('|', $this->rules)); |
438
|
|
|
|
439
|
|
|
$this->rules = array_merge($thisRuleArr, $rules); |
|
|
|
|
440
|
|
|
} elseif (is_string($rules)) { |
441
|
|
|
$rules = array_filter(explode('|', "{$this->rules}|$rules")); |
442
|
|
|
|
443
|
|
|
$this->rules = implode('|', $rules); |
444
|
|
|
} |
445
|
|
|
|
446
|
|
|
$this->validationMessages = $messages; |
447
|
|
|
|
448
|
|
|
return $this; |
449
|
|
|
} |
450
|
|
|
|
451
|
|
|
/** |
452
|
|
|
* Get field validation rules. |
453
|
|
|
* |
454
|
|
|
* @return string |
455
|
|
|
*/ |
456
|
|
|
protected function getRules() |
457
|
|
|
{ |
458
|
|
|
if ($this->rules instanceof \Closure) { |
459
|
|
|
return $this->rules->call($this, $this->form); |
460
|
|
|
} |
461
|
|
|
|
462
|
|
|
return $this->rules; |
463
|
|
|
} |
464
|
|
|
|
465
|
|
|
/** |
466
|
|
|
* Remove a specific rule by keyword. |
467
|
|
|
* |
468
|
|
|
* @param string $rule |
469
|
|
|
* |
470
|
|
|
* @return void |
471
|
|
|
*/ |
472
|
|
|
protected function removeRule($rule) |
473
|
|
|
{ |
474
|
|
|
if (!is_string($this->rules)) { |
475
|
|
|
return; |
476
|
|
|
} |
477
|
|
|
|
478
|
|
|
$pattern = "/{$rule}[^\|]?(\||$)/"; |
479
|
|
|
$this->rules = preg_replace($pattern, '', $this->rules, -1); |
480
|
|
|
} |
481
|
|
|
|
482
|
|
|
/** |
483
|
|
|
* Set field validator. |
484
|
|
|
* |
485
|
|
|
* @param callable $validator |
486
|
|
|
* |
487
|
|
|
* @return $this |
488
|
|
|
*/ |
489
|
|
|
public function validator(callable $validator) |
490
|
|
|
{ |
491
|
|
|
$this->validator = $validator; |
492
|
|
|
|
493
|
|
|
return $this; |
494
|
|
|
} |
495
|
|
|
|
496
|
|
|
/** |
497
|
|
|
* Get key for error message. |
498
|
|
|
* |
499
|
|
|
* @return string |
500
|
|
|
*/ |
501
|
|
|
public function getErrorKey() |
502
|
|
|
{ |
503
|
|
|
return $this->errorKey ?: $this->column; |
504
|
|
|
} |
505
|
|
|
|
506
|
|
|
/** |
507
|
|
|
* Set key for error message. |
508
|
|
|
* |
509
|
|
|
* @param string $key |
510
|
|
|
* |
511
|
|
|
* @return $this |
512
|
|
|
*/ |
513
|
|
|
public function setErrorKey($key) |
514
|
|
|
{ |
515
|
|
|
$this->errorKey = $key; |
516
|
|
|
|
517
|
|
|
return $this; |
518
|
|
|
} |
519
|
|
|
|
520
|
|
|
/** |
521
|
|
|
* Set or get value of the field. |
522
|
|
|
* |
523
|
|
|
* @param null $value |
524
|
|
|
* |
525
|
|
|
* @return mixed |
526
|
|
|
*/ |
527
|
|
View Code Duplication |
public function value($value = null) |
|
|
|
|
528
|
|
|
{ |
529
|
|
|
if (is_null($value)) { |
530
|
|
|
return is_null($this->value) ? $this->getDefault() : $this->value; |
531
|
|
|
} |
532
|
|
|
|
533
|
|
|
$this->value = $value; |
534
|
|
|
|
535
|
|
|
return $this; |
536
|
|
|
} |
537
|
|
|
|
538
|
|
|
/** |
539
|
|
|
* Set default value for field. |
540
|
|
|
* |
541
|
|
|
* @param $default |
542
|
|
|
* |
543
|
|
|
* @return $this |
544
|
|
|
*/ |
545
|
|
|
public function default($default) |
546
|
|
|
{ |
547
|
|
|
$this->default = $default; |
548
|
|
|
|
549
|
|
|
return $this; |
550
|
|
|
} |
551
|
|
|
|
552
|
|
|
/** |
553
|
|
|
* Get default value. |
554
|
|
|
* |
555
|
|
|
* @return mixed |
556
|
|
|
*/ |
557
|
|
|
public function getDefault() |
558
|
|
|
{ |
559
|
|
|
if ($this->default instanceof \Closure) { |
560
|
|
|
return call_user_func($this->default, $this->form); |
561
|
|
|
} |
562
|
|
|
|
563
|
|
|
return $this->default; |
564
|
|
|
} |
565
|
|
|
|
566
|
|
|
/** |
567
|
|
|
* Set help block for current field. |
568
|
|
|
* |
569
|
|
|
* @param string $text |
570
|
|
|
* @param string $icon |
571
|
|
|
* |
572
|
|
|
* @return $this |
573
|
|
|
*/ |
574
|
|
|
public function help($text = '', $icon = 'fa-info-circle') |
575
|
|
|
{ |
576
|
|
|
$this->help = compact('text', 'icon'); |
577
|
|
|
|
578
|
|
|
return $this; |
579
|
|
|
} |
580
|
|
|
|
581
|
|
|
/** |
582
|
|
|
* Get column of the field. |
583
|
|
|
* |
584
|
|
|
* @return string|array |
585
|
|
|
*/ |
586
|
|
|
public function column() |
587
|
|
|
{ |
588
|
|
|
return $this->column; |
589
|
|
|
} |
590
|
|
|
|
591
|
|
|
/** |
592
|
|
|
* Get label of the field. |
593
|
|
|
* |
594
|
|
|
* @return string |
595
|
|
|
*/ |
596
|
|
|
public function label() |
597
|
|
|
{ |
598
|
|
|
return $this->label; |
599
|
|
|
} |
600
|
|
|
|
601
|
|
|
/** |
602
|
|
|
* Get original value of the field. |
603
|
|
|
* |
604
|
|
|
* @return mixed |
605
|
|
|
*/ |
606
|
|
|
public function original() |
607
|
|
|
{ |
608
|
|
|
return $this->original; |
609
|
|
|
} |
610
|
|
|
|
611
|
|
|
/** |
612
|
|
|
* Get validator for this field. |
613
|
|
|
* |
614
|
|
|
* @param array $input |
615
|
|
|
* |
616
|
|
|
* @return bool|Validator |
617
|
|
|
*/ |
618
|
|
|
public function getValidator(array $input) |
619
|
|
|
{ |
620
|
|
|
if ($this->validator) { |
621
|
|
|
return $this->validator->call($this, $input); |
622
|
|
|
} |
623
|
|
|
|
624
|
|
|
$rules = $attributes = []; |
625
|
|
|
|
626
|
|
|
if (!$fieldRules = $this->getRules()) { |
627
|
|
|
return false; |
628
|
|
|
} |
629
|
|
|
|
630
|
|
|
if (is_string($this->column)) { |
631
|
|
|
if (!array_has($input, $this->column)) { |
632
|
|
|
return false; |
633
|
|
|
} |
634
|
|
|
|
635
|
|
|
$input = $this->sanitizeInput($input, $this->column); |
636
|
|
|
|
637
|
|
|
$rules[$this->column] = $fieldRules; |
638
|
|
|
$attributes[$this->column] = $this->label; |
639
|
|
|
} |
640
|
|
|
|
641
|
|
|
if (is_array($this->column)) { |
642
|
|
|
foreach ($this->column as $key => $column) { |
643
|
|
|
if (!array_key_exists($column, $input)) { |
644
|
|
|
continue; |
645
|
|
|
} |
646
|
|
|
$input[$column.$key] = array_get($input, $column); |
647
|
|
|
$rules[$column.$key] = $fieldRules; |
648
|
|
|
$attributes[$column.$key] = $this->label."[$column]"; |
649
|
|
|
} |
650
|
|
|
} |
651
|
|
|
|
652
|
|
|
return Validator::make($input, $rules, $this->validationMessages, $attributes); |
|
|
|
|
653
|
|
|
} |
654
|
|
|
|
655
|
|
|
/** |
656
|
|
|
* Sanitize input data. |
657
|
|
|
* |
658
|
|
|
* @param array $input |
659
|
|
|
* @param string $column |
660
|
|
|
* |
661
|
|
|
* @return array |
662
|
|
|
*/ |
663
|
|
|
protected function sanitizeInput($input, $column) |
664
|
|
|
{ |
665
|
|
|
if ($this instanceof Field\MultipleSelect) { |
666
|
|
|
$value = array_get($input, $column); |
667
|
|
|
array_set($input, $column, array_filter($value)); |
668
|
|
|
} |
669
|
|
|
|
670
|
|
|
return $input; |
671
|
|
|
} |
672
|
|
|
|
673
|
|
|
/** |
674
|
|
|
* Add html attributes to elements. |
675
|
|
|
* |
676
|
|
|
* @param array|string $attribute |
677
|
|
|
* @param mixed $value |
678
|
|
|
* |
679
|
|
|
* @return $this |
680
|
|
|
*/ |
681
|
|
|
public function attribute($attribute, $value = null) |
682
|
|
|
{ |
683
|
|
|
if (is_array($attribute)) { |
684
|
|
|
$this->attributes = array_merge($this->attributes, $attribute); |
685
|
|
|
} else { |
686
|
|
|
$this->attributes[$attribute] = (string) $value; |
687
|
|
|
} |
688
|
|
|
|
689
|
|
|
return $this; |
690
|
|
|
} |
691
|
|
|
|
692
|
|
|
/** |
693
|
|
|
* Set the field as readonly mode. |
694
|
|
|
* |
695
|
|
|
* @return Field |
696
|
|
|
*/ |
697
|
|
|
public function readOnly() |
698
|
|
|
{ |
699
|
|
|
return $this->attribute('readonly', true); |
700
|
|
|
} |
701
|
|
|
|
702
|
|
|
/** |
703
|
|
|
* Set field as disabled. |
704
|
|
|
* |
705
|
|
|
* @return Field |
706
|
|
|
*/ |
707
|
|
|
public function disable() |
708
|
|
|
{ |
709
|
|
|
return $this->attribute('disabled', true); |
710
|
|
|
} |
711
|
|
|
|
712
|
|
|
/** |
713
|
|
|
* Set field placeholder. |
714
|
|
|
* |
715
|
|
|
* @param string $placeholder |
716
|
|
|
* |
717
|
|
|
* @return Field |
718
|
|
|
*/ |
719
|
|
|
public function placeholder($placeholder = '') |
720
|
|
|
{ |
721
|
|
|
$this->placeholder = $placeholder; |
722
|
|
|
|
723
|
|
|
return $this; |
724
|
|
|
} |
725
|
|
|
|
726
|
|
|
/** |
727
|
|
|
* Get placeholder. |
728
|
|
|
* |
729
|
|
|
* @return string |
730
|
|
|
*/ |
731
|
|
|
public function getPlaceholder() |
732
|
|
|
{ |
733
|
|
|
return $this->placeholder ?: trans('admin.input').' '.$this->label; |
734
|
|
|
} |
735
|
|
|
|
736
|
|
|
/** |
737
|
|
|
* Prepare for a field value before update or insert. |
738
|
|
|
* |
739
|
|
|
* @param $value |
740
|
|
|
* |
741
|
|
|
* @return mixed |
742
|
|
|
*/ |
743
|
|
|
public function prepare($value) |
744
|
|
|
{ |
745
|
|
|
return $value; |
746
|
|
|
} |
747
|
|
|
|
748
|
|
|
/** |
749
|
|
|
* Format the field attributes. |
750
|
|
|
* |
751
|
|
|
* @return string |
752
|
|
|
*/ |
753
|
|
|
protected function formatAttributes() |
754
|
|
|
{ |
755
|
|
|
$html = []; |
756
|
|
|
|
757
|
|
|
foreach ($this->attributes as $name => $value) { |
758
|
|
|
$html[] = $name.'="'.e($value).'"'; |
759
|
|
|
} |
760
|
|
|
|
761
|
|
|
return implode(' ', $html); |
762
|
|
|
} |
763
|
|
|
|
764
|
|
|
/** |
765
|
|
|
* @return $this |
766
|
|
|
*/ |
767
|
|
|
public function disableHorizontal() |
768
|
|
|
{ |
769
|
|
|
$this->horizontal = false; |
770
|
|
|
|
771
|
|
|
return $this; |
772
|
|
|
} |
773
|
|
|
|
774
|
|
|
/** |
775
|
|
|
* @return array |
776
|
|
|
*/ |
777
|
|
|
public function getViewElementClasses() |
778
|
|
|
{ |
779
|
|
|
if ($this->horizontal) { |
780
|
|
|
return [ |
781
|
|
|
'label' => "col-sm-{$this->width['label']} {$this->getLabelClass()}", |
782
|
|
|
'field' => "col-sm-{$this->width['field']}", |
783
|
|
|
'form-group' => 'form-group ', |
784
|
|
|
]; |
785
|
|
|
} |
786
|
|
|
|
787
|
|
|
return ['label' => "{$this->getLabelClass()}", 'field' => '', 'form-group' => '']; |
788
|
|
|
} |
789
|
|
|
|
790
|
|
|
/** |
791
|
|
|
* Set form element class. |
792
|
|
|
* |
793
|
|
|
* @param string|array $class |
794
|
|
|
* |
795
|
|
|
* @return $this |
796
|
|
|
*/ |
797
|
|
|
public function setElementClass($class) |
798
|
|
|
{ |
799
|
|
|
$this->elementClass = (array) $class; |
800
|
|
|
|
801
|
|
|
return $this; |
802
|
|
|
} |
803
|
|
|
|
804
|
|
|
/** |
805
|
|
|
* Get element class. |
806
|
|
|
* |
807
|
|
|
* @return array |
808
|
|
|
*/ |
809
|
|
|
protected function getElementClass() |
810
|
|
|
{ |
811
|
|
|
if (!$this->elementClass) { |
|
|
|
|
812
|
|
|
$name = $this->elementName ?: $this->formatName($this->column); |
|
|
|
|
813
|
|
|
|
814
|
|
|
$this->elementClass = (array) str_replace(['[', ']'], '_', $name); |
815
|
|
|
} |
816
|
|
|
|
817
|
|
|
return $this->elementClass; |
818
|
|
|
} |
819
|
|
|
|
820
|
|
|
/** |
821
|
|
|
* Get element class string. |
822
|
|
|
* |
823
|
|
|
* @return mixed |
824
|
|
|
*/ |
825
|
|
View Code Duplication |
protected function getElementClassString() |
|
|
|
|
826
|
|
|
{ |
827
|
|
|
$elementClass = $this->getElementClass(); |
828
|
|
|
|
829
|
|
|
if (Arr::isAssoc($elementClass)) { |
830
|
|
|
$classes = []; |
831
|
|
|
|
832
|
|
|
foreach ($elementClass as $index => $class) { |
833
|
|
|
$classes[$index] = is_array($class) ? implode(' ', $class) : $class; |
834
|
|
|
} |
835
|
|
|
|
836
|
|
|
return $classes; |
837
|
|
|
} |
838
|
|
|
|
839
|
|
|
return implode(' ', $elementClass); |
840
|
|
|
} |
841
|
|
|
|
842
|
|
|
/** |
843
|
|
|
* Get element class selector. |
844
|
|
|
* |
845
|
|
|
* @return string |
846
|
|
|
*/ |
847
|
|
View Code Duplication |
protected function getElementClassSelector() |
|
|
|
|
848
|
|
|
{ |
849
|
|
|
$elementClass = $this->getElementClass(); |
850
|
|
|
|
851
|
|
|
if (Arr::isAssoc($elementClass)) { |
852
|
|
|
$classes = []; |
853
|
|
|
|
854
|
|
|
foreach ($elementClass as $index => $class) { |
855
|
|
|
$classes[$index] = '.'.(is_array($class) ? implode('.', $class) : $class); |
856
|
|
|
} |
857
|
|
|
|
858
|
|
|
return $classes; |
859
|
|
|
} |
860
|
|
|
|
861
|
|
|
return '.'.implode('.', $elementClass); |
862
|
|
|
} |
863
|
|
|
|
864
|
|
|
/** |
865
|
|
|
* Add the element class. |
866
|
|
|
* |
867
|
|
|
* @param $class |
868
|
|
|
* |
869
|
|
|
* @return $this |
870
|
|
|
*/ |
871
|
|
|
public function addElementClass($class) |
872
|
|
|
{ |
873
|
|
|
if (is_array($class) || is_string($class)) { |
874
|
|
|
$this->elementClass = array_merge($this->elementClass, (array) $class); |
875
|
|
|
|
876
|
|
|
$this->elementClass = array_unique($this->elementClass); |
877
|
|
|
} |
878
|
|
|
|
879
|
|
|
return $this; |
880
|
|
|
} |
881
|
|
|
|
882
|
|
|
/** |
883
|
|
|
* Remove element class. |
884
|
|
|
* |
885
|
|
|
* @param $class |
886
|
|
|
* |
887
|
|
|
* @return $this |
888
|
|
|
*/ |
889
|
|
|
public function removeElementClass($class) |
890
|
|
|
{ |
891
|
|
|
$delClass = []; |
892
|
|
|
|
893
|
|
|
if (is_string($class) || is_array($class)) { |
894
|
|
|
$delClass = (array) $class; |
895
|
|
|
} |
896
|
|
|
|
897
|
|
|
foreach ($delClass as $del) { |
898
|
|
|
if (($key = array_search($del, $this->elementClass))) { |
899
|
|
|
unset($this->elementClass[$key]); |
900
|
|
|
} |
901
|
|
|
} |
902
|
|
|
|
903
|
|
|
return $this; |
904
|
|
|
} |
905
|
|
|
|
906
|
|
|
/** |
907
|
|
|
* Add variables to field view. |
908
|
|
|
* |
909
|
|
|
* @param array $variables |
910
|
|
|
* |
911
|
|
|
* @return $this |
912
|
|
|
*/ |
913
|
|
|
protected function addVariables(array $variables = []) |
914
|
|
|
{ |
915
|
|
|
$this->variables = array_merge($this->variables, $variables); |
916
|
|
|
|
917
|
|
|
return $this; |
918
|
|
|
} |
919
|
|
|
|
920
|
|
|
/** |
921
|
|
|
* @return string |
922
|
|
|
*/ |
923
|
|
|
public function getLabelClass() |
924
|
|
|
: string |
925
|
|
|
{ |
926
|
|
|
return implode(' ', $this->labelClass); |
927
|
|
|
} |
928
|
|
|
|
929
|
|
|
/** |
930
|
|
|
* @param array $labelClass |
931
|
|
|
* |
932
|
|
|
* @return self |
933
|
|
|
*/ |
934
|
|
|
public function setLabelClass(array $labelClass) |
935
|
|
|
: self |
936
|
|
|
{ |
937
|
|
|
$this->labelClass = $labelClass; |
938
|
|
|
|
939
|
|
|
return $this; |
940
|
|
|
} |
941
|
|
|
|
942
|
|
|
/** |
943
|
|
|
* Get the view variables of this field. |
944
|
|
|
* |
945
|
|
|
* @return array |
946
|
|
|
*/ |
947
|
|
|
public function variables() |
948
|
|
|
{ |
949
|
|
|
return array_merge($this->variables, [ |
950
|
|
|
'id' => $this->id, |
951
|
|
|
'name' => $this->elementName ?: $this->formatName($this->column), |
|
|
|
|
952
|
|
|
'help' => $this->help, |
953
|
|
|
'class' => $this->getElementClassString(), |
954
|
|
|
'value' => $this->value(), |
955
|
|
|
'label' => $this->label, |
956
|
|
|
'viewClass' => $this->getViewElementClasses(), |
957
|
|
|
'column' => $this->column, |
958
|
|
|
'errorKey' => $this->getErrorKey(), |
959
|
|
|
'attributes' => $this->formatAttributes(), |
960
|
|
|
'placeholder' => $this->getPlaceholder(), |
961
|
|
|
]); |
962
|
|
|
} |
963
|
|
|
|
964
|
|
|
/** |
965
|
|
|
* Get view of this field. |
966
|
|
|
* |
967
|
|
|
* @return string |
968
|
|
|
*/ |
969
|
|
|
public function getView() |
970
|
|
|
{ |
971
|
|
|
if (!empty($this->view)) { |
972
|
|
|
return $this->view; |
973
|
|
|
} |
974
|
|
|
|
975
|
|
|
$class = explode('\\', get_called_class()); |
976
|
|
|
|
977
|
|
|
return 'admin::form.'.strtolower(end($class)); |
978
|
|
|
} |
979
|
|
|
|
980
|
|
|
/** |
981
|
|
|
* Get script of current field. |
982
|
|
|
* |
983
|
|
|
* @return string |
984
|
|
|
*/ |
985
|
|
|
public function getScript() |
986
|
|
|
{ |
987
|
|
|
return $this->script; |
988
|
|
|
} |
989
|
|
|
|
990
|
|
|
/** |
991
|
|
|
* Render this filed. |
992
|
|
|
* |
993
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
994
|
|
|
*/ |
995
|
|
|
public function render() |
996
|
|
|
{ |
997
|
|
|
if (!$this->display) { |
998
|
|
|
return ''; |
999
|
|
|
} |
1000
|
|
|
|
1001
|
|
|
Admin::script($this->script); |
1002
|
|
|
|
1003
|
|
|
return view($this->getView(), $this->variables()); |
|
|
|
|
1004
|
|
|
} |
1005
|
|
|
|
1006
|
|
|
/** |
1007
|
|
|
* @return string |
1008
|
|
|
*/ |
1009
|
|
|
public function __toString() |
1010
|
|
|
{ |
1011
|
|
|
return $this->render()->render(); |
|
|
|
|
1012
|
|
|
} |
1013
|
|
|
} |
1014
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.