2014-06-20 13:48:17 +02:00
|
|
|
<?php
|
2015-02-04 10:46:36 +01:00
|
|
|
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
|
2014-06-20 13:48:17 +02:00
|
|
|
|
|
|
|
namespace Icinga\Web\Widget;
|
|
|
|
|
2015-09-25 13:36:28 +02:00
|
|
|
use Icinga\Web\Navigation\Navigation;
|
2014-06-20 13:48:17 +02:00
|
|
|
use Icinga\Web\Url;
|
|
|
|
|
|
|
|
/**
|
2015-09-25 13:36:28 +02:00
|
|
|
* Limiter control
|
2014-06-20 13:48:17 +02:00
|
|
|
*/
|
|
|
|
class Limiter extends AbstractWidget
|
|
|
|
{
|
|
|
|
/**
|
2015-09-25 13:36:28 +02:00
|
|
|
* CSS class for the limiter widget
|
2014-06-20 13:48:17 +02:00
|
|
|
*
|
2015-09-25 13:36:28 +02:00
|
|
|
* @var string
|
2014-06-20 13:48:17 +02:00
|
|
|
*/
|
2015-09-25 13:36:28 +02:00
|
|
|
const CSS_CLASS_LIMITER = 'limiter-control';
|
2014-06-20 13:48:17 +02:00
|
|
|
|
2015-09-25 13:36:28 +02:00
|
|
|
/**
|
|
|
|
* Default limit
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
const DEFAULT_LIMIT = 50;
|
2015-05-15 15:22:22 +02:00
|
|
|
|
2015-09-25 13:36:28 +02:00
|
|
|
/**
|
|
|
|
* Selectable limits
|
|
|
|
*
|
|
|
|
* @var int[]
|
|
|
|
*/
|
|
|
|
public static $limits = array(
|
|
|
|
10 => '10',
|
|
|
|
25 => '25',
|
|
|
|
50 => '50',
|
|
|
|
100 => '100',
|
|
|
|
500 => '500'
|
|
|
|
);
|
2014-06-20 13:48:17 +02:00
|
|
|
|
2015-09-25 13:36:28 +02:00
|
|
|
/**
|
|
|
|
* Default limit for this instance
|
|
|
|
*
|
|
|
|
* @var int|null
|
|
|
|
*/
|
|
|
|
protected $defaultLimit;
|
2014-06-20 13:48:17 +02:00
|
|
|
|
2015-09-25 13:36:28 +02:00
|
|
|
/**
|
|
|
|
* Get the default limit
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getDefaultLimit()
|
2014-06-20 13:48:17 +02:00
|
|
|
{
|
2015-09-25 13:36:28 +02:00
|
|
|
return $this->defaultLimit !== null ? $this->defaultLimit : static::DEFAULT_LIMIT;
|
2014-06-20 13:48:17 +02:00
|
|
|
}
|
|
|
|
|
2015-09-25 13:36:28 +02:00
|
|
|
/**
|
|
|
|
* Set the default limit
|
|
|
|
*
|
|
|
|
* @param int $defaultLimit
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
|
|
|
public function setDefaultLimit($defaultLimit)
|
2015-05-15 15:22:22 +02:00
|
|
|
{
|
2015-09-25 13:36:28 +02:00
|
|
|
$this->defaultLimit = (int) $defaultLimit;
|
2015-05-15 15:22:22 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2015-09-25 13:36:28 +02:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
2014-06-20 13:48:17 +02:00
|
|
|
public function render()
|
|
|
|
{
|
2015-09-25 13:36:28 +02:00
|
|
|
$url = Url::fromRequest();
|
|
|
|
$activeLimit = (int) $url->getParam('limit', $this->getDefaultLimit());
|
|
|
|
$navigation = new Navigation();
|
|
|
|
$navigation->setLayout(Navigation::LAYOUT_TABS);
|
|
|
|
foreach (static::$limits as $limit => $label) {
|
2015-10-05 10:54:43 +02:00
|
|
|
$navigation->addItem(
|
|
|
|
'limit_' . $limit,
|
|
|
|
array(
|
|
|
|
'priority' => $limit,
|
|
|
|
'label' => $label,
|
|
|
|
'active' => $activeLimit === $limit,
|
|
|
|
'url' => $url->with(array('limit' => $limit)),
|
|
|
|
'title' => sprintf(t('Show %u rows on this page'), $limit)
|
2015-09-25 13:36:28 +02:00
|
|
|
)
|
2015-10-05 10:54:43 +02:00
|
|
|
);
|
2014-06-20 13:48:17 +02:00
|
|
|
}
|
2015-10-05 10:54:43 +02:00
|
|
|
|
2015-09-25 13:36:28 +02:00
|
|
|
if ($activeLimit === 0) {
|
2015-10-05 10:54:43 +02:00
|
|
|
$navigation->addItem(
|
|
|
|
'limit_0',
|
|
|
|
array(
|
|
|
|
'active' => true,
|
|
|
|
'label' => t('all'),
|
|
|
|
'title' => t('Show all items on this page'),
|
|
|
|
'priority' => max(array_keys(static::$limits)) + 1
|
|
|
|
)
|
|
|
|
);
|
2015-09-25 13:36:28 +02:00
|
|
|
}
|
2015-10-05 10:54:43 +02:00
|
|
|
|
2015-09-27 13:29:22 +02:00
|
|
|
return $navigation
|
|
|
|
->getRenderer()
|
2015-09-25 13:36:28 +02:00
|
|
|
->setCssClass(static::CSS_CLASS_LIMITER)
|
2015-09-27 13:29:22 +02:00
|
|
|
->setHeading(t('Limiter'))
|
|
|
|
->render();
|
2014-06-20 13:48:17 +02:00
|
|
|
}
|
|
|
|
}
|