- toolbar and fixes
This commit is contained in:
Jérémy Guillot
2024-06-29 14:51:10 +02:00
parent b04055ec22
commit 858a5bed06
20 changed files with 404 additions and 68 deletions

View File

@@ -2,6 +2,7 @@
namespace App\Controller;
use App\Manager\ToolbarManager;
use App\Message\DownloadChapter;
use App\Repository\ChapterRepository;
use Doctrine\DBAL\Connection;
@@ -13,7 +14,11 @@ use Symfony\Component\Routing\Annotation\Route;
class ActivityController extends AbstractController
{
public function __construct(private Connection $connection, private readonly ChapterRepository $chapterRepository)
public function __construct(
private readonly Connection $connection,
private readonly ChapterRepository $chapterRepository,
private readonly ToolbarManager $toolbarManager
)
{
}
@@ -33,6 +38,7 @@ class ActivityController extends AbstractController
return $this->render('activity/index.html.twig', [
'controller_name' => 'ActivityController',
'status' => $status,
'toolbarItems' => $this->toolbarManager->getToolbarItems(),
]);
}

View File

@@ -4,6 +4,7 @@ namespace App\Controller;
use App\Entity\Chapter;
use App\Entity\Manga;
use App\Manager\ToolbarManager;
use App\Message\DownloadChapter;
use App\Repository\ChapterRepository;
use App\Repository\MangaRepository;
@@ -34,19 +35,23 @@ class MangaController extends AbstractController
private readonly ChapterRepository $chapterRepository,
private readonly MangaUpdatesMetadataProvider $mangaUpdatesDbProvider,
private readonly MessageBusInterface $bus,
private readonly CbzService $cbzService
private readonly CbzService $cbzService,
private readonly ToolbarManager $toolbarManager
)
{
}
#[Route('/manga', name: 'app_manga')]
public function index(): Response
public function index(Request $request): Response
{
// phpinfo();
$mangas = $this->mangaRepository->findAll();
$sort = $request->query->get('sort', 'title');
$order = $request->query->get('order', 'asc');
$mangas = $this->mangaRepository->findAllSorted($sort, $order);
return $this->render('manga/index.html.twig', [
'controller_name' => 'MangaController',
'mangas' => $mangas,
'toolbarItems' => $this->toolbarManager->getToolbarItems(),
]);
}
@@ -89,6 +94,7 @@ class MangaController extends AbstractController
return $this->render('manga/show_chapters.html.twig', [
'chapters_by_volume' => $chaptersByVolume,
'manga' => $manga,
'toolbarItems' => $this->toolbarManager->getToolbarItems(),
]);
}

View File

@@ -0,0 +1,41 @@
<?php
namespace App\Manager;
class ToolbarManager
{
public function getToolbarItems(): array
{
return [
'sortItems' => $this->getSortItems(),
'filterItems' => $this->getFilterItems(),
'viewOptions' => $this->getViewOptions()
];
}
private function getSortItems(): array
{
return [
['text' => 'Par titre', 'action' => 'sort', 'data' => ['sort-option' => 'title']],
['text' => 'Par année de publication', 'action' => 'sort', 'data' => ['sort-option' => 'publicationYear']],
['text' => 'Par date d\'ajout', 'action' => 'sort', 'data' => ['sort-option' => 'createdAt']]
];
}
private function getFilterItems(): array
{
return [
['text' => 'Tous les genres', 'action' => 'filter', 'data' => ['filter-option' => 'allGenres']],
['text' => 'Mangas terminés', 'action' => 'filter', 'data' => ['filter-option' => 'completed']],
['text' => 'Mangas en cours', 'action' => 'filter', 'data' => ['filter-option' => 'ongoing']]
];
}
private function getViewOptions(): array
{
return [
['text' => 'Vue grille', 'action' => 'changeView', 'data' => ['view-option' => 'grid']],
['text' => 'Vue liste', 'action' => 'changeView', 'data' => ['view-option' => 'list']]
];
}
}

View File

@@ -87,11 +87,6 @@ class MangaRepository extends ServiceEntityRepository
return $sortedEntities;
}
private function normalizeSlug(string $slug): string
{
return strtolower(preg_replace('/[^a-z0-9]+/i', '', $slug));
}
/**
* @throws NonUniqueResultException
*/
@@ -108,6 +103,28 @@ class MangaRepository extends ServiceEntityRepository
return $query->getQuery()->getOneOrNullResult();
}
public function findAllSorted(string $sort = 'title', string $order = 'asc'): array
{
$qb = $this->createQueryBuilder('m');
switch ($sort) {
case 'title':
$qb->orderBy('m.title', $order);
break;
case 'publicationYear':
$qb->orderBy('m.publicationYear', $order);
break;
case 'createdAt':
$qb->orderBy('m.createdAt', $order);
break;
// Ajoutez d'autres cas pour les différentes options de tri
default:
$qb->orderBy('m.title', 'asc');
}
return $qb->getQuery()->getResult();
}
// /**
// * @return Manga[] Returns an array of Manga objects
// */

View File

@@ -0,0 +1,16 @@
<?php
namespace App\Twig\Components;
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\Attribute\LiveProp;
use Symfony\UX\LiveComponent\DefaultActionTrait;
#[AsLiveComponent]
class DropdownMenu
{
use DefaultActionTrait;
#[LiveProp (writable: true)]
public ?array $items = null;
}

View File

@@ -8,6 +8,7 @@ use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\Attribute\LiveAction;
use Symfony\UX\LiveComponent\Attribute\LiveProp;
@@ -27,6 +28,11 @@ class NewMangaForm
#[LiveProp(writable: true)]
public ?int $index = 0;
public function __construct(private UrlGeneratorInterface $urlGenerator)
{
}
public function mount(Manga $manga): void
{
$this->manga = $manga;
@@ -84,6 +90,8 @@ class NewMangaForm
$manga->addChapter($chapter);
}
$mangaChapterUrl = $this->urlGenerator->generate('app_manga_show', ['mangaSlug' => $manga->getSlug()]);
try {
foreach ($manga->getChapters() as $chapter) {
$entityManager->persist($chapter);
@@ -93,11 +101,11 @@ class NewMangaForm
$entityManager->flush();
} catch (\Exception $e) {
if ($e instanceof UniqueConstraintViolationException) {
return new RedirectResponse('/manga/' . $manga->getSlug());
return new RedirectResponse($mangaChapterUrl);
}
throw $e;
}
return new RedirectResponse('/manga/' . $manga->getSlug());
return new RedirectResponse($mangaChapterUrl);
}
}