Deduplicate url-attribute parsing code

Use function to fetch all host links in MonitoredObject instead.
This commit is contained in:
Matthias Jentsch 2015-05-28 10:47:44 +02:00
parent cba36ec017
commit 6c44f6a11a
2 changed files with 22 additions and 17 deletions

View File

@ -1,29 +1,21 @@
<?php
$links = array();
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'));
$linkText = '<a href="%s" target="_blank">%s ' . $newTabInfo . '</a>';
$localLinkText = '<a href="%s">%s</a>';
if ($object->action_url) {
if (strpos($object->action_url, "' ") === false) {
$links[] = sprintf($linkText, $this->resolveMacros($object->action_url, $object), 'Action');
} else {
// TODO: We should find out document what's going on here. Looks strange :p
foreach(explode("' ", $object->action_url) as $url) {
$url = strpos($url, "'") === 0 ? substr($url, 1) : $url;
$url = strrpos($url, "'") === strlen($url) - 1 ? substr($url, 0, strlen($url) - 1) : $url;
$links[] = sprintf($linkText, $this->resolveMacros($url, $object), 'Action');
}
}
$links = MonitoredObject::parseAttributeUrls($object->action_url);
foreach ($links as $i => $link) {
$links[$i] = sprintf(
'<a href="%s" target="_blank">%s ' . $newTabInfo . '</a>',
$this->resolveMacros($object->action_url, $object),
'Action'
);
}
if (isset($this->actions)) {
foreach ($this->actions as $id => $action) {
$links[] = sprintf($localLinkText, $action, $id);
$links[] = sprintf('<a href="%s">%s</a>', $action, $id);
}
}

View File

@ -578,9 +578,22 @@ abstract class MonitoredObject implements Filterable
*/
public function getNotesUrls() {}
/**
* Get all action urls configured for this monitored object
*
* @return array All note urls as a string
*/
public function getActionUrls()
{
return MonitoredObject::parseAttributeUrls($this->action_url);
}
/**
* Parse the content of the action_url or notes_url attributes
*
* Find all occurences of http links, separated by whitespaces and quoted
* by single or double-ticks.
*
* @link http://docs.icinga.org/latest/de/objectdefinitions.html
*
* @param string $urlString A string containing one or more urls