PageRenderTime 100ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/frontend/modules/blog/actions/archive.php

http://github.com/forkcms/forkcms
PHP | 152 lines | 65 code | 26 blank | 61 comment | 16 complexity | 51e238ce7a6f88ee529519e4b1a6478e 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 archive-action
  10. *
  11. * @author Tijs Verkoyen <tijs@sumocoders.be>
  12. */
  13. class FrontendBlogArchive extends FrontendBaseBlock
  14. {
  15. /**
  16. * The articles
  17. *
  18. * @var array
  19. */
  20. private $items;
  21. /**
  22. * The dates for the archive
  23. *
  24. * @var int
  25. */
  26. private $startDate, $endDate;
  27. /**
  28. * The pagination array
  29. * It will hold all needed parameters, some of them need initialization
  30. *
  31. * @var array
  32. */
  33. protected $pagination = array('limit' => 10, 'offset' => 0, 'requested_page' => 1, 'num_items' => null, 'num_pages' => null);
  34. /**
  35. * The requested year and month
  36. *
  37. * @var int
  38. */
  39. private $year, $month;
  40. /**
  41. * Execute the extra
  42. */
  43. public function execute()
  44. {
  45. parent::execute();
  46. $this->loadTemplate();
  47. $this->getData();
  48. $this->parse();
  49. }
  50. /**
  51. * Load the data, don't forget to validate the incoming data
  52. */
  53. private function getData()
  54. {
  55. // get parameters
  56. $this->year = $this->URL->getParameter(1);
  57. $this->month = $this->URL->getParameter(2);
  58. // redirect /2010/6 to /2010/06 to avoid duplicate content
  59. if($this->month !== null && mb_strlen($this->month) != 2)
  60. {
  61. $queryString = isset($_SERVER['QUERY_STRING']) ? '?' . $_SERVER['QUERY_STRING'] : '';
  62. $this->redirect(FrontendNavigation::getURLForBlock('knowledgebase', 'archive') . '/' . $this->year . '/' . str_pad($this->month, 2, '0', STR_PAD_LEFT) . $queryString, 301);
  63. }
  64. if(mb_strlen($this->year) != 4) $this->redirect(FrontendNavigation::getURL(404));
  65. // redefine
  66. $this->year = (int) $this->year;
  67. if($this->month !== null) $this->month = (int) $this->month;
  68. // validate parameters
  69. if($this->year == 0 || $this->month === 0) $this->redirect(FrontendNavigation::getURL(404));
  70. // requested page
  71. $requestedPage = $this->URL->getParameter('page', 'int', 1);
  72. // rebuild url
  73. $url = $this->year;
  74. // build timestamp
  75. if($this->month !== null)
  76. {
  77. $this->startDate = gmmktime(00, 00, 00, $this->month, 01, $this->year);
  78. $this->endDate = gmmktime(23, 59, 59, $this->month, gmdate('t', $this->startDate), $this->year);
  79. $url .= '/' . str_pad($this->month, 2, '0', STR_PAD_LEFT);
  80. }
  81. // year
  82. else
  83. {
  84. $this->startDate = gmmktime(00, 00, 00, 01, 01, $this->year);
  85. $this->endDate = gmmktime(23, 59, 59, 12, 31, $this->year);
  86. }
  87. // set URL and limit
  88. $this->pagination['url'] = FrontendNavigation::getURLForBlock('blog', 'archive') . '/' . $url;
  89. $this->pagination['limit'] = FrontendModel::getModuleSetting('blog', 'overview_num_items', 10);
  90. // populate count fields in pagination
  91. $this->pagination['num_items'] = FrontendBlogModel::getAllForDateRangeCount($this->startDate, $this->endDate);
  92. $this->pagination['num_pages'] = (int) ceil($this->pagination['num_items'] / $this->pagination['limit']);
  93. // redirect if the request page doesn't exists
  94. if($requestedPage > $this->pagination['num_pages'] || $requestedPage < 1) $this->redirect(FrontendNavigation::getURL(404));
  95. // populate calculated fields in pagination
  96. $this->pagination['requested_page'] = $requestedPage;
  97. $this->pagination['offset'] = ($this->pagination['requested_page'] * $this->pagination['limit']) - $this->pagination['limit'];
  98. // get articles
  99. $this->items = FrontendBlogModel::getAllForDateRange($this->startDate, $this->endDate, $this->pagination['limit'], $this->pagination['offset']);
  100. }
  101. /**
  102. * Parse the data into the template
  103. */
  104. private function parse()
  105. {
  106. // get RSS-link
  107. $rssLink = FrontendModel::getModuleSetting('blog', 'feedburner_url_' . FRONTEND_LANGUAGE);
  108. if($rssLink == '') $rssLink = FrontendNavigation::getURLForBlock('blog', 'rss');
  109. // add RSS-feed
  110. $this->header->addLink(array('rel' => 'alternate', 'type' => 'application/rss+xml', 'title' => FrontendModel::getModuleSetting('blog', 'rss_title_' . FRONTEND_LANGUAGE), 'href' => $rssLink), true);
  111. // add into breadcrumb
  112. $this->breadcrumb->addElement(SpoonFilter::ucfirst(FL::lbl('Archive')));
  113. $this->breadcrumb->addElement($this->year);
  114. if($this->month !== null) $this->breadcrumb->addElement(SpoonDate::getDate('F', $this->startDate, FRONTEND_LANGUAGE, true));
  115. // set pageTitle
  116. $this->header->setPageTitle(SpoonFilter::ucfirst(FL::lbl('Archive')));
  117. $this->header->setPageTitle($this->year);
  118. if($this->month !== null) $this->header->setPageTitle(SpoonDate::getDate('F', $this->startDate, FRONTEND_LANGUAGE, true));
  119. // assign category
  120. $this->tpl->assign('archive', array('start_date' => $this->startDate, 'end_date' => $this->endDate, 'year' => $this->year, 'month' => $this->month));
  121. // assign items
  122. $this->tpl->assign('items', $this->items);
  123. // parse the pagination
  124. $this->parsePagination();
  125. }
  126. }