2013-06-27 10:14:15 +02:00
|
|
|
<?php
|
2013-08-08 17:42:34 +02:00
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
/**
|
|
|
|
* This file is part of Icinga 2 Web.
|
|
|
|
*
|
|
|
|
* Icinga 2 Web - Head for multiple monitoring backends.
|
|
|
|
* Copyright (C) 2013 Icinga Development Team
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* @copyright 2013 Icinga Development Team <info@icinga.org>
|
|
|
|
* @license http://www.gnu.org/licenses/gpl-2.0.txt GPL, version 2
|
|
|
|
* @author Icinga Development Team <info@icinga.org>
|
|
|
|
*/
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
2013-06-27 10:14:15 +02:00
|
|
|
namespace Icinga\Web\Widget\Dashboard;
|
|
|
|
|
2013-08-06 11:53:42 +02:00
|
|
|
use Icinga\Util\Dimension;
|
2013-06-27 10:14:15 +02:00
|
|
|
use Icinga\Web\Url;
|
2013-08-06 11:53:42 +02:00
|
|
|
use Icinga\Web\Widget\Widget;
|
|
|
|
use Zend_Config;
|
2013-06-27 10:14:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A dashboard pane component
|
|
|
|
*
|
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
|
|
|
*
|
|
|
|
*/
|
2013-08-06 11:53:42 +02:00
|
|
|
class Component implements Widget
|
2013-06-27 10:14:15 +02:00
|
|
|
{
|
2013-08-07 17:44:18 +02:00
|
|
|
/**
|
|
|
|
* The url of this Component
|
|
|
|
*
|
|
|
|
* @var \Icinga\Web\Url
|
|
|
|
*/
|
2013-08-06 11:53:42 +02:00
|
|
|
private $url;
|
2013-08-07 17:44:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The title being displayed on top of the component
|
|
|
|
* @var
|
|
|
|
*/
|
2013-08-06 11:53:42 +02:00
|
|
|
private $title;
|
2013-06-27 10:14:15 +02:00
|
|
|
|
2013-08-06 11:53:42 +02:00
|
|
|
/**
|
2013-08-07 17:44:18 +02:00
|
|
|
* The width of the component, if set
|
|
|
|
*
|
|
|
|
* @var Dimension|null
|
|
|
|
*/
|
|
|
|
private $width = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The height of the component, if set
|
|
|
|
*
|
|
|
|
* @var Dimension|null
|
|
|
|
*/
|
|
|
|
private $height = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The pane containing this component, needed for the 'remove button'
|
2013-08-06 11:53:42 +02:00
|
|
|
* @var Pane
|
|
|
|
*/
|
|
|
|
private $pane;
|
|
|
|
|
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'
|
|
|
|
|
|
|
|
<div class="icinga-container dashboard" icingatitle="{TITLE}" style="{DIMENSION}">
|
|
|
|
<a href="{URL}"> {TITLE}</a>
|
|
|
|
<a class="btn btn-danger btn-mini pull-right" href="{REMOVE_URL}" style="color:black">X</a>
|
|
|
|
|
|
|
|
<div class="icinga-container" icingaurl="{URL}">
|
|
|
|
<noscript>
|
|
|
|
<iframe src="{URL}" style="height:100%; width:99%" frameborder="no"></iframe>
|
|
|
|
</noscript>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
EOD;
|
|
|
|
|
2013-08-07 17:44:18 +02:00
|
|
|
/**
|
|
|
|
* Create a new component displaying the given url in the provided pane
|
|
|
|
*
|
|
|
|
* @param string $title The title to use for this component
|
|
|
|
* @param Url|string $url The url this component uses for displaying information
|
|
|
|
* @param Pane $pane The pane this Component will be added to
|
|
|
|
*/
|
2013-08-06 11:53:42 +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;
|
|
|
|
} else {
|
2013-07-31 10:59:34 +02:00
|
|
|
$this->url = Url::fromPath($url);
|
2013-06-27 10:14:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-07 17:44:18 +02:00
|
|
|
/**
|
|
|
|
* Set the with for this component or use the default width if null is provided
|
|
|
|
*
|
|
|
|
* @param Dimension|null $width The width to use or null to use the default width
|
|
|
|
*/
|
2013-08-06 11:53:42 +02:00
|
|
|
public function setWidth(Dimension $width = null)
|
|
|
|
{
|
|
|
|
$this->width = $width;
|
|
|
|
}
|
|
|
|
|
2013-08-07 17:44:18 +02:00
|
|
|
/**
|
|
|
|
* Set the with for this component or use the default height if null is provided
|
|
|
|
*
|
|
|
|
* @param Dimension|null $height The height to use or null to use the default height
|
|
|
|
*/
|
2013-08-06 11:53:42 +02:00
|
|
|
public function setHeight(Dimension $height = null)
|
|
|
|
{
|
|
|
|
$this->height = $height;
|
|
|
|
}
|
|
|
|
|
2013-06-27 10:14:15 +02:00
|
|
|
/**
|
2013-08-07 17:44:18 +02:00
|
|
|
* Retrieve the components title
|
2013-06-27 10:14:15 +02:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getTitle()
|
|
|
|
{
|
|
|
|
return $this->title;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-08-07 17:44:18 +02:00
|
|
|
* Retrieve the components url
|
2013-06-27 10:14:15 +02:00
|
|
|
*
|
|
|
|
* @return Url
|
|
|
|
*/
|
|
|
|
public function getUrl()
|
|
|
|
{
|
|
|
|
return $this->url;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-08-07 17:44:18 +02:00
|
|
|
* Set the components 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
|
|
|
|
*
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
2013-08-07 17:44:18 +02:00
|
|
|
/**
|
|
|
|
* Return this component in a suitable format and encoding for ini files
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-06-27 10:14:15 +02:00
|
|
|
public function toIni()
|
|
|
|
{
|
2013-08-08 17:42:34 +02:00
|
|
|
$ini = 'url = "' . $this->url->getRelativeUrl() . '"' . PHP_EOL;
|
2013-06-27 10:14:15 +02:00
|
|
|
foreach ($this->url->getParams() as $key => $val) {
|
2013-08-08 17:42:34 +02:00
|
|
|
$ini .= $key.' = "' . $val . '"' . PHP_EOL;
|
2013-06-27 10:14:15 +02:00
|
|
|
}
|
2013-08-06 11:53:42 +02:00
|
|
|
if ($this->height !== null) {
|
2013-08-08 17:42:34 +02:00
|
|
|
$ini .= 'height = "' . ((string) $this->height) . '"' . PHP_EOL;
|
2013-08-06 11:53:42 +02:00
|
|
|
}
|
|
|
|
if ($this->width !== null) {
|
2013-08-08 17:42:34 +02:00
|
|
|
$ini .= 'width = "' . ((string) $this->width) . '"' . PHP_EOL;
|
2013-08-06 11:53:42 +02:00
|
|
|
}
|
2013-06-27 10:14:15 +02:00
|
|
|
return $ini;
|
|
|
|
}
|
|
|
|
|
2013-08-07 17:44:18 +02:00
|
|
|
/**
|
|
|
|
* @see Widget::render()
|
|
|
|
*/
|
2013-08-06 11:53:42 +02:00
|
|
|
public function render(\Zend_View_Abstract $view)
|
2013-06-27 10:14:15 +02:00
|
|
|
{
|
2013-08-06 11:53:42 +02:00
|
|
|
$url = clone($this->url);
|
2013-06-27 10:14:15 +02:00
|
|
|
$url->addParams(array('view' => 'compact'));
|
2013-08-07 17:40:18 +02:00
|
|
|
|
2013-08-06 11:53:42 +02:00
|
|
|
$removeUrl = Url::fromPath(
|
2013-08-07 17:40:18 +02:00
|
|
|
'/dashboard/removecomponent',
|
2013-08-06 11:53:42 +02:00
|
|
|
array(
|
2013-08-07 17:40:18 +02:00
|
|
|
'pane' => $this->pane->getName(),
|
|
|
|
'component' => $this->getTitle()
|
2013-08-06 11:53:42 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2013-08-07 17:40:18 +02:00
|
|
|
$html = str_replace('{URL}', $url->getAbsoluteUrl(), $this->template);
|
|
|
|
$html = str_replace('{REMOVE_URL}', $removeUrl, $html);
|
|
|
|
$html = str_replace('{DIMENSION}', $this->getBoxSizeAsCSS(), $html);
|
|
|
|
$html = str_replace('{TITLE}', $view->escape($this->getTitle()), $html);
|
2013-08-06 11:53:42 +02:00
|
|
|
return $html;
|
|
|
|
}
|
|
|
|
|
2013-08-07 17:44:18 +02:00
|
|
|
/**
|
|
|
|
* Return the height and width deifnition (if given) in CSS format
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-08-06 11:53:42 +02:00
|
|
|
private function getBoxSizeAsCSS()
|
|
|
|
{
|
2013-08-07 17:40:18 +02:00
|
|
|
$style = '';
|
2013-08-06 11:53:42 +02:00
|
|
|
if ($this->height) {
|
2013-08-08 17:42:34 +02:00
|
|
|
$style .= 'height:' . (string) $this->height . ';';
|
2013-08-06 11:53:42 +02:00
|
|
|
}
|
|
|
|
if ($this->width) {
|
2013-08-08 17:42:34 +02:00
|
|
|
$style .= 'width:' . (string) $this->width . ';';
|
2013-08-06 11:53:42 +02:00
|
|
|
}
|
2013-08-07 17:44:18 +02:00
|
|
|
return $style;
|
2013-08-06 11:53:42 +02:00
|
|
|
}
|
|
|
|
|
2013-08-07 17:44:18 +02:00
|
|
|
/**
|
|
|
|
* Create a @see Component instance from the given Zend config, using the provided title
|
|
|
|
*
|
|
|
|
* @param $title The title for this component
|
|
|
|
* @param Zend_Config $config The configuration defining url, parameters, height, width, etc.
|
|
|
|
* @param Pane $pane The pane this component belongs to
|
|
|
|
*
|
|
|
|
* @return Component A newly created Component for use in the Dashboard
|
|
|
|
*/
|
2013-08-06 11:53:42 +02:00
|
|
|
public static function fromIni($title, Zend_Config $config, Pane $pane)
|
|
|
|
{
|
|
|
|
$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
|
|
|
|
2013-08-07 17:40:18 +02:00
|
|
|
if (isset($parameters['height'])) {
|
|
|
|
$height = Dimension::fromString($parameters['height']);
|
|
|
|
unset($parameters['height']);
|
2013-08-06 11:53:42 +02:00
|
|
|
}
|
|
|
|
|
2013-08-07 17:40:18 +02:00
|
|
|
if (isset($parameters['width'])) {
|
|
|
|
$width = Dimension::fromString($parameters['width']);
|
|
|
|
unset($parameters['width']);
|
2013-08-06 11:53:42 +02:00
|
|
|
}
|
2013-06-27 10:14:15 +02:00
|
|
|
|
2013-08-06 11:53:42 +02:00
|
|
|
$cmp = new Component($title, Url::fromPath($url, $parameters), $pane);
|
|
|
|
$cmp->setHeight($height);
|
|
|
|
$cmp->setWidth($width);
|
|
|
|
return $cmp;
|
2013-06-27 10:14:15 +02:00
|
|
|
}
|
|
|
|
}
|