diff --git a/application/forms/Control/LimiterControlForm.php b/application/forms/Control/LimiterControlForm.php index 98ce84527..bd90ed16d 100644 --- a/application/forms/Control/LimiterControlForm.php +++ b/application/forms/Control/LimiterControlForm.php @@ -25,7 +25,7 @@ class LimiterControlForm extends Form const DEFAULT_LIMIT = 50; /** - * Selectable limits + * Selectable default limits * * @var int[] */ @@ -71,7 +71,14 @@ class LimiterControlForm extends Form */ public function setDefaultLimit($defaultLimit) { - $this->defaultLimit = (int) $defaultLimit; + $defaultLimit = (int) $defaultLimit; + + if (! isset(static::$limits[$defaultLimit])) { + static::$limits[$defaultLimit] = $defaultLimit; + } + + $this->defaultLimit = $defaultLimit; + return $this; } @@ -90,6 +97,13 @@ class LimiterControlForm extends Form */ public function createElements(array $formData) { + $options = static::$limits; + $pageSize = (int) $this->getRequest()->getUrl()->getParam('limit', $this->getDefaultLimit()); + + if (! isset($options[$pageSize])) { + $options[$pageSize] = $pageSize; + } + $this->addElement( 'select', 'limit', @@ -97,8 +111,8 @@ class LimiterControlForm extends Form 'autosubmit' => true, 'escape' => false, 'label' => '#', - 'multiOptions' => static::$limits, - 'value' => $this->getRequest()->getUrl()->getParam('limit', $this->getDefaultLimit()) + 'multiOptions' => $options, + 'value' => $pageSize ) ); } diff --git a/application/forms/PreferenceForm.php b/application/forms/PreferenceForm.php index 3d76a7f24..5ec8bfa17 100644 --- a/application/forms/PreferenceForm.php +++ b/application/forms/PreferenceForm.php @@ -263,6 +263,16 @@ class PreferenceForm extends Form ) ); + $this->addElement( + 'number', + 'default_page_size', + array( + 'label' => $this->translate('Default page size'), + 'description' => $this->translate('Default number of items per page for list views'), + 'step' => 1 + ) + ); + if ($this->store) { $this->addElement( 'submit', diff --git a/library/Icinga/Web/Controller.php b/library/Icinga/Web/Controller.php index 4c44fcf03..7409dfa7e 100644 --- a/library/Icinga/Web/Controller.php +++ b/library/Icinga/Web/Controller.php @@ -22,6 +22,13 @@ use Icinga\Web\Widget\SortBox; */ class Controller extends ModuleActionController { + /** + * Cache for page size configured via user preferences + * + * @var false|int + */ + protected $userPageSize; + /** * @see ActionController::init */ @@ -140,12 +147,34 @@ class Controller extends ModuleActionController { if (! $this->view->compact) { $this->view->limiter = new Limiter(); - $this->view->limiter->setDefaultLimit($itemsPerPage); + $this->view->limiter->setDefaultLimit($this->getPageSize($itemsPerPage)); } return $this; } + /** + * Get the page size configured via user preferences or return the default value + * + * @param int $default + * + * @return int + */ + protected function getPageSize($default) + { + if ($this->userPageSize === null) { + $user = $this->Auth()->getUser(); + if ($user !== null) { + $pageSize = $user->getPreferences()->getValue('icingaweb', 'default_page_size', false); + $this->userPageSize = $pageSize !== false ? (int) $pageSize : false; + } else { + $this->userPageSize = false; + } + } + + return $this->userPageSize !== false ? $this->userPageSize : $default; + } + /** * Apply the given page limit and number on the given query and setup a paginator for it * @@ -161,7 +190,7 @@ class Controller extends ModuleActionController protected function setupPaginationControl(QueryInterface $query, $itemsPerPage = 25, $pageNumber = 0) { $request = $this->getRequest(); - $limit = $request->getParam('limit', $itemsPerPage); + $limit = $request->getParam('limit', $this->getPageSize($itemsPerPage)); $page = $request->getParam('page', $pageNumber); $query->limit($limit, $page > 0 ? ($page - 1) * $limit : 0);