We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
| Total Complexity | 74 |
| Total Lines | 546 |
| Duplicated Lines | 0 % |
| Changes | 6 | ||
| Bugs | 1 | Features | 1 |
Complex classes like Fields often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Fields, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | trait Fields |
||
| 9 | { |
||
| 10 | use FieldsProtectedMethods; |
||
| 11 | use FieldsPrivateMethods; |
||
| 12 | |||
| 13 | // ------------ |
||
| 14 | // FIELDS |
||
| 15 | // ------------ |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Get the CRUD fields for the current operation with name processed to be usable in HTML. |
||
| 19 | * |
||
| 20 | * @return array |
||
| 21 | */ |
||
| 22 | public function fields() |
||
| 25 | } |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Returns the fields as they are stored inside operation setting, not running the |
||
| 29 | * presentation callbacks like converting the `dot.names` into `dot[names]` for html for example. |
||
| 30 | */ |
||
| 31 | public function getCleanStateFields() |
||
| 34 | } |
||
| 35 | |||
| 36 | /** |
||
| 37 | * The only REALLY MANDATORY attribute when defining a field is the 'name'. |
||
| 38 | * Everything else Backpack can probably guess. This method makes sure the |
||
| 39 | * field definition array is complete, by guessing missing attributes. |
||
| 40 | * |
||
| 41 | * @param string|array $field The definition of a field (string or array). |
||
| 42 | * @return array The correct definition of that field. |
||
| 43 | */ |
||
| 44 | public function makeSureFieldHasNecessaryAttributes($field) |
||
| 45 | { |
||
| 46 | $field = $this->makeSureFieldHasName($field); |
||
| 47 | $field = $this->makeSureFieldHasEntity($field); |
||
| 48 | $field = $this->makeSureFieldHasLabel($field); |
||
| 49 | |||
| 50 | if (isset($field['entity']) && $field['entity'] !== false) { |
||
| 51 | $field = $this->makeSureFieldHasRelationshipAttributes($field); |
||
| 52 | } |
||
| 53 | |||
| 54 | $field = $this->makeSureFieldHasType($field); |
||
| 55 | $field = $this->makeSureSubfieldsHaveNecessaryAttributes($field); |
||
| 56 | $field = $this->makeSureMorphSubfieldsAreDefined($field); |
||
| 57 | |||
| 58 | $this->setupFieldValidation($field, $field['parentFieldName'] ?? false); |
||
| 59 | |||
| 60 | return $field; |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * When field is a relationship, Backpack will try to guess some basic attributes from the relation. |
||
| 65 | * |
||
| 66 | * @param array $field |
||
| 67 | * @return array |
||
| 68 | */ |
||
| 69 | public function makeSureFieldHasRelationshipAttributes($field) |
||
| 70 | { |
||
| 71 | $field = $this->makeSureFieldHasRelationType($field); |
||
| 72 | $field = $this->makeSureFieldHasModel($field); |
||
| 73 | $field = $this->makeSureFieldHasAttribute($field); |
||
| 74 | $field = $this->makeSureFieldHasMultiple($field); |
||
| 75 | $field = $this->makeSureFieldHasPivot($field); |
||
| 76 | $field = $this->makeSureFieldHasType($field); |
||
| 77 | |||
| 78 | return $field; |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Register all Eloquent Model events that are defined on fields. |
||
| 83 | * Eg. saving, saved, creating, created, updating, updated. |
||
| 84 | * |
||
| 85 | * @see https://laravel.com/docs/master/eloquent#events |
||
| 86 | * |
||
| 87 | * @return void |
||
| 88 | */ |
||
| 89 | public function registerFieldEvents() |
||
| 95 | } |
||
| 96 | } |
||
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Add a field to the create/update form or both. |
||
| 102 | * |
||
| 103 | * @param string|array $field The new field. |
||
| 104 | * @return self |
||
| 105 | */ |
||
| 106 | public function addField($field) |
||
| 107 | { |
||
| 108 | $field = $this->makeSureFieldHasNecessaryAttributes($field); |
||
| 109 | |||
| 110 | $this->enableTabsIfFieldUsesThem($field); |
||
| 111 | $this->addFieldToOperationSettings($field); |
||
| 112 | (new CrudField($field['name']))->callRegisteredAttributeMacros(); |
||
| 113 | |||
| 114 | return $this; |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Add multiple fields to the create/update form or both. |
||
| 119 | * |
||
| 120 | * @param array $fields The new fields. |
||
| 121 | */ |
||
| 122 | public function addFields($fields) |
||
| 123 | { |
||
| 124 | if (count($fields)) { |
||
| 125 | foreach ($fields as $field) { |
||
| 126 | $this->addField($field); |
||
| 127 | } |
||
| 128 | } |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Move the most recently added field after the given target field. |
||
| 133 | * |
||
| 134 | * @param string $targetFieldName The target field name. |
||
| 135 | */ |
||
| 136 | public function afterField($targetFieldName) |
||
| 137 | { |
||
| 138 | $this->transformFields(function ($fields) use ($targetFieldName) { |
||
| 139 | return $this->moveField($fields, $targetFieldName, false); |
||
| 140 | }); |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Move the most recently added field before the given target field. |
||
| 145 | * |
||
| 146 | * @param string $targetFieldName The target field name. |
||
| 147 | */ |
||
| 148 | public function beforeField($targetFieldName) |
||
| 149 | { |
||
| 150 | $this->transformFields(function ($fields) use ($targetFieldName) { |
||
| 151 | return $this->moveField($fields, $targetFieldName, true); |
||
| 152 | }); |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Move this field to be first in the fields list. |
||
| 157 | * |
||
| 158 | * @return bool|null |
||
| 159 | */ |
||
| 160 | public function makeFirstField() |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Remove a certain field from the create/update/both forms by its name. |
||
| 172 | * |
||
| 173 | * @param string $name Field name (as defined with the addField() procedure) |
||
| 174 | */ |
||
| 175 | public function removeField($name) |
||
| 176 | { |
||
| 177 | $this->transformFields(function ($fields) use ($name) { |
||
| 178 | Arr::forget($fields, $name); |
||
| 179 | |||
| 180 | return $fields; |
||
| 181 | }); |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Remove many fields from the create/update/both forms by their name. |
||
| 186 | * |
||
| 187 | * @param array $array_of_names A simple array of the names of the fields to be removed. |
||
| 188 | */ |
||
| 189 | public function removeFields($array_of_names) |
||
| 190 | { |
||
| 191 | if (! empty($array_of_names)) { |
||
| 192 | foreach ($array_of_names as $name) { |
||
| 193 | $this->removeField($name); |
||
| 194 | } |
||
| 195 | } |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Remove all fields from the create/update/both forms. |
||
| 200 | */ |
||
| 201 | public function removeAllFields() |
||
| 202 | { |
||
| 203 | $current_fields = $this->getCleanStateFields(); |
||
| 204 | if (! empty($current_fields)) { |
||
| 205 | foreach ($current_fields as $field) { |
||
| 206 | $this->removeField($field['name']); |
||
| 207 | } |
||
| 208 | } |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Remove an attribute from one field's definition array. |
||
| 213 | * |
||
| 214 | * @param string $field The name of the field. |
||
| 215 | * @param string $attribute The name of the attribute being removed. |
||
| 216 | */ |
||
| 217 | public function removeFieldAttribute($field, $attribute) |
||
| 218 | { |
||
| 219 | $fields = $this->getCleanStateFields(); |
||
| 220 | |||
| 221 | unset($fields[$field][$attribute]); |
||
| 222 | |||
| 223 | $this->setOperationSetting('fields', $fields); |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Update value of a given key for a current field. |
||
| 228 | * |
||
| 229 | * @param string $fieldName The field name |
||
| 230 | * @param array $modifications An array of changes to be made. |
||
| 231 | */ |
||
| 232 | public function modifyField($fieldName, $modifications) |
||
| 233 | { |
||
| 234 | $fieldsArray = $this->getCleanStateFields(); |
||
| 235 | $field = $this->firstFieldWhere('name', $fieldName); |
||
| 236 | $fieldKey = $this->getFieldKey($field); |
||
| 237 | |||
| 238 | foreach ($modifications as $attributeName => $attributeValue) { |
||
| 239 | $fieldsArray[$fieldKey][$attributeName] = $attributeValue; |
||
| 240 | } |
||
| 241 | |||
| 242 | $this->enableTabsIfFieldUsesThem($modifications); |
||
| 243 | |||
| 244 | $this->setOperationSetting('fields', $fieldsArray); |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Set label for a specific field. |
||
| 249 | * |
||
| 250 | * @param string $field |
||
| 251 | * @param string $label |
||
| 252 | */ |
||
| 253 | public function setFieldLabel($field, $label) |
||
| 254 | { |
||
| 255 | $this->modifyField($field, ['label' => $label]); |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Check if field is the first of its type in the given fields array. |
||
| 260 | * It's used in each field_type.blade.php to determine wether to push the css and js content or not (we only need to push the js and css for a field the first time it's loaded in the form, not any subsequent times). |
||
| 261 | * |
||
| 262 | * @param array $field The current field being tested if it's the first of its type. |
||
| 263 | * @return bool true/false |
||
| 264 | */ |
||
| 265 | public function checkIfFieldIsFirstOfItsType($field) |
||
| 266 | { |
||
| 267 | $fields_array = $this->getCleanStateFields(); |
||
| 268 | $first_field = $this->getFirstOfItsTypeInArray($field['type'], $fields_array); |
||
| 269 | |||
| 270 | if ($first_field && $field['name'] == $first_field['name']) { |
||
| 271 | return true; |
||
| 272 | } |
||
| 273 | |||
| 274 | return false; |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Decode attributes that are casted as array/object/json in the model. |
||
| 279 | * So that they are not json_encoded twice before they are stored in the db |
||
| 280 | * (once by Backpack in front-end, once by Laravel Attribute Casting). |
||
| 281 | * |
||
| 282 | * @param array $input |
||
| 283 | * @param mixed $model |
||
| 284 | * @return array |
||
| 285 | */ |
||
| 286 | public function decodeJsonCastedAttributes($input, $model = false) |
||
| 287 | { |
||
| 288 | $model = $model ? $model : $this->model; |
||
| 289 | $fields = $this->getCleanStateFields(); |
||
| 290 | $casted_attributes = $model->getCastedAttributes(); |
||
| 291 | |||
| 292 | foreach ($fields as $field) { |
||
| 293 | // Test the field is castable |
||
| 294 | if (isset($field['name']) && is_string($field['name']) && array_key_exists($field['name'], $casted_attributes)) { |
||
| 295 | // Handle JSON field types |
||
| 296 | $jsonCastables = ['array', 'object', 'json']; |
||
| 297 | $fieldCasting = $casted_attributes[$field['name']]; |
||
| 298 | |||
| 299 | if (in_array($fieldCasting, $jsonCastables) && isset($input[$field['name']]) && ! empty($input[$field['name']]) && ! is_array($input[$field['name']])) { |
||
| 300 | try { |
||
| 301 | $input[$field['name']] = json_decode($input[$field['name']]); |
||
| 302 | } catch (\Exception $e) { |
||
| 303 | $input[$field['name']] = []; |
||
| 304 | } |
||
| 305 | } |
||
| 306 | } |
||
| 307 | } |
||
| 308 | |||
| 309 | return $input; |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @return array |
||
| 314 | */ |
||
| 315 | public function getCurrentFields() |
||
| 316 | { |
||
| 317 | return $this->fields(); |
||
| 318 | } |
||
| 319 | |||
| 320 | /** |
||
| 321 | * Order the CRUD fields. If certain fields are missing from the given order array, they will be |
||
| 322 | * pushed to the new fields array in the original order. |
||
| 323 | * |
||
| 324 | * @param array $order An array of field names in the desired order. |
||
| 325 | */ |
||
| 326 | public function orderFields($order) |
||
| 327 | { |
||
| 328 | $this->transformFields(function ($fields) use ($order) { |
||
| 329 | return $this->applyOrderToFields($fields, $order); |
||
| 330 | }); |
||
| 331 | } |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Get the fields for the create or update forms. |
||
| 335 | * |
||
| 336 | * @return array all the fields that need to be shown and their information |
||
| 337 | */ |
||
| 338 | public function getFields() |
||
| 339 | { |
||
| 340 | return $this->fields(); |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Check if the create/update form has upload fields. |
||
| 345 | * Upload fields are the ones that have "upload" => true defined on them. |
||
| 346 | * |
||
| 347 | * @param string $form create/update/both - defaults to 'both' |
||
| 348 | * @param bool|int $id id of the entity - defaults to false |
||
| 349 | * @return bool |
||
| 350 | */ |
||
| 351 | public function hasUploadFields() |
||
| 352 | { |
||
| 353 | $fields = $this->getCleanStateFields(); |
||
| 354 | $upload_fields = Arr::where($fields, function ($value, $key) { |
||
| 355 | // check if any subfields have uploads |
||
| 356 | if (isset($value['subfields'])) { |
||
| 357 | foreach ($value['subfields'] as $subfield) { |
||
| 358 | if (isset($subfield['upload']) && $subfield['upload'] === true) { |
||
| 359 | return true; |
||
| 360 | } |
||
| 361 | } |
||
| 362 | } |
||
| 363 | |||
| 364 | return isset($value['upload']) && $value['upload'] == true; |
||
| 365 | }); |
||
| 366 | |||
| 367 | return count($upload_fields) ? true : false; |
||
| 368 | } |
||
| 369 | |||
| 370 | // ---------------------- |
||
| 371 | // FIELD ASSET MANAGEMENT |
||
| 372 | // ---------------------- |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Get all the field types whose resources (JS and CSS) have already been loaded on page. |
||
| 376 | * |
||
| 377 | * @return array Array with the names of the field types. |
||
| 378 | */ |
||
| 379 | public function getLoadedFieldTypes() |
||
| 380 | { |
||
| 381 | return $this->getOperationSetting('loadedFieldTypes') ?? []; |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Set an array of field type names as already loaded for the current operation. |
||
| 386 | * |
||
| 387 | * @param array $fieldTypes |
||
| 388 | */ |
||
| 389 | public function setLoadedFieldTypes($fieldTypes) |
||
| 390 | { |
||
| 391 | $this->setOperationSetting('loadedFieldTypes', $fieldTypes); |
||
| 392 | } |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Get a namespaced version of the field type name. |
||
| 396 | * Appends the 'view_namespace' attribute of the field to the `type', using dot notation. |
||
| 397 | * |
||
| 398 | * @param mixed $field |
||
| 399 | * @return string Namespaced version of the field type name. Ex: 'text', 'custom.view.path.text' |
||
| 400 | */ |
||
| 401 | public function getFieldTypeWithNamespace($field) |
||
| 402 | { |
||
| 403 | if (is_array($field)) { |
||
| 404 | $fieldType = $field['type']; |
||
| 405 | if (isset($field['view_namespace'])) { |
||
| 406 | $fieldType = implode('.', [$field['view_namespace'], $field['type']]); |
||
| 407 | } |
||
| 408 | } else { |
||
| 409 | $fieldType = $field; |
||
| 410 | } |
||
| 411 | |||
| 412 | return $fieldType; |
||
| 413 | } |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Add a new field type to the loadedFieldTypes array. |
||
| 417 | * |
||
| 418 | * @param string $field Field array |
||
| 419 | * @return bool Successful operation true/false. |
||
| 420 | */ |
||
| 421 | public function addLoadedFieldType($field) |
||
| 422 | { |
||
| 423 | $alreadyLoaded = $this->getLoadedFieldTypes(); |
||
| 424 | $type = $this->getFieldTypeWithNamespace($field); |
||
| 425 | |||
| 426 | if (! in_array($type, $this->getLoadedFieldTypes(), true)) { |
||
| 427 | $alreadyLoaded[] = $type; |
||
| 428 | $this->setLoadedFieldTypes($alreadyLoaded); |
||
| 429 | |||
| 430 | return true; |
||
| 431 | } |
||
| 432 | |||
| 433 | return false; |
||
| 434 | } |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Alias of the addLoadedFieldType() method. |
||
| 438 | * Adds a new field type to the loadedFieldTypes array. |
||
| 439 | * |
||
| 440 | * @param string $field Field array |
||
| 441 | * @return bool Successful operation true/false. |
||
| 442 | */ |
||
| 443 | public function markFieldTypeAsLoaded($field) |
||
| 444 | { |
||
| 445 | return $this->addLoadedFieldType($field); |
||
| 446 | } |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Check if a field type's reasources (CSS and JS) have already been loaded. |
||
| 450 | * |
||
| 451 | * @param string $field Field array |
||
| 452 | * @return bool Whether the field type has been marked as loaded. |
||
| 453 | */ |
||
| 454 | public function fieldTypeLoaded($field) |
||
| 455 | { |
||
| 456 | return in_array($this->getFieldTypeWithNamespace($field), $this->getLoadedFieldTypes()); |
||
| 457 | } |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Check if a field type's reasources (CSS and JS) have NOT been loaded. |
||
| 461 | * |
||
| 462 | * @param string $field Field array |
||
| 463 | * @return bool Whether the field type has NOT been marked as loaded. |
||
| 464 | */ |
||
| 465 | public function fieldTypeNotLoaded($field) |
||
| 468 | } |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Get a list of all field names for the current operation. |
||
| 472 | * |
||
| 473 | * @return array |
||
| 474 | */ |
||
| 475 | public function getAllFieldNames() |
||
| 476 | { |
||
| 477 | return Arr::flatten(Arr::pluck($this->getCleanStateFields(), 'name')); |
||
| 478 | } |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Returns the request without anything that might have been maliciously inserted. |
||
| 482 | * Only specific field names that have been introduced with addField() are kept in the request. |
||
| 483 | * |
||
| 484 | * @param \Illuminate\Http\Request $request |
||
| 485 | * @return array |
||
| 486 | */ |
||
| 487 | public function getStrippedSaveRequest($request) |
||
| 488 | { |
||
| 489 | $setting = $this->getOperationSetting('strippedRequest'); |
||
| 490 | |||
| 491 | // if a closure was passed |
||
| 492 | if (is_callable($setting)) { |
||
| 493 | return $setting($request); |
||
| 494 | } |
||
| 495 | |||
| 496 | // if an invokable class was passed |
||
| 497 | // eg. \App\Http\Requests\BackpackStrippedRequest |
||
| 498 | if (is_string($setting) && class_exists($setting)) { |
||
| 499 | $setting = new $setting(); |
||
| 500 | |||
| 501 | return is_callable($setting) ? $setting($request) : abort(500, get_class($setting).' is not invokable.'); |
||
| 502 | } |
||
| 503 | |||
| 504 | return $request->only($this->getAllFieldNames()); |
||
| 505 | } |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Check if a field exists, by any given attribute. |
||
| 509 | * |
||
| 510 | * @param string $attribute Attribute name on that field definition array. |
||
| 511 | * @param string $value Value of that attribute on that field definition array. |
||
| 512 | * @return bool |
||
| 513 | */ |
||
| 514 | public function hasFieldWhere($attribute, $value) |
||
| 521 | } |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Get the first field where a given attribute has the given value. |
||
| 525 | * |
||
| 526 | * @param string $attribute Attribute name on that field definition array. |
||
| 527 | * @param string $value Value of that attribute on that field definition array. |
||
| 528 | * @return bool |
||
| 529 | */ |
||
| 530 | public function firstFieldWhere($attribute, $value) |
||
| 534 | }); |
||
| 535 | } |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Create and return a CrudField object for that field name. |
||
| 539 | * |
||
| 540 | * Enables developers to use a fluent syntax to declare their fields, |
||
| 541 | * in addition to the existing options: |
||
| 542 | * - CRUD::addField(['name' => 'price', 'type' => 'number']); |
||
| 543 | * - CRUD::field('price')->type('number'); |
||
| 544 | * |
||
| 545 | * And if the developer uses the CrudField object as Field in their CrudController: |
||
| 546 | * - Field::name('price')->type('number'); |
||
| 547 | * |
||
| 548 | * @param string $name The name of the column in the db, or model attribute. |
||
| 549 | * @return CrudField |
||
| 550 | */ |
||
| 551 | public function field($name) |
||
| 554 | } |
||
| 555 | } |
||
| 556 |