2013-06-07 11:44:37 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Web\Widget;
|
|
|
|
|
|
|
|
use Icinga\Exception\ProgrammingError;
|
2013-08-07 17:40:18 +02:00
|
|
|
use Zend_View_Abstract;
|
2013-06-07 11:44:37 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A single tab, usually used through the tabs widget
|
|
|
|
*
|
|
|
|
* Will generate an <li> list item, with an optional link and icon
|
|
|
|
*
|
|
|
|
* @property string $name Tab identifier
|
|
|
|
* @property string $title Tab title
|
|
|
|
* @property string $icon Icon URL, preferrably relative to the Icinga
|
|
|
|
* base URL
|
|
|
|
* @property string $url Action URL, preferrably relative to the Icinga
|
|
|
|
* base URL
|
|
|
|
* @property string $urlParams Action URL Parameters
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2013 Icinga-Web Team <info@icinga.org>
|
|
|
|
* @author Icinga-Web Team <info@icinga.org>
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
|
|
|
|
*/
|
2013-08-06 18:00:33 +02:00
|
|
|
class Tab implements Widget
|
2013-06-07 11:44:37 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Whether this tab is currently active
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
2013-08-06 18:00:33 +02:00
|
|
|
private $active = false;
|
2013-06-07 11:44:37 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Default values for widget properties
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2013-08-06 18:00:33 +02:00
|
|
|
private $name = null;
|
|
|
|
|
2013-08-07 17:40:18 +02:00
|
|
|
/**
|
|
|
|
* The title displayed for this tab
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2013-08-06 18:00:33 +02:00
|
|
|
private $title = '';
|
2013-08-07 17:40:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The Url this tab points to
|
|
|
|
*
|
|
|
|
* @var string|null
|
|
|
|
*/
|
2013-08-06 18:00:33 +02:00
|
|
|
private $url = null;
|
2013-08-07 17:40:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The parameters for this tab's Url
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2013-08-06 18:00:33 +02:00
|
|
|
private $urlParams = array();
|
2013-08-07 17:40:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The icon image to use for this tab or null if none
|
|
|
|
*
|
|
|
|
* @var string|null
|
|
|
|
*/
|
2013-08-06 18:00:33 +02:00
|
|
|
private $icon = null;
|
2013-08-07 17:40:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The icon class to use if $icon is null
|
|
|
|
*
|
|
|
|
* @var string|null
|
|
|
|
*/
|
2013-08-06 11:53:42 +02:00
|
|
|
private $iconCls = null;
|
2013-08-06 18:00:33 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2013-08-07 17:40:18 +02:00
|
|
|
* Sets an icon image for this tab
|
|
|
|
*
|
|
|
|
* @param string $icon The url of the image to use
|
2013-08-06 18:00:33 +02:00
|
|
|
*/
|
|
|
|
public function setIcon($icon)
|
|
|
|
{
|
|
|
|
$this->icon = $icon;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-08-07 17:40:18 +02:00
|
|
|
* Set's an icon class that will be used in an <i> tag if no icon image is set
|
|
|
|
*
|
|
|
|
* @param string $iconCls The CSS class of the icon to use
|
2013-08-06 18:00:33 +02:00
|
|
|
*/
|
2013-08-06 11:53:42 +02:00
|
|
|
public function setIconCls($iconCls)
|
|
|
|
{
|
|
|
|
$this->iconCls = $iconCls;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-06 18:00:33 +02:00
|
|
|
/**
|
|
|
|
* @param mixed $name
|
|
|
|
*/
|
|
|
|
public function setName($name)
|
|
|
|
{
|
|
|
|
$this->name = $name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getName()
|
|
|
|
{
|
|
|
|
return $this->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed $title
|
|
|
|
*/
|
|
|
|
public function setTitle($title)
|
|
|
|
{
|
|
|
|
$this->title = $title;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-08-07 17:40:18 +02:00
|
|
|
* Set the Url this tab points to
|
|
|
|
*
|
|
|
|
* @param string $url The Url to use for this tab
|
2013-08-06 18:00:33 +02:00
|
|
|
*/
|
|
|
|
public function setUrl($url)
|
|
|
|
{
|
|
|
|
$this->url = $url;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-06 11:53:42 +02:00
|
|
|
/**
|
2013-08-07 17:40:18 +02:00
|
|
|
* Set the parameters to be set for this tabs Url
|
|
|
|
*
|
|
|
|
* @param array $url The Url parameters to set
|
2013-08-06 11:53:42 +02:00
|
|
|
*/
|
|
|
|
public function setUrlParams(array $urlParams)
|
|
|
|
{
|
|
|
|
$this->urlParams = $urlParams;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-08-07 17:40:18 +02:00
|
|
|
* Create a new Tab with the given properties
|
|
|
|
*
|
|
|
|
* Allowed properties are all properties for which a setter exists
|
|
|
|
*
|
|
|
|
* @param array $properties An array of properties
|
2013-08-06 11:53:42 +02:00
|
|
|
*/
|
2013-08-06 18:00:33 +02:00
|
|
|
public function __construct(array $properties = array())
|
|
|
|
{
|
2013-08-07 18:10:39 +02:00
|
|
|
foreach ($properties as $name => $value) {
|
2013-08-06 18:00:33 +02:00
|
|
|
$setter = 'set'.ucfirst($name);
|
|
|
|
if (method_exists($this, $setter)) {
|
|
|
|
$this->$setter($value);
|
|
|
|
}
|
|
|
|
}
|
2013-06-07 11:44:37 +02:00
|
|
|
if ($this->name === null) {
|
2013-08-06 18:00:33 +02:00
|
|
|
throw new ProgrammingError('Cannot create a nameless tab');
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set this tab active (default) or inactive
|
|
|
|
*
|
|
|
|
* This is usually done through the tabs container widget, therefore it
|
|
|
|
* is not a good idea to directly call this function
|
|
|
|
*
|
|
|
|
* @param bool $active Whether the tab should be active
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function setActive($active = true)
|
|
|
|
{
|
2013-06-27 10:14:15 +02:00
|
|
|
$this->active = (bool) $active;
|
2013-06-07 11:44:37 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2013-08-07 17:40:18 +02:00
|
|
|
* @see Widget::render()
|
2013-06-07 11:44:37 +02:00
|
|
|
*/
|
2013-08-07 17:40:18 +02:00
|
|
|
public function render(Zend_View_Abstract $view)
|
2013-06-07 11:44:37 +02:00
|
|
|
{
|
2013-08-07 17:40:18 +02:00
|
|
|
$class = $this->active ? ' class="active"' : '';
|
2013-06-07 11:44:37 +02:00
|
|
|
$caption = $this->title;
|
|
|
|
if ($this->icon !== null) {
|
2013-08-07 18:10:39 +02:00
|
|
|
$caption = $view->img(
|
|
|
|
$this->icon,
|
|
|
|
array(
|
2013-08-06 18:00:33 +02:00
|
|
|
'width' => 16,
|
|
|
|
'height' => 16
|
2013-08-07 18:10:39 +02:00
|
|
|
)
|
|
|
|
) . ' ' . $caption;
|
|
|
|
} elseif ($this->iconCls !== null) {
|
2013-08-06 11:53:42 +02:00
|
|
|
$caption = '<i class="icon-'.$this->iconCls.'"></i> ' . $caption;
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
|
|
|
if ($this->url !== null) {
|
|
|
|
$tab = $view->qlink(
|
|
|
|
$caption,
|
|
|
|
$this->url,
|
|
|
|
$this->urlParams,
|
|
|
|
array('quote' => false)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$tab = $caption;
|
|
|
|
}
|
2013-08-06 18:00:33 +02:00
|
|
|
|
2013-08-07 17:40:18 +02:00
|
|
|
return '<li '.$class.'>'.$tab.'</li>'.PHP_EOL;
|
2013-06-07 11:44:37 +02:00
|
|
|
}
|
|
|
|
}
|