From aad07b3d1df698b588da1f7caa6ecddf30e37645 Mon Sep 17 00:00:00 2001 From: Thomas Gelf Date: Mon, 8 Jul 2019 13:52:48 +0200 Subject: [PATCH] IcingaDependency: fix form and rendering --- application/forms/IcingaDependencyForm.php | 7 +++- library/Director/Objects/IcingaDependency.php | 41 ++++++++++++++----- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/application/forms/IcingaDependencyForm.php b/application/forms/IcingaDependencyForm.php index c9eda419..3a48eecf 100644 --- a/application/forms/IcingaDependencyForm.php +++ b/application/forms/IcingaDependencyForm.php @@ -94,6 +94,7 @@ class IcingaDependencyForm extends DirectorObjectForm 'description' => $this->translate( 'Whether this dependency should affect hosts or services' ), + 'required' => true, 'class' => 'autosubmit', 'multiOptions' => $this->optionalEnum([ 'host' => $this->translate('Hosts'), @@ -110,7 +111,6 @@ class IcingaDependencyForm extends DirectorObjectForm $suggestionContext = ucfirst($applyTo) . 'FilterColumns'; $this->addAssignFilter([ 'suggestionContext' => $suggestionContext, - 'required' => true, 'description' => $this->translate( 'This allows you to configure an assignment filter. Please feel' . ' free to combine as many nested operators as you want. The' @@ -191,7 +191,10 @@ class IcingaDependencyForm extends DirectorObjectForm $dependency = $this->getObject(); $parentHost = $dependency->get('parent_host'); if ($parentHost === null) { - $parentHost = '$' . $dependency->get('parent_host_var') . '$'; + $parentHostVar = $dependency->get('parent_host_var'); + if (\strlen($parentHostVar) > 0) { + $parentHost = '$' . $dependency->get('parent_host_var') . '$'; + } } $this->addElement('text', 'parent_host', [ 'label' => $this->translate('Parent Host'), diff --git a/library/Director/Objects/IcingaDependency.php b/library/Director/Objects/IcingaDependency.php index 74ed4a74..125905bf 100644 --- a/library/Director/Objects/IcingaDependency.php +++ b/library/Director/Objects/IcingaDependency.php @@ -176,11 +176,23 @@ class IcingaDependency extends IcingaObject implements ExportInterface */ protected function renderSuffix() { - if ($this->parentHostIsVar() && ! $this->renderForArray) { - return parent::renderSuffix() . $this->renderCloneForArray(); - } else { + if (! $this->parentHostIsVar()) { return parent::renderSuffix(); } + + if (\strlen($this->get('assign_filter')) > 0) { + $suffix = parent::renderSuffix(); + } else { + $suffix = ' assign where ' . $this->renderAssignFilterExtension('') + . "\n" . parent::renderSuffix(); + } + + + if ($this->renderForArray) { + return $suffix; + } else { + return $suffix . $this->renderCloneForArray(); + } } protected function renderCloneForArray() @@ -197,19 +209,26 @@ class IcingaDependency extends IcingaObject implements ExportInterface public function renderAssign_Filter() { if ($this->parentHostIsVar()) { - $varName = $this->get('parent_host_var'); - if ($this->renderForArray) { - $suffix = sprintf(' && typeof(%s) == Array', $varName); - } else { - $suffix = sprintf(' && typeof(%s) == String', $varName); - } - - return preg_replace('/\n$/m', $suffix, parent::renderAssign_Filter() . "\n"); + return preg_replace( + '/\n$/m', + $this->renderAssignFilterExtension(), + parent::renderAssign_Filter() . "\n" + ); } else { return parent::renderAssign_Filter(); } } + protected function renderAssignFilterExtension($pre = ' && ') + { + $varName = $this->get('parent_host_var'); + if ($this->renderForArray) { + return sprintf('%stypeof(%s) == Array', $pre, $varName); + } else { + return sprintf('%stypeof(%s) == String', $pre, $varName); + } + } + protected function setKey($key) { // TODO: Check if this method can be removed