<?php
/**
* Blog Controller
*
* @author Vincent van Waasbergen <v.vanwaasbergen@visualmedia.nl>
*/
namespace VisualMedia\BlogBundle\Controller;
use DateTime;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use VisualMedia\LisaBundle\Controller\BaseController;
use VisualMedia\BlogBundle\Manager\BaseBlogCategoryManager;
use VisualMedia\BlogBundle\Entity\BaseBlogCategory;
use VisualMedia\BlogBundle\Entity\BaseBlogPost;
use VisualMedia\BlogBundle\Form\Type\BlogCategoryType;
use VisualMedia\PageBundle\Manager\BasePageManager;
use VisualMedia\TranslationBundle\Service\ChannelProvider;
/**
* Blog Controller
*/
class BlogController extends BaseController
{
/**
* @param Request $request
* @return Response
*/
public function indexAction(Request $request): Response
{
$now = new DateTime();
$translator = $this->get('translator');
$em = $this->getDoctrine()->getManager();
$pageManager = $this->get(BasePageManager::class);
$channelProvider = $this->get(ChannelProvider::class);
$title = $translator->trans('blog.title', [], 'pages');
$page = $pageManager->getOrCreatePage('blog', [
'title' => $title,
'published' => true,
'indexable' => true,
'browserTitle' => $title,
'metaDescription' => sprintf('%s.', $title),
]);
$channel = $channelProvider->getCurrentChannel();
$qb = $em->createQueryBuilder();
$qb->select('bc, _posts, _post');
$qb->from(BaseBlogCategory::class, 'bc');
$qb->leftJoin('bc.channels', '_channels');
$qb->leftJoin('bc.posts', '_posts');
$qb->leftJoin('_posts.post', '_post');
$qb->andWhere($qb->expr()->eq('bc.published', true));
$qb->andWhere($qb->expr()->isNull('bc.parent'));
$qb->andWhere('_post.published = 1');
$qb->andWhere($qb->expr()->orX(...[
$qb->expr()->isNull('_post.publicationDate'),
$qb->expr()->lte('_post.publicationDate', $qb->expr()->literal($now->format('Y-m-d H:i:s'))),
]));
$qb->andWhere($qb->expr()->orX(...[
$qb->expr()->isNull('_post.unpublicationDate'),
$qb->expr()->gte('_post.unpublicationDate', $qb->expr()->literal($now->format('Y-m-d H:i:s'))),
]));
$qb->andWhere(sprintf('_channels.id = %d', $channel->id));
$qb->orderBy('bc.ordering');
$categories = $qb->getQuery()->getResult();
return $this->render('@VisualMediaBlog/Blog/blog_index.html.twig', [
'browser_title' => $page->browserTitle,
'meta_description' => $page->metaDescription,
'meta_keywords' => $page->metaKeywords,
'indexable' => $page->indexable,
'page' => $page,
'categories' => $categories,
]);
}
/**
* @param Request $request
* @param string $categorySlug
* @return Response
*/
public function categoryAction(Request $request, string $categorySlug): Response
{
$now = new DateTime();
$translator = $this->get('translator');
$em = $this->getDoctrine()->getManager();
$pageManager = $this->get(BasePageManager::class);
$channelProvider = $this->get(ChannelProvider::class);
$title = $translator->trans('blog.title', [], 'pages');
$page = $pageManager->getOrCreatePage('blog', [
'title' => $title,
'published' => true,
'indexable' => true,
'browserTitle' => $title,
'metaDescription' => sprintf('%s.', $title),
]);
$channel = $channelProvider->getCurrentChannel();
$qb = $em->createQueryBuilder();
$qb->select('bc');
$qb->from(BaseBlogCategory::class, 'bc');
$qb->leftJoin('bc.channels', '_channels');
$qb->leftJoin('bc.contents', '_contents');
$qb->andWhere($qb->expr()->eq('_contents.slug', $qb->expr()->literal($categorySlug)));
$qb->andWhere($qb->expr()->eq('bc.published', true));
$qb->andWhere(sprintf('_channels.id = %d', $channel->id));
$qb->orderBy('bc.ordering');
if (null === $category = $qb->getQuery()->setMaxResults(1)->getOneOrNullResult()) {
throw $this->createNotFoundException($translator->trans('blog_category.not_found', [], 'exception'));
}
$qb = $em->createQueryBuilder();
$qb->select('bp');
$qb->from(BaseBlogPost::class, 'bp');
$qb->leftJoin('bp.channels', '_channels');
$qb->leftJoin('bp.categories', '_categories');
$qb->leftJoin('_categories.category', '_category');
$qb->andWhere($qb->expr()->eq('_category.id', $category->id));
$qb->andWhere($qb->expr()->eq('bp.published', true));
$qb->andWhere($qb->expr()->orX(...[
$qb->expr()->isNull('bp.publicationDate'),
$qb->expr()->lte('bp.publicationDate', $qb->expr()->literal($now->format('Y-m-d H:i:s'))),
]));
$qb->andWhere($qb->expr()->orX(...[
$qb->expr()->isNull('bp.unpublicationDate'),
$qb->expr()->gte('bp.unpublicationDate', $qb->expr()->literal($now->format('Y-m-d H:i:s'))),
]));
$qb->andWhere(sprintf('_channels.id = %d', $channel->id));
$qb->orderBy('bp.publicationDate');
$posts = $qb->getQuery()->getResult();
return $this->render('@VisualMediaBlog/Blog/blog_category.html.twig', [
'browser_title' => $category->browserTitle,
'meta_description' => $category->metaDescription,
'meta_keywords' => $category->metaKeywords,
'indexable' => $category->indexable,
'page' => $page,
'category' => $category,
'posts' => $posts,
]);
}
/**
* @param Request $request
* @param string $categorySlug
* @param string $postSlug
* @return Response
*/
public function postAction(Request $request, string $categorySlug, string $postSlug): Response
{
$now = new DateTime();
$translator = $this->get('translator');
$em = $this->getDoctrine()->getManager();
$pageManager = $this->get(BasePageManager::class);
$channelProvider = $this->get(ChannelProvider::class);
$title = $translator->trans('blog.title', [], 'pages');
$page = $pageManager->getOrCreatePage('blog', [
'title' => $title,
'published' => true,
'indexable' => true,
'browserTitle' => $title,
'metaDescription' => sprintf('%s.', $title),
]);
$channel = $channelProvider->getCurrentChannel();
$qb = $em->createQueryBuilder();
$qb->select('bc');
$qb->from(BaseBlogCategory::class, 'bc');
$qb->leftJoin('bc.channels', '_channels');
$qb->leftJoin('bc.contents', '_contents');
$qb->andWhere($qb->expr()->eq('_contents.slug', $qb->expr()->literal($categorySlug)));
$qb->andWhere($qb->expr()->eq('bc.published', true));
$qb->andWhere(sprintf('_channels.id = %d', $channel->id));
$qb->orderBy('bc.ordering');
if (null === $category = $qb->getQuery()->setMaxResults(1)->getOneOrNullResult()) {
throw $this->createNotFoundException($translator->trans('blog_category.not_found', [], 'exception'));
}
$qb = $em->createQueryBuilder();
$qb->select('bp');
$qb->from(BaseBlogPost::class, 'bp');
$qb->leftJoin('bp.channels', '_channels');
$qb->leftJoin('bp.contents', '_contents');
$qb->andWhere($qb->expr()->eq('_contents.slug', $qb->expr()->literal($postSlug)));
$qb->andWhere($qb->expr()->eq('bp.published', true));
$qb->andWhere($qb->expr()->orX(...[
$qb->expr()->isNull('bp.publicationDate'),
$qb->expr()->lte('bp.publicationDate', $qb->expr()->literal($now->format('Y-m-d H:i:s'))),
]));
$qb->andWhere($qb->expr()->orX(...[
$qb->expr()->isNull('bp.unpublicationDate'),
$qb->expr()->gte('bp.unpublicationDate', $qb->expr()->literal($now->format('Y-m-d H:i:s'))),
]));
$qb->andWhere(sprintf('_channels.id = %d', $channel->id));
$qb->orderBy('bp.publicationDate');
if (null === $post = $qb->getQuery()->setMaxResults(1)->getOneOrNullResult()) {
throw $this->createNotFoundException($translator->trans('blog_post.not_found', [], 'exception'));
}
return $this->render('@VisualMediaBlog/Blog/blog_post.html.twig', [
'browser_title' => $post->browserTitle,
'meta_description' => $post->metaDescription,
'meta_keywords' => $post->metaKeywords,
'indexable' => $post->indexable,
'page' => $page,
'category' => $category,
'post' => $post,
]);
}
}