<?php
//----------------------------------------------------------------------
// src/Controller/Reading/BookController.php
//----------------------------------------------------------------------
namespace App\Controller\Reading;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Security\Core\Security;
use Doctrine\Persistence\ManagerRegistry;
use App\Service\ExportReadingTools;
use App\Service\LogTools;
use App\Service\ReadingTools;
use App\Entity\Access;
use App\Entity\Reading\Author;
use App\Entity\Reading\Book;
use App\Entity\Reading\BookIdea;
use App\Entity\Reading\Review;
use App\Form\Reading\BookType;
class BookController extends AbstractController
{
public function __construct(Security $security, ManagerRegistry $doctrine, LogTools $logTools, ReadingTools $readingTools, ExportReadingTools $exportReadingTools)
{
$this->logTools = $logTools;
$this->security = $security;
$this->em = $doctrine->getManager();
$this->owner = $this->security->getUser();
$this->today = new \DateTime();
$this->year = intval($this->today->format("Y"));
$this->readingTools = $readingTools;
$this->exportReadingTools = $exportReadingTools;
}
public function exportBooks(Request $request): Response
{
$this->denyAccessUnlessGranted('access_reading');
$response = $this->exportReadingTools->exportReadingData($this->owner);
if ($response === null)
{
return $this->redirectToRoute('reading_dashboard');
}
return $response;
}
public function list(Request $request): Response
{
$this->denyAccessUnlessGranted('access_reading');
$books = $this->em->getRepository(Book::class)->findBy([
'owner' => $this->owner,
]);
return $this->render('reading/book/list.html.twig', [
'year' => $this->year,
'books' => $books,
]);
}
public function finish(Request $request, Book $book): Response
{
$this->denyAccessUnlessGranted('access_reading', $book);
$book->setReadingDate(new \DateTime());
$okey = true;
try
{
$this->em->flush();
}
catch (\Exception $e)
{
$this->logTools->errorlog($e->getMessage());
$okey = false;
}
// Inform user of process and redirect
if ($okey)
{
// All went well
$request->getSession()->getFlashBag()->add('notice', 'event.success');
}
else
{
// Something went wrong
$request->getSession()->getFlashBag()->add('error', 'event.error');
}
// Where did we came from ?
$route = $request->attributes->get('_route');
if ($route === "reading_book_finish_from_dashboard")
{
return $this->redirectToRoute('dashboard');
}
return $this->redirectToRoute('reading_book_list');
}
// Converts a BookIdea to Book
// The old BookIdea is deleted
public function convert(Request $request, BookIdea $bookIdea): Response
{
$this->denyAccessUnlessGranted('access_reading', $bookIdea);
$book = new Book();
$book->setOwner($this->owner);
// Fill with data from the BookIdea
$book->setTitle($bookIdea->getTitle());
$book->setAuthorFirstname($bookIdea->getAuthorFirstname());
$book->setAuthorLastname($bookIdea->getAuthorLastname());
// If the BookIdea has any info, create a review for the new book
if (!empty($bookIdea->getInfo()))
{
$review = new Review();
$review->setOwner($this->owner);
$review->setBook($book);
$review->setBody($bookIdea->getInfo());
}
$form = $this->createForm(BookType::class, $book);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid())
{
$this->readingTools->craftYear($book);
$this->readingTools->craftAuthor($book);
$this->em->persist($book);
$this->em->persist($review);
$okey = true;
try {
$this->em->flush();
}
catch (\Exception $e)
{
$this->logTools->errorlog($e->getMessage());
$okey = false;
}
// If all went well and the book was created, remove the BookIdea
if ($okey)
{
$this->em->remove($bookIdea);
try {
$this->em->flush();
}
catch (\Exception $e)
{
$this->logTools->errorlog($e->getMessage());
$okey = false;
}
}
// Inform user of process and redirect
if ($okey)
{
// All went well
$request->getSession()->getFlashBag()->add('notice', 'event.success');
}
else
{
// Something went wrong
$request->getSession()->getFlashBag()->add('error', 'event.error');
}
return $this->redirectToRoute('reading_book_list');
}
return $this->renderForm('reading/book/add.html.twig', [
'form' => $form,
]);
}
public function add(Request $request): Response
{
$this->denyAccessUnlessGranted('access_reading');
$book = new Book();
$book->setOwner($this->owner);
$form = $this->createForm(BookType::class, $book);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid())
{
$this->readingTools->craftYear($book);
$this->readingTools->craftAuthor($book);
$this->em->persist($book);
$okey = true;
try {
$this->em->flush();
}
catch (\Exception $e)
{
$this->logTools->errorlog($e->getMessage());
$okey = false;
}
// Inform user of process and redirect
if ($okey)
{
// All went well
$request->getSession()->getFlashBag()->add('notice', 'event.success');
}
else
{
// Something went wrong
$request->getSession()->getFlashBag()->add('error', 'event.error');
}
return $this->redirectToRoute('reading_book_list');
}
return $this->renderForm('reading/book/add.html.twig', [
'form' => $form,
]);
}
public function edit(Request $request, Book $book): Response
{
$this->denyAccessUnlessGranted('access_reading', $book);
$form = $this->createForm(BookType::class, $book);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid())
{
$this->readingTools->craftYear($book);
$this->readingTools->craftAuthor($book);
$okey = true;
try {
$this->em->flush();
}
catch (\Exception $e)
{
$this->logTools->errorlog($e->getMessage());
$okey = false;
}
// Inform user of process and redirect
if ($okey)
{
// All went well
$request->getSession()->getFlashBag()->add('notice', 'event.success');
}
else
{
// Something went wrong
$request->getSession()->getFlashBag()->add('error', 'event.error');
}
return $this->redirectToRoute('reading_book_list');
}
return $this->renderForm('reading/book/edit.html.twig', [
'form' => $form,
'book' => $book,
]);
}
public function delete(Request $request, Book $book): Response
{
$this->denyAccessUnlessGranted('access_reading', $book);
$this->em->remove($book);
$okey = true;
try {
$this->em->flush();
}
catch (\Exception $e)
{
$this->logTools->errorlog($e->getMessage());
$okey = false;
}
// Inform user of process and redirect
if ($okey)
{
// All went well
$request->getSession()->getFlashBag()->add('notice', 'event.success');
}
else
{
// Something went wrong
$request->getSession()->getFlashBag()->add('error', 'event.error');
}
return $this->redirectToRoute('reading_book_list');
}
}