1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin\Form; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
use Illuminate\Validation\Rules\In; |
8
|
|
|
use SleepingOwl\Admin\Traits\Assets; |
9
|
|
|
use Illuminate\Validation\Rules\NotIn; |
10
|
|
|
use Illuminate\Database\Eloquent\Model; |
11
|
|
|
use Illuminate\Validation\Rules\Exists; |
12
|
|
|
use Illuminate\Validation\Rules\Unique; |
13
|
|
|
use SleepingOwl\Admin\Traits\Renderable; |
14
|
|
|
use Illuminate\Validation\Rules\Dimensions; |
15
|
|
|
use SleepingOwl\Admin\Contracts\Form\FormElementInterface; |
16
|
|
|
use SleepingOwl\Admin\Contracts\Template\TemplateInterface; |
17
|
|
|
|
18
|
|
|
abstract class FormElement implements FormElementInterface |
19
|
|
|
{ |
20
|
|
|
use Assets, Renderable; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var TemplateInterface |
24
|
|
|
*/ |
25
|
|
|
protected $template; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var Renderable |
29
|
|
|
*/ |
30
|
|
|
protected $view; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var Model |
34
|
|
|
*/ |
35
|
|
|
protected $model; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
protected $validationRules = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
protected $validationMessages = []; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var bool|callable |
49
|
|
|
*/ |
50
|
|
|
protected $readonly = false; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var bool|callable |
54
|
|
|
*/ |
55
|
|
|
protected $visibled = true; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var bool|callable |
59
|
|
|
*/ |
60
|
|
|
protected $valueSkipped = false; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var mixed |
64
|
|
|
*/ |
65
|
|
|
protected $value; |
66
|
|
|
|
67
|
|
|
public function __construct() |
68
|
|
|
{ |
69
|
|
|
$this->initializePackage(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function initialize() |
73
|
|
|
{ |
74
|
|
|
$this->includePackage(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
|
|
public function getValidationMessages() |
81
|
|
|
{ |
82
|
|
|
return $this->validationMessages; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $rule |
87
|
|
|
* @param string $message |
88
|
|
|
* |
89
|
|
|
* @return $this |
90
|
|
|
*/ |
91
|
|
|
public function addValidationMessage($rule, $message) |
92
|
|
|
{ |
93
|
|
|
if (($pos = strpos($rule, ':')) !== false) { |
94
|
|
|
$rule = substr($rule, 0, $pos); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$this->validationMessages[$rule] = $message; |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param array $validationMessages |
104
|
|
|
* |
105
|
|
|
* @return $this |
106
|
|
|
*/ |
107
|
|
|
public function setValidationMessages(array $validationMessages) |
108
|
|
|
{ |
109
|
|
|
$this->validationMessages = $validationMessages; |
110
|
|
|
|
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
|
|
public function getValidationLabels() |
118
|
|
|
{ |
119
|
|
|
return []; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return array |
124
|
|
|
*/ |
125
|
|
|
public function getValidationRules() |
126
|
|
|
{ |
127
|
|
|
return $this->validationRules; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param string $rule |
132
|
|
|
* @param string|null $message |
133
|
|
|
* |
134
|
|
|
* @return $this |
135
|
|
|
*/ |
136
|
|
|
public function addValidationRule($rule, $message = null) |
137
|
|
|
{ |
138
|
|
|
$this->validationRules[] = $rule; |
139
|
|
|
|
140
|
|
|
if (is_null($message)) { |
141
|
|
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $this->addValidationMessage($rule, $message); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param array|string $validationRules |
149
|
|
|
* @param string|\Illuminate\Validation\Rule|\Illuminate\Contracts\Validation\Rule $rule |
150
|
|
|
* |
151
|
|
|
* @return $this |
152
|
|
|
*/ |
153
|
|
|
public function setValidationRules($validationRules) |
154
|
|
|
{ |
155
|
|
|
if (! is_array($validationRules)) { |
156
|
|
|
$validationRules = func_get_args(); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
$this->validationRules = []; |
160
|
|
|
foreach ($validationRules as $rule) { |
161
|
|
|
if ($rule instanceof \Illuminate\Validation\Rule || $rule instanceof \Illuminate\Contracts\Validation\Rule || $rule instanceof Dimensions || $rule instanceof Exists || $rule instanceof In || $rule instanceof NotIn || $rule instanceof Unique) { |
162
|
|
|
$this->addValidationRule($rule); |
|
|
|
|
163
|
|
|
} else { |
164
|
|
|
$rules = explode('|', $rule); |
165
|
|
|
foreach ($rules as $addRule) { |
166
|
|
|
$this->addValidationRule($addRule); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return $this; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @return Model |
176
|
|
|
*/ |
177
|
|
|
public function getModel() |
178
|
|
|
{ |
179
|
|
|
return $this->model; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param Model $model |
184
|
|
|
* |
185
|
|
|
* @return $this |
186
|
|
|
*/ |
187
|
|
|
public function setModel(Model $model) |
188
|
|
|
{ |
189
|
|
|
$this->model = $model; |
190
|
|
|
|
191
|
|
|
return $this; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @return bool|callable |
196
|
|
|
*/ |
197
|
|
|
public function isReadonly() |
198
|
|
|
{ |
199
|
|
|
if (is_callable($this->readonly)) { |
200
|
|
|
return (bool) call_user_func($this->readonly, $this->getModel()); |
|
|
|
|
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
return (bool) $this->readonly; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @return bool|callable |
208
|
|
|
*/ |
209
|
|
|
public function isVisible() |
210
|
|
|
{ |
211
|
|
|
if (is_callable($this->visibled)) { |
212
|
|
|
return (bool) call_user_func($this->visibled, $this->getModel()); |
|
|
|
|
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
return (bool) $this->visibled; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @return bool |
220
|
|
|
*/ |
221
|
|
|
public function isValueSkipped() |
222
|
|
|
{ |
223
|
|
|
if (is_callable($this->valueSkipped)) { |
224
|
|
|
return (bool) call_user_func($this->valueSkipped, $this->getModel()); |
|
|
|
|
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return (bool) $this->valueSkipped; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @param Closure|bool $valueSkipped |
232
|
|
|
* |
233
|
|
|
* @return $this |
234
|
|
|
*/ |
235
|
|
|
public function setValueSkipped($valueSkipped) |
236
|
|
|
{ |
237
|
|
|
$this->valueSkipped = $valueSkipped; |
238
|
|
|
|
239
|
|
|
return $this; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* @param Closure|bool $readonly |
244
|
|
|
* |
245
|
|
|
* @return $this |
246
|
|
|
*/ |
247
|
|
|
public function setReadonly($readonly) |
248
|
|
|
{ |
249
|
|
|
$this->readonly = $readonly; |
250
|
|
|
|
251
|
|
|
return $this; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* @param Closure|bool $visibled |
256
|
|
|
* |
257
|
|
|
* @return $this |
258
|
|
|
*/ |
259
|
|
|
public function setVisible($visibled) |
260
|
|
|
{ |
261
|
|
|
$this->visibled = $visibled; |
262
|
|
|
|
263
|
|
|
return $this; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @param \SleepingOwl\Admin\Form\FormElement $visibled |
268
|
|
|
* |
269
|
|
|
* @return $this |
270
|
|
|
* @deprecated |
271
|
|
|
*/ |
272
|
|
|
public function setVisibilityCondition($visibled) |
273
|
|
|
{ |
274
|
|
|
$this->visibled = $this->setVisible($visibled); |
|
|
|
|
275
|
|
|
|
276
|
|
|
return $this; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
/** |
280
|
|
|
* @return mixed |
281
|
|
|
*/ |
282
|
|
|
public function getValue() |
283
|
|
|
{ |
284
|
|
|
return $this->value; |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* @param mixed $value |
289
|
|
|
* |
290
|
|
|
* @return $this |
291
|
|
|
*/ |
292
|
|
|
public function setValue($value) |
293
|
|
|
{ |
294
|
|
|
$this->value = $value; |
295
|
|
|
|
296
|
|
|
return $this; |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* @param Request $request |
301
|
|
|
* |
302
|
|
|
* @return void |
303
|
|
|
*/ |
304
|
|
|
public function save(Request $request) |
305
|
|
|
{ |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* @param Request $request |
310
|
|
|
* |
311
|
|
|
* @return void |
312
|
|
|
*/ |
313
|
|
|
public function afterSave(Request $request) |
314
|
|
|
{ |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* @return array |
319
|
|
|
*/ |
320
|
|
|
public function toArray() |
321
|
|
|
{ |
322
|
|
|
return [ |
323
|
|
|
'value' => $this->getValue(), |
324
|
|
|
'readonly' => $this->isReadonly(), |
325
|
|
|
'visibled' => $this->isVisible(), |
326
|
|
|
'model' => $this->getModel(), |
327
|
|
|
]; |
328
|
|
|
} |
329
|
|
|
} |
330
|
|
|
|