|
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|\Illuminate\Validation\Rule|\Illuminate\Contracts\Validation\Rule $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
|
|
|
* |
|
150
|
|
|
* @return $this |
|
151
|
|
|
*/ |
|
152
|
|
|
public function setValidationRules($validationRules) |
|
153
|
|
|
{ |
|
154
|
|
|
if (! is_array($validationRules)) { |
|
155
|
|
|
$validationRules = func_get_args(); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
$this->validationRules = []; |
|
159
|
|
|
foreach ($validationRules as $rule) { |
|
160
|
|
|
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) { |
|
161
|
|
|
$this->addValidationRule($rule); |
|
162
|
|
|
} else { |
|
163
|
|
|
$rules = explode('|', $rule); |
|
164
|
|
|
foreach ($rules as $addRule) { |
|
165
|
|
|
$this->addValidationRule($addRule); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
return $this; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* @return Model |
|
175
|
|
|
*/ |
|
176
|
|
|
public function getModel() |
|
177
|
|
|
{ |
|
178
|
|
|
return $this->model; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* @param Model $model |
|
183
|
|
|
* |
|
184
|
|
|
* @return $this |
|
185
|
|
|
*/ |
|
186
|
|
|
public function setModel(Model $model) |
|
187
|
|
|
{ |
|
188
|
|
|
$this->model = $model; |
|
189
|
|
|
|
|
190
|
|
|
return $this; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @return bool|callable |
|
195
|
|
|
*/ |
|
196
|
|
|
public function isReadonly() |
|
197
|
|
|
{ |
|
198
|
|
|
if (is_callable($this->readonly)) { |
|
199
|
|
|
return (bool) call_user_func($this->readonly, $this->getModel()); |
|
|
|
|
|
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
return (bool) $this->readonly; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* @return bool|callable |
|
207
|
|
|
*/ |
|
208
|
|
|
public function isVisible() |
|
209
|
|
|
{ |
|
210
|
|
|
if (is_callable($this->visibled)) { |
|
211
|
|
|
return (bool) call_user_func($this->visibled, $this->getModel()); |
|
|
|
|
|
|
212
|
|
|
} |
|
213
|
|
|
|
|
214
|
|
|
return (bool) $this->visibled; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* @return bool |
|
219
|
|
|
*/ |
|
220
|
|
|
public function isValueSkipped() |
|
221
|
|
|
{ |
|
222
|
|
|
if (is_callable($this->valueSkipped)) { |
|
223
|
|
|
return (bool) call_user_func($this->valueSkipped, $this->getModel()); |
|
|
|
|
|
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
return (bool) $this->valueSkipped; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* @param Closure|bool $valueSkipped |
|
231
|
|
|
* |
|
232
|
|
|
* @return $this |
|
233
|
|
|
*/ |
|
234
|
|
|
public function setValueSkipped($valueSkipped) |
|
235
|
|
|
{ |
|
236
|
|
|
$this->valueSkipped = $valueSkipped; |
|
237
|
|
|
|
|
238
|
|
|
return $this; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* @param Closure|bool $readonly |
|
243
|
|
|
* |
|
244
|
|
|
* @return $this |
|
245
|
|
|
*/ |
|
246
|
|
|
public function setReadonly($readonly) |
|
247
|
|
|
{ |
|
248
|
|
|
$this->readonly = $readonly; |
|
249
|
|
|
|
|
250
|
|
|
return $this; |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* @param Closure|bool $visibled |
|
255
|
|
|
* |
|
256
|
|
|
* @return $this |
|
257
|
|
|
*/ |
|
258
|
|
|
public function setVisible($visibled) |
|
259
|
|
|
{ |
|
260
|
|
|
$this->visibled = $visibled; |
|
261
|
|
|
|
|
262
|
|
|
return $this; |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* @param Closure|bool $visibled |
|
267
|
|
|
* |
|
268
|
|
|
* @return $this |
|
269
|
|
|
* @deprecated |
|
270
|
|
|
*/ |
|
271
|
|
|
public function setVisibilityCondition($visibled) |
|
272
|
|
|
{ |
|
273
|
|
|
$this->visibled = $this->setVisible($visibled); |
|
|
|
|
|
|
274
|
|
|
|
|
275
|
|
|
return $this; |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
/** |
|
279
|
|
|
* @return mixed |
|
280
|
|
|
*/ |
|
281
|
|
|
public function getValue() |
|
282
|
|
|
{ |
|
283
|
|
|
return $this->value; |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
/** |
|
287
|
|
|
* @param mixed $value |
|
288
|
|
|
* |
|
289
|
|
|
* @return $this |
|
290
|
|
|
*/ |
|
291
|
|
|
public function setValue($value) |
|
292
|
|
|
{ |
|
293
|
|
|
$this->value = $value; |
|
294
|
|
|
|
|
295
|
|
|
return $this; |
|
296
|
|
|
} |
|
297
|
|
|
|
|
298
|
|
|
/** |
|
299
|
|
|
* @param Request $request |
|
300
|
|
|
* |
|
301
|
|
|
* @return void |
|
302
|
|
|
*/ |
|
303
|
|
|
public function save(Request $request) |
|
304
|
|
|
{ |
|
305
|
|
|
} |
|
306
|
|
|
|
|
307
|
|
|
/** |
|
308
|
|
|
* @param Request $request |
|
309
|
|
|
* |
|
310
|
|
|
* @return void |
|
311
|
|
|
*/ |
|
312
|
|
|
public function afterSave(Request $request) |
|
313
|
|
|
{ |
|
314
|
|
|
} |
|
315
|
|
|
|
|
316
|
|
|
/** |
|
317
|
|
|
* @return array |
|
318
|
|
|
*/ |
|
319
|
|
|
public function toArray() |
|
320
|
|
|
{ |
|
321
|
|
|
return [ |
|
322
|
|
|
'value' => $this->getValue(), |
|
323
|
|
|
'readonly' => $this->isReadonly(), |
|
324
|
|
|
'visibled' => $this->isVisible(), |
|
325
|
|
|
'model' => $this->getModel(), |
|
326
|
|
|
]; |
|
327
|
|
|
} |
|
328
|
|
|
} |
|
329
|
|
|
|