diff --git a/modules/backend/classes/FormField.php b/modules/backend/classes/FormField.php index 26f1aa548..68ae63e67 100644 --- a/modules/backend/classes/FormField.php +++ b/modules/backend/classes/FormField.php @@ -244,6 +244,10 @@ class FormField if (array_key_exists('required', $config)) $this->required = $config['required']; if (array_key_exists('disabled', $config)) $this->disabled = $config['disabled']; if (array_key_exists('stretch', $config)) $this->stretch = $config['stretch']; + + if (isset($config['valueFrom'])) $this->valueFrom = $config['valueFrom']; + else $this->valueFrom = $this->fieldName; + return $config; } diff --git a/modules/backend/classes/FormWidgetBase.php b/modules/backend/classes/FormWidgetBase.php index 92eecfcb0..5d430958b 100644 --- a/modules/backend/classes/FormWidgetBase.php +++ b/modules/backend/classes/FormWidgetBase.php @@ -18,9 +18,14 @@ abstract class FormWidgetBase extends WidgetBase public $formField; /** - * @var string Form field column name. + * @var string Form field name. */ - public $columnName; + public $fieldName; + + /** + * @var string Model attribute to get/set value from. + */ + public $valueFrom; /** * @var mixed Model object. @@ -52,8 +57,12 @@ abstract class FormWidgetBase extends WidgetBase public function __construct($controller, $model, $formField, $configuration = []) { $this->formField = $formField; - $this->columnName = $formField->columnName; + $this->fieldName = $formField->fieldName; + $this->valueFrom = $formField->valueFrom; $this->model = $model; + + /* @todo Remove line if year >= 2015 */ $this->columnName = $formField->valueFrom; + if (isset($configuration->sessionKey)) $this->sessionKey = $configuration->sessionKey; if (isset($configuration->previewMode)) $this->previewMode = $configuration->previewMode; @@ -72,7 +81,7 @@ abstract class FormWidgetBase extends WidgetBase public function getId($suffix = null) { $id = parent::getId($suffix); - $id .= '-' . $this->columnName; + $id .= '-' . $this->fieldName; return Str::evalHtmlId($id); } @@ -86,4 +95,35 @@ abstract class FormWidgetBase extends WidgetBase return $value; } + /** + * Returns the value for this form field, + * supports nesting via HTML array. + * @return string + */ + public function getLoadData() + { + list($model, $attribute) = $this->getModelArrayAttribute($this->valueFrom); + return $model->{$attribute}; + } + + /** + * Returns the final model and attribute name of + * a nested HTML array attribute. + * Eg: list($model, $attribute) = $this->getModelArrayAttribute($this->valueFrom); + * @param string $attribute. + * @return array + */ + public function getModelArrayAttribute($attribute) + { + $model = $this->model; + $parts = Str::evalHtmlArray($attribute); + $last = array_pop($parts); + + foreach ($parts as $part) { + $model = $model->{$part}; + } + + return [$model, $last]; + } + } \ No newline at end of file diff --git a/modules/backend/formwidgets/CodeEditor.php b/modules/backend/formwidgets/CodeEditor.php index a58ac875e..89419b9cf 100644 --- a/modules/backend/formwidgets/CodeEditor.php +++ b/modules/backend/formwidgets/CodeEditor.php @@ -106,7 +106,7 @@ class CodeEditor extends FormWidgetBase $this->vars['stretch'] = $this->formField->stretch; $this->vars['size'] = $this->formField->size; $this->vars['name'] = $this->formField->getName(); - $this->vars['value'] = $this->model->{$this->columnName}; + $this->vars['value'] = $this->getLoadData(); } /** diff --git a/modules/backend/formwidgets/DataGrid.php b/modules/backend/formwidgets/DataGrid.php index 42b6331bd..2dfdeda5d 100644 --- a/modules/backend/formwidgets/DataGrid.php +++ b/modules/backend/formwidgets/DataGrid.php @@ -97,7 +97,7 @@ class DataGrid extends FormWidgetBase */ public function getAutocompleteValues($field, $value, $data) { - $methodName = 'get'.studly_case($this->columnName).'AutocompleteValues'; + $methodName = 'get'.studly_case($this->fieldName).'AutocompleteValues'; if (!$this->model->methodExists($methodName) && !$this->model->methodExists('getGridAutocompleteValues')) throw new ApplicationException('Model :model does not contain a method getGridAutocompleteValues()'); @@ -105,7 +105,7 @@ class DataGrid extends FormWidgetBase if ($this->model->methodExists($methodName)) $result = $this->model->$methodName($field, $value, $data); else - $result = $this->model->getGridAutocompleteValues($this->columnName, $field, $value, $data); + $result = $this->model->getGridAutocompleteValues($this->fieldName, $field, $value, $data); if (!is_array($result)) $result = []; @@ -120,7 +120,7 @@ class DataGrid extends FormWidgetBase */ public function getDataSourceValues() { - $methodName = 'get'.studly_case($this->columnName).'DataSourceValues'; + $methodName = 'get'.studly_case($this->fieldName).'DataSourceValues'; if (!$this->model->methodExists($methodName) && !$this->model->methodExists('getGridDataSourceValues')) throw new ApplicationException('Model :model does not contain a method getGridDataSourceValues()'); @@ -128,7 +128,7 @@ class DataGrid extends FormWidgetBase if ($this->model->methodExists($methodName)) $result = $this->model->$methodName(); else - $result = $this->model->getGridDataSourceValues($this->columnName); + $result = $this->model->getGridDataSourceValues($this->fieldName); if (!is_array($result)) $result = []; diff --git a/modules/backend/formwidgets/Datepicker.php b/modules/backend/formwidgets/Datepicker.php index 730dfc384..b443542cf 100644 --- a/modules/backend/formwidgets/Datepicker.php +++ b/modules/backend/formwidgets/Datepicker.php @@ -57,7 +57,7 @@ class Datepicker extends FormWidgetBase { $this->vars['name'] = $this->formField->getName(); - $value = $this->model->{$this->columnName}; + $value = $this->getLoadData(); if ($this->mode != 'datetime' && $value) { if (is_string($value)) diff --git a/modules/backend/formwidgets/FileUpload.php b/modules/backend/formwidgets/FileUpload.php index 5b9e4666a..975d5f212 100644 --- a/modules/backend/formwidgets/FileUpload.php +++ b/modules/backend/formwidgets/FileUpload.php @@ -65,24 +65,6 @@ class FileUpload extends FormWidgetBase $this->vars['imageWidth'] = $this->imageWidth; } - /** - * Returns the attachment relation object from the model, - * supports nesting via HTML array. - * @return Relation - */ - protected function getRelationObject() - { - $model = $this->model; - $relations = Str::evalHtmlArray($this->columnName); - $lastField = array_pop($relations); - - foreach ($relations as $relation) { - $model = $model->{$relation}; - } - - return $model->$lastField(); - } - protected function getFileList() { $list = $this->getRelationObject()->withDeferred($this->sessionKey)->orderBy('sort_order')->get(); @@ -97,6 +79,10 @@ class FileUpload extends FormWidgetBase return $list; } + /** + * Returns the display mode for the file upload. Eg: file-multi, image-single, etc. + * @return string + */ protected function getDisplayMode() { $mode = $this->getConfig('mode', 'image'); @@ -104,12 +90,34 @@ class FileUpload extends FormWidgetBase if (str_contains($mode, '-')) return $mode; - $relationType = $this->model->getRelationType($this->columnName); + $relationType = $this->getRelationType(); $mode .= ($relationType == 'attachMany' || $relationType == 'morphMany') ? '-multi' : '-single'; return $mode; } + /** + * Returns the value as a relation object from the model, + * supports nesting via HTML array. + * @return Relation + */ + protected function getRelationObject() + { + list($model, $attribute) = $this->getModelArrayAttribute($this->valueFrom); + 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->getModelArrayAttribute($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 7bc8bf0e8..72b62ea64 100644 --- a/modules/backend/formwidgets/RecordFinder.php +++ b/modules/backend/formwidgets/RecordFinder.php @@ -76,7 +76,7 @@ class RecordFinder extends FormWidgetBase */ public function init() { - $this->relationName = $this->formField->columnName; + $this->relationName = $this->formField->valueFrom; $this->relationType = $this->model->getRelationType($this->relationName); $this->prompt = $this->getConfig('prompt', 'Click the %s button to find a record'); @@ -120,7 +120,9 @@ class RecordFinder extends FormWidgetBase public function onRefresh() { - $this->model->{$this->columnName} = post($this->formField->getName()); + list($model, $attribute) = $this->getModelArrayAttribute($this->valueFrom); + $model->{$attribute} = post($this->formField->getName()); + $this->prepareVars(); return ['#'.$this->getId('container') => $this->makePartial('recordfinder')]; } @@ -130,7 +132,9 @@ class RecordFinder extends FormWidgetBase */ public function prepareVars() { - $this->relationModel = $this->model->{$this->columnName}; + // This should be a relation and return a Model + $this->relationModel = $this->getLoadData(); + $this->vars['value'] = $this->getKeyValue(); $this->vars['field'] = $this->formField; $this->vars['nameValue'] = $this->getNameValue(); diff --git a/modules/backend/formwidgets/Relation.php b/modules/backend/formwidgets/Relation.php index 5335ea70f..debdaf3dc 100644 --- a/modules/backend/formwidgets/Relation.php +++ b/modules/backend/formwidgets/Relation.php @@ -53,7 +53,7 @@ class Relation extends FormWidgetBase */ public function init() { - $this->relationName = $this->formField->columnName; + $this->relationName = $this->valueFrom; $this->relationType = $this->model->getRelationType($this->relationName); $this->nameFrom = $this->getConfig('nameFrom', $this->nameFrom); diff --git a/modules/backend/formwidgets/RichEditor.php b/modules/backend/formwidgets/RichEditor.php index 154838774..87e8e4f64 100644 --- a/modules/backend/formwidgets/RichEditor.php +++ b/modules/backend/formwidgets/RichEditor.php @@ -41,7 +41,7 @@ class RichEditor extends FormWidgetBase $this->vars['stretch'] = $this->formField->stretch; $this->vars['size'] = $this->formField->size; $this->vars['name'] = $this->formField->getName(); - $this->vars['value'] = $this->model->{$this->columnName}; + $this->vars['value'] = $this->getLoadData(); } /**