[icinga-checkins] icinga.org: icingaweb2-module-director/fiddle/mfrosch: WIP: Assignments: Render fallback query_string form for complex filters

git at icinga.org git at icinga.org
Fri Jul 22 13:55:48 CEST 2016


Module: icingaweb2-module-director
Branch: fiddle/mfrosch
Commit: 802171abe998b3bd9ba858cacde3d5972ed6c262
URL:    https://git.icinga.org/?p=icingaweb2-module-director.git;a=commit;h=802171abe998b3bd9ba858cacde3d5972ed6c262

Author: Markus Frosch <lazyfrosch at icinga.org>
Date:   Fri Jul 15 09:58:32 2016 +0200

WIP: Assignments: Render fallback query_string form for complex filters

---

 application/forms/AssignListSubForm.php            |    4 +-
 application/forms/AssignmentSubForm.php            |   65 ++++++++++++--------
 .../Director/Objects/IcingaObjectAssignments.php   |   57 +++++++++--------
 3 files changed, 74 insertions(+), 52 deletions(-)

diff --git a/application/forms/AssignListSubForm.php b/application/forms/AssignListSubForm.php
index 0cc9970..f618be1 100644
--- a/application/forms/AssignListSubForm.php
+++ b/application/forms/AssignListSubForm.php
@@ -7,7 +7,7 @@ use Icinga\Module\Director\Web\Form\QuickSubForm;
 
 class AssignListSubForm extends QuickSubForm
 {
-
+    /** @var IcingaObject */
     protected $object;
 
     public function setObject($object)
@@ -31,7 +31,7 @@ class AssignListSubForm extends QuickSubForm
                 $idx++;
                 $sub = new AssignmentSubForm();
                 $sub->setObject($this->object);
-                $sub->setup();
+                $sub->setup(array_key_exists('query_string', $values));
                 $sub->populate($values);
                 $this->addSubForm($sub, $idx);
             }
diff --git a/application/forms/AssignmentSubForm.php b/application/forms/AssignmentSubForm.php
index c719d49..d5cd5f0 100644
--- a/application/forms/AssignmentSubForm.php
+++ b/application/forms/AssignmentSubForm.php
@@ -8,13 +8,14 @@ use Icinga\Module\Director\Web\Form\QuickSubForm;
 
 class AssignmentSubForm extends QuickSubForm
 {
+    /** @var IcingaObject */
     protected $object;
 
     // @codingStandardsIgnoreStart
     protected $_disableLoadDefaultDecorators = true;
     // @codingStandardsIgnoreEnd
 
-    public function setup()
+    public function setup($fallback=false)
     {
         $this->addElement('select', 'assign_type', array(
             'multiOptions' => array(
@@ -24,32 +25,43 @@ class AssignmentSubForm extends QuickSubForm
             'class' => 'assign-type',
             'value' => 'assign'
         ));
-        $this->addElement('select', 'property', array(
-            'label' => $this->translate('Property'),
-            'class' => 'assign-property autosubmit',
-            'multiOptions' => $this->optionalEnum(IcingaHost::enumProperties($this->object->getConnection(), 'host.'))
-        ));
-        $this->addElement('select', 'operator', array(
-            'label' => $this->translate('Operator'),
-            'multiOptions' => array(
-                '='  => '=',
-                '!=' => '!=',
-                '>'  => '>',
-                '>=' => '>=',
-                '<=' => '<=',
-                '<'  => '<',
-            ),
-            'required' => $this->valueIsEmpty($this->getValue('property')),
-            'value' => '=',
-            'class' => 'assign-operator',
-        ));
 
-        $this->addElement('text', 'expression', array(
-            'label'       => $this->translate('Expression'),
-            'placeholder' => $this->translate('Expression'),
-            'class'       => 'assign-expression',
-            'required'    => !$this->valueIsEmpty($this->getValue('property'))
-        ));
+        if ($fallback === true) {
+            $this->addElement('text', 'query_string', array(
+                'label'       => $this->translate('Query String'),
+                'placeholder' => $this->translate('Query String'),
+                'class'       => 'assign-querystring',
+            ));
+        }
+        else {
+            $this->addElement('select', 'property', array(
+                'label' => $this->translate('Property'),
+                'class' => 'assign-property autosubmit',
+                'multiOptions' => $this->optionalEnum(IcingaHost::enumProperties($this->object->getConnection(), 'host.'))
+            ));
+            $this->addElement('select', 'operator', array(
+                'label' => $this->translate('Operator'),
+                'multiOptions' => array(
+                    '='  => '=',
+                    '!=' => '!=',
+                    '>'  => '>',
+                    '>=' => '>=',
+                    '<=' => '<=',
+                    '<'  => '<',
+                ),
+                'required' => $this->valueIsEmpty($this->getValue('property')),
+                'value' => '=',
+                'class' => 'assign-operator',
+            ));
+
+            $this->addElement('text', 'expression', array(
+                'label'       => $this->translate('Expression'),
+                'placeholder' => $this->translate('Expression'),
+                'class'       => 'assign-expression',
+                'required'    => !$this->valueIsEmpty($this->getValue('property'))
+            ));
+        }
+
 /*
         $this->addElement('submit', 'remove', array(
             'label'  => '-',
@@ -61,6 +73,7 @@ class AssignmentSubForm extends QuickSubForm
         ));
 */
         foreach ($this->getElements() as $el) {
+            /** @var \Zend_Form_Element $el */
             $el->setDecorators(array('ViewHelper'));
         }
     }
diff --git a/library/Director/Objects/IcingaObjectAssignments.php b/library/Director/Objects/IcingaObjectAssignments.php
index 4bd8e80..556f037 100644
--- a/library/Director/Objects/IcingaObjectAssignments.php
+++ b/library/Director/Objects/IcingaObjectAssignments.php
@@ -3,6 +3,8 @@
 namespace Icinga\Module\Director\Objects;
 
 use Icinga\Data\Filter\Filter;
+use Icinga\Data\Filter\FilterExpression;
+use Icinga\Exception\IcingaException;
 use Icinga\Exception\ProgrammingError;
 use Icinga\Module\Director\IcingaConfig\AssignRenderer;
 use Icinga\Module\Director\IcingaConfig\IcingaConfigHelper as c;
@@ -76,16 +78,19 @@ class IcingaObjectAssignments
             );
 
             $filter = Filter::fromQueryString($rule['filter_string']);
-            if (!$filter->isExpression()) {
-                throw new IcingaException(
-                    'We currently support only flat filters in our forms, got %',
-                    (string) $filter
-                );
+            if ($filter->isExpression()) {
+                #throw new IcingaException(
+                #    'We currently support only flat filters in our forms, got %',
+                #    (string) $filter
+                #);
+                /** @var FilterExpression $filter */
+                $f['property']   = $filter->getColumn();
+                $f['operator']   = $filter->getSign();
+                $f['expression'] = trim(stripcslashes($filter->getExpression()), '"');
+            }
+            else {
+                $f['query_string'] = $rule['filter_string'];
             }
-
-            $f['property']   = $filter->getColumn();
-            $f['operator']   = $filter->getSign();
-            $f['expression'] = trim(stripcslashes($filter->getExpression()), '"');
 
             $result[] = $f;
         }
@@ -107,24 +112,28 @@ class IcingaObjectAssignments
                 $rows[$val['assign_type']] = array();
             }
 
-            if (empty($val['property'])) {
-                continue;
-            }
-
-            if (is_numeric($val['expression'])) {
-                $expression = $val['expression'];
-            } else {
-                $expression = '"' . addcslashes($val['expression'], '"') . '"';
+            if (array_key_exists('query_string', $val)) {
+                $rows[$val['assign_type']][] = $this->rerenderFilter($val['query_string']);
             }
+            else {
+                if (empty($val['property'])) {
+                    continue;
+                }
 
-            $rows[$val['assign_type']][] = $this->rerenderFilter(
-                implode('', array(
-                    $val['property'],
-                    $val['operator'],
-                    $expression,
-                ))
-            );
+                if (is_numeric($val['expression'])) {
+                    $expression = $val['expression'];
+                } else {
+                    $expression = '"' . addcslashes($val['expression'], '"') . '"';
+                }
 
+                $rows[$val['assign_type']][] = $this->rerenderFilter(
+                    implode('', array(
+                        $val['property'],
+                        $val['operator'],
+                        $expression,
+                    ))
+                );
+            }
         }
 
         return $this->setValues($rows);



More information about the icinga-checkins mailing list