DataTypeBoolean: provide a new data type

fixes #11594
This commit is contained in:
Thomas Gelf 2016-05-19 14:59:25 +02:00
parent da5ee55abf
commit 41731241c5
4 changed files with 112 additions and 0 deletions

View File

@ -0,0 +1,29 @@
<?php
namespace Icinga\Module\Director\DataType;
use Icinga\Module\Director\Hook\DataTypeHook;
use Icinga\Module\Director\Web\Form\Decorator\ViewHelperRaw;
use Icinga\Module\Director\Web\Form\QuickForm;
class DataTypeBoolean extends DataTypeHook
{
public function getFormElement($name, QuickForm $form)
{
return $this->applyRawViewHelper(
$form->createElement('boolean', $name)
);
}
protected function applyRawViewHelper($element)
{
$vhClass = 'Zend_Form_Decorator_ViewHelper';
$decorators = $element->getDecorators();
if (array_key_exists($vhClass, $decorators)) {
$decorators[$vhClass] = new ViewHelperRaw;
$element->setDecorators($decorators);
}
return $element;
}
}

View File

@ -298,6 +298,24 @@ abstract class DirectorObjectForm extends QuickForm
}
$mykey = substr($key, 4);
// Get value through form element.
// TODO: reorder the related code. Create elements once
if (property_exists($fields, $mykey)) {
$field = $fields->$mykey;
$datafield = DirectorDatafield::load($field->datafield_id, $this->getDb());
$name = 'var_' . $datafield->varname;
$className = $datafield->datatype;
if (class_exists($className)) {
$datatype = new $className;
$datatype->setSettings($datafield->getSettings());
$el = $datatype->getFormElement($name, $this);
}
$value = $el->setValue($value)->getValue();
}
if (property_exists($fields, $mykey) && $fields->$mykey->format === 'json') {
$value = json_decode($value);
}

View File

@ -0,0 +1,64 @@
<?php
namespace Icinga\Module\Director\Web\Form\Element;
use Zend_Form_Element_Select as ZfSelect;
/**
* Input control for booleans
*/
class Boolean extends ZfSelect
{
public $options = array(
null => '- please choose -',
'y' => 'Yes',
'n' => 'No',
);
public function getValue()
{
$value = $this->getUnfilteredValue();
if ($value === 'y' || $value === true) {
return true;
} elseif ($value === 'n' || $value === false) {
return false;
}
return null;
}
public function isValid($value, $context = null)
{
return $value === 'y'
|| $value === 'n'
|| $value === null
|| $value === true
|| $value === false;
}
public function setValue($value)
{
if ($value === true) {
$value = 'y';
} elseif ($value === false) {
$value = 'n';
}
return parent::setValue($value);
}
protected function _translateOption($option, $value)
{
if (!isset($this->_translated[$option]) && !empty($value)) {
$this->options[$option] = mt('director', $value);
if ($this->options[$option] === $value) {
return false;
}
$this->_translated[$option] = true;
return true;
}
return false;
}
}

View File

@ -15,6 +15,7 @@ $this->provideHook('director/DataType', $prefix . 'DataType\\DataTypeString');
$this->provideHook('director/DataType', $prefix . 'DataType\\DataTypeArray');
$this->provideHook('director/DataType', $prefix . 'DataType\\DataTypeNumber');
$this->provideHook('director/DataType', $prefix . 'DataType\\DataTypeTime');
$this->provideHook('director/DataType', $prefix . 'DataType\\DataTypeBoolean');
$this->provideHook('director/DataType', $prefix . 'DataType\\DataTypeDatalist');
$this->provideHook('director/DataType', $prefix . 'DataType\\DataTypeSqlQuery');