|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Encore\Admin\Form\Field; |
|
4
|
|
|
|
|
5
|
|
|
use Encore\Admin\Form\Field; |
|
6
|
|
|
|
|
7
|
|
|
class Text extends Field |
|
8
|
|
|
{ |
|
9
|
|
|
use PlainInput; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Render this filed. |
|
13
|
|
|
* |
|
14
|
|
|
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
|
15
|
|
|
*/ |
|
16
|
|
|
public function render() |
|
17
|
|
|
{ |
|
18
|
|
|
$this->initPlainInput(); |
|
19
|
|
|
|
|
20
|
|
|
$this->prepend('<i class="fa fa-pencil fa-fw"></i>') |
|
21
|
|
|
->defaultAttribute('type', 'text') |
|
22
|
|
|
->defaultAttribute('id', $this->id) |
|
23
|
|
|
->defaultAttribute('name', $this->elementName ?: $this->formatName($this->column)) |
|
|
|
|
|
|
24
|
|
|
->defaultAttribute('value', old($this->column, $this->value())) |
|
|
|
|
|
|
25
|
|
|
->defaultAttribute('class', 'form-control '.$this->getElementClassString()) |
|
26
|
|
|
->defaultAttribute('placeholder', $this->getPlaceholder()); |
|
27
|
|
|
|
|
28
|
|
|
$this->addVariables([ |
|
29
|
|
|
'prepend' => $this->prepend, |
|
30
|
|
|
'append' => $this->append, |
|
31
|
|
|
]); |
|
32
|
|
|
|
|
33
|
|
|
return parent::render(); |
|
|
|
|
|
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Add inputmask to an elements. |
|
38
|
|
|
* |
|
39
|
|
|
* @param array $options |
|
40
|
|
|
* |
|
41
|
|
|
* @return $this |
|
42
|
|
|
*/ |
|
43
|
|
|
public function inputmask($options) |
|
44
|
|
|
{ |
|
45
|
|
|
$options = $this->json_encode_options($options); |
|
46
|
|
|
|
|
47
|
|
|
$this->script = "$('{$this->getElementClassSelector()}').inputmask($options);"; |
|
48
|
|
|
|
|
49
|
|
|
return $this; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Encode options to Json. |
|
54
|
|
|
* |
|
55
|
|
|
* @param array $options |
|
56
|
|
|
* |
|
57
|
|
|
* @return $json |
|
|
|
|
|
|
58
|
|
|
*/ |
|
59
|
|
|
protected function json_encode_options($options) |
|
60
|
|
|
{ |
|
61
|
|
|
$data = $this->prepare_options($options); |
|
62
|
|
|
|
|
63
|
|
|
$json = json_encode($data['options']); |
|
64
|
|
|
|
|
65
|
|
|
$json = str_replace($data['toReplace'], $data['original'], $json); |
|
66
|
|
|
|
|
67
|
|
|
return $json; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Prepare options. |
|
72
|
|
|
* |
|
73
|
|
|
* @param array $options |
|
74
|
|
|
* |
|
75
|
|
|
* @return array |
|
76
|
|
|
*/ |
|
77
|
|
|
protected function prepare_options($options) |
|
78
|
|
|
{ |
|
79
|
|
|
$original = []; |
|
80
|
|
|
$toReplace = []; |
|
81
|
|
|
|
|
82
|
|
|
foreach ($options as $key => &$value) { |
|
83
|
|
|
if (is_array($value)) { |
|
84
|
|
|
$subArray = $this->prepare_options($value); |
|
85
|
|
|
$value = $subArray['options']; |
|
86
|
|
|
$original = array_merge($original, $subArray['original']); |
|
87
|
|
|
$toReplace = array_merge($toReplace, $subArray['toReplace']); |
|
88
|
|
|
} elseif (preg_match('/function.*?/', $value)) { |
|
89
|
|
|
$original[] = $value; |
|
90
|
|
|
$value = "%{$key}%"; |
|
91
|
|
|
$toReplace[] = "\"{$value}\""; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return compact('original', 'toReplace', 'options'); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public function datalist( $entries = [] ) |
|
99
|
|
|
{ |
|
100
|
|
|
$this->defaultAttribute('list', "list-{$this->id}"); |
|
101
|
|
|
|
|
102
|
|
|
$datalist = "<datalist id=\"list-{$this->id}\">"; |
|
103
|
|
|
foreach($entries as $k => $v) { |
|
104
|
|
|
$datalist .= "<option value=\"{$k}\">{$v}</option>"; |
|
105
|
|
|
} |
|
106
|
|
|
$datalist .= "</datalist>"; |
|
107
|
|
|
$this->append($datalist); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.