diff --git a/composer.json b/composer.json index cc038e4a..8d69614c 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,10 @@ "autoload": { "psr-4": { "Whitecube\\NovaFlexibleContent\\": "src/" - } + }, + "files": [ + "src/Helper.php" + ] }, "extra": { "laravel": { diff --git a/src/Collections/FieldCollection.php b/src/Collections/FieldCollection.php new file mode 100644 index 00000000..f20990b9 --- /dev/null +++ b/src/Collections/FieldCollection.php @@ -0,0 +1,114 @@ +findResourceOrFail(); + $fields = $resource->updateFields($request); + + $attribute_parts = explode('__', $attribute, 2); + + $groups = []; + foreach ($fields as $i => $field) { + if ($field instanceof Flexible) { + $field->index = $i; + $groups = array_merge($groups, $this->flattenGroups($field)); + } + } + + foreach ($groups as $group) { + if ($group->inUseKey() !== $attribute_parts[0]) { + continue; + } + + $field = $group->collectionFields()->first(function ($field) use ($attribute_parts, $group) { + $field->group = $group; + + return isset($field->attribute) && + $field->attribute == $attribute_parts[1]; + }, $default); + + return $this->addDeleteCallback($field); + } + } + + return $this->first(function ($field) use ($attribute) { + return isset($field->attribute) && + $field->attribute == $attribute; + }, $default); + } + + /** + * Flatten all groups into a single array + * + * @param Field $field + * @param $parentGroup + * @return array + */ + private function flattenGroups(Field $field, $parentGroup = null) + { + if (!$field->groups()) { + return []; + } + + $flattened = []; + + foreach ($field->groups() as $groupIndex => $group) { + $group->originalField = ($parentGroup ? $parentGroup->originalField . (isset($field->index) ? '.' . $field->index : '') . '.attributes.' : '') . $field->attribute . '.' . $groupIndex; + + foreach ($group->collectionFields() as $groupField) { + if ($groupField instanceof Flexible) { + $flattened = array_merge($flattened, $this->flattenGroups($groupField, $group)); + } + } + + $flattened[] = $group; + } + return $flattened; + } + + /** + * Add the delete callback helper + * + * @param Field|null $field + * @return Field|null + */ + private function addDeleteCallback(?Field $field) + { + if (!$field || !isset($field->deleteCallback)) { + return $field; + } + + $callback = false; + if (is_callable($field->deleteCallback)) { + $callback = $field->deleteCallback; + } + + $field->delete(function(NovaRequest $request, $model) use ($callback, $field) { + if ($callback && $callback->call($field, ...func_get_args()) === true) { + return true; + } + + return \Whitecube\NovaFlexibleContent\deleteFile($request, $model, $field); + }); + + return $field; + } +} diff --git a/src/Flexible.php b/src/Flexible.php index 317522a7..8b8fc26c 100644 --- a/src/Flexible.php +++ b/src/Flexible.php @@ -74,6 +74,26 @@ public function __construct($name, $attribute = null, $resolveCallback = null) $this->hideFromIndex(); } + /** + * Get the field layouts + * + * @return Collection + */ + public function layouts() + { + return $this->layouts; + } + + /** + * Get the field groups + * + * @return Collection + */ + public function groups() + { + return $this->groups; + } + /** * Set the button's label * diff --git a/src/Helper.php b/src/Helper.php new file mode 100644 index 00000000..33c72b1d --- /dev/null +++ b/src/Helper.php @@ -0,0 +1,29 @@ +group->originalField); + $path[] = 'attributes'; + $path[] = $field->attribute; + + $mainField = array_shift($path); + $data = $model->{$mainField}; + + Arr::set($data, implode('.', $path), ''); + + $model->{$mainField} = $data; + $model->save(); + return true; +} diff --git a/src/Layouts/Layout.php b/src/Layouts/Layout.php index 39a440ff..6b493319 100644 --- a/src/Layouts/Layout.php +++ b/src/Layouts/Layout.php @@ -114,6 +114,16 @@ public function fields() return $this->fields ? $this->fields->all() : []; } + /** + * Retrieve the layout's fields as a collection + * + * @return \Illuminate\Support\Collection + */ + public function collectionFields() + { + return $this->fields; + } + /** * Retrieve the layout's unique key * diff --git a/src/Traits/AvailableFields.php b/src/Traits/AvailableFields.php new file mode 100644 index 00000000..72fdcb0e --- /dev/null +++ b/src/Traits/AvailableFields.php @@ -0,0 +1,21 @@ +filter($this->fields($request)))); + } +}