PageRenderTime 25ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/controllers/components/archive.php

https://github.com/sc0ttyd/utils
PHP | 170 lines | 95 code | 15 blank | 60 comment | 18 complexity | 5f5ff1f16feb530b0e3758f8dfce8988 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright 2007-2010, Cake Development Corporation (http://cakedc.com)
  4. *
  5. * Licensed under The MIT License
  6. * Redistributions of files must retain the above copyright notice.
  7. *
  8. * @copyright Copyright 2007-2010, Cake Development Corporation (http://cakedc.com)
  9. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  10. */
  11. /**
  12. * Utils Plugin
  13. *
  14. * Utils Archive Component
  15. *
  16. * @package utils
  17. * @subpackage utils.controllers.components
  18. */
  19. class ArchiveComponent extends Object {
  20. /**
  21. * Date parameters to find
  22. *
  23. * @var array
  24. */
  25. protected $_parameters = array(
  26. 'year', 'month', 'day');
  27. /**
  28. * Controller reference
  29. *
  30. * @var object
  31. */
  32. public $controller = null;
  33. /**
  34. * Name of model
  35. *
  36. * Customizable in beforeFilter(), or default controller's model name is used
  37. *
  38. * @var string
  39. */
  40. public $modelName = null;
  41. /**
  42. * DateField
  43. *
  44. * @var string
  45. */
  46. public $dateField = 'created';
  47. /**
  48. * Startup method, checks request for required parameters
  49. *
  50. * Builds the pagination conditions
  51. *
  52. * @param object
  53. */
  54. public function startup(&$controller) {
  55. $this->controller =& $controller;
  56. if (empty($this->modelName)) {
  57. $this->modelName = $controller->modelClass;
  58. }
  59. $parsedParams = array();
  60. foreach ($this->_parameters as $param) {
  61. if (isset($controller->params[$param]) && is_numeric($controller->params[$param])) {
  62. $parsedParams[$param] = $controller->params[$param];
  63. }
  64. }
  65. if (empty($parsedParams)) {
  66. return false;
  67. }
  68. if (method_exists($controller->{$this->modelName}, 'buildArchiveConditions')) {
  69. $archiveConditions = $controller->{$this->modelName}->buildArchiveConditions($parsedParams);
  70. } else {
  71. $archiveConditions = $this->_buildArchiveConditions($parsedParams);
  72. }
  73. $paginate = array();
  74. if (!empty($controller->paginate[$this->modelName])) {
  75. $paginate = $controller->paginate[$this->modelName];
  76. }
  77. if (isset($paginate['conditions'])) {
  78. $paginate['conditions'] = array_merge($paginate['conditions'], $archiveConditions);
  79. } else {
  80. $paginate['conditions'] = $archiveConditions;
  81. }
  82. $controller->paginate[$this->modelName] = $paginate;
  83. return true;
  84. }
  85. /**
  86. * Create an array that indicates which year/month combinations have elements in them
  87. *
  88. * @param array $conditions Array of conditions to use on $this->modelName when doing the find
  89. * @return mixed either false on missing modelName or array of year/month combos
  90. */
  91. public function archiveLinks($conditions = array()) {
  92. $modelName = $this->modelName;
  93. $defaults = array(
  94. 'order' => array("{$modelName}.$this->dateField" => 'DESC'),
  95. 'fields' => array("{$modelName}.$this->dateField", "COUNT(*) AS month_count"),
  96. 'conditions' => array(),
  97. 'group' => array(
  98. "MONTH({$modelName}.$this->dateField)",
  99. "YEAR({$modelName}.$this->dateField)",
  100. ),
  101. 'limit' => 10
  102. );
  103. $conditions = Set::merge($defaults, $conditions);
  104. $elements = $this->controller->{$modelName}->find('all', $conditions);
  105. $dates = array();
  106. foreach ($elements as $element) {
  107. $date = $element[$modelName][$this->dateField];
  108. $year = date('Y', strtotime($date));
  109. $month = date('m', strtotime($date));
  110. $count = $element[0]['month_count'];
  111. $dates[] = compact('year', 'month', 'count');
  112. }
  113. return $dates;
  114. }
  115. /**
  116. * Build conditions based on the date passed in an url
  117. *
  118. * Default construction of date based parameters for archive pagination
  119. * Can be overloaded in a model by implementing buildArchiveConditions()
  120. *
  121. * @param array
  122. */
  123. protected function _buildArchiveConditions($dateParams) {
  124. $duration = '1 month';
  125. extract($dateParams, EXTR_SKIP);
  126. if (!isset($year)) {
  127. $year = date('Y');
  128. $duration = '1 year';
  129. }
  130. if (!isset($month) || $month > 12 || $month < 1) {
  131. $month = '01';
  132. $duration = '1 year';
  133. }
  134. if (!isset($day) || $day > 31 || $day < 1) {
  135. $day = '01';
  136. } else {
  137. $duration = '2 days';
  138. }
  139. $startDate = sprintf('%s-%s-%s', $year, $month, $day);
  140. if (strtotime($startDate) > time()) {
  141. $this->cakeError('error', array(
  142. 'name' => sprintf(__d('utils', 'No %s found for that date range', true), Inflector::humanize(Inflector::pluralize($this->modelName))),
  143. 'message' => $this->controller->here,
  144. 'code' => 404,
  145. ));
  146. $this->_stop();
  147. }
  148. $endDatetime = new DateTime($startDate);
  149. $endDatetime->modify($duration);
  150. $endDatetime->modify('-1 day');
  151. $endDate = $endDatetime->format('Y-m-d');
  152. $field = $this->modelName . '.' . $this->dateField .' BETWEEN ? AND ?';
  153. return array($field => array($startDate, $endDate));
  154. }
  155. }