src/UI/WebBundle/Controller/Blog/PublicBlogArticleController.php line 103

Open in your IDE?
  1. <?php
  2. namespace Whater\UI\WebBundle\Controller\Blog;
  3. use Whater\Application\UseCase\Blog\CommandRequest\GetBlogArticleCategoryForBlogCommand;
  4. use Whater\Application\UseCase\Blog\CommandRequest\GetBlogArticleForBlogCommand;
  5. use Whater\Application\UseCase\Blog\CommandRequest\PublicListBlogArticlesCommand;
  6. use Whater\Domain\User\Model\Role;
  7. use Whater\UI\WebBundle\Controller\AbstractBusController;
  8. use Symfony\Component\HttpFoundation\RedirectResponse;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\Routing\Annotation\Route;
  11. /**
  12. * @Route("public/blob-article")
  13. */
  14. class PublicBlogArticleController extends AbstractBusController
  15. {
  16. /**
  17. * @Route("/{_locale}/list",
  18. * requirements={"_locale" = "en|es"},
  19. * name="web_blog_list_articles",
  20. * defaults={"_format" = "html", "_locale" = "es"})
  21. */
  22. public function publicBlogArticlesAction(Request $request, string $_locale)
  23. {
  24. $pageNumber = $request->query->get('page');
  25. if (!$pageNumber) {
  26. $pageNumber = 1;
  27. }
  28. try {
  29. $publicListBlogArticlesCommand = new PublicListBlogArticlesCommand($pageNumber, 10, 'publish_at', 'desc','show_in','["BLOG"]');
  30. $page = $this->handle($publicListBlogArticlesCommand);
  31. } catch (\Exception $e) {
  32. return new RedirectResponse($this->router()->generate('web_blog_list_articles', array(), true));
  33. }
  34. return $this->render('Blog/articles_list.html.twig', array(
  35. 'articlePage' => $page,
  36. 'locale' => $_locale
  37. ));
  38. }
  39. // public function publicBlogRecentArticlesAction(Request $request, string $_locale)
  40. // {
  41. // return new RedirectResponse($this->router()->generate('web_init', array(), true));
  42. // try {
  43. // $publicListBlogArticlesCommand = new PublicListBlogArticlesCommand(1, 4, 'publish_at', 'desc');
  44. // $page = $this->handle($publicListBlogArticlesCommand);
  45. // } catch (\Exception $e) {
  46. // return '';
  47. // }
  48. // return $this->render('Blog/articles_recent.html.twig', array(
  49. // 'articlePage' => $page,
  50. // 'locale' => $_locale
  51. // ));
  52. // }
  53. /**
  54. * @Route("/{_locale}/list/{categorySlug}",
  55. * requirements={"_locale" = "en|es"},
  56. * name="web_blog_list_articles_category",
  57. * defaults={"_format" = "html", "_locale" = "es"})
  58. */
  59. public function publicBlogArticlesCategoryAction(Request $request, string $_locale, string $categorySlug)
  60. {
  61. $articlePage = null;
  62. $articleCategory = null;
  63. $pageNumber = $request->query->get('page');
  64. if (!$pageNumber) {
  65. $pageNumber = 1;
  66. }
  67. try {
  68. $getBlogArticleCategoryForBlogCommand = new GetBlogArticleCategoryForBlogCommand($categorySlug);
  69. $articleCategory = $this->handle($getBlogArticleCategoryForBlogCommand);
  70. $publicListBlogArticlesCommand = new PublicListBlogArticlesCommand($pageNumber, 10, 'publish_date', '[asc]', 'category', '["' . $articleCategory->id() . '"]');
  71. $articlePage = $this->handle($publicListBlogArticlesCommand);
  72. } catch (\Exception $e) {
  73. return new RedirectResponse($this->router()->generate('web_blog_list_articles', array(), true));
  74. }
  75. return $this->render('Blog/articles_list.html.twig', array(
  76. 'articlePage' => $articlePage,
  77. 'articleCategory' => $articleCategory,
  78. 'locale' => $_locale
  79. ));
  80. }
  81. /**
  82. * @Route("/{_locale}/{articleSlug}",
  83. * requirements={"_locale" = "en|es"},
  84. * name="web_blog_show_article",
  85. * defaults={"_format" = "html", "_locale" = "es"})
  86. */
  87. public function publicBlogArticleAction(Request $request, string $_locale, string $articleSlug)
  88. {
  89. $article = null;
  90. $preview = false;
  91. try {
  92. $grantUser = $this->getGrantedUser();
  93. if ($grantUser != null && $grantUser->hasRole(Role::ROLE_ADMIN)) {
  94. $preview = true;
  95. }
  96. $getBlogArticleForBlogCommand = new GetBlogArticleForBlogCommand($articleSlug, $preview);
  97. $article = $this->handle($getBlogArticleForBlogCommand);
  98. } catch (\Exception $e) {
  99. $this->setFlash('error', $this->translator()->trans('blog.show_article.not_found'));
  100. return new RedirectResponse($this->router()->generate('web_blog_list_articles', array(), true));
  101. }
  102. return $this->render('Blog/article_show.html.twig', array(
  103. 'article' => $article,
  104. 'locale' => $_locale
  105. ));
  106. }
  107. }