From 0abbe2583b5cba27e57eb8a642c904f09c871d51 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Thu, 12 Nov 2015 13:04:52 +0100 Subject: [PATCH] Introduce a form the limiter control refs #5543 --- .../forms/Control/LimiterControlForm.php | 113 ++++++++++++++++++ library/Icinga/Web/Widget/Limiter.php | 75 ++---------- 2 files changed, 122 insertions(+), 66 deletions(-) create mode 100644 application/forms/Control/LimiterControlForm.php diff --git a/application/forms/Control/LimiterControlForm.php b/application/forms/Control/LimiterControlForm.php new file mode 100644 index 0000000000..e665ab21aa --- /dev/null +++ b/application/forms/Control/LimiterControlForm.php @@ -0,0 +1,113 @@ + '10', + 25 => '25', + 50 => '50', + 100 => '100', + 500 => '500' + ); + + /** + * Default limit for this instance + * + * @var int|null + */ + protected $defaultLimit; + + /** + * {@inheritdoc} + */ + public function init() + { + $this->setAttrib('class', static::CSS_CLASS_LIMITER); + } + + /** + * Get the default limit + * + * @return int + */ + public function getDefaultLimit() + { + 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 getRedirectUrl() + { + return $this->getRequest()->getUrl()->setParam('limit', $this->getElement('limit')->getValue()); + } + + /** + * {@inheritdoc} + */ + public function createElements(array $formData) + { + $this->addElement( + 'select', + 'limit', + array( + 'autosubmit' => true, + 'escape' => false, + 'label' => '#', + 'multiOptions' => static::$limits, + 'value' => $this->getRequest()->getUrl()->getParam('limit', $this->getDefaultLimit()) + ) + ); + } + + /** + * Limiter control is always successful + * + * @return bool + */ + public function onSuccess() + { + return true; + } +} diff --git a/library/Icinga/Web/Widget/Limiter.php b/library/Icinga/Web/Widget/Limiter.php index 365e540582..b4df838652 100644 --- a/library/Icinga/Web/Widget/Limiter.php +++ b/library/Icinga/Web/Widget/Limiter.php @@ -3,41 +3,13 @@ namespace Icinga\Web\Widget; -use Icinga\Web\Navigation\Navigation; -use Icinga\Web\Url; +use Icinga\Forms\Control\LimiterControlForm; /** - * Limiter control + * Limiter control widget */ class Limiter extends AbstractWidget { - /** - * CSS class for the limiter widget - * - * @var string - */ - const CSS_CLASS_LIMITER = 'limiter-control'; - - /** - * Default limit - * - * @var int - */ - const DEFAULT_LIMIT = 50; - - /** - * Selectable limits - * - * @var int[] - */ - public static $limits = array( - 10 => '10', - 25 => '25', - 50 => '50', - 100 => '100', - 500 => '500' - ); - /** * Default limit for this instance * @@ -48,11 +20,11 @@ class Limiter extends AbstractWidget /** * Get the default limit * - * @return int + * @return int|null */ public function getDefaultLimit() { - return $this->defaultLimit !== null ? $this->defaultLimit : static::DEFAULT_LIMIT; + return $this->defaultLimit; } /** @@ -73,39 +45,10 @@ public function setDefaultLimit($defaultLimit) */ public function render() { - $url = Url::fromRequest(); - $activeLimit = (int) $url->getParam('limit', $this->getDefaultLimit()); - $navigation = new Navigation(); - $navigation->setLayout(Navigation::LAYOUT_TABS); - foreach (static::$limits as $limit => $label) { - $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) - ) - ); - } - - if ($activeLimit === 0) { - $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 - ) - ); - } - - return $navigation - ->getRenderer() - ->setCssClass(static::CSS_CLASS_LIMITER) - ->setHeading(t('Limiter')) - ->render(); + $control = new LimiterControlForm(); + $control + ->setDefaultLimit($this->defaultLimit) + ->handleRequest(); + return (string)$control; } }