columnName -> fieldName + general improvements to FormWidgetBase

This commit is contained in:
Sam Georges 2014-09-17 19:46:49 +10:00
parent 4763c9598a
commit c8f3c640b8
9 changed files with 90 additions and 34 deletions

View File

@ -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;
}

View File

@ -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];
}
}

View File

@ -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();
}
/**

View File

@ -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 = [];

View File

@ -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))

View File

@ -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.
*/

View File

@ -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();

View File

@ -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);

View File

@ -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();
}
/**