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-08-09 10:32:57 +02:00
|
|
|
|
2013-06-27 10:14:15 +02:00
|
|
|
namespace Icinga\Web\Widget\Dashboard;
|
|
|
|
|
|
|
|
use Icinga\Exception\ConfigurationError;
|
2013-08-08 17:42:34 +02:00
|
|
|
use Icinga\Exception\ProgrammingError;
|
2013-08-06 11:53:42 +02:00
|
|
|
use Icinga\Web\Widget\Widget;
|
|
|
|
use Zend_Config;
|
2013-08-07 17:40:18 +02:00
|
|
|
use Zend_View_Abstract;
|
2013-06-27 10:14:15 +02:00
|
|
|
|
2013-08-07 17:44:18 +02:00
|
|
|
/**
|
|
|
|
* A pane, displaying different Dashboard components
|
|
|
|
*
|
|
|
|
*/
|
2013-08-06 11:53:42 +02:00
|
|
|
class Pane implements Widget
|
2013-06-27 10:14:15 +02:00
|
|
|
{
|
2013-08-07 17:44:18 +02:00
|
|
|
/**
|
|
|
|
* The name of this pane, as defined in the ini file
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $name;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The title of this pane, as displayed in the dashboard tabs
|
|
|
|
* @TODO: Currently the same as $name, evaluate if distinguishing is needed
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $title;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An array of @see Components that are displayed in this pane
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $components = array();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new pane
|
|
|
|
*
|
|
|
|
* @param $name The pane to create
|
|
|
|
*/
|
2013-06-27 10:14:15 +02:00
|
|
|
public function __construct($name)
|
|
|
|
{
|
|
|
|
$this->name = $name;
|
|
|
|
$this->title = $name;
|
|
|
|
}
|
|
|
|
|
2013-08-07 17:44:18 +02:00
|
|
|
/**
|
|
|
|
* Returns the name of this pane
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-06-27 10:14:15 +02:00
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return $this->name;
|
|
|
|
}
|
|
|
|
|
2013-08-07 17:44:18 +02:00
|
|
|
/**
|
|
|
|
* Returns the title of this pane
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-06-27 10:14:15 +02:00
|
|
|
public function getTitle()
|
|
|
|
{
|
|
|
|
return $this->title;
|
|
|
|
}
|
|
|
|
|
2013-08-07 17:44:18 +02:00
|
|
|
/**
|
|
|
|
* Overwrite the title of this pane
|
|
|
|
*
|
|
|
|
* @param string $title The new title to use for this pane
|
2013-08-08 17:42:34 +02:00
|
|
|
*
|
2013-08-09 10:32:57 +02:00
|
|
|
* @return self
|
2013-08-07 17:44:18 +02:00
|
|
|
*/
|
2013-06-27 10:14:15 +02:00
|
|
|
public function setTitle($title)
|
|
|
|
{
|
|
|
|
$this->title = $title;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-08-07 17:44:18 +02:00
|
|
|
/**
|
|
|
|
* Return true if a component with the given title exists in this pane
|
|
|
|
*
|
|
|
|
* @param string $title The title of the component to check for existence
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2013-06-27 10:14:15 +02:00
|
|
|
public function hasComponent($title)
|
|
|
|
{
|
|
|
|
return array_key_exists($title, $this->components);
|
|
|
|
}
|
|
|
|
|
2013-08-07 17:44:18 +02:00
|
|
|
/**
|
|
|
|
* Return a component with the given name if existing
|
|
|
|
*
|
|
|
|
* @param string $title The title of the component to return
|
|
|
|
*
|
|
|
|
* @return Component The component with the given title
|
|
|
|
* @throws ProgrammingError If the component doesn't exist
|
|
|
|
*/
|
2013-06-27 10:14:15 +02:00
|
|
|
public function getComponent($title)
|
|
|
|
{
|
|
|
|
if ($this->hasComponent($title)) {
|
|
|
|
return $this->components[$title];
|
|
|
|
}
|
2013-08-07 18:10:39 +02:00
|
|
|
throw new ProgrammingError(sprintf('Trying to access invalid component: %s', $title));
|
2013-06-27 10:14:15 +02:00
|
|
|
}
|
|
|
|
|
2013-08-07 17:44:18 +02:00
|
|
|
/**
|
|
|
|
* Removes the component with the given title if it exists in this pane
|
|
|
|
*
|
|
|
|
* @param string $title The pane
|
|
|
|
* @return Pane $this
|
|
|
|
*/
|
2013-06-27 10:14:15 +02:00
|
|
|
public function removeComponent($title)
|
|
|
|
{
|
|
|
|
if ($this->hasComponent($title)) {
|
|
|
|
unset($this->components[$title]);
|
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-08-07 17:44:18 +02:00
|
|
|
/**
|
|
|
|
* Return all components added at this pane
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2013-06-27 10:14:15 +02:00
|
|
|
public function getComponents()
|
|
|
|
{
|
|
|
|
return $this->components;
|
|
|
|
}
|
2013-08-06 11:53:42 +02:00
|
|
|
|
2013-08-07 17:44:18 +02:00
|
|
|
/**
|
|
|
|
* @see Widget::render
|
|
|
|
*/
|
2013-08-07 17:40:18 +02:00
|
|
|
public function render(Zend_View_Abstract $view)
|
2013-08-06 11:53:42 +02:00
|
|
|
{
|
|
|
|
$html = PHP_EOL;
|
2013-08-07 17:40:18 +02:00
|
|
|
foreach ($this->components as $component) {
|
2013-08-06 11:53:42 +02:00
|
|
|
$html .= PHP_EOL.$component->render($view);
|
|
|
|
}
|
|
|
|
return $html;
|
|
|
|
}
|
|
|
|
|
2013-08-07 17:44:18 +02:00
|
|
|
/**
|
|
|
|
* Add a component to this pane, optionally creating it if $component is a string
|
|
|
|
*
|
2013-08-07 18:10:39 +02:00
|
|
|
* @param string|Component $component The component object or title
|
|
|
|
* (if a new component will be created)
|
2013-08-07 17:44:18 +02:00
|
|
|
* @param string|null $url An Url to be used when component is a string
|
|
|
|
*
|
2013-08-09 10:32:57 +02:00
|
|
|
* @return self
|
2013-08-07 17:44:18 +02:00
|
|
|
* @throws \Icinga\Exception\ConfigurationError
|
|
|
|
*/
|
2013-06-27 10:14:15 +02:00
|
|
|
public function addComponent($component, $url = null)
|
|
|
|
{
|
|
|
|
if ($component instanceof Component) {
|
2013-08-06 11:53:42 +02:00
|
|
|
$this->components[$component->getTitle()] = $component;
|
2013-06-27 10:14:15 +02:00
|
|
|
} elseif (is_string($component) && $url !== null) {
|
2013-08-06 11:53:42 +02:00
|
|
|
$this->components[$component] = new Component($component, $url, $this);
|
2013-08-07 18:10:39 +02:00
|
|
|
} else {
|
2013-08-08 17:42:34 +02:00
|
|
|
throw new ConfigurationError('Invalid component added: ' . $component);
|
2013-06-27 10:14:15 +02:00
|
|
|
}
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2013-08-07 17:44:18 +02:00
|
|
|
/**
|
|
|
|
* Return the ini representation of this pane as a string
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-06-27 10:14:15 +02:00
|
|
|
public function toIni()
|
|
|
|
{
|
2013-08-07 18:10:39 +02:00
|
|
|
if (empty($this->components)) {
|
2013-08-07 17:44:18 +02:00
|
|
|
return '';
|
2013-08-06 11:53:42 +02:00
|
|
|
}
|
2013-08-08 17:42:34 +02:00
|
|
|
$ini = '[' . $this->getName() . ']' . PHP_EOL.
|
|
|
|
'title = "' . $this->getTitle() . '"' . PHP_EOL;
|
2013-06-27 10:14:15 +02:00
|
|
|
|
|
|
|
foreach ($this->components as $title => $component) {
|
2013-08-06 11:53:42 +02:00
|
|
|
// component header
|
2013-08-08 17:42:34 +02:00
|
|
|
$ini .= '[' . $this->getName() . '.' . $title . ']' . PHP_EOL;
|
2013-08-06 11:53:42 +02:00
|
|
|
// component content
|
2013-08-08 17:42:34 +02:00
|
|
|
$ini .= $component->toIni() . PHP_EOL;
|
2013-06-27 10:14:15 +02:00
|
|
|
}
|
|
|
|
return $ini;
|
|
|
|
}
|
|
|
|
|
2013-08-07 17:44:18 +02:00
|
|
|
/**
|
|
|
|
* Create a new pane with the title $title from the given configuration
|
|
|
|
*
|
|
|
|
* @param $title The title for this pane
|
|
|
|
* @param Zend_Config $config The configuration to use for setup
|
|
|
|
*
|
|
|
|
* @return Pane
|
|
|
|
*/
|
2013-08-06 11:53:42 +02:00
|
|
|
public static function fromIni($title, Zend_Config $config)
|
2013-06-27 10:14:15 +02:00
|
|
|
{
|
2013-08-06 11:53:42 +02:00
|
|
|
$pane = new Pane($title);
|
|
|
|
if ($config->get('title', false)) {
|
|
|
|
$pane->setTitle($config->get('title'));
|
|
|
|
}
|
|
|
|
return $pane;
|
2013-06-27 10:14:15 +02:00
|
|
|
}
|
|
|
|
}
|