DirectorDatafieldForm: more robustness...

...to protect ourselves from foreign failures
This commit is contained in:
Thomas Gelf 2015-11-17 17:27:55 +01:00
parent 03ba27d159
commit a185177e14

View File

@ -2,8 +2,10 @@
namespace Icinga\Module\Director\Forms; namespace Icinga\Module\Director\Forms;
use Icinga\Exception\ConfigurationError;
use Icinga\Module\Director\Web\Form\DirectorObjectForm; use Icinga\Module\Director\Web\Form\DirectorObjectForm;
use Icinga\Web\Hook; use Icinga\Web\Hook;
use Exception;
class DirectorDatafieldForm extends DirectorObjectForm class DirectorDatafieldForm extends DirectorObjectForm
{ {
@ -33,24 +35,40 @@ class DirectorDatafieldForm extends DirectorObjectForm
'rows' => '3', 'rows' => '3',
)); ));
$error = false;
try {
$types = $this->enumDataTypes();
} catch (Exception $e) {
$error = $e->getMessage();
$types = $this->optionalEnum(array());
}
$this->addElement('select', 'datatype', array( $this->addElement('select', 'datatype', array(
'label' => $this->translate('Data type'), 'label' => $this->translate('Data type'),
'description' => $this->translate('Field type'), 'description' => $this->translate('Field type'),
'required' => true, 'required' => true,
'multiOptions' => $this->enumDataTypes(), 'multiOptions' => $types,
'class' => 'autosubmit', 'class' => 'autosubmit',
)); ));
if ($error) {
$this->getElement('datatype')->addError($error);
if ($class = $this->getSentValue('datatype')) { }
if ($class && array_key_exists($class, $this->enumDataTypes())) {
$this->addSettings($class); try {
} if ($class = $this->getSentValue('datatype')) {
} elseif ($class = $this->object()->datatype) { if ($class && array_key_exists($class, $types)) {
$this->addSettings($class); $this->addSettings($class);
}
} elseif ($class = $this->object()->datatype) {
$this->addSettings($class);
}
// TODO: next line looks like obsolete duplicate code to me
$this->addSettings();
} catch (Exception $e) {
$this->getElement('datatype')->addError($e->getMessage());
} }
$this->addSettings();
foreach ($this->object()->getSettings() as $key => $val) { foreach ($this->object()->getSettings() as $key => $val) {
if ($el = $this->getElement($key)) { if ($el = $this->getElement($key)) {
$el->setValue($val); $el->setValue($val);
@ -61,10 +79,17 @@ class DirectorDatafieldForm extends DirectorObjectForm
protected function addSettings($class = null) protected function addSettings($class = null)
{ {
if ($class === null) { if ($class === null) {
if ($class = $this->getValue('datatype')) { $class = $this->getValue('datatype');
$class::addSettingsFormFields($this); }
if ($class !== null) {
if (! class_exists($class)) {
throw new ConfigurationError(
'The hooked class "%s" for this data field does no longer exist',
$class
);
} }
} else {
$class::addSettingsFormFields($this); $class::addSettingsFormFields($this);
} }
} }