src/Uniski/CMSBundle/Controller/BlogController.php line 83

Open in your IDE?
  1. <?php
  2. namespace Uniski\CMSBundle\Controller;
  3. use Symfony\Component\HttpFoundation\Request;
  4. use Symfony\Component\Routing\Annotation\Route;
  5. use Uniski\CMSBundle\Entity\Post;
  6. use Uniski\ResourceBundle\Controller\BaseController;
  7. /**
  8. * Blog Controller
  9. * Migrado a BaseController (AbstractController) para Symfony 4.4
  10. */
  11. class BlogController extends BaseController
  12. {
  13.     
  14.   /**
  15.    * @Route("noticias", name="blog")
  16.    * @Route("noticias/tag/{slug}", name="blog_tag")
  17.    * @Route("noticias/archive/{year}/{month}", name="blog_archive")
  18.    */
  19.   public function listAction(Request $request$slug null$year null$month null)
  20.   {
  21.     //TODO: Apply request filters like archive an tags
  22.     $page $request->get('page'1);
  23.     $paginator =  $this->get('knp_paginator');
  24.     $em $this->get('doctrine.orm.entity_manager');
  25.     $query $em->getRepository(\Uniski\CMSBundle\Entity\Post::class)->createQueryBuilder('p')
  26.               ->orderBy('p.created''desc');
  27.     if ($slug != null) {
  28.       $query->join('p.tags''t')
  29.             ->andWhere('t.slug = :slug')
  30.             ->setParameter('slug',$slug);
  31.     }
  32.     if ($year != null) {
  33.       $query->andWhere('YEAR(p.created) = :year')
  34.             ->andWhere('MONTH(p.created) = :month')
  35.             ->setParameter('year',$year)
  36.             ->setParameter('month',$month);
  37.     }
  38.     $posts $paginator->paginate($query$page10);
  39.     return $this->render('blog/list.html.twig', [
  40.       'posts' => $posts,
  41.       'menu'  => 'blog'
  42.       ]);
  43.   }
  44.   /**
  45.    * @Route("noticias/{slug}", name="show_post")
  46.    */
  47.   public function showAction(Post $post)
  48.   {
  49.     $searchManager $this->get('app.search_manager');
  50.     $offerManager  $this->get('app.offer_manager');
  51.     $results       = [];
  52.         
  53.     $products $searchManager->searchProductsByTag($post->getTags());
  54.     foreach ($products as $product) {
  55.       $results[] = [
  56.         'product'  => $product,
  57.         'minPrice' => $offerManager->calculateMinPrice($product)
  58.       ];
  59.     }
  60.     return $this->render('blog/show.html.twig', [
  61.       'post'    => $post,
  62.       'menu'    => 'blog',
  63.       'results' => $results,
  64.       ]);
  65.   }
  66.   public function recentPostsAction()
  67.   {
  68.     $posts $this->getDoctrine()->getRepository(\Uniski\CMSBundle\Entity\Post::class)->findBy([],['created' => 'DESC'],5);
  69.     return $this->render('blog/block/recent.html.twig', ['posts' => $posts]);
  70.   }
  71.   public function archivePostsAction()
  72.   {
  73.     $archive $this->getDoctrine()->getRepository(\Uniski\CMSBundle\Entity\Post::class)->getArchive();
  74.     return $this->render('blog/block/archive.html.twig', ['archive' => $archive]);
  75.   }
  76.   public function tagsPostsAction()
  77.   {
  78.     $tags $this->getDoctrine()->getRepository(\Uniski\CMSBundle\Entity\Post::class)->getTags();
  79.     return $this->render('blog/block/tags.html.twig', ['tags' => $tags]);
  80.   }
  81. }