PageRenderTime 46ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/frontend/modules/blog/actions/index.php

http://github.com/forkcms/forkcms
PHP | 92 lines | 34 code | 15 blank | 43 comment | 6 complexity | d3ce2375a8983f444176925ddca96f25 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. /*
  3. * This file is part of Fork CMS.
  4. *
  5. * For the full copyright and license information, please view the license
  6. * file that was distributed with this source code.
  7. */
  8. /**
  9. * This is the overview-action
  10. *
  11. * @author Tijs Verkoyen <tijs@sumocoders.be>
  12. * @author Davy Hellemans <davy.hellemans@netlash.com>
  13. */
  14. class FrontendBlogIndex extends FrontendBaseBlock
  15. {
  16. /**
  17. * The articles
  18. *
  19. * @var array
  20. */
  21. private $items;
  22. /**
  23. * The pagination array
  24. * It will hold all needed parameters, some of them need initialization.
  25. *
  26. * @var array
  27. */
  28. protected $pagination = array('limit' => 10, 'offset' => 0, 'requested_page' => 1, 'num_items' => null, 'num_pages' => null);
  29. /**
  30. * Execute the extra
  31. */
  32. public function execute()
  33. {
  34. parent::execute();
  35. $this->loadTemplate();
  36. $this->getData();
  37. $this->parse();
  38. }
  39. /**
  40. * Load the data, don't forget to validate the incoming data
  41. */
  42. private function getData()
  43. {
  44. // requested page
  45. $requestedPage = $this->URL->getParameter('page', 'int', 1);
  46. // set URL and limit
  47. $this->pagination['url'] = FrontendNavigation::getURLForBlock('blog');
  48. $this->pagination['limit'] = FrontendModel::getModuleSetting('blog', 'overview_num_items', 10);
  49. // populate count fields in pagination
  50. $this->pagination['num_items'] = FrontendBlogModel::getAllCount();
  51. $this->pagination['num_pages'] = (int) ceil($this->pagination['num_items'] / $this->pagination['limit']);
  52. // num pages is always equal to at least 1
  53. if($this->pagination['num_pages'] == 0) $this->pagination['num_pages'] = 1;
  54. // redirect if the request page doesn't exist
  55. if($requestedPage > $this->pagination['num_pages'] || $requestedPage < 1) $this->redirect(FrontendNavigation::getURL(404));
  56. // populate calculated fields in pagination
  57. $this->pagination['requested_page'] = $requestedPage;
  58. $this->pagination['offset'] = ($this->pagination['requested_page'] * $this->pagination['limit']) - $this->pagination['limit'];
  59. // get articles
  60. $this->items = FrontendBlogModel::getAll($this->pagination['limit'], $this->pagination['offset']);
  61. }
  62. /**
  63. * Parse the data into the template
  64. */
  65. private function parse()
  66. {
  67. // get RSS-link
  68. $rssLink = FrontendModel::getModuleSetting('blog', 'feedburner_url_' . FRONTEND_LANGUAGE);
  69. if($rssLink == '') $rssLink = FrontendNavigation::getURLForBlock('blog', 'rss');
  70. // add RSS-feed
  71. $this->header->addLink(array('rel' => 'alternate', 'type' => 'application/rss+xml', 'title' => FrontendModel::getModuleSetting('blog', 'rss_title_' . FRONTEND_LANGUAGE), 'href' => $rssLink), true);
  72. // assign articles
  73. $this->tpl->assign('items', $this->items);
  74. // parse the pagination
  75. $this->parsePagination();
  76. }
  77. }