DirectorDatafield: simplify usage

This commit is contained in:
Thomas Gelf 2016-09-09 21:32:26 +00:00
parent ffe16b472c
commit f52cd5ea77
2 changed files with 41 additions and 45 deletions

View File

@ -3,6 +3,7 @@
namespace Icinga\Module\Director\Objects;
use Icinga\Module\Director\Data\Db\DbObjectWithSettings;
use Icinga\Module\Director\Web\Form\QuickBaseForm;
class DirectorDatafield extends DbObjectWithSettings
{
@ -24,4 +25,35 @@ class DirectorDatafield extends DbObjectWithSettings
protected $settingsTable = 'director_datafield_setting';
protected $settingsRemoteId = 'datafield_id';
public function getFormElement(QuickBaseForm $form, $name = null)
{
$className = $this->datatype;
if ($name === null) {
$name = 'var_' . $this->varname;
}
if (! class_exists($className)) {
$form->addElement('text', $name, array('disabled' => 'disabled'));
$el = $form->getElement($name);
$msg = $form->translate('Form element could not be created, %s is missing');
$el->addError(sprintf($msg, $className));
return $el;
}
$datatype = new $className;
$datatype->setSettings($this->getSettings());
$el = $datatype->getFormElement($name, $form);
if ($this->caption) {
$el->setLabel($this->caption);
}
if ($this->description) {
$el->setDescription($this->description);
}
return $el;
}
}

View File

@ -304,15 +304,7 @@ abstract class DirectorObjectForm extends QuickForm
if (property_exists($fieldSet, $mykey)) {
$field = $fieldSet->$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);
}
$el = $datafield->getFormElement($this);
$value = $el->setValue($value)->getValue();
}
}
@ -524,31 +516,17 @@ abstract class DirectorObjectForm extends QuickForm
protected function addField($field, $value = null, $inherited = null, $inheritedFrom = null)
{
$datafield = DirectorDatafield::load($field->datafield_id, $this->getDb());
$name = 'var_' . $datafield->varname;
$className = $datafield->datatype;
if (! class_exists($className)) {
$this->addElement('text', $name, array('disabled' => 'disabled'));
$el = $this->getElement($name);
$el->addError(sprintf('Form element could not be created, %s is missing', $className));
$this->addToFieldsDisplayGroup($el);
return $el;
}
$datatype = new $className;
$datatype->setSettings($datafield->getSettings());
$el = $datatype->getFormElement($name, $this);
$el->setLabel($datafield->caption);
$el->setDescription($datafield->description);
$el = $datafield->getFormElement($this);
if ($field->is_required === 'y' && ! $this->isTemplate() && $inherited === null) {
$el->setRequired(true);
}
$this->addElement($el);
$this->setElementValue($name, $value, $inherited, $inheritedFrom);
$this->addToFieldsDisplayGroup($el);
if (! $el->hasErrors()) {
$this->setElementValue($el->getName(), $value, $inherited, $inheritedFrom);
}
return $el;
}
@ -556,31 +534,17 @@ abstract class DirectorObjectForm extends QuickForm
protected function addCommandField($field, $value = null, $inherited = null, $inheritedFrom = null)
{
$datafield = DirectorDatafield::load($field->datafield_id, $this->getDb());
$name = 'var_' . $datafield->varname;
$className = $datafield->datatype;
if (! class_exists($className)) {
$this->addElement('text', $name, array('disabled' => 'disabled'));
$el = $this->getElement($name);
$el->addError(sprintf('Form element could not be created, %s is missing', $className));
$this->addToCommandFieldsDisplayGroup($el);
return $el;
}
$datatype = new $className;
$datatype->setSettings($datafield->getSettings());
$el = $datatype->getFormElement($name, $this);
$el->setLabel($datafield->caption);
$el->setDescription($datafield->description);
$el = $datafield->getFormElement($this);
if ($field->is_required === 'y' && ! $this->isTemplate() && $inherited === null) {
$el->setRequired(true);
}
$this->addElement($el);
$this->setElementValue($name, $value, $inherited, $inheritedFrom);
$this->addToCommandFieldsDisplayGroup($el);
if (! $el->hasErrors()) {
$this->setElementValue($name, $value, $inherited, $inheritedFrom);
}
return $el;
}