LaravelRUS /
SleepingOwlAdmin
| 1 | <?php |
||
| 2 | |||
| 3 | namespace SleepingOwl\Admin\Traits; |
||
| 4 | |||
| 5 | use Illuminate\Database\Eloquent\Model; |
||
| 6 | use SleepingOwl\Admin\Contracts\Form\ElementsInterface; |
||
| 7 | use SleepingOwl\Admin\Contracts\Form\FormElementInterface; |
||
| 8 | use SleepingOwl\Admin\Contracts\Initializable; |
||
| 9 | use SleepingOwl\Admin\Contracts\Validable; |
||
| 10 | use SleepingOwl\Admin\Contracts\WithModelInterface; |
||
| 11 | use SleepingOwl\Admin\Form\Element\NamedFormElement; |
||
| 12 | use SleepingOwl\Admin\Form\FormElementsCollection; |
||
| 13 | |||
| 14 | trait FormElements |
||
| 15 | { |
||
| 16 | use FormElementsRecursiveIterator; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var FormElementsCollection |
||
| 20 | */ |
||
| 21 | protected $elements; |
||
| 22 | |||
| 23 | /** |
||
| 24 | 5 | * @return void |
|
| 25 | */ |
||
| 26 | public function initializeElements() |
||
| 27 | 5 | { |
|
| 28 | 5 | $this->getElements()->each(function ($element) { |
|
| 29 | 5 | if ($element instanceof Initializable) { |
|
| 30 | 5 | $element->initialize(); |
|
| 31 | 5 | } |
|
| 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); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 48 | } else { |
||
| 49 | $result = $callback($element); |
||
| 50 | } |
||
| 51 | |||
| 52 | if ($result === true) { |
||
| 53 | break; |
||
| 54 | } |
||
| 55 | } |
||
| 56 | |||
| 57 | return $result; |
||
| 58 | 12 | } |
|
| 59 | |||
| 60 | 12 | /** |
|
| 61 | * @param string $path |
||
| 62 | * |
||
| 63 | * @return FormElementInterface|null |
||
| 64 | */ |
||
| 65 | public function getElement($path) |
||
| 66 | { |
||
| 67 | $found = null; |
||
|
0 ignored issues
–
show
|
|||
| 68 | 19 | ||
| 69 | foreach ($this->getElements() as $element) { |
||
| 70 | 19 | if ($element instanceof ElementsInterface) { |
|
| 71 | if (! is_null($found = $element->getElement($path))) { |
||
| 72 | 19 | 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 | 2 | { |
|
| 109 | $this->elements->push($element); |
||
| 110 | 2 | ||
| 111 | return $this; |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @param Model $model |
||
| 116 | 2 | * |
|
| 117 | * @return $this |
||
| 118 | 2 | */ |
|
| 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 | 5 | } |
|
| 147 | |||
| 148 | /** |
||
| 149 | 5 | * @param \Illuminate\Http\Request $request |
|
| 150 | * |
||
| 151 | 5 | * @return void |
|
| 152 | 5 | */ |
|
| 153 | 5 | public function save(\Illuminate\Http\Request $request) |
|
| 154 | 5 | { |
|
| 155 | $this->saveElements($request); |
||
| 156 | 5 | } |
|
| 157 | |||
| 158 | /** |
||
| 159 | * @param \Illuminate\Http\Request $request |
||
| 160 | * |
||
| 161 | * @return void |
||
| 162 | */ |
||
| 163 | public function afterSave(\Illuminate\Http\Request $request) |
||
| 164 | 3 | { |
|
| 165 | $this->afterSaveElements($request); |
||
| 166 | } |
||
| 167 | 3 | ||
| 168 | /** |
||
| 169 | 3 | * @param Model $model |
|
| 170 | 3 | * |
|
| 171 | 3 | * @return $this |
|
| 172 | 3 | */ |
|
| 173 | protected function setModelForElements(Model $model) |
||
| 174 | 3 | { |
|
| 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 | 2 | ||
| 183 | return $this; |
||
| 184 | } |
||
| 185 | 2 | ||
| 186 | /** |
||
| 187 | 2 | * @param array $rules |
|
| 188 | 2 | * |
|
| 189 | 2 | * @return array |
|
| 190 | 2 | */ |
|
| 191 | protected function getValidationRulesFromElements(array $rules = []) |
||
| 192 | 2 | { |
|
| 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 | 2 | ||
| 201 | return $rules; |
||
| 202 | } |
||
| 203 | 2 | ||
| 204 | /** |
||
| 205 | 2 | * @param array $messages |
|
| 206 | 2 | * |
|
| 207 | 2 | * @return array |
|
| 208 | 2 | */ |
|
| 209 | protected function getValidationMessagesForElements(array $messages = []) |
||
| 210 | 2 | { |
|
| 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 | 2 | ||
| 219 | return $messages; |
||
| 220 | } |
||
| 221 | 2 | ||
| 222 | /** |
||
| 223 | 2 | * @param array $labels |
|
| 224 | 2 | * |
|
| 225 | 2 | * @return array |
|
| 226 | 2 | */ |
|
| 227 | 2 | 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 | 2 | ||
| 237 | 2 | return $labels; |
|
| 238 | } |
||
| 239 | 2 | ||
| 240 | 2 | /** |
|
| 241 | 2 | * @param \Illuminate\Http\Request $request |
|
| 242 | 2 | * |
|
| 243 | 2 | * @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 | 8 | if ($element instanceof FormElementInterface) { |
|
| 251 | $element->save($request); |
||
| 252 | 8 | } |
|
| 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 |