Skip to content

Commit

Permalink
Transform compat action & notes url macros automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
yhabteab committed Sep 27, 2023
1 parent cd41a20 commit b34fcc4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
20 changes: 16 additions & 4 deletions library/Icingadb/Common/Macros.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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';
Expand Down Expand Up @@ -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;
}
}
19 changes: 17 additions & 2 deletions library/Icingadb/Widget/Detail/ObjectDetail.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b34fcc4

Please sign in to comment.