lib: Use navigation classes in the limiter control

refs #5543
This commit is contained in:
Eric Lippmann 2015-09-25 13:36:28 +02:00
parent 823a2cc8ea
commit 7ff74ae36a
1 changed files with 85 additions and 72 deletions

View File

@ -3,98 +3,111 @@
namespace Icinga\Web\Widget;
use Icinga\Web\Navigation\Navigation;
use Icinga\Web\Navigation\NavigationItem;
use Icinga\Web\Navigation\NavigationRenderer;
use Icinga\Web\Url;
/**
* Limiter
* Limiter control
*/
class Limiter extends AbstractWidget
{
/**
* The url
* CSS class for the limiter widget
*
* @var Url
* @var string
*/
private $url;
const CSS_CLASS_LIMITER = 'limiter-control';
private $max;
/**
* Default limit
*
* @var int
*/
const DEFAULT_LIMIT = 50;
private $pages;
/**
* Selectable limits
*
* @var int[]
*/
public static $limits = array(
10 => '10',
25 => '25',
50 => '50',
100 => '100',
500 => '500'
);
private $default;
/**
* Default limit for this instance
*
* @var int|null
*/
protected $defaultLimit;
public function setUrl(Url $url)
/**
* Get the default limit
*
* @return int
*/
public function getDefaultLimit()
{
$this->url = $url;
return $this;
}
public function setCurrentPageCount($pages)
{
$this->pages = $pages;
return $this;
}
public function setMaxLimit($max)
{
$this->max = $max;
return $this;
}
public function setDefaultLimit($limit)
{
$this->default = $limit;
return $this->defaultLimit !== null ? $this->defaultLimit : static::DEFAULT_LIMIT;
}
/**
* Set the default limit
*
* @param int $defaultLimit
*
* @return $this
*/
public function setDefaultLimit($defaultLimit)
{
$this->defaultLimit = (int) $defaultLimit;
return $this;
}
/**
* {@inheritdoc}
*/
public function render()
{
if ($this->url === null) {
$this->url = Url::fromRequest();
}
$currentLimit = (int) $this->url->getParam('limit', $this->default);
$availableLimits = array(
10 => '10',
25 => '25',
50 => '50',
100 => '100',
500 => '500'
);
if ($currentLimit === 0) {
$availableLimits[0] = t('all');
}
// if ($this->pages === 1 && $currentLimit === 10) return '';
$limits = array();
$view = $this->view();
$gotCurrent = false;
foreach ($availableLimits as $limit => $caption) {
if ($gotCurrent) {
if ($this->pages === 1) {
// break;
}
}
if ($this->max !== null && ($limit === 0 || $limit > $this->max)) {
//echo "$limit > $this->max"; break;
}
if ($limit === $currentLimit) {
$gotCurrent = true;
$limits[] = $caption;
} else {
$limits[] = $view->qlink(
$caption,
$this->url->setParam('limit', $limit),
null,
array(
'title' => sprintf($view->translate('Limit each page to a maximum of %u rows'), $caption)
$url = Url::fromRequest();
$activeLimit = (int) $url->getParam('limit', $this->getDefaultLimit());
$navigation = new Navigation();
$navigation->setLayout(Navigation::LAYOUT_TABS);
foreach (static::$limits as $limit => $label) {
$navigationItem = new NavigationItem();
$navigationItem
->setActive($activeLimit === $limit)
->setAttribute(
'title',
sprintf(
t('Show %u rows on this page'),
$limit
)
);
}
)
->setId($limit)
->setLabel($label)
->setUrl($url->with(array('limit' => $limit)));
$navigation->addItem($navigationItem);
}
if (empty($limits)) return '';
return '<span class="widgetLimiter">' . implode(' ', $limits) . '</span>';
if ($activeLimit === 0) {
$navigationItem = new NavigationItem();
$navigationItem
->setActive(true)
->setAttribute('title', t('Show all items on this page'))
->setId(0)
->setLabel(t('all'));
$navigation->addItem($navigationItem);
}
$navigationRenderer = new NavigationRenderer($navigation);
$navigationRenderer
->setCssClass(static::CSS_CLASS_LIMITER)
->setHeading(t('Limiter'));
return $navigationRenderer->render();
}
}