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
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var FormField Object containing general form field information.
|
|
|
|
*/
|
|
|
|
public $formField;
|
|
|
|
|
|
|
|
/**
|
2014-09-17 19:46:49 +10:00
|
|
|
* @var string Form field name.
|
2014-05-14 23:24:20 +10:00
|
|
|
*/
|
2014-09-17 19:46:49 +10:00
|
|
|
public $fieldName;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string Model attribute to get/set value from.
|
|
|
|
*/
|
|
|
|
public $valueFrom;
|
2014-05-14 23:24:20 +10:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var mixed Model object.
|
|
|
|
*/
|
|
|
|
public $model;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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, $model, $formField, $configuration = [])
|
|
|
|
{
|
|
|
|
$this->formField = $formField;
|
2014-09-17 19:46:49 +10:00
|
|
|
$this->fieldName = $formField->fieldName;
|
|
|
|
$this->valueFrom = $formField->valueFrom;
|
2014-05-14 23:24:20 +10:00
|
|
|
$this->model = $model;
|
2014-09-17 19:46:49 +10:00
|
|
|
|
2014-10-10 23:12:50 +02:00
|
|
|
if (isset($configuration->sessionKey)) {
|
|
|
|
$this->sessionKey = $configuration->sessionKey;
|
|
|
|
}
|
|
|
|
if (isset($configuration->previewMode)) {
|
|
|
|
$this->previewMode = $configuration->previewMode;
|
|
|
|
}
|
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);
|
2014-09-17 19:46:49 +10:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2014-09-17 19:46:49 +10:00
|
|
|
/**
|
|
|
|
* 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()
|
2014-09-17 19:46:49 +10:00
|
|
|
{
|
2015-01-05 12:54:14 +11:00
|
|
|
return $this->formField->getValueFromData($this->model);
|
2014-09-17 19:46:49 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the final model and attribute name of
|
|
|
|
* a nested HTML array attribute.
|
2015-01-05 11:18:43 +11:00
|
|
|
* Eg: list($model, $attribute) = $this->resolveModelAttribute($this->valueFrom);
|
2014-09-17 19:46:49 +10:00
|
|
|
* @param string $attribute.
|
|
|
|
* @return array
|
|
|
|
*/
|
2015-01-05 11:18:43 +11:00
|
|
|
public function resolveModelAttribute($attribute)
|
2014-09-17 19:46:49 +10:00
|
|
|
{
|
|
|
|
$model = $this->model;
|
|
|
|
$parts = Str::evalHtmlArray($attribute);
|
|
|
|
$last = array_pop($parts);
|
|
|
|
|
|
|
|
foreach ($parts as $part) {
|
|
|
|
$model = $model->{$part};
|
|
|
|
}
|
|
|
|
|
|
|
|
return [$model, $last];
|
|
|
|
}
|
2015-01-05 09:43:39 +11:00
|
|
|
|
2014-10-10 23:12:50 +02:00
|
|
|
}
|