lib/composer/BlogBundle/src/VisualMedia/BlogBundle/Controller/BlogController.php line 42

Open in your IDE?
  1. <?php
  2. /**
  3. * Blog Controller
  4. *
  5. * @author Vincent van Waasbergen <v.vanwaasbergen@visualmedia.nl>
  6. */
  7. namespace VisualMedia\BlogBundle\Controller;
  8. use DateTime;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use VisualMedia\LisaBundle\Controller\BaseController;
  12. use VisualMedia\BlogBundle\Manager\BaseBlogCategoryManager;
  13. use VisualMedia\BlogBundle\Entity\BaseBlogCategory;
  14. use VisualMedia\BlogBundle\Entity\BaseBlogPost;
  15. use VisualMedia\BlogBundle\Form\Type\BlogCategoryType;
  16. use VisualMedia\PageBundle\Manager\BasePageManager;
  17. use VisualMedia\TranslationBundle\Service\ChannelProvider;
  18. /**
  19. * Blog Controller
  20. */
  21. class BlogController extends BaseController
  22. {
  23. /**
  24. * @param Request $request
  25. * @return Response
  26. */
  27. public function indexAction(Request $request): Response
  28. {
  29. $now = new DateTime();
  30. $translator = $this->get('translator');
  31. $em = $this->getDoctrine()->getManager();
  32. $pageManager = $this->get(BasePageManager::class);
  33. $channelProvider = $this->get(ChannelProvider::class);
  34. $title = $translator->trans('blog.title', [], 'pages');
  35. $page = $pageManager->getOrCreatePage('blog', [
  36. 'title' => $title,
  37. 'published' => true,
  38. 'indexable' => true,
  39. 'browserTitle' => $title,
  40. 'metaDescription' => sprintf('%s.', $title),
  41. ]);
  42. $channel = $channelProvider->getCurrentChannel();
  43. $qb = $em->createQueryBuilder();
  44. $qb->select('bc, _posts, _post');
  45. $qb->from(BaseBlogCategory::class, 'bc');
  46. $qb->leftJoin('bc.channels', '_channels');
  47. $qb->leftJoin('bc.posts', '_posts');
  48. $qb->leftJoin('_posts.post', '_post');
  49. $qb->andWhere($qb->expr()->eq('bc.published', true));
  50. $qb->andWhere($qb->expr()->isNull('bc.parent'));
  51. $qb->andWhere('_post.published = 1');
  52. $qb->andWhere($qb->expr()->orX(...[
  53. $qb->expr()->isNull('_post.publicationDate'),
  54. $qb->expr()->lte('_post.publicationDate', $qb->expr()->literal((string) $now->format('Y-m-d H:i:s'))),
  55. ]));
  56. $qb->andWhere($qb->expr()->orX(...[
  57. $qb->expr()->isNull('_post.unpublicationDate'),
  58. $qb->expr()->gte('_post.unpublicationDate', $qb->expr()->literal((string) $now->format('Y-m-d H:i:s'))),
  59. ]));
  60. $qb->andWhere(sprintf('_channels.id = %d', $channel->id));
  61. $qb->orderBy('bc.ordering');
  62. $categories = $qb->getQuery()->getResult();
  63. return $this->render('@VisualMediaBlog/Blog/blog_index.html.twig', [
  64. 'browser_title' => $page->browserTitle,
  65. 'meta_description' => $page->metaDescription,
  66. 'meta_keywords' => $page->metaKeywords,
  67. 'indexable' => $page->indexable,
  68. 'page' => $page,
  69. 'categories' => $categories,
  70. ]);
  71. }
  72. /**
  73. * @param Request $request
  74. * @param string $categorySlug
  75. * @return Response
  76. */
  77. public function categoryAction(Request $request, string $categorySlug): Response
  78. {
  79. $now = new DateTime();
  80. $translator = $this->get('translator');
  81. $em = $this->getDoctrine()->getManager();
  82. $pageManager = $this->get(BasePageManager::class);
  83. $channelProvider = $this->get(ChannelProvider::class);
  84. $title = $translator->trans('blog.title', [], 'pages');
  85. $page = $pageManager->getOrCreatePage('blog', [
  86. 'title' => $title,
  87. 'published' => true,
  88. 'indexable' => true,
  89. 'browserTitle' => $title,
  90. 'metaDescription' => sprintf('%s.', $title),
  91. ]);
  92. $channel = $channelProvider->getCurrentChannel();
  93. $qb = $em->createQueryBuilder();
  94. $qb->select('bc');
  95. $qb->from(BaseBlogCategory::class, 'bc');
  96. $qb->leftJoin('bc.channels', '_channels');
  97. $qb->leftJoin('bc.contents', '_contents');
  98. $qb->andWhere($qb->expr()->eq('_contents.slug', $qb->expr()->literal((string) $categorySlug)));
  99. $qb->andWhere($qb->expr()->eq('bc.published', true));
  100. $qb->andWhere(sprintf('_channels.id = %d', $channel->id));
  101. $qb->orderBy('bc.ordering');
  102. if (null === $category = $qb->getQuery()->setMaxResults(1)->getOneOrNullResult()) {
  103. throw $this->createNotFoundException($translator->trans('blog_category.not_found', [], 'exception'));
  104. }
  105. $qb = $em->createQueryBuilder();
  106. $qb->select('bp');
  107. $qb->from(BaseBlogPost::class, 'bp');
  108. $qb->leftJoin('bp.channels', '_channels');
  109. $qb->leftJoin('bp.categories', '_categories');
  110. $qb->leftJoin('_categories.category', '_category');
  111. $qb->andWhere($qb->expr()->eq('_category.id', $category->id));
  112. $qb->andWhere($qb->expr()->eq('bp.published', true));
  113. $qb->andWhere($qb->expr()->orX(...[
  114. $qb->expr()->isNull('bp.publicationDate'),
  115. $qb->expr()->lte('bp.publicationDate', $qb->expr()->literal((string) $now->format('Y-m-d H:i:s'))),
  116. ]));
  117. $qb->andWhere($qb->expr()->orX(...[
  118. $qb->expr()->isNull('bp.unpublicationDate'),
  119. $qb->expr()->gte('bp.unpublicationDate', $qb->expr()->literal((string) $now->format('Y-m-d H:i:s'))),
  120. ]));
  121. $qb->andWhere(sprintf('_channels.id = %d', $channel->id));
  122. $qb->orderBy('bp.publicationDate');
  123. $posts = $qb->getQuery()->getResult();
  124. return $this->render('@VisualMediaBlog/Blog/blog_category.html.twig', [
  125. 'browser_title' => $category->browserTitle,
  126. 'meta_description' => $category->metaDescription,
  127. 'meta_keywords' => $category->metaKeywords,
  128. 'indexable' => $category->indexable,
  129. 'page' => $page,
  130. 'category' => $category,
  131. 'posts' => $posts,
  132. ]);
  133. }
  134. /**
  135. * @param Request $request
  136. * @param string $categorySlug
  137. * @param string $postSlug
  138. * @return Response
  139. */
  140. public function postAction(Request $request, string $categorySlug, string $postSlug): Response
  141. {
  142. $now = new DateTime();
  143. $translator = $this->get('translator');
  144. $em = $this->getDoctrine()->getManager();
  145. $pageManager = $this->get(BasePageManager::class);
  146. $channelProvider = $this->get(ChannelProvider::class);
  147. $title = $translator->trans('blog.title', [], 'pages');
  148. $page = $pageManager->getOrCreatePage('blog', [
  149. 'title' => $title,
  150. 'published' => true,
  151. 'indexable' => true,
  152. 'browserTitle' => $title,
  153. 'metaDescription' => sprintf('%s.', $title),
  154. ]);
  155. $channel = $channelProvider->getCurrentChannel();
  156. $qb = $em->createQueryBuilder();
  157. $qb->select('bc');
  158. $qb->from(BaseBlogCategory::class, 'bc');
  159. $qb->leftJoin('bc.channels', '_channels');
  160. $qb->leftJoin('bc.contents', '_contents');
  161. $qb->andWhere($qb->expr()->eq('_contents.slug', $qb->expr()->literal((string) $categorySlug)));
  162. $qb->andWhere($qb->expr()->eq('bc.published', true));
  163. $qb->andWhere(sprintf('_channels.id = %d', $channel->id));
  164. $qb->orderBy('bc.ordering');
  165. if (null === $category = $qb->getQuery()->setMaxResults(1)->getOneOrNullResult()) {
  166. throw $this->createNotFoundException($translator->trans('blog_category.not_found', [], 'exception'));
  167. }
  168. $qb = $em->createQueryBuilder();
  169. $qb->select('bp');
  170. $qb->from(BaseBlogPost::class, 'bp');
  171. $qb->leftJoin('bp.channels', '_channels');
  172. $qb->leftJoin('bp.contents', '_contents');
  173. $qb->andWhere($qb->expr()->eq('_contents.slug', $qb->expr()->literal((string) $postSlug)));
  174. $qb->andWhere($qb->expr()->eq('bp.published', true));
  175. $qb->andWhere($qb->expr()->orX(...[
  176. $qb->expr()->isNull('bp.publicationDate'),
  177. $qb->expr()->lte('bp.publicationDate', $qb->expr()->literal((string) $now->format('Y-m-d H:i:s'))),
  178. ]));
  179. $qb->andWhere($qb->expr()->orX(...[
  180. $qb->expr()->isNull('bp.unpublicationDate'),
  181. $qb->expr()->gte('bp.unpublicationDate', $qb->expr()->literal((string) $now->format('Y-m-d H:i:s'))),
  182. ]));
  183. $qb->andWhere(sprintf('_channels.id = %d', $channel->id));
  184. $qb->orderBy('bp.publicationDate');
  185. if (null === $post = $qb->getQuery()->setMaxResults(1)->getOneOrNullResult()) {
  186. throw $this->createNotFoundException($translator->trans('blog_post.not_found', [], 'exception'));
  187. }
  188. return $this->render('@VisualMediaBlog/Blog/blog_post.html.twig', [
  189. 'browser_title' => $post->browserTitle,
  190. 'meta_description' => $post->metaDescription,
  191. 'meta_keywords' => $post->metaKeywords,
  192. 'indexable' => $post->indexable,
  193. 'page' => $page,
  194. 'category' => $category,
  195. 'post' => $post,
  196. ]);
  197. }
  198. }