2013-06-27 10:14:15 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
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
|
|
|
|
*
|
|
|
|
* Needs a title and an URL
|
|
|
|
*
|
|
|
|
*/
|
2013-08-06 11:53:42 +02:00
|
|
|
class Component implements Widget
|
2013-06-27 10:14:15 +02:00
|
|
|
{
|
2013-08-06 11:53:42 +02:00
|
|
|
private $url;
|
|
|
|
private $title;
|
|
|
|
private $width;
|
|
|
|
private $height;
|
2013-06-27 10:14:15 +02:00
|
|
|
|
2013-08-06 11:53:42 +02:00
|
|
|
/**
|
|
|
|
* @var Pane
|
|
|
|
*/
|
|
|
|
private $pane;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
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-06 11:53:42 +02:00
|
|
|
public function setWidth(Dimension $width = null)
|
|
|
|
{
|
|
|
|
$this->width = $width;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setHeight(Dimension $height = null)
|
|
|
|
{
|
|
|
|
$this->height = $height;
|
|
|
|
}
|
|
|
|
|
2013-06-27 10:14:15 +02:00
|
|
|
/**
|
|
|
|
* Retrieve this components title
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getTitle()
|
|
|
|
{
|
|
|
|
return $this->title;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve my url
|
|
|
|
*
|
|
|
|
* @return Url
|
|
|
|
*/
|
|
|
|
public function getUrl()
|
|
|
|
{
|
|
|
|
return $this->url;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set this components URL
|
|
|
|
*
|
|
|
|
* @param string|Url $url Component URL
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function iniPair($key, $val)
|
|
|
|
{
|
|
|
|
return sprintf(
|
|
|
|
"%s = %s\n",
|
|
|
|
$key,
|
|
|
|
$this->quoteIni($val)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function quoteIni($str)
|
|
|
|
{
|
|
|
|
return '"' . $str . '"';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function toIni()
|
|
|
|
{
|
2013-08-06 11:53:42 +02:00
|
|
|
$ini = $this->iniPair('url', $this->url->getRelativeUrl());
|
2013-06-27 10:14:15 +02:00
|
|
|
foreach ($this->url->getParams() as $key => $val) {
|
|
|
|
$ini .= $this->iniPair($key, $val);
|
|
|
|
}
|
2013-08-06 11:53:42 +02:00
|
|
|
if ($this->height !== null) {
|
|
|
|
$ini .= 'height: '.((string) $this->height).'\n';
|
|
|
|
}
|
|
|
|
if ($this->width !== null) {
|
|
|
|
$ini .= 'width: '.((string) $this->width).'\n';
|
|
|
|
}
|
2013-06-27 10:14:15 +02:00
|
|
|
return $ini;
|
|
|
|
}
|
|
|
|
|
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'));
|
|
|
|
if (isset($_GET['layout'])) {
|
|
|
|
$url->addParams(array('layout' => $_GET['layout']));
|
|
|
|
}
|
2013-08-06 11:53:42 +02:00
|
|
|
$removeUrl = Url::fromPath(
|
|
|
|
"/dashboard/removecomponent",
|
|
|
|
array(
|
|
|
|
"pane" => $this->pane->getName(),
|
|
|
|
"component" => $this->getTitle()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
$html = str_replace("{URL}", $url->getAbsoluteUrl(), $this->template);
|
|
|
|
$html = str_replace("{REMOVE_URL}", $removeUrl, $html);
|
|
|
|
$html = str_replace("{STYLE}", $this->getBoxSizeAsCSS(), $html);
|
|
|
|
$html = str_replace("{TITLE}", $view->escape($this->getTitle()), $html);
|
|
|
|
return $html;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getBoxSizeAsCSS()
|
|
|
|
{
|
|
|
|
$style = "";
|
|
|
|
if ($this->height) {
|
|
|
|
$style .= 'height:'.(string) $this->height.';';
|
|
|
|
}
|
|
|
|
if ($this->width) {
|
|
|
|
$style .= 'width:'.(string) $this->width.';';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function fromIni($title, Zend_Config $config, Pane $pane)
|
|
|
|
{
|
|
|
|
$height = null;
|
|
|
|
$width = null;
|
|
|
|
$url = $config->get('url');
|
|
|
|
$parameters = $config->toArray();
|
|
|
|
unset($parameters["url"]); // otherwise there's an url = parameter in the Url
|
|
|
|
|
|
|
|
if (isset($parameters["height"])) {
|
|
|
|
$height = Dimension::fromString($parameters["height"]);
|
|
|
|
unset($parameters["height"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($parameters["width"])) {
|
|
|
|
$width = Dimension::fromString($parameters["width"]);
|
|
|
|
unset($parameters["width"]);
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
}
|