1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin\Traits; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use SleepingOwl\Admin\Contracts\Validable; |
7
|
|
|
use SleepingOwl\Admin\Contracts\Initializable; |
8
|
|
|
use SleepingOwl\Admin\Form\FormElementsCollection; |
9
|
|
|
use SleepingOwl\Admin\Contracts\WithModelInterface; |
10
|
|
|
use SleepingOwl\Admin\Form\Element\NamedFormElement; |
11
|
|
|
use SleepingOwl\Admin\Contracts\Form\ElementsInterface; |
12
|
|
|
use SleepingOwl\Admin\Contracts\Form\FormElementInterface; |
13
|
|
|
|
14
|
|
|
trait FormElements |
15
|
|
|
{ |
16
|
|
|
use FormElementsRecursiveIterator; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var FormElementsCollection |
20
|
|
|
*/ |
21
|
|
|
protected $elements; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @return void |
25
|
|
|
*/ |
26
|
|
|
public function initializeElements() |
27
|
|
|
{ |
28
|
|
|
$this->getElements()->each(function ($element) { |
29
|
|
|
if ($element instanceof Initializable) { |
30
|
|
|
$element->initialize(); |
31
|
|
|
} |
32
|
|
|
}); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param \Closure $callback |
37
|
|
|
* |
38
|
|
|
* @return bool|void |
39
|
|
|
*/ |
40
|
|
|
public function recursiveIterateElements(\Closure $callback) |
41
|
|
|
{ |
42
|
|
|
// If Callback function returns TRUE then recurse iterator should stop. |
43
|
|
|
$result = null; |
44
|
|
|
|
45
|
|
|
foreach ($this->getElements() as $element) { |
46
|
|
|
if ($element instanceof ElementsInterface) { |
47
|
|
|
$result = $element->recursiveIterateElements($callback); |
|
|
|
|
48
|
|
|
} else { |
49
|
|
|
$result = $callback($element); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if ($result === true) { |
53
|
|
|
break; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $result; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param string $path |
62
|
|
|
* |
63
|
|
|
* @return FormElementInterface|null |
64
|
|
|
*/ |
65
|
|
|
public function getElement($path) |
66
|
|
|
{ |
67
|
|
|
$found = null; |
|
|
|
|
68
|
|
|
|
69
|
|
|
foreach ($this->getElements() as $element) { |
70
|
|
|
if ($element instanceof ElementsInterface) { |
71
|
|
|
if (! is_null($found = $element->getElement($path))) { |
72
|
|
|
return $found; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if ($element instanceof NamedFormElement && $element->getPath() == $path) { |
77
|
|
|
return $element; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return FormElementsCollection |
84
|
|
|
*/ |
85
|
|
|
public function getElements() |
86
|
|
|
{ |
87
|
|
|
return $this->elements; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param array $elements |
92
|
|
|
* |
93
|
|
|
* @return $this |
94
|
|
|
*/ |
95
|
|
|
public function setElements(array $elements) |
96
|
|
|
{ |
97
|
|
|
$this->elements = new FormElementsCollection($elements); |
98
|
|
|
|
99
|
|
|
return $this; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param FormElementInterface $element |
104
|
|
|
* |
105
|
|
|
* @return $this |
106
|
|
|
*/ |
107
|
|
|
public function addElement($element) |
108
|
|
|
{ |
109
|
|
|
$this->elements->push($element); |
110
|
|
|
|
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param Model $model |
116
|
|
|
* |
117
|
|
|
* @return $this |
118
|
|
|
*/ |
119
|
|
|
public function setModel(Model $model) |
120
|
|
|
{ |
121
|
|
|
return $this->setModelForElements($model); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return array |
126
|
|
|
*/ |
127
|
|
|
public function getValidationRules() |
128
|
|
|
{ |
129
|
|
|
return $this->getValidationRulesFromElements(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return array |
134
|
|
|
*/ |
135
|
|
|
public function getValidationMessages() |
136
|
|
|
{ |
137
|
|
|
return $this->getValidationMessagesForElements(); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return array |
142
|
|
|
*/ |
143
|
|
|
public function getValidationLabels() |
144
|
|
|
{ |
145
|
|
|
return $this->getValidationLabelsForElements(); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param \Illuminate\Http\Request $request |
150
|
|
|
* |
151
|
|
|
* @return void |
152
|
|
|
*/ |
153
|
|
|
public function save(\Illuminate\Http\Request $request) |
154
|
|
|
{ |
155
|
|
|
$this->saveElements($request); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param \Illuminate\Http\Request $request |
160
|
|
|
* |
161
|
|
|
* @return void |
162
|
|
|
*/ |
163
|
|
|
public function afterSave(\Illuminate\Http\Request $request) |
164
|
|
|
{ |
165
|
|
|
$this->afterSaveElements($request); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param Model $model |
170
|
|
|
* |
171
|
|
|
* @return $this |
172
|
|
|
*/ |
173
|
|
|
protected function setModelForElements(Model $model) |
174
|
|
|
{ |
175
|
|
|
$this->getElements()->each(function ($element) use ($model) { |
176
|
|
|
$element = $this->getElementContainer($element); |
177
|
|
|
|
178
|
|
|
if ($element instanceof WithModelInterface) { |
179
|
|
|
$element->setModel($model); |
180
|
|
|
} |
181
|
|
|
}); |
182
|
|
|
|
183
|
|
|
return $this; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param array $rules |
188
|
|
|
* |
189
|
|
|
* @return array |
190
|
|
|
*/ |
191
|
|
|
protected function getValidationRulesFromElements(array $rules = []) |
192
|
|
|
{ |
193
|
|
|
$this->getElements()->onlyActive()->each(function ($element) use (&$rules) { |
194
|
|
|
$element = $this->getElementContainer($element); |
195
|
|
|
|
196
|
|
|
if ($element instanceof Validable) { |
197
|
|
|
$rules += $element->getValidationRules(); |
198
|
|
|
} |
199
|
|
|
}); |
200
|
|
|
|
201
|
|
|
return $rules; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @param array $messages |
206
|
|
|
* |
207
|
|
|
* @return array |
208
|
|
|
*/ |
209
|
|
|
protected function getValidationMessagesForElements(array $messages = []) |
210
|
|
|
{ |
211
|
|
|
$this->getElements()->onlyActive()->each(function ($element) use (&$messages) { |
212
|
|
|
$element = $this->getElementContainer($element); |
213
|
|
|
|
214
|
|
|
if ($element instanceof Validable) { |
215
|
|
|
$messages += $element->getValidationMessages(); |
216
|
|
|
} |
217
|
|
|
}); |
218
|
|
|
|
219
|
|
|
return $messages; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* @param array $labels |
224
|
|
|
* |
225
|
|
|
* @return array |
226
|
|
|
*/ |
227
|
|
|
protected function getValidationLabelsForElements(array $labels = []) |
228
|
|
|
{ |
229
|
|
|
$this->getElements()->onlyActive()->each(function ($element) use (&$labels) { |
230
|
|
|
$element = $this->getElementContainer($element); |
231
|
|
|
|
232
|
|
|
if ($element instanceof Validable) { |
233
|
|
|
$labels += $element->getValidationLabels(); |
234
|
|
|
} |
235
|
|
|
}); |
236
|
|
|
|
237
|
|
|
return $labels; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @param \Illuminate\Http\Request $request |
242
|
|
|
* |
243
|
|
|
* @return void |
244
|
|
|
*/ |
245
|
|
|
protected function saveElements(\Illuminate\Http\Request $request) |
246
|
|
|
{ |
247
|
|
|
$this->getElements()->onlyActive()->each(function ($element) use ($request) { |
248
|
|
|
$element = $this->getElementContainer($element); |
249
|
|
|
|
250
|
|
|
if ($element instanceof FormElementInterface) { |
251
|
|
|
$element->save($request); |
252
|
|
|
} |
253
|
|
|
}); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* @param \Illuminate\Http\Request $request |
258
|
|
|
* |
259
|
|
|
* @return void |
260
|
|
|
*/ |
261
|
|
|
protected function afterSaveElements(\Illuminate\Http\Request $request) |
262
|
|
|
{ |
263
|
|
|
$this->getElements()->onlyActive()->each(function ($element) use ($request) { |
264
|
|
|
$element = $this->getElementContainer($element); |
265
|
|
|
|
266
|
|
|
if ($element instanceof FormElementInterface) { |
267
|
|
|
$element->afterSave($request); |
268
|
|
|
} |
269
|
|
|
}); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* @param $object |
274
|
|
|
* |
275
|
|
|
* @return mixed |
276
|
|
|
*/ |
277
|
|
|
protected function getElementContainer($object) |
278
|
|
|
{ |
279
|
|
|
return $object; |
280
|
|
|
} |
281
|
|
|
} |
282
|
|
|
|