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

View file

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

View file

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

View file

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

View file

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

View file

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