2013-06-27 10:14:15 +02:00
|
|
|
<?php
|
2015-02-04 10:46:36 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
|
2013-08-09 10:32:57 +02:00
|
|
|
|
2013-06-27 10:14:15 +02:00
|
|
|
namespace Icinga\Web\Widget\Dashboard;
|
|
|
|
|
2014-11-07 13:53:03 +01:00
|
|
|
use Zend_Form_Element_Button;
|
2013-10-22 12:08:44 +02:00
|
|
|
use Icinga\Web\Form;
|
2013-06-27 10:14:15 +02:00
|
|
|
use Icinga\Web\Url;
|
2014-11-18 13:11:52 +01:00
|
|
|
use Icinga\Data\ConfigObject;
|
2014-11-07 13:53:03 +01:00
|
|
|
use Icinga\Exception\IcingaException;
|
2013-06-27 10:14:15 +02:00
|
|
|
|
|
|
|
/**
|
2014-11-20 12:08:50 +01:00
|
|
|
* A dashboard pane dashlet
|
2013-06-27 10:14:15 +02:00
|
|
|
*
|
2013-08-07 17:44:18 +02:00
|
|
|
* This is the element displaying a specific view in icinga2web
|
2013-06-27 10:14:15 +02:00
|
|
|
*
|
|
|
|
*/
|
2014-11-20 12:08:50 +01:00
|
|
|
class Dashlet extends UserWidget
|
2013-06-27 10:14:15 +02:00
|
|
|
{
|
2013-08-07 17:44:18 +02:00
|
|
|
/**
|
2014-11-20 12:08:50 +01:00
|
|
|
* The url of this Dashlet
|
2013-08-07 17:44:18 +02:00
|
|
|
*
|
|
|
|
* @var \Icinga\Web\Url
|
|
|
|
*/
|
2013-08-06 11:53:42 +02:00
|
|
|
private $url;
|
2013-08-07 17:44:18 +02:00
|
|
|
|
|
|
|
/**
|
2014-11-20 12:08:50 +01:00
|
|
|
* The title being displayed on top of the dashlet
|
2013-08-07 17:44:18 +02:00
|
|
|
* @var
|
|
|
|
*/
|
2013-08-06 11:53:42 +02:00
|
|
|
private $title;
|
2013-06-27 10:14:15 +02:00
|
|
|
|
2013-08-07 17:44:18 +02:00
|
|
|
/**
|
2014-11-20 12:08:50 +01:00
|
|
|
* The pane containing this dashlet, needed for the 'remove button'
|
2013-08-06 11:53:42 +02:00
|
|
|
* @var Pane
|
|
|
|
*/
|
|
|
|
private $pane;
|
|
|
|
|
2014-09-02 13:16:21 +02:00
|
|
|
/**
|
|
|
|
* The disabled option is used to "delete" default dashlets provided by modules
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $disabled = false;
|
|
|
|
|
2013-08-07 17:44:18 +02:00
|
|
|
/**
|
|
|
|
* The template string used for rendering this widget
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2013-08-06 11:53:42 +02:00
|
|
|
private $template =<<<'EOD'
|
|
|
|
|
2014-02-18 18:45:37 +01:00
|
|
|
<div class="container" data-icinga-url="{URL}">
|
2015-02-23 17:05:02 +01:00
|
|
|
<h1><a href="{FULL_URL}" aria-label="{TOOLTIP}" title="{TOOLTIP}" data-base-target="col1">{TITLE}</a></h1>
|
2013-10-22 12:08:44 +02:00
|
|
|
<noscript>
|
2015-02-23 16:20:08 +01:00
|
|
|
<iframe
|
|
|
|
src="{IFRAME_URL}"
|
|
|
|
style="height:100%; width:99%"
|
|
|
|
frameborder="no"
|
|
|
|
title="{TITLE_PREFIX}{TITLE}">
|
|
|
|
</iframe>
|
2013-10-22 12:08:44 +02:00
|
|
|
</noscript>
|
2013-08-06 11:53:42 +02:00
|
|
|
</div>
|
|
|
|
EOD;
|
|
|
|
|
2013-08-07 17:44:18 +02:00
|
|
|
/**
|
2014-11-20 12:08:50 +01:00
|
|
|
* Create a new dashlet displaying the given url in the provided pane
|
2013-08-07 17:44:18 +02:00
|
|
|
*
|
2014-11-20 12:08:50 +01:00
|
|
|
* @param string $title The title to use for this dashlet
|
|
|
|
* @param Url|string $url The url this dashlet uses for displaying information
|
|
|
|
* @param Pane $pane The pane this Dashlet will be added to
|
2013-08-07 17:44:18 +02:00
|
|
|
*/
|
2014-09-02 10:21:56 +02:00
|
|
|
public function __construct($title, $url, Pane $pane)
|
2013-06-27 10:14:15 +02:00
|
|
|
{
|
|
|
|
$this->title = $title;
|
2013-08-06 11:53:42 +02:00
|
|
|
$this->pane = $pane;
|
2013-06-27 10:14:15 +02:00
|
|
|
if ($url instanceof Url) {
|
|
|
|
$this->url = $url;
|
2014-02-18 18:45:37 +01:00
|
|
|
} elseif ($url) {
|
2013-07-31 10:59:34 +02:00
|
|
|
$this->url = Url::fromPath($url);
|
2014-02-18 18:45:37 +01:00
|
|
|
} else {
|
2014-08-27 16:03:15 +02:00
|
|
|
throw new IcingaException(
|
2014-11-20 12:08:50 +01:00
|
|
|
'Cannot create dashboard dashlet "%s" without valid URL',
|
2014-08-27 16:03:15 +02:00
|
|
|
$title
|
2014-02-18 18:45:37 +01:00
|
|
|
);
|
2013-06-27 10:14:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-11-20 12:08:50 +01:00
|
|
|
* Retrieve the dashlets title
|
2013-06-27 10:14:15 +02:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getTitle()
|
|
|
|
{
|
|
|
|
return $this->title;
|
|
|
|
}
|
|
|
|
|
2014-11-18 09:50:10 +01:00
|
|
|
/**
|
|
|
|
* @param string $title
|
|
|
|
*/
|
|
|
|
public function setTitle($title)
|
|
|
|
{
|
|
|
|
$this->title = $title;
|
|
|
|
}
|
|
|
|
|
2013-06-27 10:14:15 +02:00
|
|
|
/**
|
2014-11-20 12:08:50 +01:00
|
|
|
* Retrieve the dashlets url
|
2013-06-27 10:14:15 +02:00
|
|
|
*
|
|
|
|
* @return Url
|
|
|
|
*/
|
|
|
|
public function getUrl()
|
|
|
|
{
|
|
|
|
return $this->url;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-11-20 12:08:50 +01:00
|
|
|
* Set the dashlets URL
|
2013-06-27 10:14:15 +02:00
|
|
|
*
|
2013-08-07 17:44:18 +02:00
|
|
|
* @param string|Url $url The url to use, either as an Url object or as a path
|
|
|
|
*
|
2015-04-07 14:23:26 +02:00
|
|
|
* @return $this
|
2013-06-27 10:14:15 +02:00
|
|
|
*/
|
|
|
|
public function setUrl($url)
|
|
|
|
{
|
|
|
|
if ($url instanceof Url) {
|
|
|
|
$this->url = $url;
|
|
|
|
} else {
|
2013-07-31 10:59:34 +02:00
|
|
|
$this->url = Url::fromPath($url);
|
2013-06-27 10:14:15 +02:00
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2014-09-02 13:16:21 +02:00
|
|
|
/**
|
|
|
|
* Set the disabled property
|
|
|
|
*
|
|
|
|
* @param boolean $disabled
|
|
|
|
*/
|
|
|
|
public function setDisabled($disabled)
|
|
|
|
{
|
|
|
|
$this->disabled = $disabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the disabled property
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function getDisabled()
|
|
|
|
{
|
|
|
|
return $this->disabled;
|
|
|
|
}
|
|
|
|
|
2013-08-07 17:44:18 +02:00
|
|
|
/**
|
2014-11-20 12:08:50 +01:00
|
|
|
* Return this dashlet's structure as array
|
2013-08-07 17:44:18 +02:00
|
|
|
*
|
2014-04-29 11:30:34 +02:00
|
|
|
* @return array
|
2013-08-07 17:44:18 +02:00
|
|
|
*/
|
2014-04-29 11:30:34 +02:00
|
|
|
public function toArray()
|
2013-06-27 10:14:15 +02:00
|
|
|
{
|
2014-11-11 15:59:59 +01:00
|
|
|
$array = array(
|
2014-11-19 16:44:45 +01:00
|
|
|
'url' => $this->url->getRelativeUrl(),
|
2014-11-11 15:59:59 +01:00
|
|
|
'title' => $this->getTitle()
|
|
|
|
);
|
|
|
|
if ($this->getDisabled() === true) {
|
|
|
|
$array['disabled'] = 1;
|
|
|
|
}
|
2014-04-29 11:30:34 +02:00
|
|
|
return $array;
|
2013-06-27 10:14:15 +02:00
|
|
|
}
|
|
|
|
|
2013-08-07 17:44:18 +02:00
|
|
|
/**
|
|
|
|
* @see Widget::render()
|
|
|
|
*/
|
2014-03-17 17:04:09 +01:00
|
|
|
public function render()
|
2013-06-27 10:14:15 +02:00
|
|
|
{
|
2014-09-02 13:16:21 +02:00
|
|
|
if ($this->disabled === true) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2014-03-17 17:04:09 +01:00
|
|
|
$view = $this->view();
|
2013-08-06 11:53:42 +02:00
|
|
|
$url = clone($this->url);
|
2014-06-22 16:18:23 +02:00
|
|
|
$url->setParam('view', 'compact');
|
2014-04-17 18:20:03 +02:00
|
|
|
$iframeUrl = clone($url);
|
2014-06-22 16:18:23 +02:00
|
|
|
$iframeUrl->setParam('isIframe');
|
2013-08-07 17:40:18 +02:00
|
|
|
|
2014-11-11 16:04:51 +01:00
|
|
|
$searchTokens = array(
|
|
|
|
'{URL}',
|
|
|
|
'{IFRAME_URL}',
|
|
|
|
'{FULL_URL}',
|
2015-02-23 17:05:02 +01:00
|
|
|
'{TOOLTIP}',
|
2014-11-11 16:04:51 +01:00
|
|
|
'{TITLE}',
|
2015-02-23 16:20:08 +01:00
|
|
|
'{TITLE_PREFIX}'
|
2014-11-11 16:04:51 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
$replaceTokens = array(
|
|
|
|
$url,
|
|
|
|
$iframeUrl,
|
|
|
|
$url->getUrlWithout(array('view', 'limit')),
|
2015-02-23 17:05:02 +01:00
|
|
|
sprintf($view->translate('Show %s', 'dashboard.dashlet.tooltip'), $view->escape($this->getTitle())),
|
2014-11-11 16:04:51 +01:00
|
|
|
$view->escape($this->getTitle()),
|
2015-02-24 08:42:36 +01:00
|
|
|
$view->translate('Dashlet') . ': '
|
2014-11-11 16:04:51 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
return str_replace($searchTokens, $replaceTokens, $this->template);
|
2013-10-22 12:08:44 +02:00
|
|
|
}
|
|
|
|
|
2013-08-07 17:44:18 +02:00
|
|
|
/**
|
2014-11-20 12:08:50 +01:00
|
|
|
* Create a @see Dashlet instance from the given Zend config, using the provided title
|
2014-09-02 10:21:56 +02:00
|
|
|
*
|
2014-11-20 12:08:50 +01:00
|
|
|
* @param $title The title for this dashlet
|
2014-11-18 13:11:52 +01:00
|
|
|
* @param ConfigObject $config The configuration defining url, parameters, height, width, etc.
|
2014-11-20 12:08:50 +01:00
|
|
|
* @param Pane $pane The pane this dashlet belongs to
|
2013-08-07 17:44:18 +02:00
|
|
|
*
|
2014-11-20 12:08:50 +01:00
|
|
|
* @return Dashlet A newly created Dashlet for use in the Dashboard
|
2013-08-07 17:44:18 +02:00
|
|
|
*/
|
2014-11-18 13:11:52 +01:00
|
|
|
public static function fromIni($title, ConfigObject $config, Pane $pane)
|
2013-08-06 11:53:42 +02:00
|
|
|
{
|
|
|
|
$height = null;
|
|
|
|
$width = null;
|
|
|
|
$url = $config->get('url');
|
|
|
|
$parameters = $config->toArray();
|
2013-08-07 17:40:18 +02:00
|
|
|
unset($parameters['url']); // otherwise there's an url = parameter in the Url
|
2013-08-06 11:53:42 +02:00
|
|
|
|
2014-11-20 12:08:50 +01:00
|
|
|
$cmp = new Dashlet($title, Url::fromPath($url, $parameters), $pane);
|
2013-08-06 11:53:42 +02:00
|
|
|
return $cmp;
|
2013-06-27 10:14:15 +02:00
|
|
|
}
|
2014-11-18 09:50:10 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \Icinga\Web\Widget\Dashboard\Pane $pane
|
|
|
|
*/
|
2014-11-20 12:08:50 +01:00
|
|
|
public function setPane(Pane $pane)
|
2014-11-18 09:50:10 +01:00
|
|
|
{
|
|
|
|
$this->pane = $pane;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \Icinga\Web\Widget\Dashboard\Pane
|
|
|
|
*/
|
|
|
|
public function getPane()
|
|
|
|
{
|
|
|
|
return $this->pane;
|
|
|
|
}
|
2013-06-27 10:14:15 +02:00
|
|
|
}
|