2014-05-14 23:24:20 +10:00
|
|
|
<?php namespace Backend\Classes;
|
|
|
|
|
2015-03-07 12:48:39 +11:00
|
|
|
use October\Rain\Html\Helper as HtmlHelper;
|
2014-09-06 21:59:40 +10:00
|
|
|
|
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
|
|
|
|
{
|
|
|
|
|
2015-03-06 20:37:05 +11:00
|
|
|
//
|
|
|
|
// Configurable properties
|
|
|
|
//
|
2014-09-17 19:46:49 +10:00
|
|
|
|
|
|
|
/**
|
2016-03-30 18:17:18 +02:00
|
|
|
* @var \October\Rain\Database\Model Form model object.
|
2014-09-17 19:46:49 +10:00
|
|
|
*/
|
2015-03-06 20:37:05 +11:00
|
|
|
public $model;
|
2014-05-14 23:24:20 +10:00
|
|
|
|
|
|
|
/**
|
2015-03-06 20:37:05 +11:00
|
|
|
* @var array Dataset containing field values, if none supplied model should be used.
|
2014-05-14 23:24:20 +10:00
|
|
|
*/
|
2015-03-06 20:37:05 +11: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;
|
|
|
|
|
2015-03-03 20:24:14 +11:00
|
|
|
/**
|
|
|
|
* @var bool Determines if this form field should display comments and labels.
|
|
|
|
*/
|
|
|
|
public $showLabels = true;
|
|
|
|
|
2015-03-06 20:37:05 +11:00
|
|
|
//
|
|
|
|
// Object properties
|
|
|
|
//
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var FormField Object containing general form field information.
|
|
|
|
*/
|
|
|
|
protected $formField;
|
|
|
|
|
2019-01-10 18:59:29 -06:00
|
|
|
/**
|
|
|
|
* @var Backend\Widgets\Form The parent form that contains this field
|
|
|
|
*/
|
|
|
|
protected $parentForm = null;
|
|
|
|
|
2015-03-06 20:37:05 +11:00
|
|
|
/**
|
|
|
|
* @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 $formField FormField Object containing general form field information.
|
|
|
|
* @param $configuration array Configuration the relates to this widget.
|
|
|
|
*/
|
2015-03-06 20:37:05 +11:00
|
|
|
public function __construct($controller, $formField, $configuration = [])
|
2014-05-14 23:24:20 +10:00
|
|
|
{
|
|
|
|
$this->formField = $formField;
|
2014-09-17 19:46:49 +10:00
|
|
|
$this->fieldName = $formField->fieldName;
|
|
|
|
$this->valueFrom = $formField->valueFrom;
|
|
|
|
|
2015-03-06 20:37:05 +11:00
|
|
|
$this->config = $this->makeConfig($configuration);
|
|
|
|
|
|
|
|
$this->fillFromConfig([
|
|
|
|
'model',
|
|
|
|
'data',
|
|
|
|
'sessionKey',
|
|
|
|
'previewMode',
|
2019-01-10 18:59:29 -06:00
|
|
|
'showLabels',
|
|
|
|
'parentForm',
|
2015-03-06 20:37:05 +11:00
|
|
|
]);
|
2014-05-14 23:24:20 +10:00
|
|
|
|
|
|
|
parent::__construct($controller, $configuration);
|
|
|
|
}
|
|
|
|
|
2019-01-10 18:59:29 -06:00
|
|
|
/**
|
|
|
|
* Retrieve the parent form for this formwidget
|
|
|
|
*
|
|
|
|
* @return Backend\Widgets\Form|null
|
|
|
|
*/
|
|
|
|
public function getParentForm()
|
|
|
|
{
|
|
|
|
return $this->parentForm;
|
|
|
|
}
|
|
|
|
|
2016-11-24 08:59:07 +11:00
|
|
|
/**
|
2016-11-28 07:50:06 +11:00
|
|
|
* Returns the HTML element field name for this widget, used for capturing
|
|
|
|
* user input, passed back to the getSaveValue method when saving.
|
2016-11-24 08:59:07 +11:00
|
|
|
* @return string HTML element name
|
|
|
|
*/
|
|
|
|
public function getFieldName()
|
|
|
|
{
|
|
|
|
return $this->formField->getName();
|
|
|
|
}
|
|
|
|
|
2014-05-14 23:24:20 +10:00
|
|
|
/**
|
|
|
|
* 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;
|
2015-03-07 12:48:39 +11:00
|
|
|
return HtmlHelper::nameToId($id);
|
2014-05-14 23:24:20 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-12-09 08:19:53 +11:00
|
|
|
* Process the postback value for this widget. If the value is omitted from
|
|
|
|
* postback data, it will be NULL, otherwise it will be an empty string.
|
2016-01-23 15:37:15 -05:00
|
|
|
* @param mixed $value The existing value for this widget.
|
2014-05-14 23:24:20 +10:00
|
|
|
* @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
|
|
|
{
|
2017-06-06 20:44:15 +10:00
|
|
|
if ($this->formField->value !== null) {
|
|
|
|
return $this->formField->value;
|
|
|
|
}
|
|
|
|
|
2015-10-17 10:16:49 +11:00
|
|
|
$defaultValue = !$this->model->exists
|
|
|
|
? $this->formField->getDefaultFromData($this->data ?: $this->model)
|
|
|
|
: null;
|
2015-10-09 13:51:33 +10:00
|
|
|
|
|
|
|
return $this->formField->getValueFromData($this->data ?: $this->model, $defaultValue);
|
2014-09-17 19:46:49 +10:00
|
|
|
}
|
2014-10-10 23:12:50 +02:00
|
|
|
}
|