winter/modules/backend/classes/FormWidgetBase.php

133 lines
3.1 KiB
PHP
Raw Normal View History

2014-05-14 23:24:20 +10:00
<?php namespace Backend\Classes;
2014-09-06 21:59:40 +10:00
use Str;
2014-05-14 23:24:20 +10:00
/**
* Form Widget base class
* Widgets used specifically for forms
*
* @package october\backend
* @author Alexey Bobkov, Samuel Georges
*/
abstract class FormWidgetBase extends WidgetBase
{
//
// Configurable properties
//
/**
* @var Model Form model object.
*/
public $model;
2014-05-14 23:24:20 +10:00
/**
* @var array Dataset containing field values, if none supplied model should be used.
2014-05-14 23:24:20 +10:00
*/
public $data;
2014-05-14 23:24:20 +10:00
/**
* @var string Active session key, used for editing forms and deferred bindings.
*/
public $sessionKey;
/**
* @var bool Render this form with uneditable preview data.
*/
public $previewMode = false;
/**
* @var bool Determines if this form field should display comments and labels.
*/
public $showLabels = true;
//
// Object properties
//
/**
* @var FormField Object containing general form field information.
*/
protected $formField;
/**
* @var string Form field name.
*/
protected $fieldName;
/**
* @var string Model attribute to get/set value from.
*/
protected $valueFrom;
2014-05-14 23:24:20 +10:00
/**
* Constructor
* @param $controller Controller Active controller object.
* @param $model Model The relevant model to reference.
* @param $formField FormField Object containing general form field information.
* @param $configuration array Configuration the relates to this widget.
*/
public function __construct($controller, $formField, $configuration = [])
2014-05-14 23:24:20 +10:00
{
$this->formField = $formField;
$this->fieldName = $formField->fieldName;
$this->valueFrom = $formField->valueFrom;
$this->config = $this->makeConfig($configuration);
$this->fillFromConfig([
'model',
'data',
'sessionKey',
'previewMode',
'showLabels'
]);
2014-05-14 23:24:20 +10:00
parent::__construct($controller, $configuration);
}
/**
* Returns a unique ID for this widget. Useful in creating HTML markup.
*/
public function getId($suffix = null)
{
$id = parent::getId($suffix);
$id .= '-' . $this->fieldName;
2014-09-06 21:58:45 +10:00
return Str::evalHtmlId($id);
2014-05-14 23:24:20 +10:00
}
/**
2015-01-05 09:43:39 +11:00
* Process the postback value for this widget.
2014-05-14 23:24:20 +10:00
* @param $value The existing value for this widget.
* @return string The new value for this widget.
*/
2015-01-05 09:43:39 +11:00
public function getSaveValue($value)
2014-05-14 23:24:20 +10:00
{
return $value;
}
/**
* Returns the value for this form field,
* supports nesting via HTML array.
* @return string
*/
2015-01-05 09:45:04 +11:00
public function getLoadValue()
{
return $this->formField->getValueFromData($this->data ?: $this->model);
}
/**
* 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)
{
$parts = Str::evalHtmlArray($attribute);
2015-03-07 12:28:50 +11:00
return $this->model->resolveAttribute($parts);
}
2015-01-05 09:43:39 +11:00
2014-10-10 23:12:50 +02:00
}