PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/frontend/modules/blog/actions/category.php

http://github.com/forkcms/forkcms
PHP | 125 lines | 47 code | 23 blank | 55 comment | 8 complexity | e53da4092837e46dcf8d49d8852494df 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 category-action
  10. *
  11. * @author Tijs Verkoyen <tijs@sumocoders.be>
  12. * @author Davy Hellemans <davy.hellemans@netlash.com>
  13. */
  14. class FrontendBlogCategory extends FrontendBaseBlock
  15. {
  16. /**
  17. * The articles
  18. *
  19. * @var array
  20. */
  21. private $items;
  22. /**
  23. * The requested category
  24. *
  25. * @var array
  26. */
  27. private $category;
  28. /**
  29. * The pagination array
  30. * It will hold all needed parameters, some of them need initialization
  31. *
  32. * @var array
  33. */
  34. protected $pagination = array('limit' => 10, 'offset' => 0, 'requested_page' => 1, 'num_items' => null, 'num_pages' => null);
  35. /**
  36. * Execute the extra
  37. */
  38. public function execute()
  39. {
  40. parent::execute();
  41. $this->loadTemplate();
  42. $this->getData();
  43. $this->parse();
  44. }
  45. /**
  46. * Load the data, don't forget to validate the incoming data
  47. */
  48. private function getData()
  49. {
  50. // get categories
  51. $categories = FrontendBlogModel::getAllCategories();
  52. $possibleCategories = array();
  53. foreach($categories as $category) $possibleCategories[$category['url']] = $category['id'];
  54. // requested category
  55. $requestedCategory = SpoonFilter::getValue($this->URL->getParameter(1, 'string'), array_keys($possibleCategories), 'false');
  56. // requested page
  57. $requestedPage = $this->URL->getParameter('page', 'int', 1);
  58. // validate category
  59. if($requestedCategory == 'false') $this->redirect(FrontendNavigation::getURL(404));
  60. // set category
  61. $this->category = $categories[$possibleCategories[$requestedCategory]];
  62. // set URL and limit
  63. $this->pagination['url'] = FrontendNavigation::getURLForBlock('blog', 'category') . '/' . $requestedCategory;
  64. $this->pagination['limit'] = FrontendModel::getModuleSetting('blog', 'overview_num_items', 10);
  65. // populate count fields in pagination
  66. $this->pagination['num_items'] = FrontendBlogModel::getAllForCategoryCount($requestedCategory);
  67. $this->pagination['num_pages'] = (int) ceil($this->pagination['num_items'] / $this->pagination['limit']);
  68. // redirect if the request page doesn't exists
  69. if($requestedPage > $this->pagination['num_pages'] || $requestedPage < 1) $this->redirect(FrontendNavigation::getURL(404));
  70. // populate calculated fields in pagination
  71. $this->pagination['requested_page'] = $requestedPage;
  72. $this->pagination['offset'] = ($this->pagination['requested_page'] * $this->pagination['limit']) - $this->pagination['limit'];
  73. // get articles
  74. $this->items = FrontendBlogModel::getAllForCategory($requestedCategory, $this->pagination['limit'], $this->pagination['offset']);
  75. }
  76. /**
  77. * Parse the data into the template
  78. */
  79. private function parse()
  80. {
  81. // get RSS-link
  82. $rssLink = FrontendModel::getModuleSetting('blog', 'feedburner_url_' . FRONTEND_LANGUAGE);
  83. if($rssLink == '') $rssLink = FrontendNavigation::getURLForBlock('blog', 'rss');
  84. // add RSS-feed
  85. $this->header->addLink(array('rel' => 'alternate', 'type' => 'application/rss+xml', 'title' => FrontendModel::getModuleSetting('blog', 'rss_title_' . FRONTEND_LANGUAGE), 'href' => $rssLink), true);
  86. // add into breadcrumb
  87. $this->breadcrumb->addElement(SpoonFilter::ucfirst(FL::lbl('Category')));
  88. $this->breadcrumb->addElement($this->category['label']);
  89. // set pageTitle
  90. $this->header->setPageTitle(SpoonFilter::ucfirst(FL::lbl('Category')));
  91. $this->header->setPageTitle($this->category['label']);
  92. // advanced SEO-attributes
  93. if(isset($this->category['meta_data']['seo_index'])) $this->header->addMetaData(array('name' => 'robots', 'content' => $this->category['meta_data']['seo_index']));
  94. if(isset($this->category['meta_data']['seo_follow'])) $this->header->addMetaData(array('name' => 'robots', 'content' => $this->category['meta_data']['seo_follow']));
  95. // assign category
  96. $this->tpl->assign('category', $this->category);
  97. // assign articles
  98. $this->tpl->assign('items', $this->items);
  99. // parse the pagination
  100. $this->parsePagination();
  101. }
  102. }