PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/sally/core/lib/sly/Util/Pager.php

https://bitbucket.org/mediastuttgart/sallycms-0.7
PHP | 321 lines | 206 code | 51 blank | 64 comment | 43 complexity | 459ad1d791ac9982f0ad0a44dcc6b106 MD5 | raw file
  1. <?php
  2. /*
  3. * Copyright (c) 2013, webvariants GbR, http://www.webvariants.de
  4. *
  5. * This file is released under the terms of the MIT license. You can find the
  6. * complete text in the attached LICENSE file or online at:
  7. *
  8. * http://www.opensource.org/licenses/mit-license.php
  9. */
  10. /**
  11. * @ingroup util
  12. */
  13. class sly_Util_Pager {
  14. const FIRST_ACTIVE = -1; ///< int
  15. const FIRST_INACTIVE = -2; ///< int
  16. const PREV_ACTIVE = -3; ///< int
  17. const PREV_INACTIVE = -4; ///< int
  18. const NEXT_ACTIVE = -5; ///< int
  19. const NEXT_INACTIVE = -6; ///< int
  20. const LAST_ACTIVE = -7; ///< int
  21. const LAST_INACTIVE = -8; ///< int
  22. const ELLIPSIS_LEFT = -9; ///< int
  23. const ELLIPSIS_RIGHT = -10; ///< int
  24. protected $currentPage; ///< int
  25. protected $totalElements; ///< int
  26. protected $perPage; ///< int
  27. protected $maxLinks; ///< int
  28. protected $linksLeftRight; ///< int
  29. protected $linksOnEnd; ///< int
  30. private $pages; ///< int
  31. /**
  32. * @param int $currentPage
  33. * @param int $totalElements
  34. * @param int $perPage
  35. * @param int $maxLinks
  36. * @param int $linksLeftRight
  37. * @param int $linksOnEnds
  38. */
  39. public function __construct($currentPage, $totalElements, $perPage = 10, $maxLinks = 10, $linksLeftRight = 2, $linksOnEnds = 2) {
  40. $this->currentPage = abs((int) $currentPage);
  41. $this->totalElements = abs((int) $totalElements);
  42. $this->perPage = abs((int) $perPage);
  43. $this->maxLinks = abs((int) $maxLinks);
  44. $this->linksLeftRight = abs((int) $linksLeftRight);
  45. $this->linksOnEnd = abs((int) $linksOnEnds);
  46. $this->pages = ceil($this->totalElements / $this->perPage);
  47. if ($this->currentPage > $this->pages-1) $this->currentPage = $this->pages - 1;
  48. if ($this->currentPage < 0) $this->currentPage = 0;
  49. if ($this->maxLinks < 5) $this->maxLinks = 5;
  50. }
  51. /**
  52. * @return array
  53. */
  54. public function getPaginationData() {
  55. $result = array();
  56. if ($this->currentPage > 0) {
  57. $result[] = self::FIRST_ACTIVE;
  58. $result[] = self::PREV_ACTIVE;
  59. }
  60. else {
  61. $result[] = self::FIRST_INACTIVE;
  62. $result[] = self::PREV_INACTIVE;
  63. }
  64. if ($this->pages <= $this->maxLinks) {
  65. for ($i = 0; $i < $this->pages; ++$i) $result[] = $i;
  66. if ($this->pages == 0) $result[] = 0;
  67. }
  68. else {
  69. // Links am Anfang
  70. $result[] = 0;
  71. for ($i = 0; $i < $this->linksOnEnd; ++$i) $result[] = $i+1;
  72. // Links um die aktuelle Seite herum
  73. $begin = $this->currentPage - $this->linksLeftRight;
  74. $end = $this->currentPage + $this->linksLeftRight;
  75. if ($begin-1 > $this->linksOnEnd) $result[] = self::ELLIPSIS_LEFT;
  76. for ($i = $begin; $i <= $end; ++$i) {
  77. if ($i > 0 && $i < $this->pages) $result[] = $i;
  78. }
  79. if ($end < ($this->pages - $this->linksOnEnd - 2)) $result[] = self::ELLIPSIS_RIGHT;
  80. // Links am Ende
  81. for ($i = $this->linksOnEnd; $i > 0; --$i) $result[] = $this->pages - $i - 1;
  82. $result[] = $this->pages - 1;
  83. // Doppelte entfernen
  84. $result = array_unique($result);
  85. }
  86. if ($this->currentPage < $this->pages - 1) {
  87. $result[] = self::NEXT_ACTIVE;
  88. $result[] = self::LAST_ACTIVE;
  89. }
  90. else {
  91. $result[] = self::NEXT_INACTIVE;
  92. $result[] = self::LAST_INACTIVE;
  93. }
  94. return $result;
  95. }
  96. /**
  97. * @param array $getParams
  98. * @param string $pageParamName
  99. * @param string $filename
  100. * @param array $specialSymbols
  101. * @return array
  102. */
  103. public function getRawLinks($getParams = array(), $pageParamName = 'p', $filename = 'index.php', $specialSymbols = array()) {
  104. // Standardwerte fĂźr die Spezialsymbole
  105. if (!isset($specialSymbols['first_active'])) $specialSymbols['first_active'] = '|&laquo;';
  106. if (!isset($specialSymbols['first_inactive'])) $specialSymbols['first_inactive'] = '|&laquo;';
  107. if (!isset($specialSymbols['prev_active'])) $specialSymbols['prev_active'] = '&laquo;';
  108. if (!isset($specialSymbols['prev_inactive'])) $specialSymbols['prev_inactive'] = '&laquo;';
  109. if (!isset($specialSymbols['next_active'])) $specialSymbols['next_active'] = '&raquo;';
  110. if (!isset($specialSymbols['next_inactive'])) $specialSymbols['next_inactive'] = '&raquo;';
  111. if (!isset($specialSymbols['last_active'])) $specialSymbols['last_active'] = '&raquo;|';
  112. if (!isset($specialSymbols['last_inactive'])) $specialSymbols['last_inactive'] = '&raquo;|';
  113. if (!isset($specialSymbols['ellipsis'])) $specialSymbols['ellipsis'] = '&hellip;';
  114. // Auf geht's!
  115. $data = $this->getPaginationData();
  116. if (isset($getParams[$pageParamName])) unset($getParams[$pageParamName]);
  117. $links = array();
  118. foreach ($data as $pageCode) {
  119. $url = '';
  120. $text = '';
  121. $attribs = array();
  122. switch ($pageCode) {
  123. case self::FIRST_ACTIVE:
  124. $url = $this->getURL($filename, $getParams, $pageParamName, 0);
  125. $text = $specialSymbols['first_active'];
  126. $attribs = array('class' => 'first');
  127. break;
  128. case self::FIRST_INACTIVE:
  129. $url = '';
  130. $text = $specialSymbols['first_inactive'];
  131. $attribs = array('class' => 'first');
  132. break;
  133. case self::PREV_ACTIVE:
  134. $url = $this->getURL($filename, $getParams, $pageParamName, $this->currentPage-1);
  135. $text = $specialSymbols['prev_active'];
  136. $attribs = array('class' => 'prev');
  137. break;
  138. case self::PREV_INACTIVE:
  139. $url = '';
  140. $text = $specialSymbols['prev_inactive'];
  141. $attribs = array('class' => 'prev');
  142. break;
  143. case self::NEXT_ACTIVE:
  144. $url = $this->getURL($filename, $getParams, $pageParamName, $this->currentPage+1);
  145. $text = $specialSymbols['next_active'];
  146. $attribs = array('class' => 'next');
  147. break;
  148. case self::NEXT_INACTIVE:
  149. $url = '';
  150. $text = $specialSymbols['next_inactive'];
  151. $attribs = array('class' => 'next');
  152. break;
  153. case self::LAST_ACTIVE:
  154. $url = $this->getURL($filename, $getParams, $pageParamName, $this->pages - 1);
  155. $text = $specialSymbols['last_active'];
  156. $attribs = array('class' => 'last');
  157. break;
  158. case self::LAST_INACTIVE:
  159. $url = '';
  160. $text = $specialSymbols['last_inactive'];
  161. $attribs = array('class' => 'last');
  162. break;
  163. case self::ELLIPSIS_LEFT:
  164. case self::ELLIPSIS_RIGHT:
  165. $url = '';
  166. $text = $specialSymbols['ellipsis'];
  167. $attribs = array('class' => 'ellipsis');
  168. break;
  169. case $this->currentPage:
  170. $url = '';
  171. $text = $pageCode + 1;
  172. $attribs = array('class' => 'active page'.($this->currentPage + 1));
  173. break;
  174. default:
  175. $url = $this->getURL($filename, $getParams, $pageParamName, $pageCode);
  176. $text = $pageCode + 1;
  177. $direction = $pageCode < $this->currentPage ? 'before' : 'after';
  178. $attribs = array('class' => 'normal page'.($pageCode+1).' '.$direction);
  179. break;
  180. }
  181. if (mb_strlen($text) > 0) {
  182. $links[] = array(
  183. 'url' => $url,
  184. 'text' => $text,
  185. 'attributes' => $attribs
  186. );
  187. }
  188. }
  189. return $links;
  190. }
  191. /**
  192. * @param array $getParams
  193. * @param string $pageParamName
  194. * @param string $filename
  195. * @param array $specialSymbols
  196. * @return string
  197. */
  198. public function getHTMLString($getParams = array(), $pageParamName = 'p', $filename = 'index.php', $specialSymbols = array()) {
  199. $links = $this->getRawLinks($getParams, $pageParamName, $filename, $specialSymbols);
  200. foreach ($links as $idx => $data) {
  201. if (!empty($data['url'])) {
  202. $data['attributes']['href'] = $data['url'];
  203. }
  204. foreach ($data['attributes'] as $name => $value) {
  205. $data['attributes'][$name] = $name.'="'.$value.'"';
  206. }
  207. $attributes = implode(' ', $data['attributes']);
  208. $attributes = empty($attributes) ? '' : " $attributes";
  209. $tagName = empty($data['url']) ? 'span' : 'a';
  210. $links[$idx] = '<'.$tagName.$attributes.'>'.$data['text'].'</'.$tagName.'>';
  211. }
  212. return "\n".implode("\n", $links)."\n";
  213. }
  214. /**
  215. * @param string $tag
  216. * @param array $getParams
  217. * @param string $pageParamName
  218. * @param string $filename
  219. * @param array $specialSymbols
  220. * @return string
  221. */
  222. public function getHTMLList($tag = 'ul', $getParams = array(), $pageParamName = 'p', $filename = 'index.php', $specialSymbols = array()) {
  223. $links = $this->getRawLinks($getParams, $pageParamName, $filename, $specialSymbols);
  224. $result = "\n<$tag class=\"pager\">";
  225. foreach ($links as $idx => $data) {
  226. foreach ($data['attributes'] as $name => $value) {
  227. if ($name == 'class' && $value == 'first') unset($data['attributes'][$name]);
  228. else $data['attributes'][$name] = $name.'="'.$value.'"';
  229. }
  230. $attributes = implode(' ', $data['attributes']);
  231. $attributes = empty($attributes) ? '' : " $attributes";
  232. $link = empty($data['url']) ? '<span>'.$data['text'].'</span>' : '<a href="'.$data['url'].'">'.$data['text'].'</a>';
  233. $result .= "\n<li$attributes>$link</li>";
  234. }
  235. $result .= "\n</$tag>\n";
  236. return $result;
  237. }
  238. /**
  239. * @return array
  240. */
  241. public function getCurrentElements() {
  242. $elements = array();
  243. $base = $this->currentPage * $this->perPage;
  244. for ($i = 0; $i < $this->perPage; ++$i) {
  245. $elements[] = $base + $i;
  246. }
  247. return $elements;
  248. }
  249. /**
  250. * @param string $filename
  251. * @param array $getParams
  252. * @param string $pageParamName
  253. * @param int $page
  254. * @return string
  255. */
  256. protected function getURL($filename, $getParams = array(), $pageParamName = 'p', $page = 0) {
  257. $link = $filename;
  258. if ($page > 0) $getParams[$pageParamName] = $page;
  259. $getString = http_build_query($getParams, '', '&amp;');
  260. if (!empty($getString)) $link .= '?'.$getString;
  261. return $link;
  262. }
  263. /**
  264. * @param int $code
  265. * @return boolean
  266. */
  267. public static function isEllipsis($code) {
  268. return $code == self::ELLIPSIS_LEFT || $code == self::ELLIPSIS_RIGHT;
  269. }
  270. }