[icinga-checkins] icinga.org: icingaweb2-module-director/master: ImportSource: use target_property for transforms

git at icinga.org git at icinga.org
Wed Jul 20 15:21:40 CEST 2016


Module: icingaweb2-module-director
Branch: master
Commit: 9a6320c2f4231a6ae0fc146958d41bd812d1e87e
URL:    https://git.icinga.org/?p=icingaweb2-module-director.git;a=commit;h=9a6320c2f4231a6ae0fc146958d41bd812d1e87e

Author: Thomas Gelf <thomas at gelf.net>
Date:   Wed Jul 20 15:21:07 2016 +0200

ImportSource: use target_property for transforms

---

 application/tables/ImportsourceHookTable.php   |    7 ++++++-
 library/Director/Hook/PropertyModifierHook.php |    6 +++++-
 library/Director/Objects/ImportSource.php      |   25 +++++++++++++++++++++---
 3 files changed, 33 insertions(+), 5 deletions(-)

diff --git a/application/tables/ImportsourceHookTable.php b/application/tables/ImportsourceHookTable.php
index 1de1927..17828df 100644
--- a/application/tables/ImportsourceHookTable.php
+++ b/application/tables/ImportsourceHookTable.php
@@ -21,7 +21,12 @@ class ImportsourceHookTable extends QuickTable
     public function getColumns()
     {
         if ($this->columnCache === null) {
-            $this->columnCache = $this->sourceHook()->listColumns();
+            $this->columnCache = array_merge(
+                $this->sourceHook()->listColumns(),
+                $this->source->listModifierTargetProperties()
+            );
+
+            sort($this->columnCache);
         }
 
         return $this->columnCache;
diff --git a/library/Director/Hook/PropertyModifierHook.php b/library/Director/Hook/PropertyModifierHook.php
index 8ad195c..bd5f986 100644
--- a/library/Director/Hook/PropertyModifierHook.php
+++ b/library/Director/Hook/PropertyModifierHook.php
@@ -39,8 +39,12 @@ abstract class PropertyModifierHook
         return $this->targetProperty !== null;
     }
 
-    public function getTargetProperty()
+    public function getTargetProperty($default = null)
     {
+        if ($this->targetProperty === null) {
+            return $default;
+        }
+
         return $this->targetProperty;
     }
 
diff --git a/library/Director/Objects/ImportSource.php b/library/Director/Objects/ImportSource.php
index 226500a..e1bf868 100644
--- a/library/Director/Objects/ImportSource.php
+++ b/library/Director/Objects/ImportSource.php
@@ -99,12 +99,17 @@ class ImportSource extends DbObjectWithSettings
                 if (! property_exists($row, $key)) {
                     continue;
                 }
+
+                $target = $mod->getTargetProperty($key);
+
                 if (is_array($row->$key)) {
-                    foreach ($row->$key as & $k) {
-                        $k = $mod->transform($k);
+                    $new = array();
+                    foreach ($row->$key as $k => $v) {
+                        $new[$k] = $mod->transform($v);
                     }
+                    $row->$target = $new;
                 } else {
-                    $row->$key = $mod->transform($row->$key);
+                    $row->$target = $mod->transform($row->$key);
                 }
             }
         }
@@ -153,6 +158,20 @@ class ImportSource extends DbObjectWithSettings
         $this->rowModifiers = $modifiers;
     }
 
+    public function listModifierTargetProperties()
+    {
+        $list = array();
+        foreach ($this->getRowModifiers() as $rowMods) {
+            foreach ($rowMods as $mod) {
+                if ($mod->hasTargetProperty()) {
+                    $list[$mod->getTargetProperty()] = true;
+                }
+            }
+        }
+
+        return array_keys($list);
+    }
+
     public function checkForChanges($runImport = false)
     {
         $hadChanges = false;



More information about the icinga-checkins mailing list