src/Controller/Blog/DefaultController.php line 51

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Blog;
  3. use App\Entity\Blog\Comment;
  4. use App\Form\Blog\CommentFormType;
  5. use App\Form\Blog\SearchFormType;
  6. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  8. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  11. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  12. /**
  13.  * @Route("/{_locale}/blog", requirements={"_locale" = "es|en"})
  14.  */
  15. class DefaultController extends Controller
  16. {
  17.     public function getEm()
  18.     {
  19.         return $this->getDoctrine()->getManager();
  20.     }
  21.     /**
  22.      * @Route("/", name="blog_index")
  23.      * @Template()
  24.      */
  25.     public function indexAction(Request $request)
  26.     {
  27.         $em $this->getEm();
  28.         $page $request->query->getInt('page'1);
  29.         $perPage 9;
  30.         $posts $em->getRepository('App\Entity\Blog\Post')->findBy(['enabled' => true], ['highlight' => 'desc''created' => 'desc']);
  31.         // Search posts
  32.         $form $this->createForm(SearchFormType::class);
  33.         $form->handleRequest($request);
  34.         if ($form->isSubmitted() && $form->isValid()) {
  35.             $data $form->getData();
  36.             $keywords $data['q'];
  37.             $posts $em->getRepository('App\Entity\Blog\Post')->searchPosts($keywords);
  38.         }
  39.         // Get paginated collection
  40.         $postsPager $this->get('knp_paginator')->paginate($posts$page$perPage);
  41.         return $this->render('blog/index.html.twig', [
  42.             'posts' => $postsPager,
  43.             'form' => $form->createView(),
  44.         ]);
  45.     }
  46.     /**
  47.      * @Route("/{year}/{month}/{day}/{slug}", name="show_post", requirements={"year" = "\d+", "month": "\d+", "day": "\d+"})
  48.      * @Template()
  49.      *
  50.      * @param mixed $year
  51.      * @param mixed $month
  52.      * @param mixed $day
  53.      * @param mixed $slug
  54.      */
  55.     public function postAction(Request $request$year$month$day$slug)
  56.     {
  57.         $em $this->getEm();
  58.         $date = new \DateTime($year.'-'.$month.'-'.$day);
  59.         $post $em->getRepository('App\Entity\Blog\Post')->findOneBy(['slug' => $slug'date' => $date]);
  60.         $comment = (new Comment())->setPost($post);
  61.         $form $this->createForm(CommentFormType::class, $comment);
  62.         $form->handleRequest($request);
  63.         if ($form->isSubmitted() && $form->isValid()) {
  64.             $comment->setPost($post);
  65.             $em->persist($comment);
  66.             $em->flush();
  67.             // Trigger email to admin to approve?
  68.             $this->addFlash('success''Gracias por su comentario. Se publicará una vez aprobado por nuestros administradores.');
  69.             return $this->redirect($this->generateUrl('show_post', ['slug' => $post->getSlug(), 'year' => $post->getDate()->format('Y'), 'month' => $post->getDate()->format('m'), 'day' => $post->getDate()->format('d')]));
  70.         }
  71.         if (!$post) {
  72.             throw new NotFoundHttpException(sprintf('Unable to find post for : %s'$slug));
  73.         }
  74.         return $this->render('blog/post.html.twig', [
  75.             'commentForm' => $form->createView(),
  76.             'post' => $post,
  77.             'categoriesHtml' => $this->categoriesHtml($post),
  78.             'shareUrl' => $this->generateUrl('show_post', ['slug' => $post->getSlug(), 'year' => $post->getDate()->format('Y'), 'month' => $post->getDate()->format('m'), 'day' => $post->getDate()->format('d')], UrlGeneratorInterface::ABSOLUTE_URL),
  79.         ]);
  80.     }
  81.     /**
  82.      * @Route("/{slug}", name="show_posts_by_category")
  83.      * @Template()
  84.      *
  85.      * @param mixed $slug
  86.      */
  87.     public function byCategoryAction(Request $request$slug)
  88.     {
  89.         $em $this->getEm();
  90.         $category $em->getRepository('App\Entity\Blog\Category')->findOneBy(['slug' => $slug]);
  91.         $posts $em->getRepository('App\Entity\Blog\Post')->getPostsInCategory($category->getId());
  92.         $page $request->query->getInt('page'1);
  93.         $perPage 9;
  94.         // Search posts
  95.         $form $this->createForm(SearchFormType::class);
  96.         $form->handleRequest($request);
  97.         if ($form->isSubmitted() && $form->isValid()) {
  98.             $data $form->getData();
  99.             $keywords $data['q'];
  100.             $posts $em->getRepository('App\Entity\Blog\Post')->searchPosts($keywords);
  101.         }
  102.         // Get paginated collection
  103.         $postsPager $this->get('knp_paginator')->paginate($posts$page$perPage);
  104.         return $this->render('blog/byCategory.html.twig', [
  105.             'category' => $category,
  106.             'posts' => $postsPager,
  107.             'form' => $form->createView(),
  108.         ]);
  109.     }
  110.     /**
  111.      * @Route("/{year}/{month}", name="show_posts_by_month", requirements={"year" = "\d+", "month": "\d+"})
  112.      * @Template()
  113.      *
  114.      * @param mixed $year
  115.      * @param mixed $month
  116.      */
  117.     public function byMonthAction(Request $request$year$month)
  118.     {
  119.         $em $this->getEm();
  120.         $date = new \DateTime($year.'-'.$month.'-01');
  121.         $posts $em->getRepository('App\Entity\Blog\Post')->getPostsInMonth($year$month);
  122.         $page $request->query->getInt('page'1);
  123.         $perPage 9;
  124.         // Search posts
  125.         $form $this->createForm(SearchFormType::class);
  126.         $form->handleRequest($request);
  127.         if ($form->isSubmitted() && $form->isValid()) {
  128.             $data $form->getData();
  129.             $keywords $data['q'];
  130.             $posts $em->getRepository('App\Entity\Blog\Post')->searchPosts($keywords);
  131.         }
  132.         // Get paginated collection
  133.         $postsPager $this->get('knp_paginator')->paginate($posts$page$perPage);
  134.         return $this->render('blog/byMonth.html.twig', [
  135.             'date' => $date,
  136.             'posts' => $postsPager,
  137.             'form' => $form->createView(),
  138.         ]);
  139.     }
  140.     public function categoriesAction()
  141.     {
  142.         $em $this->getEm();
  143.         $categories $em->getRepository('App\Entity\Blog\Category')->findBy(['enabled' => true]);
  144.         $cats = [];
  145.         foreach ($categories as $category) {
  146.             if (count($category->getPosts()) > 0) {
  147.                 $cats[$category->getSlug()] = $category->getName();
  148.             }
  149.         }
  150.         return $this->render('blog/categories.html.twig', [
  151.             'categories' => $cats,
  152.         ]);
  153.     }
  154.     public function recentPostsAction()
  155.     {
  156.         $em $this->getEm();
  157.         $posts $em->getRepository('App\Entity\Blog\Post')->findBy(['enabled' => true], ['created' => 'desc'], 5);
  158.         return $this->render('blog/recent.html.twig', [
  159.             'posts' => $posts,
  160.         ]);
  161.     }
  162.     public function monthsAction()
  163.     {
  164.         $em $this->getEm();
  165.         $months $em->getRepository('App\Entity\Blog\Post')->getMonthsWithPosts();
  166.         return $this->render('blog/months.html.twig', [
  167.             'months' => $months,
  168.         ]);
  169.     }
  170.     public function commentsAction($post)
  171.     {
  172.         $em $this->getEm();
  173.         $comments $em->getRepository('App\Entity\Blog\Comment')->findBy(['post' => $post'approved' => true]);
  174.         return $this->render('blog/comments.html.twig', [
  175.             'comments' => $comments,
  176.         ]);
  177.     }
  178.     public function categoriesHtml($post)
  179.     {
  180.         $arr = [];
  181.         foreach ($post->getCategories() as $category) {
  182.             $arr[] = '<a href="'.$this->generateUrl('show_posts_by_category', ['slug' => $category->getSlug()]).'" class="category-link">'.$category->getName().'</a>';
  183.         }
  184.         return implode(', '$arr);
  185.     }
  186. }