diff --git a/CHANGELOG.md b/CHANGELOG.md index 624e03bf0..f04d3a995 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +* **Build 2xx** (2015-03-xx) + - Default context of `manage` and `pivot` forms is now *create* and *update* respectively, instead of the old value *relation*. Use the `context` option to set it back to the old value (see Backend > Relations docs). + * **Build 229** (2015-03-19) - Belongs-to-many model relations now support defining a custom pivot model with the `pivotModel` option (see Database > Model docs). - The config definitions for behavior `RelationController` have been refactored. When using `pivot` mode all columns and fields should now reside in a `pivot[]` array (see Backend > Relations docs). diff --git a/modules/backend/behaviors/RelationController.php b/modules/backend/behaviors/RelationController.php index b09c7e37f..771b8f6ac 100644 --- a/modules/backend/behaviors/RelationController.php +++ b/modules/backend/behaviors/RelationController.php @@ -154,6 +154,11 @@ class RelationController extends ControllerBehavior */ protected $manageId; + /** + * @var int Foeign id of a selected pivot record. + */ + protected $foreignId; + /** * @var string Active session key, used for deferred bindings. */ @@ -305,6 +310,7 @@ class RelationController extends ControllerBehavior $this->viewMode = $this->evalViewMode(); $this->manageMode = $this->evalManageMode(); $this->manageId = post('manage_id'); + $this->foreignId = post('foreign_id'); /* * Toolbar widget @@ -676,23 +682,10 @@ class RelationController extends ControllerBehavior */ elseif ($this->manageMode == 'form') { - /* - * Determine supplied form context - */ - $manageConfig = isset($this->config->manage) ? $this->config->manage : []; - - if ($context = array_get($manageConfig, 'context')) { - if (is_array($context)) { - $context = $this->manageId - ? array_get($context, 'update') - : array_get($context, 'create'); - } - } - $config = $this->makeConfigForMode('manage', 'form'); $config->model = $this->relationModel; $config->arrayName = class_basename($this->relationModel); - $config->context = $context ?: 'relation'; + $config->context = $this->evalFormContext('manage', !!$this->manageId); $config->alias = $this->alias . 'ManageForm'; /* @@ -740,7 +733,7 @@ class RelationController extends ControllerBehavior $config = $this->makeConfigForMode('pivot', 'form'); $config->model = $this->relationModel; $config->arrayName = class_basename($this->relationModel); - $config->context = 'relation'; + $config->context = $this->evalFormContext('pivot', !!$this->manageId); $config->alias = $this->alias . 'ManagePivotForm'; /* @@ -758,6 +751,11 @@ class RelationController extends ControllerBehavior } } else { + if ($this->foreignId && ($foreignModel = $this->relationModel->find($this->foreignId))) { + $foreignModel->exists = false; + $config->model = $foreignModel; + } + $pivotModel = $this->relationObject->newPivot(); $config->model->setRelation('pivot', $pivotModel); } @@ -1069,7 +1067,7 @@ class RelationController extends ControllerBehavior { $this->beforeAjax(); - $this->vars['foreignId'] = post('foreign_id', post('checked')); + $this->vars['foreignId'] = $this->foreignId ?: post('checked'); return $this->relationMakePartial('pivot_form'); } @@ -1077,8 +1075,7 @@ class RelationController extends ControllerBehavior { $this->beforeAjax(); - $foreignIds = (array) post('foreign_id'); - foreach ($foreignIds as $foreignId) { + foreach ((array) $this->foreignId as $foreignId) { /* * Check for existing relation @@ -1241,6 +1238,28 @@ class RelationController extends ControllerBehavior } } + /** + * Determine supplied form context. + */ + protected function evalFormContext($mode = 'manage', $exists = false) + { + $config = isset($this->config->{$mode}) ? $this->config->{$mode} : []; + + if ($context = array_get($config, 'context')) { + if (is_array($context)) { + $context = $exists + ? array_get($context, 'update') + : array_get($context, 'create'); + } + } + + if (!$context) { + $context = $exists ? 'update' : 'create'; + } + + return $context; + } + /** * Returns the configuration for a mode (view, manage, pivot) for an * expected type (list, form). Uses fallback configuration. diff --git a/modules/backend/classes/FormField.php b/modules/backend/classes/FormField.php index 1c198fcb6..33f96ce4f 100644 --- a/modules/backend/classes/FormField.php +++ b/modules/backend/classes/FormField.php @@ -49,6 +49,16 @@ class FormField */ public $valueFrom; + /** + * @var string Specifies a default value for supported fields. + */ + public $defaults; + + /** + * @var string Model attribute to use for the default value. + */ + public $defaultFrom; + /** * @var string Specifies if this field belongs to a tab. */ @@ -114,11 +124,6 @@ class FormField */ public $commentHtml = false; - /** - * @var string Specifies a default value for supported fields. - */ - public $defaults; - /** * @var string Specifies a message to display when there is no value supplied (placeholder). */ @@ -296,6 +301,9 @@ class FormField if (isset($config['default'])) { $this->defaults = $config['default']; } + if (isset($config['defaultFrom'])) { + $this->defaultFrom = $config['defaultFrom']; + } if (isset($config['attributes'])) { $this->attributes($config['attributes']); } @@ -518,7 +526,7 @@ class FormField */ public function getValueFromData($data, $default = null) { - $fieldName = $this->fieldName; + $fieldName = $this->valueFrom ?: $this->fieldName; /* * Array field name, eg: field[key][key2][key3] diff --git a/modules/backend/widgets/Form.php b/modules/backend/widgets/Form.php index 108b2c440..d27da6180 100644 --- a/modules/backend/widgets/Form.php +++ b/modules/backend/widgets/Form.php @@ -775,9 +775,17 @@ class Form extends WidgetBase $field = $this->allFields[$field]; } - $defaultValue = (!$this->model->exists && $field->defaults !== '') - ? $field->defaults - : null; + $defaultValue = null; + + if (!$this->model->exists) { + if ($field->defaultFrom) { + list($model, $attribute) = $field->resolveModelAttribute($this->model, $field->defaultFrom); + $defaultValue = $model->{$attribute}; + } + elseif ($field->defaults !== '') { + $defaultValue = $field->defaults; + } + } return $field->getValueFromData($this->data, $defaultValue); } @@ -863,34 +871,35 @@ class Form extends WidgetBase /* * Handle fields that differ by fieldName and valueFrom + * @todo @deprecated / Not needed? Remove if year >= 2016 */ - $remappedFields = []; - foreach ($this->allFields as $field) { - if ($field->fieldName == $field->valueFrom) { - continue; - } + // $remappedFields = []; + // foreach ($this->allFields as $field) { + // if ($field->fieldName == $field->valueFrom) { + // continue; + // } - /* - * Get the value, remove it from the data collection - */ - $parts = HtmlHelper::nameToArray($field->fieldName); - $dotted = implode('.', $parts); - $value = array_get($data, $dotted); - array_forget($data, $dotted); + // /* + // * Get the value, remove it from the data collection + // */ + // $parts = HtmlHelper::nameToArray($field->fieldName); + // $dotted = implode('.', $parts); + // $value = array_get($data, $dotted); + // array_forget($data, $dotted); - /* - * Set the new value to the data collection - */ - $parts = HtmlHelper::nameToArray($field->valueFrom); - $dotted = implode('.', $parts); - array_set($remappedFields, $dotted, $value); - } + // /* + // * Set the new value to the data collection + // */ + // $parts = HtmlHelper::nameToArray($field->valueFrom); + // $dotted = implode('.', $parts); + // array_set($remappedFields, $dotted, $value); + // } - if (count($remappedFields) > 0) { - $data = array_merge($remappedFields, $data); - // Could be useful one day for field name collisions - // $data['X_OCTOBER_REMAPPED_FIELDS'] = $remappedFields; - } + // if (count($remappedFields) > 0) { + // $data = array_merge($remappedFields, $data); + // // Could be useful one day for field name collisions + // // $data['X_OCTOBER_REMAPPED_FIELDS'] = $remappedFields; + // } return $data; }