src/Application/Controller/BlogController.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\Application\Controller;
  3. use App\Admin\Document\Article;
  4. use App\Admin\Document\Page;
  5. use App\Admin\Document\Tag;
  6. use Symfony\Component\HttpFoundation\JsonResponse;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use \Symfony\Component\Routing\Annotation\Route;
  10. class BlogController extends AbstractController
  11. {
  12.     const URL 'blog';
  13.     /**
  14.      * @Route("/blog", name="blog")
  15.      * @param Request $request
  16.      * @return \Symfony\Component\HttpFoundation\Response
  17.      */
  18.     public function index(Request $request)
  19.     {
  20.         $page $this->setSeoByUrl(self::URL$request->getLocale());
  21.         $params $this->getItemsParams($request->get('tag'));
  22.         $criteria = ['active' => true];
  23.         if ($params['tag']) {
  24.             $criteria['tags'] = $params['tag']->getId();
  25.         }
  26.         return $this->render('application/blog/index.html.twig'array_merge($params, [
  27.             'page' => $page,
  28.             'topItems' => $this->getDocumentRepository(Article::class)->findBy($criteria, ['date' => 'DESC'], 4),
  29.             'tags' => $this->getDocumentRepository(Tag::class)->findBy([], [$request->getLocale() == 'en' 'enName' 'name' => 'asc']),
  30.         ]));
  31.     }
  32.     private function getItemsParams($tagUrl) {
  33.         $qb $this->getDocumentRepository(Article::class)->createQueryBuilder();
  34.         $qb->field('active')->equals(true);
  35.         $qb->sort('date''desc');
  36.         if ($tagUrl) {
  37.             $tag $this->getDocumentRepository(Tag::class)->findOneBy(['url' => $tagUrl]);
  38.             if ($tag) {
  39.                 $qb->field('tags')->equals($tag->getId());
  40.             }
  41.         } else {
  42.             $tag null;
  43.         }
  44.         $pagination $this->paginate($qb);
  45.         $items $pagination->getItems();
  46.         return [
  47.             'items' => $items,
  48.             'pagination' => $pagination->getPaginationData(),
  49.             'tag' => $tag,
  50.         ];
  51.     }
  52.     /**
  53.      * @Route("/blog-show-more", name="blog-show-more")
  54.      * @return Response
  55.      */
  56.     public function showMore(Request $request): Response
  57.     {
  58.         $params $this->getItemsParams($request->get('tag'));
  59.         return new JsonResponse([
  60.             'html' => $this->renderView('application/blog/list.html.twig'$params),
  61.             'paginationHtml' => $this->renderView('application/partial/pagination.html.twig'$params),
  62.         ]);
  63.     }
  64.     /**
  65.      * @Route("/blog/{url}", name="blog/item")
  66.      * @param string $url
  67.      * @return \Symfony\Component\HttpFoundation\Response
  68.      * @throws \Doctrine\ODM\MongoDB\MongoDBException
  69.      */
  70.     public function item(Request $request$url)
  71.     {
  72.         /**
  73.          * @var Article $blog
  74.          */
  75.         $blog $this->getDocumentRepository(Article::class)->findOneBy(['active' => true'url' => $url]);
  76.         if (!$blog) {
  77.             throw $this->createNotFoundException("Article with url '{$url}' not found or not active");
  78.         }
  79.         $this->setSeo($blog$request->getLocale());
  80.         return $this->render('application/blog/item.html.twig', [
  81.             'item' => $blog,
  82.         ]);
  83.     }
  84. }