src/Controller/Reading/BookController.php line 67

Open in your IDE?
  1. <?php
  2. //----------------------------------------------------------------------
  3. // src/Controller/Reading/BookController.php
  4. //----------------------------------------------------------------------
  5. namespace App\Controller\Reading;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpKernel\Exception\HttpException;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\HttpFoundation\JsonResponse;
  11. use Symfony\Component\Security\Core\Security;
  12. use Doctrine\Persistence\ManagerRegistry;
  13. use App\Service\ExportReadingTools;
  14. use App\Service\LogTools;
  15. use App\Service\ReadingTools;
  16. use App\Entity\Access;
  17. use App\Entity\Reading\Author;
  18. use App\Entity\Reading\Book;
  19. use App\Entity\Reading\BookIdea;
  20. use App\Entity\Reading\Review;
  21. use App\Form\Reading\BookType;
  22. class BookController extends AbstractController
  23. {
  24.     public function __construct(Security $securityManagerRegistry $doctrineLogTools $logToolsReadingTools $readingToolsExportReadingTools $exportReadingTools)
  25.     {
  26.         $this->logTools $logTools;
  27.         $this->security $security;
  28.         $this->em $doctrine->getManager();
  29.         $this->owner $this->security->getUser();
  30.         $this->today = new \DateTime();
  31.         $this->year intval($this->today->format("Y"));
  32.         $this->readingTools $readingTools;
  33.         $this->exportReadingTools $exportReadingTools;
  34.     }
  35.     public function exportBooks(Request $request): Response
  36.     {
  37.         $this->denyAccessUnlessGranted('access_reading');
  38.         $response $this->exportReadingTools->exportReadingData($this->owner);
  39.         if ($response === null)
  40.         {
  41.             return $this->redirectToRoute('reading_dashboard');
  42.         }
  43.         return $response;
  44.     }
  45.     public function list(Request $request): Response
  46.     {
  47.         $this->denyAccessUnlessGranted('access_reading');
  48.         $books $this->em->getRepository(Book::class)->findBy([
  49.             'owner'     => $this->owner,
  50.         ]);
  51.         return $this->render('reading/book/list.html.twig', [
  52.             'year'          =>  $this->year,
  53.             'books'         =>  $books,
  54.         ]);
  55.     }
  56.     public function finish(Request $requestBook $book): Response
  57.     {
  58.         $this->denyAccessUnlessGranted('access_reading'$book);
  59.         $book->setReadingDate(new \DateTime());
  60.         $okey true;
  61.         try
  62.         {
  63.             $this->em->flush();
  64.         }
  65.         catch (\Exception $e)
  66.         {
  67.             $this->logTools->errorlog($e->getMessage());
  68.             $okey false;
  69.         }
  70.         // Inform user of process and redirect
  71.         if ($okey)
  72.         {
  73.             // All went well
  74.             $request->getSession()->getFlashBag()->add('notice''event.success');
  75.         }
  76.         else
  77.         {
  78.             // Something went wrong
  79.             $request->getSession()->getFlashBag()->add('error''event.error');
  80.         }
  81.         // Where did we came from ?
  82.         $route $request->attributes->get('_route');
  83.         if ($route === "reading_book_finish_from_dashboard")
  84.         {
  85.             return $this->redirectToRoute('dashboard');
  86.         }
  87.         return $this->redirectToRoute('reading_book_list');
  88.     }
  89.     // Converts a BookIdea to Book
  90.     // The old BookIdea is deleted
  91.     public function convert(Request $requestBookIdea $bookIdea): Response
  92.     {
  93.         $this->denyAccessUnlessGranted('access_reading'$bookIdea);
  94.         $book = new Book();
  95.         $book->setOwner($this->owner);
  96.         // Fill with data from the BookIdea
  97.         $book->setTitle($bookIdea->getTitle());
  98.         $book->setAuthorFirstname($bookIdea->getAuthorFirstname());
  99.         $book->setAuthorLastname($bookIdea->getAuthorLastname());
  100.         // If the BookIdea has any info, create a review for the new book
  101.         if (!empty($bookIdea->getInfo()))
  102.         {
  103.             $review = new Review();
  104.             $review->setOwner($this->owner);
  105.             $review->setBook($book);
  106.             $review->setBody($bookIdea->getInfo());
  107.         }
  108.         $form $this->createForm(BookType::class, $book);
  109.         $form->handleRequest($request);
  110.         if ($form->isSubmitted() && $form->isValid())
  111.         {
  112.             $this->readingTools->craftYear($book);
  113.             $this->readingTools->craftAuthor($book);
  114.             $this->em->persist($book);
  115.             $this->em->persist($review);
  116.             $okey true;
  117.             try {
  118.                 $this->em->flush();
  119.             }
  120.             catch (\Exception $e)
  121.             {
  122.                 $this->logTools->errorlog($e->getMessage());
  123.                 $okey false;
  124.             }
  125.             // If all went well and the book was created, remove the BookIdea
  126.             if ($okey)
  127.             {
  128.                 $this->em->remove($bookIdea);
  129.                 try {
  130.                     $this->em->flush();
  131.                 }
  132.                 catch (\Exception $e)
  133.                 {
  134.                     $this->logTools->errorlog($e->getMessage());
  135.                     $okey false;
  136.                 }
  137.             }
  138.             // Inform user of process and redirect
  139.             if ($okey)
  140.             {
  141.                 // All went well
  142.                 $request->getSession()->getFlashBag()->add('notice''event.success');
  143.             }
  144.             else
  145.             {
  146.                 // Something went wrong
  147.                 $request->getSession()->getFlashBag()->add('error''event.error');
  148.             }
  149.             return $this->redirectToRoute('reading_book_list');
  150.         }
  151.         return $this->renderForm('reading/book/add.html.twig', [
  152.             'form'  =>  $form,
  153.         ]);
  154.     }
  155.     public function add(Request $request): Response
  156.     {
  157.         $this->denyAccessUnlessGranted('access_reading');
  158.         $book = new Book();
  159.         $book->setOwner($this->owner);
  160.         $form $this->createForm(BookType::class, $book);
  161.         $form->handleRequest($request);
  162.         if ($form->isSubmitted() && $form->isValid())
  163.         {
  164.             $this->readingTools->craftYear($book);
  165.             $this->readingTools->craftAuthor($book);
  166.             $this->em->persist($book);
  167.             $okey true;
  168.             try {
  169.                 $this->em->flush();
  170.             }
  171.             catch (\Exception $e)
  172.             {
  173.                 $this->logTools->errorlog($e->getMessage());
  174.                 $okey false;
  175.             }
  176.             // Inform user of process and redirect
  177.             if ($okey)
  178.             {
  179.                 // All went well
  180.                 $request->getSession()->getFlashBag()->add('notice''event.success');
  181.             }
  182.             else
  183.             {
  184.                 // Something went wrong
  185.                 $request->getSession()->getFlashBag()->add('error''event.error');
  186.             }
  187.             return $this->redirectToRoute('reading_book_list');
  188.         }
  189.         return $this->renderForm('reading/book/add.html.twig', [
  190.             'form'  =>  $form,
  191.         ]);
  192.     }
  193.     public function edit(Request $requestBook $book): Response
  194.     {
  195.         $this->denyAccessUnlessGranted('access_reading'$book);
  196.         $form $this->createForm(BookType::class, $book);
  197.         $form->handleRequest($request);
  198.         if ($form->isSubmitted() && $form->isValid())
  199.         {
  200.             $this->readingTools->craftYear($book);
  201.             $this->readingTools->craftAuthor($book);
  202.             $okey true;
  203.             try {
  204.                 $this->em->flush();
  205.             }
  206.             catch (\Exception $e)
  207.             {
  208.                 $this->logTools->errorlog($e->getMessage());
  209.                 $okey false;
  210.             }
  211.             // Inform user of process and redirect
  212.             if ($okey)
  213.             {
  214.                 // All went well
  215.                 $request->getSession()->getFlashBag()->add('notice''event.success');
  216.             }
  217.             else
  218.             {
  219.                 // Something went wrong
  220.                 $request->getSession()->getFlashBag()->add('error''event.error');
  221.             }
  222.             return $this->redirectToRoute('reading_book_list');
  223.         }
  224.         return $this->renderForm('reading/book/edit.html.twig', [
  225.             'form'      =>  $form,
  226.             'book'      =>  $book,
  227.         ]);
  228.     }
  229.     public function delete(Request $requestBook $book): Response
  230.     {
  231.         $this->denyAccessUnlessGranted('access_reading'$book);
  232.         $this->em->remove($book);
  233.         $okey true;
  234.         try {
  235.             $this->em->flush();
  236.         }
  237.         catch (\Exception $e)
  238.         {
  239.             $this->logTools->errorlog($e->getMessage());
  240.             $okey false;
  241.         }
  242.         // Inform user of process and redirect
  243.         if ($okey)
  244.         {
  245.             // All went well
  246.             $request->getSession()->getFlashBag()->add('notice''event.success');
  247.         }
  248.         else
  249.         {
  250.             // Something went wrong
  251.             $request->getSession()->getFlashBag()->add('error''event.error');
  252.         }
  253.         return $this->redirectToRoute('reading_book_list');
  254.     }
  255. }