Skip to content

Commit

Permalink
Rework group pages
Browse files Browse the repository at this point in the history
  • Loading branch information
SergioMendolia committed Aug 14, 2024
1 parent fdbfb76 commit 3dc8aaa
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 28 deletions.
29 changes: 13 additions & 16 deletions src/Controller/GroupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,26 @@

use App\Repository\BookRepository;
use http\Exception\RuntimeException;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

#[Route('/groups')]
class GroupController extends AbstractController
{
public function __construct(private BookRepository $bookRepository, private PaginatorInterface $paginator)
public function __construct(private BookRepository $bookRepository)
{
}

#[Route('/{type}/{page}', name: 'app_groups', requirements: ['page' => '\d+'])]
public function groups(Request $request, string $type, int $page = 1): Response
#[Route('/{type}/{letter}', name: 'app_groups')]
public function groups(Request $request, string $type, string $letter = 'a'): Response
{
$group = [];
$incomplete = $request->get('incomplete', 0);
if (!is_numeric($incomplete)) {
throw new RuntimeException('Invalid incomplete type');
$letter = strtolower($letter);
if (strlen($letter) !== 1 || !ctype_alpha($letter)) {
return $this->redirectToRoute('app_groups', ['type' => $type, 'letter' => 'a']);
}
$incomplete = (int) $incomplete;

switch ($type) {
case 'authors':
$group = $this->bookRepository->getAllAuthors();
Expand All @@ -36,10 +35,6 @@ public function groups(Request $request, string $type, int $page = 1): Response
$group = $this->bookRepository->getAllPublishers()->getResult();
break;
case 'serie':
if ($incomplete === 1) {
$group = $this->bookRepository->getIncompleteSeries()->getResult();
break;
}
$group = $this->bookRepository->getAllSeries()->getResult();
break;
}
Expand All @@ -56,14 +51,16 @@ public function groups(Request $request, string $type, int $page = 1): Response
$group = array_filter($group, static function ($item) use ($search) {
return str_contains(strtolower($item['item']), strtolower($search));
});
} else {
$group = array_filter($group, static function ($item) use ($letter) {
return str_starts_with(strtolower($item['item']), $letter);
});
}
$pagination = $this->paginator->paginate($group, $page, 150);

return $this->render('group/index.html.twig', [
'pagination' => $pagination,
'page' => $page,
'group' => $group,
'letter' => $letter,
'type' => $type,
'incomplete' => $incomplete,
'search' => $search,
]);
}
Expand Down
34 changes: 22 additions & 12 deletions templates/group/index.html.twig
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{% extends themedTemplate('base.html.twig') %}

{% block title %}{{ pagination.getTotalItemCount }} {{ type }} - Page {{ page }}{% endblock %}
{% block title %}{{ type }} - {{ letter }}{% endblock %}

{% block body %}

<div class="count">
{{ pagination.getTotalItemCount }} {{ type }}
{{ type }} - {{ letter|upper }}
</div>

<form method="get" class="form-inline my-2">
Expand All @@ -15,21 +15,31 @@
</div>
</form>

{{ knp_pagination_render(pagination) }}
<div class="row">
{% for item in pagination %}
<div class="btn-group btn-group-sm">
{% for other_letter in "a".."z" %}
<a href="{{ path('app_groups', {'type':type, 'letter': other_letter}) }}" class="btn {{ other_letter==letter?'btn-primary':'btn-outline-primary' }}">{{ other_letter|upper }}</a>
{% endfor %}
</div>
<ul class="list-unstyled my-3">
{% for item in group %}
{% set status = ((item.booksFinished/(item.bookCount+0.00001))*100)|round %}
{% set class = status==100?'success':(status>0?'warning':'secondary') %}
<div class="bg-{{ class }}-subtle col-md-4 p-2 border-3 border-{{ class }}">
{% set class = status==100?'success':(status>0?'warning':'') %}
<li class="px-3">
{% set params = { (type): item.item } %}
<a href="{{ filter_book_url(params) }}" class="text-decoration-none">
{{ item.item }}
<span class="badge bg-{{ class }} rounded-pill">{{ item.booksFinished }}/{{ item.bookCount }}</span>
{% if type=='author' %}
{% set icon='person-fill' %}
{% elseif type=='serie' %}
{% set icon='list-ol' %}
{% else %}
{% set icon='tag-fill' %}
{% endif %}
<i class="bi bi-{{ icon }}"></i> {{ item.item }}
<span class="text-{{ class }} px-2">{{ item.booksFinished }}/{{ item.bookCount }}</span>
</a>
{{ component('InlineEditGroup', {'existingValue':item.item,'fieldValue':item.item, 'field':type}) }}
</div>
</li>
{% endfor %}
</div>
{{ knp_pagination_render(pagination) }}
</ul>

{% endblock %}

0 comments on commit 3dc8aaa

Please sign in to comment.