diff --git a/library/Director/Web/Form/DirectorObjectForm.php b/library/Director/Web/Form/DirectorObjectForm.php
index ef402e40..dfbdb134 100644
--- a/library/Director/Web/Form/DirectorObjectForm.php
+++ b/library/Director/Web/Form/DirectorObjectForm.php
@@ -41,31 +41,6 @@ abstract class DirectorObjectForm extends QuickForm
return;
}
- if ($object->supportsCustomVars()) {
- $this->addElement('note', '_newvar_hint', array('label' => 'New custom variable'));
- $this->addElement('text', '_newvar_name', array(
- 'label' => 'Name'
- ));
- $this->addElement('text', '_newvar_value', array(
- 'label' => 'Value'
- ));
- $this->addElement('select', '_newvar_format', array(
- 'label' => 'Type',
- 'multiOptions' => array('string' => $this->translate('String'))
- ));
- }
-
- if (false && $object->supportsRanges()) {
- /* TODO implement when new logic is there
- $this->addElement('note', '_newrange_hint', array('label' => 'New range'));
- $this->addElement('text', '_newrange_name', array(
- 'label' => 'Name'
- ));
- $this->addElement('text', '_newrange_value', array(
- 'label' => 'Value'
- ));
- */
- }
}
protected function isTemplate()
@@ -73,23 +48,117 @@ abstract class DirectorObjectForm extends QuickForm
return $this->objectType === 'template';
}
- protected function handleIcingaObject(& $values)
+ protected function handleImports($object, & $values)
{
- $object = $this->object();
- $handled = array();
-
- if ($object->supportsGroups()) {
- if (array_key_exists('groups', $values)) {
- $object->groups()->set(
- preg_split('/\s*,\s*/', $values['groups'], -1, PREG_SPLIT_NO_EMPTY)
- );
- $handled['groups'] = true;
- }
+ if (! $object->supportsImports()) {
+ return;
}
- if ($object->supportsCustomVars()) {
+ if (array_key_exists('imports', $values)) {
+ $value = $values['imports'];
+ unset($values['imports']);
+ $object->clearImportedObjects();
+ $object->imports()->set($value);
+ }
+ $el = $this->getElement('imports');
+ if ($el) {
+ $el->setMultiOptions($this->enumAllowedTemplates());
+ $el->setValue($object->imports()->listImportNames());
+ }
+ }
+
+ protected function handleRanges($object, & $values)
+ {
+ if (! $object->supportsRanges()) {
+ return;
+ }
+
+ $key = 'ranges';
+ $object = $this->object();
+
+ /* Sample:
+
+ array(
+ 'monday' => 'eins',
+ 'tuesday' => '00:00-24:00',
+ 'sunday' => 'zwei',
+ );
+
+ */
+ if (array_key_exists($key, $values)) {
+ $object->ranges()->set($values[$key]);
+ unset($values[$key]);
+ }
+
+ foreach ($object->ranges()->getRanges() as $key => $value) {
+ $this->addRange($key, $value);
+ }
+
+ /*
+ // TODO implement when new logic is there
+ $this->addElement('note', '_newrange_hint', array('label' => 'New range'));
+ $this->addElement('text', '_newrange_name', array(
+ 'label' => 'Name'
+ ));
+ $this->addElement('text', '_newrange_value', array(
+ 'label' => 'Value'
+ ));
+ */
+ }
+
+ protected function handleGroups($object, & $values)
+ {
+ if (! $object->supportsGroups()) {
+ return;
+ }
+
+ if (array_key_exists('groups', $values)) {
+ $value = $values['groups'];
+ unset($values['groups']);
+
+ // TODO: Drop this once we have arrays everwhere
+ if (is_string($value)) {
+ $value = preg_split('/\s*,\s*/', $value, -1, PREG_SPLIT_NO_EMPTY);
+ }
+
+ $object->groups()->set($value);
+ }
+ }
+
+ protected function handleProperties($object, & $values)
+ {
+ if ($this->hasBeenSent()) {
+ $object->setProperties($values);
+ }
+
+ $props = $object->getProperties();
+ if (! $object instanceof IcingaObject) {
+ $this->setDefaults($props);
+ return $this;
+ }
+
+ $inherited = $object->getInheritedProperties();
+ $origins = $object->getOriginsProperties();
+
+ foreach ($props as $k => $v) {
+ if (property_exists($inherited, $k)) {
+ $this->setElementValue($k, $v, $inherited->$k, $origins->$k);
+ } else {
+ $this->setElementValue($k, $v);
+ }
+ }
+ }
+
+ protected function handleCustomVars($object, & $values)
+ {
+ if (! $object->supportsCustomVars()) {
+ return;
+ }
+
+ if ($this->hasBeenSent()) {
$vars = array();
+ $handled = array();
$newvar = array(
'type' => 'string',
'name' => null,
@@ -109,39 +178,83 @@ abstract class DirectorObjectForm extends QuickForm
}
foreach ($vars as $k => $v) {
- $this->object->vars()->$k = $v;
+ if ($v === '' || $v === null) {
+ unset($object->vars()->$k);
+ } else {
+ $object->vars()->$k = $v;
+ }
}
if ($newvar['name'] && $newvar['value']) {
- $this->object->vars()->{$newvar['name']} = $newvar['value'];
+ $object->vars()->{$newvar['name']} = $newvar['value'];
+ }
+
+ foreach ($handled as $key) {
+ unset($values[$key]);
}
}
- if ($object->supportsImports()) {
- if (array_key_exists('imports', $values)) {
- $value = $values['imports'];
+ $vars = $object->getVars();
- // TODO: Compat for comma separated string, check if still needed
- if (! is_array($value)) {
- $value = preg_split('/\s*,\s*/', $value, -1, PREG_SPLIT_NO_EMPTY);
+ $fields = $object->getResolvedFields();
+ $inherits = $object->getInheritedVars();
+ $origins = $object->getOriginsVars();
+
+ foreach ($fields as $field) {
+ $varname = $field->varname;
+
+ // Get value from the related varname if set:
+ if (property_exists($vars, $varname)) {
+ $value = $vars->$varname;
+ } else {
+ $value = null;
+ }
+
+ if (property_exists($inherits, $varname)) {
+ $inheritedValue = $inherits->$varname;
+ $inheritFrom = $origins->$varname;
+ if ($inheritFrom === $object->object_name) {
+ $inherited = false;
+ } else {
+ $inherited = true;
}
-
- $object->imports()->set($value);
- $handled['imports'] = true;
- $object->clearImportedObjects();
+ } else {
+ $inheritedValue = null;
+ $inheritFrom = false;
+ $inherited = false;
}
+
+ $this->addField($field, $value, $inheritedValue, $inheritFrom);
}
- if ($object->supportsRanges()) {
- $object->ranges()->set(array(
- 'monday' => 'eins',
- 'tuesday' => '00:00-24:00',
- 'sunday' => 'zwei',
+ // Additional vars
+ foreach ($vars as $key => $value) {
+ // Did we already create a field for this var? Then skip it:
+ if (array_key_exists($key, $fields)) {
+ continue;
+ }
+
+ // Show inheritance information in case we inherited this var:
+ if (isset($inherited->$key)) {
+ $this->addCustomVar($key, $value, $inherited->$key, $origins->$key);
+ } else {
+ $this->addCustomVar($key, $value);
+ }
+
+ }
+
+ if ($object->isTemplate()) {
+ $this->addHtml('
Add a custom variable
');
+ $this->addElement('text', '_newvar_name', array(
+ 'label' => 'Name'
+ ));
+ $this->addElement('text', '_newvar_value', array(
+ 'label' => 'Value'
+ ));
+ $this->addElement('select', '_newvar_format', array(
+ 'label' => 'Type',
+ 'multiOptions' => array('string' => $this->translate('String'))
));
- }
-
- foreach ($handled as $key => $value) {
- unset($values[$key]);
}
}
@@ -151,25 +264,7 @@ abstract class DirectorObjectForm extends QuickForm
return $this;
}
- public function addFields()
- {
- $object = $this->object();
- $fields = $object->getResolvedFields();
- $vars = $object->vars();
-
- foreach ($fields as $field) {
- $varname = $field->varname;
- if (isset($vars->$varname)) {
- $value = $vars->{$varname}->getValue();
- } else {
- $value = null;
- }
-$inherited = null; // Just testing
- $this->addField($field, $value, $inherited);
- }
- }
-
- protected function addField($field, $value = null, $inherited = null)
+ protected function addField($field, $value = null, $inherited = null, $inheritedFrom = null)
{
$datafield = DirectorDatafield::load($field->datafield_id, $this->getDb());
$datatype = new $datafield->datatype;
@@ -181,15 +276,17 @@ $inherited = null; // Just testing
$el->setLabel($datafield->caption);
$el->setDescription($datafield->description);
- if ($field->is_required === 'y') {
+ if ($field->is_required === 'y' && ! $this->isTemplate()) {
$el->setRequired(true);
}
$this->addElement($el);
- $this->setElementValue($name, $value, $inherited);
+ $this->setElementValue($name, $value, $inherited, $inheritedFrom);
+
+ return $el;
}
- protected function setElementValue($name, $value = null, $inherited = null)
+ protected function setElementValue($name, $value = null, $inherited = null, $inheritedFrom = null)
{
$el = $this->getElement($name);
if (! $el) {
@@ -200,44 +297,38 @@ $inherited = null; // Just testing
$el->setValue($value);
}
- if ($inherited === null) {
+ if ($inherited === null || empty($inherited)) {
return;
}
- $strInherited = $this->translate('(inherited)');
if ($el instanceof Zf_Select) {
$multi = $el->getMultiOptions();
if (array_key_exists($inherited, $multi)) {
- $multi[null] = $multi[$inherited] . ' ' . $strInherited;
+ $multi[null] = $multi[$inherited] . sprintf(' (%s)', $inheritedFrom);
} else {
- $multi[null] = $strInherited;
+ $multi[null] = $this->translate('- inherited -');
}
$el->setMultiOptions($multi);
} else {
- $el->setAttrib('placeholder', $inherited . ' ' . $strInherited);
+ $el->setAttrib('placeholder', $inherited . sprintf(' (%s)', $inheritedFrom));
}
}
public function onSuccess()
{
- $object = $this->object;
- $values = $this->getValues();
- if ($object instanceof IcingaObject) {
- $this->handleIcingaObject($values);
- if (! array_key_exists('object_type', $values)) {
- $object->object_type = $this->objectType;
- }
+ $object = $this->object();
+ if ($object->hasBeenModified()) {
+
+ $msg = sprintf(
+ $object->hasBeenLoadedFromDb()
+ ? $this->translate('The Icinga %s has successfully been stored')
+ : $this->translate('A new Icinga %s has successfully been created'),
+ $this->translate($this->getObjectName())
+ );
+ $object->store($this->db);
+ } else {
+ $msg = $this->translate('No action taken, object has not been modified');
}
- $object->setProperties($values);
-
- $msg = sprintf(
- $object->hasBeenLoadedFromDb()
- ? 'The Icinga %s has successfully been stored'
- : 'A new Icinga %s has successfully been created',
- $this->translate($this->getObjectName())
- );
-
- $object->store($this->db);
$this->redirectOnSuccess($msg);
}
@@ -288,71 +379,54 @@ $inherited = null; // Just testing
return $this->objectName;
}
+ protected function onRequest()
+ {
+ $object = $this->object();
+ $values = array();
+ if ($this->hasBeenSent()) {
+ $post = $this->getRequest()->getPost();
+ foreach ($post as $key => $value) {
+ $el = $this->getElement($key);
+ if ($el && ! $el->getIgnore()) {
+ $values[$key] = $value;
+ }
+ }
+ }
+
+ if ($object instanceof IcingaObject) {
+ if (! $object->hasBeenLoadedFromDb()) {
+ $object->object_type = $this->objectType;
+ }
+ $this->handleImports($object, $values);
+ $this->handleCustomVars($object, $values);
+ $this->handleGroups($object, $values);
+ $this->handleRanges($object, $values);
+ }
+
+ $this->handleProperties($object, $values);
+
+ $this->moveSubmitToBottom();
+ }
+
public function loadObject($id)
{
$class = $this->getObjectClassname();
- $object = $this->object = $class::load($id, $this->db);
- if ($object instanceof IcingaObject) {
- $this->objectType = $object->object_type;
- }
+ $this->object = $class::load($id, $this->db);
+ // TODO: hmmmm...
if (! is_array($id)) {
$this->addHidden('id');
}
- $this->prepareElements();
-
- $props = $object->getProperties();
- if (! $object instanceof IcingaObject) {
- $this->setDefaults($props);
- return $this;
- }
-
- $inherited = $this->object->getInheritedProperties();
-
- foreach ($props as $k => $v) {
- $i = property_exists($inherited, $k) ? $inherited->$k : null;
- $this->setElementValue($k, $v, $i);
- }
-
- if ($object->supportsGroups()) {
- $this->getElement('groups')->setValue(
- implode(', ', $object->groups()->listGroupNames())
- );
- }
-
- if ($object->supportsImports()) {
- $el = $this->getElement('imports');
- if ($el) {
- $el->setMultiOptions($this->enumAllowedTemplates());
- $el->setValue($object->imports()->listImportNames());
- }
- }
-
- if ($object->supportsFields() && ! $object->isTemplate()) {
- foreach ($this->object->vars() as $key => $value) {
- $this->addCustomVar($key, $value);
- }
- }
-
- if ($object->supportsRanges()) {
- /* TODO implement when new logic for customvars is there
- foreach ($this->object->ranges()->getRanges() as $key => $value) {
- $this->addRange($key, $value);
- }
- */
- }
-
- $this->moveSubmitToBottom();
return $this;
}
- protected function addCustomVar($key, $range)
+ protected function addCustomVar($key, $value, $inherited = null, $inheritedFrom = null)
{
- $this->addElement('text', 'var_' . $key, array(
- 'label' => 'vars.' . $key,
- 'value' => $range->getValue()
- ));
+ $label = 'vars.' . $key;
+ $key = 'var_' . $key;
+ $this->addElement('text', $key, array('label' => $label));
+ $this->setElementValue($key, $value, $inherited, $inheritedFrom);
}
protected function addRange($key, $range)