PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/src/Frontend/Modules/Blog/Actions/Category.php

http://github.com/forkcms/forkcms
PHP | 118 lines | 93 code | 22 blank | 3 comment | 5 complexity | 0a0594035b597679b7b1e4d3ccecf0b4 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, MIT, AGPL-3.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. namespace Frontend\Modules\Blog\Actions;
  3. use Frontend\Core\Engine\Base\Block as FrontendBaseBlock;
  4. use Frontend\Core\Language\Language as FL;
  5. use Frontend\Core\Engine\Navigation as FrontendNavigation;
  6. use Frontend\Modules\Blog\Engine\Model as FrontendBlogModel;
  7. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  8. class Category extends FrontendBaseBlock
  9. {
  10. /** @var array */
  11. private $articles;
  12. /** @var array */
  13. private $category;
  14. public function execute(): void
  15. {
  16. parent::execute();
  17. $this->loadTemplate();
  18. $this->getData();
  19. $this->parse();
  20. }
  21. private function getCategory(): array
  22. {
  23. $slug = $this->url->getParameter(1);
  24. if (empty($slug)) {
  25. throw new NotFoundHttpException();
  26. }
  27. $category = FrontendBlogModel::getCategory($slug);
  28. if (empty($category)) {
  29. throw new NotFoundHttpException();
  30. }
  31. return $category;
  32. }
  33. private function buildUrl(): string
  34. {
  35. return FrontendNavigation::getUrlForBlock($this->getModule(), $this->getAction())
  36. . '/' . $this->category['url'];
  37. }
  38. private function buildPaginationConfig(): array
  39. {
  40. $requestedPage = $this->url->getParameter('page', 'int', 1);
  41. $numberOfItems = FrontendBlogModel::getAllForCategoryCount($this->category['url']);
  42. $limit = $this->get('fork.settings')->get($this->getModule(), 'overview_num_items', 10);
  43. $numberOfPages = (int) ceil($numberOfItems / $limit);
  44. if ($numberOfPages === 0) {
  45. $numberOfPages = 1;
  46. }
  47. // Check if the page exists
  48. if ($requestedPage > $numberOfPages || $requestedPage < 1) {
  49. throw new NotFoundHttpException();
  50. }
  51. return [
  52. 'url' => $this->buildUrl(),
  53. 'limit' => $limit,
  54. 'offset' => ($requestedPage * $limit) - $limit,
  55. 'requested_page' => $requestedPage,
  56. 'num_items' => $numberOfItems,
  57. 'num_pages' => $numberOfPages,
  58. ];
  59. }
  60. private function getData(): void
  61. {
  62. $this->category = $this->getCategory();
  63. $this->pagination = $this->buildPaginationConfig();
  64. $this->articles = FrontendBlogModel::getAllForCategory(
  65. $this->category['url'],
  66. $this->pagination['limit'],
  67. $this->pagination['offset']
  68. );
  69. }
  70. private function addLinkToRssFeed(): void
  71. {
  72. $this->header->addRssLink(
  73. $this->get('fork.settings')->get($this->getModule(), 'rss_title_' . LANGUAGE, SITE_DEFAULT_TITLE),
  74. FrontendNavigation::getUrlForBlock($this->getModule(), 'Rss')
  75. );
  76. }
  77. private function addCategoryToBreadcrumb(): void
  78. {
  79. $this->breadcrumb->addElement(\SpoonFilter::ucfirst(FL::lbl('Category')));
  80. $this->breadcrumb->addElement($this->category['label']);
  81. }
  82. private function setPageTitle(): void
  83. {
  84. $this->header->setPageTitle(\SpoonFilter::ucfirst(FL::lbl('Category')));
  85. }
  86. private function parse(): void
  87. {
  88. $this->addLinkToRssFeed();
  89. $this->addCategoryToBreadcrumb();
  90. $this->setPageTitle();
  91. $this->parsePagination();
  92. $this->template->assign('category', $this->category);
  93. $this->template->assign('items', $this->articles);
  94. $this->setMeta($this->category['meta']);
  95. }
  96. }