Jacek Kowalski
4 hours ago 6b9278a5d57091fce636dbb640b380cfb990db6c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
declare(strict_types=1);
 
namespace App\Controller;
 
use App\Entity\Book;
use App\Form\AddLabelType;
use App\Form\BookType;
use App\Form\BorrowType;
use App\Form\CoverType;
use App\Form\OustType;
use App\Repository\BookRepository;
use App\Service\Library;
use Doctrine\ORM\EntityManagerInterface;
use Knp\Component\Pager\PaginatorInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Routing\Requirement\Requirement;
 
class BookController extends AbstractController {
    #[Route('/', name: 'home', methods: ['GET'])]
    #[Route('/list', name: 'book_list', methods: ['GET'])]
    public function list(Request $request, BookRepository $books, PaginatorInterface $paginator): Response {
        return $this->render('book/list.html.twig', [
            'pagination' => $paginator->paginate(
                $books->createListQueryBuilder(),
                max(1, $request->query->getInt('page', 1)),
            ),
        ]);
    }
 
    #[Route('/book/{id}', name: 'book_show', requirements: ['id' => Requirement::DIGITS], methods: ['GET'])]
    public function show(Book $book): Response {
        return $this->render('book/show.html.twig', [
            'book' => $book,
            'borrow_form' => $this->createForm(BorrowType::class, null, [
                'action' => $this->generateUrl('book_lend', ['id' => $book->getId()]),
            ]),
            'cover_form' => $this->createForm(CoverType::class, null, [
                'action' => $this->generateUrl('cover_upload', ['id' => $book->getId()]),
            ]),
            'label_form' => $this->createForm(AddLabelType::class, null, [
                'action' => $this->generateUrl('label_add', ['id' => $book->getId()]),
            ]),
            'oust_form' => $this->createForm(OustType::class, null, [
                'action' => $this->generateUrl('book_oust', ['id' => $book->getId()]),
            ]),
        ]);
    }
 
    #[Route('/add', name: 'book_add', methods: ['GET'])]
    public function add(): Response {
        return $this->render('book/add.html.twig');
    }
 
    #[Route('/add/form', name: 'book_new', methods: ['GET', 'POST'])]
    public function new(Request $request, EntityManagerInterface $entityManager, Library $library): Response {
        $book = $this->prefilledBook($request);
        $form = $this->createForm(BookType::class, $book, ['validation_groups' => ['Default', 'creation']]);
        $form->handleRequest($request);
 
        if ($form->isSubmitted() && $form->isValid()) {
            $entityManager->persist($book);
            $entityManager->flush();
            $this->addFlash('success', 'book_saved');
 
            if ($library->downloadCover($book)) {
                $this->addFlash('success', 'cover_downloaded');
            }
 
            return $this->redirectToRoute('book_show', ['id' => $book->getId()]);
        }
 
        return $this->render('book/new.html.twig', ['form' => $form]);
    }
 
    #[Route('/book/{id}/edit', name: 'book_edit', requirements: ['id' => Requirement::DIGITS], methods: ['GET', 'POST'])]
    public function edit(Request $request, Book $book, EntityManagerInterface $entityManager): Response {
        $form = $this->createForm(BookType::class, $book, ['lock_id' => true]);
        $form->handleRequest($request);
 
        if ($form->isSubmitted() && $form->isValid()) {
            $entityManager->flush();
            $this->addFlash('success', 'book_saved');
 
            return $this->redirectToRoute('book_show', ['id' => $book->getId()]);
        }
 
        return $this->render('book/edit.html.twig', ['book' => $book, 'form' => $form]);
    }
 
    private function prefilledBook(Request $request): Book {
        $query = $request->query;
        $id = $query->get('id', '');
        $year = $query->get('publicationYear', '');
 
        return (new Book())
            ->setId(ctype_digit($id) ? (int)$id : null)
            ->setTitle($query->get('title'))
            ->setAuthor($query->get('author'))
            ->setLanguage($query->get('language'))
            ->setPublisher($query->get('publisher'))
            ->setPublicationLoc($query->get('publicationLoc'))
            ->setPublicationYear(ctype_digit($year) ? (int)$year : null)
            ->setIsbn($query->get('isbn'))
            ->setIssn($query->get('issn'));
    }
}