[icinga-checkins] icinga.org: icingaweb2-module-director/feature/nested-apply-rules-12033: DataFilter: automagic fix for not(multiple)

git at icinga.org git at icinga.org
Fri Oct 21 20:23:39 CEST 2016


Module: icingaweb2-module-director
Branch: feature/nested-apply-rules-12033
Commit: 96c6d74062ae55a1e2c89e36f4ec8e20848d77d4
URL:    https://git.icinga.org/?p=icingaweb2-module-director.git;a=commit;h=96c6d74062ae55a1e2c89e36f4ec8e20848d77d4

Author: Thomas Gelf <thomas at gelf.net>
Date:   Fri Oct 21 18:18:00 2016 +0000

DataFilter: automagic fix for not(multiple)

---

 library/Director/Web/Form/Element/DataFilter.php |   97 ++++++++++++++++------
 1 file changed, 71 insertions(+), 26 deletions(-)

diff --git a/library/Director/Web/Form/Element/DataFilter.php b/library/Director/Web/Form/Element/DataFilter.php
index 81de2e8..2c45d4b 100644
--- a/library/Director/Web/Form/Element/DataFilter.php
+++ b/library/Director/Web/Form/Element/DataFilter.php
@@ -17,11 +17,13 @@ class DataFilter extends FormElement
      */
     public $helper = 'formDataFilter';
 
-    protected $addTo;
+    private $addTo;
 
-    protected $removeFilter;
+    private $removeFilter;
 
-    protected $stripFilter;
+    private $stripFilter;
+
+    private $filter;
 
     /**
      * @codingStandardsIgnoreStart
@@ -58,70 +60,113 @@ class DataFilter extends FormElement
             return Filter::matchAll();
         }
 
-        $filter = null;
+        $this->filter = null;
         foreach ($array as $id => $entry) {
             $filterId = $this->idToFilterId($id);
             $sub = $this->entryToFilter($entry);
             $this->checkEntryForActions($filterId, $entry);
             $parentId = $this->parentIdFor($filterId);
 
-            if ($filter === null) {
-                $filter = $sub;
+            if ($this->filter === null) {
+                $this->filter = $sub;
             } else {
-                $filter->getById($parentId)->addFilter($sub);
+                $this->filter->getById($parentId)->addFilter($sub);
             }
         }
 
+        $this->removeFilterIfRequested()
+             ->stripFilterIfRequested()
+             ->addNewFilterIfRequested()
+             ->fixNotsWithMultipleChildren();
+
+        return $this->filter;
+    }
+
+    protected function removeFilterIfRequested()
+    {
         if ($this->removeFilter !== null) {
-            if ($filter->getById($this->removeFilter)->isRootNode()) {
-                $filter = $this->emptyExpression();
+            if ($this->filter->getById($this->removeFilter)->isRootNode()) {
+                $this->filter = $this->emptyExpression();
             } else {
-                $filter->removeId($this->removeFilter);
+                $this->filter->removeId($this->removeFilter);
             }
         }
 
+        return $this;
+    }
+
+
+    protected function stripFilterIfRequested()
+    {
         if ($this->stripFilter !== null) {
             $strip = $this->stripFilter;
             $subId = $strip . '-1';
-            if ($filter->getId() === $strip) {
-                $filter = $filter->getById($strip . '-1');
+            if ($this->filter->getId() === $strip) {
+                $this->filter = $this->filter->getById($strip . '-1');
             } else {
-                $filter->replaceById($strip, $filter->getById($strip . '-1'));
+                $this->filter->replaceById($strip, $this->filter->getById($strip . '-1'));
             }
         }
 
+        return $this;
+    }
+
+    protected function addNewFilterIfRequested()
+    {
         if ($this->addTo !== null) {
-            $parent = $filter->getById($this->addTo);
+            $parent = $this->filter->getById($this->addTo);
 
             if ($parent->isChain()) {
                 if ($parent->isEmpty()) {
                     $parent->addFilter($this->emptyExpression());
-                } elseif ($parent->getOperatorName() === 'NOT') {
-                    $andNot = Filter::matchAll();
-                    foreach ($parent->filters() as $sub) {
-                        $andNot->addFilter(clone($sub));
-                    }
-                    $clone->addFilter($this->emptyExpression());
-                    $filter->replaceById(
-                        $parent->getId(),
-                        Filter::not($andNot)
-                    );
                 } else {
                     $parent->addFilter($this->emptyExpression());
                 }
             } else {
                 $replacement = Filter::matchAll(clone($parent));
                 if ($parent->isRootNode()) {
-                    $filter = $replacement;
+                    $this->filter = $replacement;
                 } else {
-                    $filter->replaceById($parent->getId(), $replacement);
+                    $this->filter->replaceById($parent->getId(), $replacement);
+                }
+            }
+        }
+
+        return $this;
+    }
+
+    protected function fixNotsWithMultipleChildren(Filter $filter = null)
+    {
+        $this->filter = $this->fixNotsWithMultipleChildrenForFilter($this->filter);
+        return $this;
+    }
+
+    protected function fixNotsWithMultipleChildrenForFilter(Filter $filter)
+    {
+        if ($filter->isChain()) {
+            if ($filter->getOperatorName() === 'NOT') {
+                if ($filter->count() > 1) {
+                    $filter = $this->notToNotAnd($filter);
                 }
             }
+            foreach ($filter->filters() as $sub) {
+                $filter->replaceById($sub->getId(), $this->fixNotsWithMultipleChildrenForFilter($sub));
+            }
         }
 
         return $filter;
     }
 
+    protected function notToNotAnd(Filter $not)
+    {
+        $and = Filter::matchAll();
+        foreach ($not->filters() as $sub) {
+            $and->addFilter(clone($sub));
+        }
+
+        return Filter::not($and);
+    }
+
     protected function emptyExpression()
     {
         return Filter::expression('', '=', '');



More information about the icinga-checkins mailing list