Fix backend pagination.

This commit is contained in:
flash 2018-05-16 23:06:14 +02:00
parent 48f68cfa2d
commit fc18634e09
6 changed files with 111 additions and 48 deletions

View file

@ -11,7 +11,7 @@
&__option {
transition: box-shadow .2s, background-color .2s;
&:hover {
&:not(&--disabled):hover {
background-color: #444;
box-shadow: 0 1px 5px 0 fade(#555, 80%);
}
@ -20,6 +20,10 @@
&:active {
background-color: #444;
}
&--disabled {
opacity: .5;
}
}
&__separator {
@ -37,6 +41,8 @@
min-width: 40px;
min-height: 40px;
&--first,
&--last,
&--prev,
&--next {
font-size: 2em;

View file

@ -7,22 +7,38 @@ $db = Database::connection();
$templating = $app->getTemplating();
$is_post_request = $_SERVER['REQUEST_METHOD'] === 'POST';
$queryQffset = (int)($_GET['o'] ?? 0);
$page_id = (int)($_GET['p'] ?? 1);
switch ($_GET['v'] ?? null) {
case 'listing':
$manage_users = $db->query('
$usersTake = 32;
$manageUsersCount = $db->query('
SELECT COUNT(`user_id`)
FROM `msz_users`
')->fetchColumn();
$getManageUsers = $db->prepare('
SELECT
u.`user_id`, u.`username`,
COALESCE(r.`role_colour`, CAST(0x40000000 AS UNSIGNED)) as `colour`
FROM `msz_users` as u
LEFT JOIN `msz_roles` as r
ON u.`display_role` = r.`role_id`
LIMIT 0, 32
')->fetchAll();
LIMIT :offset, :take
');
$getManageUsers->bindValue('offset', $queryQffset);
$getManageUsers->bindValue('take', $usersTake);
$manageUsers = $getManageUsers->execute() ? $getManageUsers->fetchAll() : [];
//$manage_users = UserV1::paginate(32, ['*'], 'p', $page_id);
$templating->vars(compact('manage_users'));
$templating->vars([
'manage_users' => $manageUsers,
'manage_users_count' => $manageUsersCount,
'manage_users_range' => $usersTake,
'manage_users_offset' => $queryQffset,
]);
echo $templating->render('@manage.users.listing');
break;
@ -59,7 +75,14 @@ switch ($_GET['v'] ?? null) {
break;
case 'roles':
$manage_roles = $db->query('
$rolesTake = 10;
$manageRolesCount = $db->query('
SELECT COUNT(`role_id`)
FROM `msz_roles`
')->fetchColumn();
$getManageRoles = $db->prepare('
SELECT
`role_id`, `role_colour`, `role_name`,
(
@ -68,11 +91,19 @@ switch ($_GET['v'] ?? null) {
WHERE ur.`role_id` = r.`role_id`
) as `users`
FROM `msz_roles` as r
LIMIT 0, 10
')->fetchAll();
LIMIT :offset, :take
');
$getManageRoles->bindValue('offset', $queryQffset);
$getManageRoles->bindValue('take', $rolesTake);
$manageRoles = $getManageRoles->execute() ? $getManageRoles->fetchAll() : [];
//$manage_roles = Role::paginate(10, ['*'], 'p', $page_id);
$templating->vars(compact('manage_roles'));
$templating->vars([
'manage_roles' => $manageRoles,
'manage_roles_count' => $manageRolesCount,
'manage_roles_range' => $rolesTake,
'manage_roles_offset' => $queryQffset,
]);
echo $templating->render('@manage.users.roles');
break;

View file

@ -14,60 +14,85 @@
{% endfor %}
{% endmacro %}
{% macro paginate(paginator, base_url, className, alwaysRender) %}
{% macro paginate(itemCount, itemRange, currentOffset, baseUrl, classPrefix, alwaysRender, useRanges, offsetParam, pageRange) %}
{% set alwaysRender = alwaysRender|default(false) %}
{% if alwaysRender or paginator.lastPage > 1 %}
{% set separator = '%3F' in base_url|default('')|url_encode ? '&' : '?' %}
{% set base_url = base_url ~ separator %}
{% if alwaysRender or itemCount > itemRange %}
{% set classPrefix = classPrefix|default('') %}
{% set separator = '%3F' in baseUrl|default('')|url_encode ? '&' : '?' %}
{% set baseUrl = baseUrl ~ separator %}
{% set pageCount = (itemCount / itemRange)|round(0, 'ceil') %}
{% set currentPage = currentOffset // itemRange %}
{% set useRanges = useRanges|default(true) %}
{% set offsetParam = offsetParam|default(useRanges ? 'o' : 'p') %}
{% set pageRange = pageRange|default(3) %}
<ul class="pagination{{ className is defined and className|length > 0 ? ' ' ~ className : '' }}">
{% if paginator.onFirstPage %}
<li class="pagination__option pagination__option--prev">
<span class="pagination__link pagination__link--prev">
<ul class="{{ classPrefix }}pagination">
{% if currentPage < 1 %}
<li class="{{ classPrefix }}pagination__option {{ classPrefix }}pagination__option--first {{ classPrefix }}pagination__option--disabled">
<span class="{{ classPrefix }}pagination__link {{ classPrefix }}pagination__link--first">
&laquo;
</span>
</li>
<li class="{{ classPrefix }}pagination__option {{ classPrefix }}pagination__option--prev {{ classPrefix }}pagination__option--disabled">
<span class="{{ classPrefix }}pagination__link {{ classPrefix }}pagination__link--prev">
&lsaquo;
</span>
</li>
{% else %}
<li class="pagination__option pagination__option--prev">
<a href="{{ base_url ~ paginator.previousPageUrl|slice(2) }}" class="pagination__link pagination__link--prev" rel="prev">
{% set firstUrl = baseUrl|slice(0, (baseUrl|length) - (separator|length)) %}
<li class="{{ classPrefix }}pagination__option {{ classPrefix }}pagination__option--first">
<a href="{{ firstUrl }}" class="{{ classPrefix }}pagination__link {{ classPrefix }}pagination__link--first" rel="first">
&laquo;
</a>
</li>
<li class="{{ classPrefix }}pagination__option {{ classPrefix }}pagination__option--prev">
<a href="{{ currentPage < 2 ? firstUrl : baseUrl ~ offsetParam ~ '=' ~ (useRanges ? ((currentPage - 1) * itemRange) : currentPage) }}" class="{{ classPrefix }}pagination__link {{ classPrefix }}pagination__link--prev" rel="prev">
&lsaquo;
</a>
</li>
{% endif %}
<li class="pagination__separator"></li>
<li class="{{ classPrefix }}pagination__separator"></li>
{% from _self import pagination_segment %}
{% set url_window = paginator|create_pagination %}
{% set paginationStart = currentPage - pageRange %}
{% set paginationStop = currentPage + pageRange %}
{% if url_window.first is iterable %}
{{ pagination_segment(url_window.first, base_url, paginator.currentPage) }}
<li class="pagination__separator"></li>
{% endif %}
{% if url_window.slider is iterable %}
{{ pagination_segment(url_window.slider, base_url, paginator.currentPage) }}
<li class="pagination__separator"></li>
{% endif %}
{% if url_window.last is iterable %}
{{ pagination_segment(url_window.last, base_url, paginator.currentPage) }}
<li class="pagination__separator"></li>
{% endif %}
{% if paginator.hasMorePages %}
<li class="pagination__option pagination__option--next">
<a href="{{ base_url ~ paginator.nextPageUrl|slice(2) }}" class="pagination__link pagination__link--next" rel="next">
&raquo;
{% for i in paginationStart..paginationStop %}
{% if i >= 0 and i < pageCount %}
<li class="{{ classPrefix }}pagination__option{{ currentPage == i ? ' ' ~ classPrefix ~ 'pagination__option--active' : '' }}">
<a href="{{ baseUrl ~ offsetParam ~ '=' ~ (useRanges ? i * itemRange : i + 1) }}" class="{{ classPrefix }}pagination__link{{ currentPage == i ? ' ' ~ classPrefix ~ 'pagination__link--active' : '' }}">
{{ i + 1 }}
</a>
</li>
{% else %}
<li class="pagination__option pagination__option--next">
<span class="pagination__link pagination__link--next">
{% endif %}
{% endfor %}
<li class="{{ classPrefix }}pagination__separator"></li>
{% if currentPage >= pageCount - 1 %}
<li class="{{ classPrefix }}pagination__option {{ classPrefix }}pagination__option--next {{ classPrefix }}pagination__option--disabled">
<span class="{{ classPrefix }}pagination__link {{ classPrefix }}pagination__link--next">
&rsaquo;
</span>
</li>
<li class="{{ classPrefix }}pagination__option {{ classPrefix }}pagination__option--last {{ classPrefix }}pagination__option--disabled">
<span class="{{ classPrefix }}pagination__link {{ classPrefix }}pagination__link--last">
&raquo;
</span>
</li>
{% else %}
<li class="{{ classPrefix }}pagination__option {{ classPrefix }}pagination__option--next">
<a href="{{ baseUrl ~ offsetParam ~ '=' ~ (useRanges ? ((currentPage + 1) * itemRange) : currentPage + 2) }}" class="{{ classPrefix }}pagination__link {{ classPrefix }}pagination__link--next" rel="next">
&rsaquo;
</a>
</li>
<li class="{{ classPrefix }}pagination__option {{ classPrefix }}pagination__option--last">
<a href="{{ baseUrl ~ offsetParam ~ '=' ~ (useRanges ? ((pageCount - 1) * itemRange) : pageCount) }}" class="{{ classPrefix }}pagination__link {{ classPrefix }}pagination__link--last" rel="last">
&raquo;
</a>
</li>
{% endif %}
</ul>
{% endif %}

View file

@ -16,6 +16,6 @@
</div>
<div class="container container--center">
{# paginate(manage_users, '?v=listing') #}
{{ paginate(manage_users_count, manage_users_range, manage_users_offset, '?v=listing') }}
</div>
{% endblock %}

View file

@ -18,6 +18,6 @@
</div>
<div class="container container--center">
{# paginate(manage_roles, '?v=roles') #}
{{ paginate(manage_roles_count, manage_roles_range, manage_roles_offset, '?v=roles') }}
</div>
{% endblock %}

View file

@ -16,7 +16,7 @@
</ul>
{% endmacro %}
{% macro paginate(itemCount, itemRange, currentOffset, baseUrl, classPrefix, alwaysRender, useRanges, offsetParam) %}
{% macro paginate(itemCount, itemRange, currentOffset, baseUrl, classPrefix, alwaysRender, useRanges, offsetParam, pageRange) %}
{% set alwaysRender = alwaysRender|default(false) %}
{% if alwaysRender or itemCount > itemRange %}
@ -27,6 +27,7 @@
{% set currentPage = currentOffset // itemRange %}
{% set useRanges = useRanges|default(true) %}
{% set offsetParam = offsetParam|default(useRanges ? 'o' : 'p') %}
{% set pageRange = pageRange|default(3) %}
<ul class="{{ classPrefix }}pagination">
{% if currentPage < 1 %}
@ -57,8 +58,8 @@
<li class="{{ classPrefix }}pagination__separator"></li>
{% set paginationStart = currentPage - 3 %}
{% set paginationStop = currentPage + 3 %}
{% set paginationStart = currentPage - pageRange %}
{% set paginationStop = currentPage + pageRange %}
{% for i in paginationStart..paginationStop %}
{% if i >= 0 and i < pageCount %}