Skip to content

Commit

Permalink
add modvar for items per page to ZAuth mappings list
Browse files Browse the repository at this point in the history
refs #2915

also remove legacy pager and user doctrine pager with custom wrapper
  • Loading branch information
craigh committed Mar 30, 2020
1 parent 68d6775 commit b513d71
Show file tree
Hide file tree
Showing 13 changed files with 411 additions and 43 deletions.
173 changes: 173 additions & 0 deletions src/Zikula/CoreBundle/Doctrine/Paginator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Zikula package.
*
* Copyright Zikula Foundation - https://ziku.la/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Zikula\Bundle\CoreBundle\Doctrine;

use Doctrine\ORM\QueryBuilder as DoctrineQueryBuilder;
use Doctrine\ORM\Tools\Pagination\CountWalker;
use Doctrine\ORM\Tools\Pagination\Paginator as DoctrinePaginator;

/**
* @author Javier Eguiluz <[email protected]>
* Most of this file is copied from https://github.com/javiereguiluz/symfony-demo/blob/master/src/Pagination/Paginator.php
*
* usage:
* in Repository class:
* return (new Paginator($qb))->paginate($page);
* in controller:
* return $this->render('blog/index.'.$_format.'.twig', [
* 'paginator' => $latestPosts,
* ]);
* results in template {% for post in paginator.results %}
* include template: {{ include(paginator.template) }}
*/
class Paginator
{
private const PAGE_SIZE = 25;

private $queryBuilder;

private $currentPage;

private $pageSize;

private $results;

private $numResults;

private $route;

private $routeParameters;

private $template = '@Core/Paginator/Paginator.html.twig';

public function __construct(DoctrineQueryBuilder $queryBuilder, int $pageSize = self::PAGE_SIZE)
{
$this->queryBuilder = $queryBuilder;
$this->pageSize = $pageSize;
}

public function paginate(int $page = 1): self
{
$this->currentPage = max(1, $page);
$firstResult = ($this->currentPage - 1) * $this->pageSize;

$query = $this->queryBuilder
->setFirstResult($firstResult)
->setMaxResults($this->pageSize)
->getQuery();

if (0 === \count($this->queryBuilder->getDQLPart('join'))) {
$query->setHint(CountWalker::HINT_DISTINCT, false);
}

$paginator = new DoctrinePaginator($query, true);

$useOutputWalkers = \count($this->queryBuilder->getDQLPart('having') ?: []) > 0;
$paginator->setUseOutputWalkers($useOutputWalkers);

$this->results = $paginator->getIterator();
$this->numResults = $paginator->count();

return $this;
}

public function getCurrentPage(): int
{
return $this->currentPage;
}

public function getLastPage(): int
{
return (int) ceil($this->numResults / $this->pageSize);
}

public function getPageSize(): int
{
return $this->pageSize;
}

public function hasPreviousPage(): bool
{
return $this->currentPage > 1;
}

public function getPreviousPage(): int
{
return max(1, $this->currentPage - 1);
}

public function hasNextPage(): bool
{
return $this->currentPage < $this->getLastPage();
}

public function getNextPage(): int
{
return min($this->getLastPage(), $this->currentPage + 1);
}

public function hasToPaginate(): bool
{
return $this->numResults > $this->pageSize;
}

public function getNumResults(): int
{
return $this->numResults;
}

public function getResults(): \Traversable
{
return $this->results;
}

public function setRoute(string $route): self
{
$this->route = $route;

return $this;
}

public function getRoute(): string
{
return $this->route;
}

public function setRouteParameters(array $parameters): self
{
$this->routeParameters = $parameters;

return $this;
}

public function setRouteParameter(string $name, string $value): void
{
$this->routeParameters[$name] = $value;
}

public function getRouteParameters(): array
{
return $this->routeParameters;
}

public function setTemplate(string $templateName): void
{
$this->template = $templateName;
}

public function getTemplate(): string
{
return $this->template;
}
}
89 changes: 89 additions & 0 deletions src/Zikula/CoreBundle/Filter/AlphaFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Zikula package.
*
* Copyright Zikula Foundation - https://ziku.la/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Zikula\Bundle\CoreBundle\Filter;

class AlphaFilter
{
private $currentLetter;

private $route;

private $routeParameters;

private $template = '@Core/Filter/AlphaFilter.html.twig';

private $includeNumbers = false;

public function __construct(string $route, array $routeParameters = [], $currentLetter = 'a', $includeNumbers = false)
{
$this->route = $route;
$this->routeParameters = $routeParameters;
$this->currentLetter = $currentLetter;
$this->includeNumbers = $includeNumbers;
}

public function getCurrentLetter(): string
{
return $this->currentLetter;
}

public function setRoute(string $route): self
{
$this->route = $route;

return $this;
}

public function getRoute(): string
{
return $this->route;
}

public function setRouteParameters(array $parameters): self
{
$this->routeParameters = $parameters;

return $this;
}

public function setRouteParameter(string $name, string $value): void
{
$this->routeParameters[$name] = $value;
}

public function getRouteParameters(): array
{
return $this->routeParameters;
}

public function setTemplate(string $templateName): void
{
$this->template = $templateName;
}

public function getTemplate(): string
{
return $this->template;
}

public function setIncludeNumbers(bool $include): void
{
$this->includeNumbers = $include;
}

public function getIncludeNumbers(): bool
{
return $this->includeNumbers;
}
}
42 changes: 42 additions & 0 deletions src/Zikula/CoreBundle/Resources/views/Filter/AlphaFilter.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{% if alpha.route is empty and app.request.attributes.has('_route') %}
{% set alpha = alpha|merge({ ('route'): app.request.attributes.get('_route')}) %}
{% endif %}
{% if alpha.routeParameters is empty and app.request.attributes.has('_route_params') %}
{% set alpha = alpha|merge({ ('routeParameters'): app.request.attributes.get('_route_params')}) %}
{% endif %}
<nav aria-label="Alphabetcial page navigation">
<ul class="pagination pagination-sm justify-content-center">
<li class="page-item{% if alpha.currentLetter is empty or alpha.currentLetter == 'all' %} active{% endif %}">
<a class="page-link" href="{{ path(alpha.route, alpha.routeParameters) }}">{% trans %}All{% endtrans %}</a>
</li>

{% for letter in 'a'|upper..'z'|upper %}
{% do alpha.setRouteParameter('letter', letter) %}
{% if letter == alpha.currentLetter %}
<li class="page-item active" aria-current="page">
<a class="page-link" href="{{ path(alpha.route, alpha.routeParameters) }}">{{ letter }} <span class="sr-only">{{ 'alpha.current'|trans }}</span></a>
</li>
{% else %}
<li class="page-item">
<a class="page-link" href="{{ path(alpha.route, alpha.routeParameters) }}">{{ letter }}</a>
</li>
{% endif %}
{% endfor %}

{% if alpha.includeNumbers %}
{% for number in 0..9 %}
{% do alpha.setRouteParameter('letter', number) %}
{% if number == alpha.currentLetter %}
<li class="page-item active" aria-current="page">
<a class="page-link" href="{{ path(alpha.route, alpha.routeParameters) }}">{{ number }} <span class="sr-only">{{ 'alpha.current'|trans }}</span></a>
</li>
{% else %}
<li class="page-item">
<a class="page-link" href="{{ path(alpha.route, alpha.routeParameters) }}">{{ number }}</a>
</li>
{% endif %}
{% endfor %}
{% endif %}

</ul>
</nav>
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{% if paginator.route is empty and app.request.attributes.has('_route') %}
{% do paginator.setRoute(app.request.attributes.get('_route')) %}
{% endif %}
{% if paginator.hasToPaginate %}
<nav aria-label="Page navigation">
<ul class="pagination justify-content-center">
{% if paginator.hasPreviousPage %}
{% do paginator.setRouteParameter('page', paginator.previousPage) %}
<li class="page-item">
<a class="page-link" href="{{ path(paginator.route, paginator.routeParameters) }}">
<i class="fa fw fa-arrow-left"></i>
</a>
</li>
{% else %}
<li class="page-item disabled">
<a class="page-link" href="#" aria-label="Previous">
<span aria-hidden="true"><i class="fa fw fa-arrow-left"></i></span>
</a>
</li>
{% endif %}

{% for i in 1..paginator.lastPage %}
{% do paginator.setRouteParameter('page', i) %}
{% if i == paginator.currentPage %}
<li class="page-item active" aria-current="page">
<a class="page-link" href="{{ path(paginator.route, paginator.routeParameters) }}">{{ i }} <span class="sr-only">{{ 'paginator.current'|trans }}</span></a>
</li>
{% else %}
<li class="page-item">
<a class="page-link" href="{{ path(paginator.route, paginator.routeParameters) }}">{{ i }}</a>
</li>
{% endif %}
{% endfor %}

{% if paginator.hasNextPage %}
{% do paginator.setRouteParameter('page', paginator.nextPage) %}
<li class="page-item">
<a class="page-link" href="{{ path(paginator.route, paginator.routeParameters) }}">
<i class="fa fw fa-arrow-right"></i>
</a>
</li>
{% else %}
<li class="page-item disabled">
<a class="page-link" href="#" aria-label="Previous">
<span aria-hidden="true"><i class="fa fw fa-arrow-right"></i></span>
</a>
</li>
{% endif %}
</ul>
</nav>
{% endif %}
Loading

0 comments on commit b513d71

Please sign in to comment.