/wolf/plugins/archive/archive.php

https://github.com/silentworks/wolfcms · PHP · 181 lines · 107 code · 43 blank · 31 comment · 18 complexity · 20c3c9cc54dc5d044f52bbc55ea4f22c MD5 · raw file

  1. <?php
  2. /*
  3. * Wolf CMS - Content Management Simplified. <http://www.wolfcms.org>
  4. * Copyright (C) 2009-2011 Martijn van der Kleijn <martijn.niji@gmail.com>
  5. * Copyright (C) 2008 Philippe Archambault <philippe.archambault@gmail.com>
  6. *
  7. * This file is part of Wolf CMS. Wolf CMS is licensed under the GNU GPLv3 license.
  8. * Please see license.txt for the full license text.
  9. */
  10. /* Security measure */
  11. if (!defined('IN_CMS')) {
  12. exit();
  13. }
  14. /**
  15. * The Archive plugin provides an Archive pagetype behaving similar to a blog or news archive.
  16. *
  17. * @package Plugins
  18. * @subpackage archive
  19. *
  20. * @author Martijn van der Kleijn <martijn.niji@gmail.com>
  21. * @copyright Martijn van der Kleijn, 2011
  22. * @author Philippe Archambault <philippe.archambault@gmail.com>
  23. * @copyright Philippe Archambault, 2008
  24. * @license http://www.gnu.org/licenses/gpl.html GPLv3 license
  25. */
  26. /**
  27. * The Archive class...
  28. */
  29. class Archive {
  30. public function __construct(&$page, $params) {
  31. $this->page = & $page;
  32. $this->params = $params;
  33. switch (count($params)) {
  34. case 0: break;
  35. case 1:
  36. if (strlen((int) $params[0]) == 4)
  37. $this->_archiveBy('year', $params);
  38. else
  39. $this->_displayPage($params[0]);
  40. break;
  41. case 2:
  42. $this->_archiveBy('month', $params);
  43. break;
  44. case 3:
  45. $this->_archiveBy('day', $params);
  46. break;
  47. case 4:
  48. $this->_displayPage($params[3]);
  49. break;
  50. default:
  51. pageNotFound();
  52. }
  53. }
  54. private function _archiveBy($interval, $params) {
  55. $this->interval = $interval;
  56. global $__CMS_CONN__;
  57. $page = $this->page->children(array(
  58. 'where' => "behavior_id = 'archive_{$interval}_index'",
  59. 'limit' => 1
  60. ), array(), true);
  61. if ($page instanceof Page) {
  62. $this->page = $page;
  63. $month = isset($params[1]) ? (int) $params[1] : 1;
  64. $day = isset($params[2]) ? (int) $params[2] : 1;
  65. $this->page->time = mktime(0, 0, 0, $month, $day, (int) $params[0]);
  66. } else {
  67. pageNotFound();
  68. }
  69. }
  70. private function _displayPage($slug) {
  71. if (!$this->page = Page::findBySlug($slug, $this->page))
  72. pageNotFound($slug);
  73. }
  74. function get() {
  75. $date = join('-', $this->params);
  76. $pages = $this->page->parent()->children(array(
  77. 'where' => "page.created_on LIKE '{$date}%'",
  78. 'order' => 'page.created_on DESC'
  79. ));
  80. return $pages;
  81. }
  82. function archivesByYear() {
  83. global $__CMS_CONN__;
  84. $out = array();
  85. $sql = "SELECT DISTINCT(DATE_FORMAT(created_on, '%Y')) FROM " . TABLE_PREFIX . "page WHERE parent_id=? AND status_id != " . Page::STATUS_HIDDEN . " ORDER BY created_on DESC";
  86. $stmt = $__CMS_CONN__->prepare($sql);
  87. $stmt->execute(array($this->page->id));
  88. while ($date = $stmt->fetchColumn())
  89. $out[] = $date;
  90. return $out;
  91. }
  92. function archivesByMonth($year='all') {
  93. global $__CMS_CONN__;
  94. $out = array();
  95. $sql = "SELECT DISTINCT(DATE_FORMAT(created_on, '%Y/%m')) FROM " . TABLE_PREFIX . "page WHERE parent_id=? AND status_id != " . Page::STATUS_HIDDEN . " ORDER BY created_on DESC";
  96. $stmt = $__CMS_CONN__->prepare($sql);
  97. $stmt->execute(array($this->page->id));
  98. while ($date = $stmt->fetchColumn())
  99. $out[] = $date;
  100. return $out;
  101. }
  102. function archivesByDay($year='all') {
  103. global $__CMS_CONN__;
  104. $out = array();
  105. if ($year == 'all')
  106. $year = '';
  107. $sql = "SELECT DISTINCT(DATE_FORMAT(created_on, '%Y/%m/%d')) FROM " . TABLE_PREFIX . "page WHERE parent_id=? AND status_id != " . Page::STATUS_HIDDEN . " ORDER BY created_on DESC";
  108. $stmt = $__CMS_CONN__->prepare($sql);
  109. $stmt->execute(array($this->page->id));
  110. while ($date = $stmt->fetchColumn())
  111. $out[] = $date;
  112. return $out;
  113. }
  114. }
  115. class PageArchive extends Page {
  116. /**
  117. * Returns the current PageArchive object's url.
  118. *
  119. * Note: overrides the Page::url() method.
  120. *
  121. * @return string A fully qualified url.
  122. */
  123. public function url($suffix=false) {
  124. $use_date = Plugin::getSetting('use_dates', 'archive');
  125. if ($use_date === '1') {
  126. return BASE_URL . trim($this->parent()->uri() . date('/Y/m/d/', strtotime($this->created_on)) . $this->slug, '/') . ($this->uri() != '' ? URL_SUFFIX : '');
  127. }
  128. elseif ($use_date === '0') {
  129. return BASE_URL . trim($this->parent()->uri() . '/' . $this->slug, '/') . ($this->uri() != '' ? URL_SUFFIX : '');
  130. }
  131. }
  132. public function title() {
  133. return isset($this->time) ? strftime($this->title, $this->time) : $this->title;
  134. }
  135. public function breadcrumb() {
  136. return isset($this->time) ? strftime($this->breadcrumb, $this->time) : $this->breadcrumb;
  137. }
  138. }