Skip to content

Commit

Permalink
lib: Use navigation classes in the limiter control
Browse files Browse the repository at this point in the history
refs #5543
  • Loading branch information
lippserd committed Sep 25, 2015
1 parent 823a2cc commit 7ff74ae
Showing 1 changed file with 82 additions and 69 deletions.
151 changes: 82 additions & 69 deletions library/Icinga/Web/Widget/Limiter.php
Expand Up @@ -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;

private $pages;

private $default;
/**
* Default limit
*
* @var int
*/
const DEFAULT_LIMIT = 50;

public function setUrl(Url $url)
{
$this->url = $url;
return $this;
}
/**
* Selectable limits
*
* @var int[]
*/
public static $limits = array(
10 => '10',
25 => '25',
50 => '50',
100 => '100',
500 => '500'
);

public function setCurrentPageCount($pages)
{
$this->pages = $pages;
return $this;
}
/**
* Default limit for this instance
*
* @var int|null
*/
protected $defaultLimit;

public function setMaxLimit($max)
/**
* Get the default limit
*
* @return int
*/
public function getDefaultLimit()
{
$this->max = $max;
return $this;
return $this->defaultLimit !== null ? $this->defaultLimit : static::DEFAULT_LIMIT;
}

public function setDefaultLimit($limit)
/**
* Set the default limit
*
* @param int $defaultLimit
*
* @return $this
*/
public function setDefaultLimit($defaultLimit)
{
$this->default = $limit;
$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();
}
}

0 comments on commit 7ff74ae

Please sign in to comment.