From e0b3003d0679e806bd709d9507a285aa02e15936 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Thu, 18 Feb 2016 23:33:44 +0100 Subject: [PATCH] ImportRowModifierForm: implement basic functionality --- application/forms/ImportRowModifierForm.php | 110 ++++++++++++++------ 1 file changed, 78 insertions(+), 32 deletions(-) diff --git a/application/forms/ImportRowModifierForm.php b/application/forms/ImportRowModifierForm.php index a078611d..341dfb6f 100644 --- a/application/forms/ImportRowModifierForm.php +++ b/application/forms/ImportRowModifierForm.php @@ -2,51 +2,97 @@ namespace Icinga\Module\Director\Forms; +use Icinga\Application\Hook; +use Icinga\Module\Director\Objects\ImportSource; use Icinga\Module\Director\Web\Form\DirectorObjectForm; class ImportRowModifierForm extends DirectorObjectForm { + protected $source; + public function setup() { - $this->addElement('text', 'source_name', array( - 'label' => $this->translate('Source name'), + $this->addHidden('source_id', $this->source->id); + $this->addHidden('priority', 1); + + $this->addElement('text', 'property_name', array( + 'label' => $this->translate('Property'), + 'description' => $this->translate('This must be an import source column'), 'required' => true, )); - $this->addElement('text', 'source_field', array( - 'label' => $this->translate('Source field'), - 'description' => $this->translate('This must be a column from the source'), - 'required' => true, + $error = false; + try { + $mods = $this->enumModifiers(); + } catch (Exception $e) { + $error = $e->getMessage(); + $mods = $this->optionalEnum(array()); + } + + $this->addElement('select', 'provider_class', array( + 'label' => $this->translate('Modifier'), + 'required' => true, + 'multiOptions' => $this->optionalEnum($mods), + 'class' => 'autosubmit', )); + if ($error) { + $this->getElement('provider_class')->addError($error); + } - $this->addElement('text', 'destination_field', array( - 'label' => $this->translate('Destination field'), - 'description' => $this->translate('The value of the source will be transformed to the given attribute'), - 'required' => true, - )); + try { + if ($class = $this->getSentValue('provider_class')) { + if ($class && array_key_exists($class, $mods)) { + $this->addSettings($class); + } + } elseif ($class = $this->object()->provider_class) { + $this->addSettings($class); + } - $this->addElement('text', 'priority', array( - 'label' => $this->translate('Priority'), - 'description' => $this->translate('This allows to prioritize the import of a field, synced from different sources for the same object'), - 'required' => true, - )); - - $this->addElement('text', 'filter', array( - 'label' => $this->translate('Filter Expression'), - 'description' => $this->translate('This allows to filter for specific parts within the given source field'), - 'required' => true, - )); - - $this->addElement('select', 'merge', array( - 'label' => $this->translate('Source Type'), - 'required' => true, - 'multiOptions' => array( - 'null' => '- please choose -', - 'merge' => 'merge', - 'override' => 'override' - ) - )); + // TODO: next line looks like obsolete duplicate code to me + $this->addSettings(); + } catch (Exception $e) { + $this->getElement('provider_class')->addError($e->getMessage()); + } + foreach ($this->object()->getSettings() as $key => $val) { + if ($el = $this->getElement($key)) { + $el->setValue($val); + } + } } + protected function enumModifiers() + { + $hooks = Hook::all('Director\\PropertyModifier'); + $enum = array(); + foreach ($hooks as $hook) { + $enum[get_class($hook)] = $hook->getName(); + } + + return $enum; + } + + protected function addSettings($class = null) + { + if ($class === null) { + $class = $this->getValue('provider_class'); + } + + if ($class !== null) { + if (! class_exists($class)) { + throw new ConfigurationError( + 'The hooked class "%s" for this property modifier does no longer exist', + $class + ); + } + + $class::addSettingsFormFields($this); + } + } + + public function setSource(ImportSource $source) + { + $this->source = $source; + return $this; + } }