<?php
namespace App\Controller\Blog;
use App\Entity\Blog\Comment;
use App\Form\Blog\CommentFormType;
use App\Form\Blog\SearchFormType;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
/**
* @Route("/{_locale}/blog", requirements={"_locale" = "es|en"})
*/
class DefaultController extends Controller
{
public function getEm()
{
return $this->getDoctrine()->getManager();
}
/**
* @Route("/", name="blog_index")
* @Template()
*/
public function indexAction(Request $request)
{
$em = $this->getEm();
$page = $request->query->getInt('page', 1);
$perPage = 9;
$posts = $em->getRepository('App\Entity\Blog\Post')->findBy(['enabled' => true], ['highlight' => 'desc', 'created' => 'desc']);
// Search posts
$form = $this->createForm(SearchFormType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
$keywords = $data['q'];
$posts = $em->getRepository('App\Entity\Blog\Post')->searchPosts($keywords);
}
// Get paginated collection
$postsPager = $this->get('knp_paginator')->paginate($posts, $page, $perPage);
return $this->render('blog/index.html.twig', [
'posts' => $postsPager,
'form' => $form->createView(),
]);
}
/**
* @Route("/{year}/{month}/{day}/{slug}", name="show_post", requirements={"year" = "\d+", "month": "\d+", "day": "\d+"})
* @Template()
*
* @param mixed $year
* @param mixed $month
* @param mixed $day
* @param mixed $slug
*/
public function postAction(Request $request, $year, $month, $day, $slug)
{
$em = $this->getEm();
$date = new \DateTime($year.'-'.$month.'-'.$day);
$post = $em->getRepository('App\Entity\Blog\Post')->findOneBy(['slug' => $slug, 'date' => $date]);
$comment = (new Comment())->setPost($post);
$form = $this->createForm(CommentFormType::class, $comment);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$comment->setPost($post);
$em->persist($comment);
$em->flush();
// Trigger email to admin to approve?
$this->addFlash('success', 'Gracias por su comentario. Se publicará una vez aprobado por nuestros administradores.');
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')]));
}
if (!$post) {
throw new NotFoundHttpException(sprintf('Unable to find post for : %s', $slug));
}
return $this->render('blog/post.html.twig', [
'commentForm' => $form->createView(),
'post' => $post,
'categoriesHtml' => $this->categoriesHtml($post),
'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),
]);
}
/**
* @Route("/{slug}", name="show_posts_by_category")
* @Template()
*
* @param mixed $slug
*/
public function byCategoryAction(Request $request, $slug)
{
$em = $this->getEm();
$category = $em->getRepository('App\Entity\Blog\Category')->findOneBy(['slug' => $slug]);
$posts = $em->getRepository('App\Entity\Blog\Post')->getPostsInCategory($category->getId());
$page = $request->query->getInt('page', 1);
$perPage = 9;
// Search posts
$form = $this->createForm(SearchFormType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
$keywords = $data['q'];
$posts = $em->getRepository('App\Entity\Blog\Post')->searchPosts($keywords);
}
// Get paginated collection
$postsPager = $this->get('knp_paginator')->paginate($posts, $page, $perPage);
return $this->render('blog/byCategory.html.twig', [
'category' => $category,
'posts' => $postsPager,
'form' => $form->createView(),
]);
}
/**
* @Route("/{year}/{month}", name="show_posts_by_month", requirements={"year" = "\d+", "month": "\d+"})
* @Template()
*
* @param mixed $year
* @param mixed $month
*/
public function byMonthAction(Request $request, $year, $month)
{
$em = $this->getEm();
$date = new \DateTime($year.'-'.$month.'-01');
$posts = $em->getRepository('App\Entity\Blog\Post')->getPostsInMonth($year, $month);
$page = $request->query->getInt('page', 1);
$perPage = 9;
// Search posts
$form = $this->createForm(SearchFormType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
$keywords = $data['q'];
$posts = $em->getRepository('App\Entity\Blog\Post')->searchPosts($keywords);
}
// Get paginated collection
$postsPager = $this->get('knp_paginator')->paginate($posts, $page, $perPage);
return $this->render('blog/byMonth.html.twig', [
'date' => $date,
'posts' => $postsPager,
'form' => $form->createView(),
]);
}
public function categoriesAction()
{
$em = $this->getEm();
$categories = $em->getRepository('App\Entity\Blog\Category')->findBy(['enabled' => true]);
$cats = [];
foreach ($categories as $category) {
if (count($category->getPosts()) > 0) {
$cats[$category->getSlug()] = $category->getName();
}
}
return $this->render('blog/categories.html.twig', [
'categories' => $cats,
]);
}
public function recentPostsAction()
{
$em = $this->getEm();
$posts = $em->getRepository('App\Entity\Blog\Post')->findBy(['enabled' => true], ['created' => 'desc'], 5);
return $this->render('blog/recent.html.twig', [
'posts' => $posts,
]);
}
public function monthsAction()
{
$em = $this->getEm();
$months = $em->getRepository('App\Entity\Blog\Post')->getMonthsWithPosts();
return $this->render('blog/months.html.twig', [
'months' => $months,
]);
}
public function commentsAction($post)
{
$em = $this->getEm();
$comments = $em->getRepository('App\Entity\Blog\Comment')->findBy(['post' => $post, 'approved' => true]);
return $this->render('blog/comments.html.twig', [
'comments' => $comments,
]);
}
public function categoriesHtml($post)
{
$arr = [];
foreach ($post->getCategories() as $category) {
$arr[] = '<a href="'.$this->generateUrl('show_posts_by_category', ['slug' => $category->getSlug()]).'" class="category-link">'.$category->getName().'</a>';
}
return implode(', ', $arr);
}
}