PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/forkcms/forkcms
PHP | 79 lines | 61 code | 14 blank | 4 comment | 3 complexity | 8adc507c81bba8ec878ab25d8100fa4c 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\Engine\Navigation as FrontendNavigation;
  5. use Frontend\Core\Language\Language;
  6. use Frontend\Modules\Blog\Engine\Model as FrontendBlogModel;
  7. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  8. class Index extends FrontendBaseBlock
  9. {
  10. /** @var array */
  11. private $articles;
  12. public function execute(): void
  13. {
  14. parent::execute();
  15. $this->loadTemplate();
  16. $this->getData();
  17. $this->parse();
  18. }
  19. private function buildPaginationConfig(): array
  20. {
  21. $requestedPage = $this->url->getParameter('page', 'int', 1);
  22. $numberOfItems = FrontendBlogModel::getAllCount();
  23. $limit = $this->get('fork.settings')->get($this->getModule(), 'overview_num_items', 10);
  24. $numberOfPages = (int) ceil($numberOfItems / $limit);
  25. if ($numberOfPages === 0) {
  26. $numberOfPages = 1;
  27. }
  28. // Check if the page exists
  29. if ($requestedPage > $numberOfPages || $requestedPage < 1) {
  30. throw new NotFoundHttpException();
  31. }
  32. return [
  33. 'url' => FrontendNavigation::getUrlForBlock($this->getModule()),
  34. 'limit' => $limit,
  35. 'offset' => ($requestedPage * $limit) - $limit,
  36. 'requested_page' => $requestedPage,
  37. 'num_items' => $numberOfItems,
  38. 'num_pages' => $numberOfPages,
  39. ];
  40. }
  41. private function getData(): void
  42. {
  43. $this->pagination = $this->buildPaginationConfig();
  44. $this->articles = FrontendBlogModel::getAll($this->pagination['limit'], $this->pagination['offset']);
  45. }
  46. private function addLinksToRssFeeds(): void
  47. {
  48. // General rss feed
  49. $this->header->addRssLink(
  50. $this->get('fork.settings')->get($this->getModule(), 'rss_title_' . LANGUAGE, SITE_DEFAULT_TITLE),
  51. FrontendNavigation::getUrlForBlock($this->getModule(), 'Rss')
  52. );
  53. // Rss feed for the comments of this blog
  54. $this->header->addRssLink(
  55. Language::lbl('RecentComments'),
  56. FrontendNavigation::getUrlForBlock($this->getModule(), 'CommentsRss')
  57. );
  58. }
  59. private function parse(): void
  60. {
  61. $this->addLinksToRssFeeds();
  62. $this->parsePagination();
  63. $this->template->assign('items', $this->articles);
  64. }
  65. }