Add user preference for default number of items per page

refs #2859

Signed-off-by: Eric Lippmann <eric.lippmann@icinga.com>
This commit is contained in:
Alexander A. Klimov 2016-12-14 18:25:18 +01:00 committed by Eric Lippmann
parent 166f27b947
commit bdcb6389d8
3 changed files with 59 additions and 6 deletions

View File

@ -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
)
);
}

View File

@ -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',

View File

@ -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);