Skip to content

Commit

Permalink
Resolve macros when accessing getters on MonitoredObjects
Browse files Browse the repository at this point in the history
refs #6392
  • Loading branch information
majentsch committed May 28, 2015
1 parent 37f58e5 commit a669491
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 17 deletions.
@@ -1,16 +1,11 @@
<?php
use Icinga\Module\Monitoring\Object\MonitoredObject;

// add warning to links that open in new tabs to improve accessibility, as recommended by WCAG20 G201
$newTabInfo = sprintf('<span class="info-box display-on-hover"> %s </span>', $this->translate('opens in new window'));

$links = MonitoredObject::parseAttributeUrls($object->action_url);
$links = $object->getActionUrls();
foreach ($links as $i => $link) {
$links[$i] = sprintf(
'<a href="%s" target="_blank">%s ' . $newTabInfo . '</a>',
$this->resolveMacros($object->action_url, $object),
'Action'
);
$links[$i] = sprintf('<a href="%s" target="_blank">%s ' . $newTabInfo . '</a>', $link, 'Action');
}

if (isset($this->actions)) {
Expand Down
@@ -1,15 +1,13 @@
<?php
$notes = trim($object->getNotes());
$links = $object->getNotesUrls();
?>

<?php if (! empty($links) || ! empty($notes)): ?>
if (! empty($links) || ! empty($notes)): ?>
<tr>
<th><?= $this->translate('Notes') ?></th>
<td>
<?php
if (! empty($notes)) {
$notes = $this->resolveMacros($notes, $object);
echo $notes . '<br>';
}
// add warning to links that open in new tabs to improve accessibility, as recommended by WCAG20 G201
Expand All @@ -19,8 +17,7 @@ $links = $object->getNotesUrls();
);
$linkText = '<a href="%s" target="_blank">%s ' . $newTabInfo . '</a>';
foreach ($links as $i => $link) {
$resolved = $this->resolveMacros($link, $object);
$links[$i] = sprintf($linkText, $this->escape($resolved), $this->escape($resolved));
$links[$i] = sprintf($linkText, $this->escape($link), $this->escape($link));
}
echo implode('<br>', $links);
?>
Expand Down
4 changes: 3 additions & 1 deletion modules/monitoring/library/Monitoring/Object/Host.php
Expand Up @@ -192,7 +192,9 @@ public static function getStateText($state, $translate = false)

public function getNotesUrls()
{
return MonitoredObject::parseAttributeUrls($this->host_notes_url);
return $this->resolveAllStrings(
MonitoredObject::parseAttributeUrls($this->host_notes_url)
);
}

public function getNotes()
Expand Down
22 changes: 19 additions & 3 deletions modules/monitoring/library/Monitoring/Object/MonitoredObject.php
Expand Up @@ -569,14 +569,14 @@ public static function fromParams(UrlParams $params)
*
* @return string The notes as a string
*/
public function getNotes() {}
public abstract function getNotes();

/**
* Get all note urls configured for this monitored object
*
* @return array All note urls as a string
*/
public function getNotesUrls() {}
public abstract function getNotesUrls();

/**
* Get all action urls configured for this monitored object
Expand All @@ -585,7 +585,23 @@ public function getNotesUrls() {}
*/
public function getActionUrls()
{
return MonitoredObject::parseAttributeUrls($this->action_url);
return $this->resolveAllStrings(
MonitoredObject::parseAttributeUrls($this->action_url)
);
}

/**
* Resolve macros in all given strings in the current object context
*
* @param array $strs An array of urls as string
* @return type
*/
protected function resolveAllStrings(array $strs)
{
foreach ($strs as $i => $str) {
$strs[$i] = Macro::resolveMacros($str, $this);
}
return $strs;
}

/**
Expand Down
4 changes: 3 additions & 1 deletion modules/monitoring/library/Monitoring/Object/Service.php
Expand Up @@ -202,7 +202,9 @@ public static function getStateText($state, $translate = false)

public function getNotesUrls()
{
return MonitoredObject::parseAttributeUrls($this->service_notes_url);
return $this->resolveAllStrings(
MonitoredObject::parseAttributeUrls($this->service_notes_url)
);
}

public function getNotes()
Expand Down

0 comments on commit a669491

Please sign in to comment.