src/Controller/BlogController.php line 90

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\BlogArticle;
  4. use App\Repository\BlogArticleRepository;
  5. use App\Repository\BlogCategoryRepository;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\Routing\Annotation\Route;
  10. use Doctrine\Common\Collections\Criteria;
  11. /**
  12. * @Route("/blog")
  13. */
  14. class BlogController extends AbstractController {
  15.     /**
  16.      * Blog Article List.
  17.      * @Route(
  18.      *      "/list/{page}/{sort}/{dir}",
  19.      *      name="client_blog_article_list",
  20.      *      methods={"GET","POST"},
  21.      *      requirements={"page"="\d+","sort"="[0-9a-zA-Z\.]+","dir"="asc|desc"},
  22.      *      defaults={"page"=1,"sort"="createdAt","dir"="desc"}
  23.      *  )
  24.      *
  25.      * @return string
  26.      */
  27.     public function list(
  28.         Request $request$page$sort$dir,
  29.         \App\Service\SeoHelper $seoHelper,
  30.         BlogArticleRepository $blogArticleRepository
  31.     ): Response {
  32.         $limit $request->query->getInt('limit'20);
  33.         $seoHelper->setInternalSeo('Blog Articles''client_blog_article_list');
  34.         $criteria Criteria::create();
  35.         $criteria->andWhere(Criteria::expr()->eq('isEnabled'true));
  36.         $criteria->orderBy([$sort => $dir]);
  37.         //$totalCriteria->andWhere(Criteria::expr()->eq('isDeceased', false));
  38.         $entries $blogArticleRepository->matching($criteria);
  39.         $entries $entries->slice(($page-1)*$limit$limit);
  40.         $total $blogArticleRepository->countAll();
  41.         $pages ceil($total/$limit);
  42.         return $this->render('client/blog/list.html.twig', [
  43.             'title' => 'Blog Articles',
  44.             'listParams' => [],
  45.             'entries' => $entries,
  46.             'ordering' => [
  47.                 'sort' => $sort,
  48.                 'dir' => $dir
  49.             ],
  50.             'meta' => [
  51.                 'title' => 'Blog Articles',
  52.                 'type' => 'website',
  53.             ],
  54.             'page' => [
  55.                 'icon' => 'list',
  56.                 'title' => 'Blog Articles',
  57.                 'subtitle' => 'View the list with the Blog Articles',
  58.                 'header' => 'Training Manager - Blog Articles List'
  59.             ],
  60.             'pagination' => [
  61.                 'listURL' => 'client_blog_article_list',
  62.                 'totalSearch' => $total,
  63.                 'total' => $total,
  64.                 'limit' => $limit,
  65.                 'totalPages' => $pages,
  66.                 'page' => $page,
  67.             ],
  68.         ]);
  69.     }
  70.     /**
  71.      * Blog Category Article List.
  72.      * @Route(
  73.      *      "/category/{slug}/{page}/{sort}/{dir}",
  74.      *      name="client_blog_category_article_list",
  75.      *      methods={"GET","POST"},
  76.      *      requirements={"page"="\d+","sort"="[0-9a-zA-Z\.]+","dir"="asc|desc"},
  77.      *      defaults={"page"=1,"sort"="createdAt","dir"="desc"}
  78.      *  )
  79.      *
  80.      * @return string
  81.      */
  82.     public function category(
  83.         Request $request$page$sort$dir$slug,
  84.         \App\Service\SeoHelper $seoHelper,
  85.         BlogCategoryRepository $blogCategoryRepository
  86.     ): Response {
  87.         $limit $request->query->getInt('limit'20);
  88.         $category $blogCategoryRepository->findOneBy(["slug" => $slug"isEnabled" => true], ["createdAt" => "Desc"]);
  89.         if (!$category) throw $this->createNotFoundException('The blog category does not exist');
  90.         $seoHelper->setBlogCategorySeo($category);
  91.         $entries $category->getArticles();
  92.         /*$criteria = Criteria::create();
  93.         $criteria->andWhere(Criteria::expr()->eq('isEnabled', true));
  94.         $criteria->andWhere(Criteria::expr()->eq('slug', $slug));
  95.         $criteria->orderBy([$sort => $dir]);*/
  96.         //$entries = $blogArticleRepository->matching($criteria);
  97.         $entries $entries->slice(($page-1)*$limit$limit);
  98.         $total $category->getArticles()->count();
  99.         $pages ceil($total/$limit);
  100.         return $this->render('client/blog/list.html.twig', [
  101.             'title' => $category->getTitle(),
  102.             'listParams' => [],
  103.             'entries' => $entries,
  104.             'ordering' => [
  105.                 'sort' => $sort,
  106.                 'dir' => $dir
  107.             ],
  108.             'page' => [
  109.                 'icon' => 'list',
  110.                 'title' => 'Blog Articles',
  111.                 'subtitle' => 'View the list with the Blog Articles',
  112.                 'header' => 'Training Manager - Blog Articles List',
  113.             ],
  114.             'meta' => [
  115.                 'title' => $category->getTitle(),
  116.                 'type' => 'website',
  117.                 'description' => $category->getMetaDescription(),
  118.                 'keywords' => $category->getMetaKeywords()
  119.             ],
  120.             'pagination' => [
  121.                 'listURL' => 'client_blog_article_list',
  122.                 'totalSearch' => $total,
  123.                 'total' => $total,
  124.                 'limit' => $limit,
  125.                 'totalPages' => $pages,
  126.                 'page' => $page,
  127.             ],
  128.         ]);
  129.     }
  130.     /**
  131.      * @Route("/view/{slug}", name="client_blog_article_view", methods={"GET"})
  132.      */
  133.     public function show(
  134.         BlogArticleRepository $blogArticleRepository,
  135.         \App\Service\SeoHelper $seoHelper,
  136.         $slug
  137.     ): Response {
  138.         $article $blogArticleRepository->findOneBy(['slug' => $slug'isEnabled' => true]);
  139.         if (!$article) throw $this->createNotFoundException('The blog article does not exist');
  140.         $seoHelper->setArticleSeo($article);
  141.         return $this->render('client/blog/show.html.twig', [
  142.             'title' => $article->getTitle(),
  143.             'page' => [
  144.                 'icon' => 'star',
  145.                 'title' => $article->getTitle()
  146.             ],
  147.             'entry' => $article,
  148.         ]);
  149.     }
  150. }