From 23f4d40ef20bb046414069c15390d7049e17799a Mon Sep 17 00:00:00 2001 From: Samuel Georges Date: Fri, 25 Mar 2016 18:01:58 +1100 Subject: [PATCH] Merge common functionality in to FormModelWidget --- modules/backend/classes/FormWidgetBase.php | 12 --- modules/backend/formwidgets/FileUpload.php | 35 +------- modules/backend/formwidgets/RecordFinder.php | 21 +---- modules/backend/formwidgets/Relation.php | 23 +---- modules/backend/traits/FormModelWidget.php | 93 ++++++++++++++++++++ 5 files changed, 100 insertions(+), 84 deletions(-) create mode 100644 modules/backend/traits/FormModelWidget.php diff --git a/modules/backend/classes/FormWidgetBase.php b/modules/backend/classes/FormWidgetBase.php index 3ab2c4499..c335c791a 100644 --- a/modules/backend/classes/FormWidgetBase.php +++ b/modules/backend/classes/FormWidgetBase.php @@ -121,16 +121,4 @@ abstract class FormWidgetBase extends WidgetBase return $this->formField->getValueFromData($this->data ?: $this->model, $defaultValue); } - /** - * Returns the final model and attribute name of - * a nested HTML array attribute. - * Eg: list($model, $attribute) = $this->resolveModelAttribute($this->valueFrom); - * @param string $attribute. - * @return array - */ - public function resolveModelAttribute($attribute) - { - return $this->formField->resolveModelAttribute($this->model, $attribute); - } - } diff --git a/modules/backend/formwidgets/FileUpload.php b/modules/backend/formwidgets/FileUpload.php index 1f5876b33..112c076ca 100644 --- a/modules/backend/formwidgets/FileUpload.php +++ b/modules/backend/formwidgets/FileUpload.php @@ -1,17 +1,16 @@ resolveModelAttribute($this->valueFrom); - - if (!$model->hasRelation($attribute)) { - throw new ApplicationException(Lang::get('backend::lang.model.missing_relation', [ - 'class' => get_class($model), - 'relation' => $attribute - ])); - } - - return $model->{$attribute}(); - } - - /** - * Returns the value as a relation type from the model, - * supports nesting via HTML array. - * @return Relation - */ - protected function getRelationType() - { - list($model, $attribute) = $this->resolveModelAttribute($this->valueFrom); - return $model->getRelationType($attribute); - } - /** * Removes a file attachment. */ diff --git a/modules/backend/formwidgets/RecordFinder.php b/modules/backend/formwidgets/RecordFinder.php index bc5c985a1..fc27b3e99 100644 --- a/modules/backend/formwidgets/RecordFinder.php +++ b/modules/backend/formwidgets/RecordFinder.php @@ -22,6 +22,8 @@ use Backend\Classes\FormWidgetBase; */ class RecordFinder extends FormWidgetBase { + use \Backend\Traits\FormModelWidget; + // // Configurable properties // @@ -107,25 +109,6 @@ class RecordFinder extends FormWidgetBase } } - /** - * Returns the model of a relation type, - * supports nesting via HTML array. - * @return Relation - */ - protected function getRelationModel() - { - list($model, $attribute) = $this->resolveModelAttribute($this->valueFrom); - - if (!$model->hasRelation($attribute)) { - throw new ApplicationException(Lang::get('backend::lang.model.missing_relation', [ - 'class' => get_class($model), - 'relation' => $attribute - ])); - } - - return $model->makeRelation($attribute); - } - /** * {@inheritDoc} */ diff --git a/modules/backend/formwidgets/Relation.php b/modules/backend/formwidgets/Relation.php index e6ffdad51..3075c6e2f 100644 --- a/modules/backend/formwidgets/Relation.php +++ b/modules/backend/formwidgets/Relation.php @@ -16,6 +16,8 @@ use Illuminate\Database\Eloquent\Relations\Relation as RelationBase; */ class Relation extends FormWidgetBase { + use \Backend\Traits\FormModelWidget; + // // Configurable properties // @@ -161,25 +163,4 @@ class Relation extends FormWidgetBase return $value; } - - - /** - * Returns the value as a relation object from the model, - * supports nesting via HTML array. - * @return Relation - */ - protected function getRelationObject() - { - list($model, $attribute) = $this->resolveModelAttribute($this->valueFrom); - - if (!$model->hasRelation($attribute)) { - throw new ApplicationException(Lang::get('backend::lang.model.missing_relation', [ - 'class' => get_class($model), - 'relation' => $attribute - ])); - } - - return $model->{$attribute}(); - } - } diff --git a/modules/backend/traits/FormModelWidget.php b/modules/backend/traits/FormModelWidget.php new file mode 100644 index 000000000..6b8840fe1 --- /dev/null +++ b/modules/backend/traits/FormModelWidget.php @@ -0,0 +1,93 @@ +resolveModelAttribute($this->valueFrom); + * @param string $attribute. + * @return array + */ + public function resolveModelAttribute($attribute) + { + return $this->formField->resolveModelAttribute($this->model, $attribute); + } + + /** + * Returns the model of a relation type, + * supports nesting via HTML array. + * @return Relation + */ + protected function getRelationModel() + { + list($model, $attribute) = $this->resolveModelAttribute($this->valueFrom); + + if (!$model) { + throw new ApplicationException(Lang::get('backend::lang.model.missing_relation', [ + 'class' => get_class($this->model), + 'relation' => $this->valueFrom + ])); + } + + if (!$model->hasRelation($attribute)) { + throw new ApplicationException(Lang::get('backend::lang.model.missing_relation', [ + 'class' => get_class($model), + 'relation' => $attribute + ])); + } + + return $model->makeRelation($attribute); + } + + /** + * Returns the value as a relation object from the model, + * supports nesting via HTML array. + * @return Relation + */ + protected function getRelationObject() + { + list($model, $attribute) = $this->resolveModelAttribute($this->valueFrom); + + if (!$model) { + throw new ApplicationException(Lang::get('backend::lang.model.missing_relation', [ + 'class' => get_class($this->model), + 'relation' => $this->valueFrom + ])); + } + + if (!$model->hasRelation($attribute)) { + throw new ApplicationException(Lang::get('backend::lang.model.missing_relation', [ + 'class' => get_class($model), + 'relation' => $attribute + ])); + } + + return $model->{$attribute}(); + } + + /** + * Returns the value as a relation type from the model, + * supports nesting via HTML array. + * @return Relation + */ + protected function getRelationType() + { + list($model, $attribute) = $this->resolveModelAttribute($this->valueFrom); + return $model->getRelationType($attribute); + } + +}