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;
|
|
|
|
use Icinga\Web\Navigation\NavigationItem;
|
|
|
|
use Icinga\Web\Navigation\NavigationRenderer;
|
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-09-27 13:22:43 +02:00
|
|
|
$navigationItem = new NavigationItem($limit);
|
2015-09-25 13:36:28 +02:00
|
|
|
$navigationItem
|
|
|
|
->setActive($activeLimit === $limit)
|
|
|
|
->setAttribute(
|
|
|
|
'title',
|
|
|
|
sprintf(
|
|
|
|
t('Show %u rows on this page'),
|
|
|
|
$limit
|
2014-06-20 13:48:17 +02:00
|
|
|
)
|
2015-09-25 13:36:28 +02:00
|
|
|
)
|
|
|
|
->setLabel($label)
|
|
|
|
->setUrl($url->with(array('limit' => $limit)));
|
|
|
|
$navigation->addItem($navigationItem);
|
2014-06-20 13:48:17 +02:00
|
|
|
}
|
2015-09-25 13:36:28 +02:00
|
|
|
if ($activeLimit === 0) {
|
2015-09-27 13:22:43 +02:00
|
|
|
$navigationItem = new NavigationItem(0);
|
2015-09-25 13:36:28 +02:00
|
|
|
$navigationItem
|
|
|
|
->setActive(true)
|
|
|
|
->setAttribute('title', t('Show all items on this page'))
|
|
|
|
->setLabel(t('all'));
|
|
|
|
$navigation->addItem($navigationItem);
|
|
|
|
}
|
|
|
|
$navigationRenderer = new NavigationRenderer($navigation);
|
|
|
|
$navigationRenderer
|
|
|
|
->setCssClass(static::CSS_CLASS_LIMITER)
|
|
|
|
->setHeading(t('Limiter'));
|
|
|
|
return $navigationRenderer->render();
|
2014-06-20 13:48:17 +02:00
|
|
|
}
|
|
|
|
}
|