diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-14 13:17:31 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-14 13:17:31 +0000 |
commit | f66ab8dae2f3d0418759f81a3a64dc9517a62449 (patch) | |
tree | fbff2135e7013f196b891bbde54618eb050e4aaf /library/Director/PropertyModifier/PropertyModifierMap.php | |
parent | Initial commit. (diff) | |
download | icingaweb2-module-director-f66ab8dae2f3d0418759f81a3a64dc9517a62449.tar.xz icingaweb2-module-director-f66ab8dae2f3d0418759f81a3a64dc9517a62449.zip |
Adding upstream version 1.10.2.upstream/1.10.2
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'library/Director/PropertyModifier/PropertyModifierMap.php')
-rw-r--r-- | library/Director/PropertyModifier/PropertyModifierMap.php | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/library/Director/PropertyModifier/PropertyModifierMap.php b/library/Director/PropertyModifier/PropertyModifierMap.php new file mode 100644 index 0000000..a6cb422 --- /dev/null +++ b/library/Director/PropertyModifier/PropertyModifierMap.php @@ -0,0 +1,97 @@ +<?php + +namespace Icinga\Module\Director\PropertyModifier; + +use Icinga\Exception\InvalidPropertyException; +use Icinga\Module\Director\Hook\PropertyModifierHook; +use Icinga\Module\Director\Web\Form\QuickForm; + +class PropertyModifierMap extends PropertyModifierHook +{ + private $cache; + + public static function addSettingsFormFields(QuickForm $form) + { + $form->addElement('select', 'datalist_id', array( + 'label' => 'Lookup list', + 'required' => true, + 'description' => $form->translate( + 'Please choose a data list that can be used for map lookups' + ), + 'multiOptions' => $form->optionalEnum($form->getDb()->enumDatalist()), + )); + + $form->addElement('select', 'on_missing', array( + 'label' => 'Missing entries', + 'required' => true, + 'description' => $form->translate( + 'What should happen if the lookup key does not exist in the data list?' + . ' You could return a null value, keep the unmodified imported value' + . ' or interrupt the import process' + ), + 'multiOptions' => $form->optionalEnum(array( + 'null' => $form->translate('Set null'), + 'keep' => $form->translate('Return lookup key unmodified'), + 'fail' => $form->translate('Let the import fail'), + )), + )); + + // TODO: ignore case + } + + public function transform($value) + { + $this->loadCache(); + if (array_key_exists($value, $this->cache)) { + return $this->cache[$value]; + } + + switch ($this->getSetting('on_missing')) { + case 'null': + return null; + + case 'keep': + return $value; + + case 'fail': + default: + throw new InvalidPropertyException( + '"%s" cannot be found in the "%s" data list', + $value, + $this->getDatalistName() + ); + } + } + + protected function getDatalistName() + { + $db = $this->getDb()->getDbAdapter(); + $query = $db->select()->from( + 'director_datalist', + 'list_name' + )->where( + 'id = ?', + $this->getSetting('datalist_id') + ); + $result = $db->fetchOne($query); + + return $result; + } + + protected function loadCache($force = false) + { + if ($this->cache === null || $force) { + $this->cache = array(); + $db = $this->getDb()->getDbAdapter(); + $select = $db->select()->from( + 'director_datalist_entry', + array('entry_name', 'entry_value') + )->where('list_id = ?', $this->getSetting('datalist_id')) + ->order('entry_value'); + + $this->cache = $db->fetchPairs($select); + } + + return $this; + } +} |