From 04f16008fe0b3cbadd4d99033e0bfdfcf86ff767 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Tue, 8 Mar 2016 22:30:45 +0100 Subject: [PATCH] PropertyModifierMap: new list lookup modifier --- .../PropertyModifier/PropertyModifierMap.php | 88 +++++++++++++++++++ run.php | 1 + 2 files changed, 89 insertions(+) create mode 100644 library/Director/PropertyModifier/PropertyModifierMap.php diff --git a/library/Director/PropertyModifier/PropertyModifierMap.php b/library/Director/PropertyModifier/PropertyModifierMap.php new file mode 100644 index 00000000..9d3c43ba --- /dev/null +++ b/library/Director/PropertyModifier/PropertyModifierMap.php @@ -0,0 +1,88 @@ +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() + { + // TODO: need the db for ->enumDatalist() + return sprintf('List with id %s', $this->getSetting('datalist')); + } + + 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; + } +} diff --git a/run.php b/run.php index 8d188cb3..1cac4672 100644 --- a/run.php +++ b/run.php @@ -22,4 +22,5 @@ $this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\Pro $this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\PropertyModifierStripDomain'); $this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\PropertyModifierSubstring'); $this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\PropertyModifierUppercase'); +$this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\PropertyModifierMap'); $this->provideHook('director/PropertyModifier', $prefix . 'PropertyModifier\\PropertyModifierGetHostByName');