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')); } }