src/Controller/Front/BlogController.php line 67

  1. <?php
  2. namespace App\Controller\Front;
  3. use App\Entity\Animal;
  4. use App\Entity\BlogCategory;
  5. use App\Entity\BlogKeyword;
  6. use App\Entity\BlogNote;
  7. use App\Entity\Role;
  8. use App\Entity\Rotator;
  9. use App\Entity\RotatorPlace;
  10. use App\Entity\RotatorType;
  11. use App\Entity\User;
  12. use App\Repository\AnimalRepository;
  13. use App\Repository\BlogAuthorRepository;
  14. use App\Repository\BlogCategoryRepository;
  15. use App\Repository\BlogKeywordRepository;
  16. use App\Repository\BlogNoteRepository;
  17. use App\Repository\RoleRepository;
  18. use App\Repository\RotatorRepository;
  19. use App\Repository\UserRepository;
  20. use App\Service\BlogService;
  21. use Doctrine\Persistence\ManagerRegistry;
  22. use Knp\Component\Pager\PaginatorInterface;
  23. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  24. use Symfony\Component\HttpFoundation\RedirectResponse;
  25. use Symfony\Component\HttpFoundation\Request;
  26. use Symfony\Component\HttpFoundation\Response;
  27. use Symfony\Component\Routing\Annotation\Route;
  28. class BlogController extends AbstractController
  29. {
  30.     #[Route('/autorzy')]
  31.     #[Route('/front/blog')]
  32.     public function indexAction(Request $requestManagerRegistry $doctrine)
  33.     {
  34.         $roles $doctrine->getRepository(Role::class)->findBy(['role' => 'ROLE_AUTHOR']);
  35.         $blogAuthors $doctrine->getRepository(User::class)->findAllqb('id''DESC'$roles);
  36.         return $this->render(
  37.             'front/blog/index.html.twig',
  38.             [
  39.                 'blogAuthors' => $blogAuthors->getQuery()->getResult(),
  40.             ]
  41.         );
  42.     }
  43.     #[Route('/autor/{firstName}_{lastName}/{id}'name'base_front_blog_authors')]
  44.     public function notesAction(Request $requestManagerRegistry $doctrine$id)
  45.     {
  46.         $blogNotes $doctrine->getRepository(BlogNote::class)->findBy(['author' => $id'isPublished' => true]);
  47.         $author $doctrine->getRepository(User::class)->find($id);
  48.         $anotherAuthors $doctrine
  49.             ->getRepository(BlogNote::class)
  50.             ->findMostPostedAuthors(2$id);
  51.         $rotator $doctrine->getRepository(Rotator::class)->findOneByTypeAndPlace(
  52.             RotatorType::TYPE_BANNER,
  53.             RotatorPlace::TYPE_AUTHOR
  54.         );
  55.         return $this->render(
  56.             'front/blog/notes.html.twig',
  57.             [
  58.                 'author' => $author,
  59.                 'anotherAuthors' => $anotherAuthors,
  60.                 'blogNotes' => $blogNotes,
  61.                 'blogRotator' => $rotator,
  62.             ]
  63.         );
  64.     }
  65.     #[Route('/blog'name'base_front_blog_notes')]
  66.     #[Route('/front/blog/notesList')]
  67.     public function notesListAction(
  68.         Request $request,
  69.         ManagerRegistry $doctrine)
  70.     {
  71.         $categories $doctrine
  72.             ->getRepository(BlogCategory::class)
  73.             ->getCategoriesForBlog();
  74.         $blogNotes $doctrine
  75.             ->getRepository(BlogNote::class)
  76.             ->findBlogPostsForList($categories);
  77.         $bestAuthors $doctrine
  78.             ->getRepository(BlogNote::class)
  79.             ->findMostPostedAuthors();
  80.         return $this->render(
  81.             'front/blog/notesList.html.twig',
  82.             [
  83.                 'blogNotes' => $blogNotes,
  84.                 'bestAuthors' => $bestAuthors,
  85.             ]
  86.         );
  87.     }
  88.     #[Route('/blog/popular'name'base_front_blog_popular')]
  89.     #[Route('/front/blog/popular')]
  90.     public function popularNotesAction(
  91.         Request $request,
  92.         ManagerRegistry $doctrine)
  93.     {
  94.         $categories $doctrine
  95.             ->getRepository(BlogCategory::class)
  96.             ->getCategoriesForBlog();
  97.         $blogNotes $doctrine
  98.             ->getRepository(BlogNote::class)
  99.             ->findBlogPostsPopularForList($categories);
  100.         $bestAuthors $doctrine
  101.             ->getRepository(BlogNote::class)
  102.             ->findMostPostedAuthors();
  103.         return $this->render(
  104.             'front/blog/notesList.html.twig',
  105.             [
  106.                 'blogNotes' => $blogNotes,
  107.                 'bestAuthors' => $bestAuthors,
  108.             ]
  109.         );
  110.     }
  111.     #[Route('/blog/{id}'name'base_front_blog_notesingle')]
  112.     public function noteSingleAction(Request $requestManagerRegistry $doctrineBlogService $blogService$id)
  113.     {
  114.         $isAdmin $this->isGranted('ROLE_ADMIN');
  115.         if(!$isAdmin) {
  116.             $blogNote $doctrine->getRepository(BlogNote::class)->findOneBy(['id' => $id'isPublished' => true]);
  117.         } else {
  118.             $blogNote $doctrine->getRepository(BlogNote::class)->findOneBy(['id' => $id]);
  119.         }
  120.         if (!$blogNote) {
  121.             throw $this->createNotFoundException('Wpis nie istnieje');
  122.         }
  123.         $blogService->incrementPv($blogNote);
  124.         $nextNotesAuthor = [];
  125.         if (!is_null($blogNote->getAuthor())) {
  126.             $nextNotesAuthor $doctrine->getRepository(BlogNote::class)->findNextNotesFromAuthor($blogNote);
  127.         }
  128.         $nextNotesCategory $doctrine->getRepository(BlogNote::class)->findNextNotesFromCategory($blogNote);
  129.         $animals $doctrine->getRepository(Animal::class)->findBy(['isPublished' => true], ['id' => 'DESC'], 5);
  130.         return $this->render(
  131.             'front/blog/noteSingle.html.twig',
  132.             [
  133.                 'blogNote' => $blogNote,
  134.                 'categories' => $doctrine->getRepository(BlogCategory::class)->getCategoriesForBlog(),
  135.                 'nextNotesCategory' => $nextNotesCategory,
  136.                 'nextNotesAuthor' => $nextNotesAuthor,
  137.                 'animals' => $animals,
  138.             ]
  139.         );
  140.     }
  141.     #[Route('blog/tag/{word}'name'base_front_blog_noteslistwithkeyword')]
  142.     public function notesListWithKeywordAction(ManagerRegistry $doctrine$word)
  143.     {
  144.         $keywords $doctrine->getRepository(BlogKeyword::class)->findBy(
  145.             ['name' => $word],
  146.             ['publishedDate' => 'DESC']
  147.         );
  148.         $blogNotes $doctrine->getRepository(BlogNote::class)->findBlogPostsForKeyword($keywords);
  149.         $bestAuthors $doctrine->getRepository(BlogNote::class)->findMostPostedAuthors();
  150.         return $this->render(
  151.             'front/blog/notesListWithKeyword.html.twig',
  152.             [
  153.                 'blogNotes' => $blogNotes,
  154.                 'bestAuthors' => $bestAuthors,
  155.                 'keyword' => $word,
  156.             ]
  157.         );
  158.     }
  159.     #[Route('/blog/kategoria/{id}/{slug}'name'base_front_blog_notesincategory')]
  160.     public function notesInCategoryAction($idManagerRegistry $doctrine$slug)
  161.     {
  162.         $blogNotes $doctrine->getRepository(BlogNote::class)
  163.             ->findByCategories($id);
  164.         $bestAuthors $doctrine->getRepository(BlogNote::class)->findMostPostedAuthors();
  165.         $category $doctrine->getRepository(BlogCategory::class)->find($id);
  166.     
  167.         return $this->render(
  168.             'front/blog/notesInCategory.html.twig',
  169.             [
  170.                 'blogNotes' => $blogNotes,
  171.                 'bestAuthors' => $bestAuthors,
  172.                 'category' => $category,
  173.             ]
  174.         );
  175.     }
  176.     #[Route('/jak-pomoc')]
  177.     public function howToHelpNotesList(Request $request)
  178.     {
  179.         return $this->render(
  180.             'front/blog/howToHelpNotesList.html.twig',
  181.             [
  182.                 'blogNotes' => $this->getAllPostsFromHowToHelpCategory(),
  183.             ]
  184.         );
  185.     }
  186.     #[Route('/jak-pomoc/{id}')]
  187.     public function howToHelpSingleAction(ManagerRegistry $doctrine$id)
  188.     {
  189.         $blogNote $doctrine->getRepository(BlogNote::class)->findOneBy(['id' => $id'isPublished' => true]);
  190.         if (empty($blogNote)) {
  191.             return new Response(''404);
  192.         }
  193.         if ($blogNote->getCategory()->getType() != BlogCategory::TYPE_HOW_TO_HELP) {
  194.             return new RedirectResponse($this->generateUrl('base_front_blog_notesingle', ['id' => $id]));
  195.         } else {
  196.             return $this->render(
  197.                 'front/blog/howToHelpSingle.html.twig',
  198.                 [
  199.                     'blogNote' => $blogNote,
  200.                     'blogNotes' => $this->getAllPostsFromHowToHelpCategory(),
  201.                 ]
  202.             );
  203.         }
  204.     }
  205.     private function getAllPostsFromHowToHelpCategory(ManagerRegistry $doctrine)
  206.     {
  207.         $category $doctrine->getRepository(BlogCategory::class)->findOneBy(['type' => BlogCategory::TYPE_HOW_TO_HELP]);
  208.         return $doctrine->getRepository(BlogNote::class)->getAllPostsInHowToHelpCategory($category);
  209.     }
  210. }