|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Encore\Admin\Widgets; |
|
4
|
|
|
|
|
5
|
|
|
use Encore\Admin\Form\Field; |
|
6
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
|
7
|
|
|
use Illuminate\Contracts\Support\Renderable; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class Form. |
|
11
|
|
|
* |
|
12
|
|
|
* @method \Encore\Admin\Form\Field\Text text($name, $label = '') |
|
13
|
|
|
* @method \Encore\Admin\Form\Field\Password password($name, $label = '') |
|
14
|
|
|
* @method \Encore\Admin\Form\Field\Checkbox checkbox($name, $label = '') |
|
15
|
|
|
* @method \Encore\Admin\Form\Field\Radio radio($name, $label = '') |
|
16
|
|
|
* @method \Encore\Admin\Form\Field\Select select($name, $label = '') |
|
17
|
|
|
* @method \Encore\Admin\Form\Field\MultipleSelect multipleSelect($name, $label = '') |
|
18
|
|
|
* @method \Encore\Admin\Form\Field\Textarea textarea($name, $label = '') |
|
19
|
|
|
* @method \Encore\Admin\Form\Field\Hidden hidden($name, $label = '') |
|
20
|
|
|
* @method \Encore\Admin\Form\Field\Id id($name, $label = '') |
|
21
|
|
|
* @method \Encore\Admin\Form\Field\Ip ip($name, $label = '') |
|
22
|
|
|
* @method \Encore\Admin\Form\Field\Url url($name, $label = '') |
|
23
|
|
|
* @method \Encore\Admin\Form\Field\Color color($name, $label = '') |
|
24
|
|
|
* @method \Encore\Admin\Form\Field\Email email($name, $label = '') |
|
25
|
|
|
* @method \Encore\Admin\Form\Field\Mobile mobile($name, $label = '') |
|
26
|
|
|
* @method \Encore\Admin\Form\Field\Slider slider($name, $label = '') |
|
27
|
|
|
* @method \Encore\Admin\Form\Field\Map map($latitude, $longitude, $label = '') |
|
28
|
|
|
* @method \Encore\Admin\Form\Field\Editor editor($name, $label = '') |
|
29
|
|
|
* @method \Encore\Admin\Form\Field\File file($name, $label = '') |
|
30
|
|
|
* @method \Encore\Admin\Form\Field\Image image($name, $label = '') |
|
31
|
|
|
* @method \Encore\Admin\Form\Field\Date date($name, $label = '') |
|
32
|
|
|
* @method \Encore\Admin\Form\Field\Datetime datetime($name, $label = '') |
|
33
|
|
|
* @method \Encore\Admin\Form\Field\Time time($name, $label = '') |
|
34
|
|
|
* @method \Encore\Admin\Form\Field\DateRange dateRange($start, $end, $label = '') |
|
35
|
|
|
* @method \Encore\Admin\Form\Field\DateTimeRange dateTimeRange($start, $end, $label = '') |
|
36
|
|
|
* @method \Encore\Admin\Form\Field\TimeRange timeRange($start, $end, $label = '') |
|
37
|
|
|
* @method \Encore\Admin\Form\Field\Number number($name, $label = '') |
|
38
|
|
|
* @method \Encore\Admin\Form\Field\Currency currency($name, $label = '') |
|
39
|
|
|
* @method \Encore\Admin\Form\Field\Json json($name, $label = '') |
|
40
|
|
|
* @method \Encore\Admin\Form\Field\SwitchField switch($name, $label = '') |
|
41
|
|
|
* @method \Encore\Admin\Form\Field\Display display($name, $label = '') |
|
42
|
|
|
* @method \Encore\Admin\Form\Field\Rate rate($name, $label = '') |
|
43
|
|
|
* @method \Encore\Admin\Form\Field\Divide divide() |
|
44
|
|
|
*/ |
|
45
|
|
|
class Form implements Renderable |
|
46
|
|
|
{ |
|
47
|
|
|
/** |
|
48
|
|
|
* @var string |
|
49
|
|
|
*/ |
|
50
|
|
|
protected $id = ''; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var string |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $action = '/'; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var string |
|
59
|
|
|
*/ |
|
60
|
|
|
protected $method = 'POST'; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @var []Field |
|
64
|
|
|
*/ |
|
65
|
|
|
protected $fields = []; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @var array |
|
69
|
|
|
*/ |
|
70
|
|
|
protected $data = []; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Form constructor. |
|
74
|
|
|
* |
|
75
|
|
|
* @param array $data |
|
76
|
|
|
*/ |
|
77
|
|
|
public function __construct($data = []) |
|
78
|
|
|
{ |
|
79
|
|
|
if ($data instanceof Arrayable) { |
|
80
|
|
|
$data = $data->toArray(); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
if (!empty($data)) { |
|
84
|
|
|
$this->data = $data; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$this->id = 'form_id_'.uniqid(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Action uri of the form. |
|
92
|
|
|
* |
|
93
|
|
|
* @param string $action |
|
94
|
|
|
* @return $this |
|
95
|
|
|
*/ |
|
96
|
|
|
public function action($action) |
|
97
|
|
|
{ |
|
98
|
|
|
$this->action = $action; |
|
99
|
|
|
|
|
100
|
|
|
return $this; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Method of the form. |
|
105
|
|
|
* |
|
106
|
|
|
* @param string $method |
|
107
|
|
|
* @return $this |
|
108
|
|
|
*/ |
|
109
|
|
|
public function method($method = 'POST') |
|
110
|
|
|
{ |
|
111
|
|
|
$this->method = strtoupper($method); |
|
112
|
|
|
|
|
113
|
|
|
return $this; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Find field class with given name. |
|
118
|
|
|
* |
|
119
|
|
|
* @param string $method |
|
120
|
|
|
* @return bool|string |
|
121
|
|
|
*/ |
|
122
|
|
View Code Duplication |
public static function findFieldClass($method) |
|
|
|
|
|
|
123
|
|
|
{ |
|
124
|
|
|
$className = "\\Encore\\Admin\\Form\\Field\\".ucfirst($method); |
|
125
|
|
|
|
|
126
|
|
|
if (class_exists($className)) { |
|
127
|
|
|
return $className; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
if ($method == 'switch') { |
|
131
|
|
|
return '\\Encore\\Admin\\Form\\Field\\SwitchField'; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
return false; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Add a form field to form. |
|
139
|
|
|
* |
|
140
|
|
|
* @param Field $field |
|
141
|
|
|
* @return $this |
|
142
|
|
|
*/ |
|
143
|
|
|
protected function pushField(Field &$field) |
|
144
|
|
|
{ |
|
145
|
|
|
array_push($this->fields, $field); |
|
146
|
|
|
|
|
147
|
|
|
return $this; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* Get variables for render form. |
|
152
|
|
|
* |
|
153
|
|
|
* @return array |
|
154
|
|
|
*/ |
|
155
|
|
|
protected function getVariables() |
|
156
|
|
|
{ |
|
157
|
|
|
foreach ($this->fields as $field) { |
|
158
|
|
|
$field->fill($this->data); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
return [ |
|
162
|
|
|
'fields' => $this->fields, |
|
163
|
|
|
'action' => $this->action, |
|
164
|
|
|
'method' => $this->method, |
|
165
|
|
|
]; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Generate a Field object and add to form builder if Field exists. |
|
170
|
|
|
* |
|
171
|
|
|
* @param string $method |
|
172
|
|
|
* @param array $arguments |
|
173
|
|
|
* |
|
174
|
|
|
* @return Field|null |
|
175
|
|
|
*/ |
|
176
|
|
View Code Duplication |
public function __call($method, $arguments) |
|
|
|
|
|
|
177
|
|
|
{ |
|
178
|
|
|
if ($className = static::findFieldClass($method)) { |
|
179
|
|
|
$name = array_get($arguments, 0, ''); //[0]; |
|
180
|
|
|
|
|
181
|
|
|
$element = new $className($name, array_slice($arguments, 1)); |
|
182
|
|
|
|
|
183
|
|
|
$this->pushField($element); |
|
184
|
|
|
|
|
185
|
|
|
return $element; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
return null; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Render the form. |
|
193
|
|
|
* |
|
194
|
|
|
* @return string |
|
195
|
|
|
*/ |
|
196
|
|
|
public function render() |
|
197
|
|
|
{ |
|
198
|
|
|
return view('admin::widgets.form', $this->getVariables()); |
|
|
|
|
|
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Output as string. |
|
203
|
|
|
* |
|
204
|
|
|
* @return string |
|
205
|
|
|
*/ |
|
206
|
|
|
public function __toString() |
|
207
|
|
|
{ |
|
208
|
|
|
return $this->render(); |
|
209
|
|
|
} |
|
210
|
|
|
} |
|
211
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.