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