From b34fcc4d1235a290750dcebb1d686331a793b4a0 Mon Sep 17 00:00:00 2001 From: Yonas Habteab Date: Wed, 27 Sep 2023 11:27:45 +0200 Subject: [PATCH] Transform compat `action` & `notes` url macros automatically --- library/Icingadb/Common/Macros.php | 20 +++++++++++++++---- .../Icingadb/Widget/Detail/ObjectDetail.php | 19 ++++++++++++++++-- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/library/Icingadb/Common/Macros.php b/library/Icingadb/Common/Macros.php index 733c116ef..a913f945e 100644 --- a/library/Icingadb/Common/Macros.php +++ b/library/Icingadb/Common/Macros.php @@ -5,6 +5,8 @@ namespace Icinga\Module\Icingadb\Common; use Icinga\Application\Logger; +use Icinga\Module\Icingadb\Compat\CompatHost; +use Icinga\Module\Icingadb\Compat\CompatService; use Icinga\Module\Icingadb\Model\Host; use ipl\Orm\Model; use ipl\Orm\Query; @@ -18,11 +20,11 @@ trait Macros * Get the given string with macros being resolved * * @param string $input The string in which to look for macros - * @param Model $object The host or service used to resolve the macros + * @param Model|CompatService|CompatHost $object The host or service used to resolve the macros * * @return string */ - public function expandMacros(string $input, Model $object): string + public function expandMacros(string $input, $object): string { if (preg_match_all('@\$([^\$\s]+)\$@', $input, $matches)) { foreach ($matches[1] as $key => $value) { @@ -40,11 +42,11 @@ public function expandMacros(string $input, Model $object): string * Resolve a macro based on the given object * * @param string $macro The macro to resolve - * @param Model $object The host or service used to resolve the macros + * @param Model|CompatService|CompatHost $object The host or service used to resolve the macros * * @return string */ - public function resolveMacro(string $macro, Model $object): string + public function resolveMacro(string $macro, $object): string { if ($object instanceof Host) { $objectType = 'host'; @@ -115,4 +117,14 @@ public function resolveMacro(string $macro, Model $object): string return $value !== null ? $value : $macro; } + + /** + * Get the associated monitoring object + * + * @return CompatHost|CompatService|null + */ + protected function getCompatObject() + { + return null; + } } diff --git a/library/Icingadb/Widget/Detail/ObjectDetail.php b/library/Icingadb/Widget/Detail/ObjectDetail.php index c431a84bd..26f7a4f6f 100644 --- a/library/Icingadb/Widget/Detail/ObjectDetail.php +++ b/library/Icingadb/Widget/Detail/ObjectDetail.php @@ -20,6 +20,7 @@ use Icinga\Module\Icingadb\Common\Links; use Icinga\Module\Icingadb\Common\Macros; use Icinga\Module\Icingadb\Compat\CompatHost; +use Icinga\Module\Icingadb\Compat\CompatService; use Icinga\Module\Icingadb\Model\CustomvarFlat; use Icinga\Module\Icingadb\Web\Navigation\Action; use Icinga\Module\Icingadb\Widget\MarkdownText; @@ -132,8 +133,15 @@ protected function createActions() $item->setObject($this->object); } + $monitoringInstalled = Icinga::app()->getModuleManager()->hasInstalled('monitoring'); foreach ($this->object->action_url->first()->action_url ?? [] as $url) { - $url = $this->expandMacros($url, $this->object); + if ($monitoringInstalled) { + $obj = $this->compatObject(); + } else { + $obj = $this->object; + } + + $url = $this->expandMacros($url, $obj); $navigation->addItem( Html::wantHtml([ // Add warning to links that open in new tabs, as recommended by WCAG20 G201 @@ -320,8 +328,15 @@ protected function createNotes() $navigation = new Navigation(); $notes = trim($this->object->notes); + $monitoringInstalled = Icinga::app()->getModuleManager()->hasInstalled('monitoring'); foreach ($this->object->notes_url->first()->notes_url ?? [] as $url) { - $url = $this->expandMacros($url, $this->object); + if ($monitoringInstalled) { + $obj = $this->compatObject(); + } else { + $obj = $this->object; + } + + $url = $this->expandMacros($url, $obj); $navigation->addItem( Html::wantHtml([ // Add warning to links that open in new tabs, as recommended by WCAG20 G201