Merge pull request #3090 from Icinga/feature/configure-the-default-limit-13010
Add user preference for default number of items per page
This commit is contained in:
commit
17718d4863
|
@ -25,7 +25,7 @@ class LimiterControlForm extends Form
|
||||||
const DEFAULT_LIMIT = 50;
|
const DEFAULT_LIMIT = 50;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Selectable limits
|
* Selectable default limits
|
||||||
*
|
*
|
||||||
* @var int[]
|
* @var int[]
|
||||||
*/
|
*/
|
||||||
|
@ -71,7 +71,14 @@ class LimiterControlForm extends Form
|
||||||
*/
|
*/
|
||||||
public function setDefaultLimit($defaultLimit)
|
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;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,6 +97,13 @@ class LimiterControlForm extends Form
|
||||||
*/
|
*/
|
||||||
public function createElements(array $formData)
|
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(
|
$this->addElement(
|
||||||
'select',
|
'select',
|
||||||
'limit',
|
'limit',
|
||||||
|
@ -97,8 +111,8 @@ class LimiterControlForm extends Form
|
||||||
'autosubmit' => true,
|
'autosubmit' => true,
|
||||||
'escape' => false,
|
'escape' => false,
|
||||||
'label' => '#',
|
'label' => '#',
|
||||||
'multiOptions' => static::$limits,
|
'multiOptions' => $options,
|
||||||
'value' => $this->getRequest()->getUrl()->getParam('limit', $this->getDefaultLimit())
|
'value' => $pageSize
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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) {
|
if ($this->store) {
|
||||||
$this->addElement(
|
$this->addElement(
|
||||||
'submit',
|
'submit',
|
||||||
|
|
|
@ -22,6 +22,13 @@ use Icinga\Web\Widget\SortBox;
|
||||||
*/
|
*/
|
||||||
class Controller extends ModuleActionController
|
class Controller extends ModuleActionController
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* Cache for page size configured via user preferences
|
||||||
|
*
|
||||||
|
* @var false|int
|
||||||
|
*/
|
||||||
|
protected $userPageSize;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @see ActionController::init
|
* @see ActionController::init
|
||||||
*/
|
*/
|
||||||
|
@ -140,12 +147,34 @@ class Controller extends ModuleActionController
|
||||||
{
|
{
|
||||||
if (! $this->view->compact) {
|
if (! $this->view->compact) {
|
||||||
$this->view->limiter = new Limiter();
|
$this->view->limiter = new Limiter();
|
||||||
$this->view->limiter->setDefaultLimit($itemsPerPage);
|
$this->view->limiter->setDefaultLimit($this->getPageSize($itemsPerPage));
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
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
|
* 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)
|
protected function setupPaginationControl(QueryInterface $query, $itemsPerPage = 25, $pageNumber = 0)
|
||||||
{
|
{
|
||||||
$request = $this->getRequest();
|
$request = $this->getRequest();
|
||||||
$limit = $request->getParam('limit', $itemsPerPage);
|
$limit = $request->getParam('limit', $this->getPageSize($itemsPerPage));
|
||||||
$page = $request->getParam('page', $pageNumber);
|
$page = $request->getParam('page', $pageNumber);
|
||||||
$query->limit($limit, $page > 0 ? ($page - 1) * $limit : 0);
|
$query->limit($limit, $page > 0 ? ($page - 1) * $limit : 0);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue