Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Transform compat action & notes url macros automatically #886

Merged
merged 1 commit into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 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
9 changes: 7 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,10 @@ protected function createActions()
$item->setObject($this->object);
}

$monitoringInstalled = Icinga::app()->getModuleManager()->hasInstalled('monitoring');
$obj = $monitoringInstalled ? $this->compatObject() : $this->object;
foreach ($this->object->action_url->first()->action_url ?? [] as $url) {
$url = $this->expandMacros($url, $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 +323,10 @@ protected function createNotes()
$navigation = new Navigation();
$notes = trim($this->object->notes);

$monitoringInstalled = Icinga::app()->getModuleManager()->hasInstalled('monitoring');
$obj = $monitoringInstalled ? $this->compatObject() : $this->object;
foreach ($this->object->notes_url->first()->notes_url ?? [] as $url) {
$url = $this->expandMacros($url, $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