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

/lib/sources/paging.class.php

https://github.com/pijulius/jcore
PHP | 447 lines | 332 code | 107 blank | 8 comment | 27 complexity | c335c884e280ef6a0e9a99b6d802b892 MD5 | raw file
  1. <?php
  2. /***************************************************************************
  3. * paging.class.php
  4. *
  5. * Jul 05, 07:00:00 2009
  6. * Copyright 2009 Istvan Petres (aka P.I.Julius)
  7. * me@pijulius.com
  8. * For licensing, see LICENSE or http://jcore.net/license
  9. ****************************************************************************/
  10. class _paging {
  11. var $limit;
  12. var $otherArgs;
  13. var $ignoreArgs;
  14. var $items = 0;
  15. var $maxLimit = 100;
  16. var $defaultLimit = 10;
  17. var $variable = 'limit';
  18. var $pageNumbers = 10;
  19. var $ajax = false;
  20. function __construct($limit = 10, $otherargs = null) {
  21. $handled = api::callHooks(API_HOOK_BEFORE,
  22. 'paging::paging', $this, $limit, $otherargs);
  23. if (isset($handled)) {
  24. api::callHooks(API_HOOK_AFTER,
  25. 'paging::paging', $this, $limit, $otherargs, $handled);
  26. return $handled;
  27. }
  28. $this->limit = "0,".$limit;
  29. $this->defaultLimit = $limit;
  30. $this->track($this->variable);
  31. $this->otherArgs = $otherargs;
  32. api::callHooks(API_HOOK_AFTER,
  33. 'paging::paging', $this, $limit, $otherargs);
  34. }
  35. function parse($variable) {
  36. preg_match('/([0-9]*?),([0-9]*)/', $variable, $matches);
  37. if (!$matches[2]) {
  38. $matches[2] = (int)$matches[0];
  39. $matches[1] = 0;
  40. }
  41. return array(
  42. 'Start' => (int)$matches[1],
  43. 'End' => (int)((int)$matches[1]+(int)$matches[2]),
  44. 'Limit' => (int)$matches[2]);
  45. }
  46. function getStart() {
  47. $limit = $this->parse($this->limit);
  48. return (int)$limit['Start'];
  49. }
  50. function getEnd() {
  51. $limit = $this->parse($this->limit);
  52. return (int)$limit['End'];
  53. }
  54. function track($variable) {
  55. $this->variable = $variable;
  56. $this->limit = "0,".$this->defaultLimit;
  57. if (isset($_GET[$this->variable])) {
  58. $limit = $this->parse(str_replace('-', ',', strip_tags((string)$_GET[$this->variable])));
  59. $this->limit = $limit['Start'].",".(int)($limit['Limit']-$limit['Start']);
  60. }
  61. $limit = $this->parse($this->limit);
  62. if (!$limit['Limit'] || $limit['Limit'] > $this->maxLimit) {
  63. $limit['Limit'] = $this->maxLimit;
  64. $limit['End'] = $limit['Start']+$limit['Limit'];
  65. }
  66. $this->limit = (int)$limit['Start'].",".(int)$limit['Limit'];
  67. }
  68. function reset() {
  69. $this->limit = "0,".$this->defaultLimit;
  70. }
  71. function setTotalItems($items) {
  72. $this->items = $items;
  73. }
  74. function displayLastPage($link) {
  75. $handled = api::callHooks(API_HOOK_BEFORE,
  76. 'paging::displayLastPage', $this, $link);
  77. if (isset($handled)) {
  78. api::callHooks(API_HOOK_AFTER,
  79. 'paging::displayLastPage', $this, $link, $handled);
  80. return $handled;
  81. }
  82. echo
  83. "<a title='".htmlchars(__("Last page"), ENT_QUOTES)."' " .
  84. "href='".$link."'>" .
  85. "<span>&gt;&gt;</span>" .
  86. "</a>";
  87. api::callHooks(API_HOOK_AFTER,
  88. 'paging::displayLastPage', $this, $link);
  89. }
  90. function displayNextPage($link) {
  91. $handled = api::callHooks(API_HOOK_BEFORE,
  92. 'paging::displayNextPage', $this, $link);
  93. if (isset($handled)) {
  94. api::callHooks(API_HOOK_AFTER,
  95. 'paging::displayNextPage', $this, $link, $handled);
  96. return $handled;
  97. }
  98. echo
  99. "<a title='".htmlchars(__("Next page"), ENT_QUOTES)."' " .
  100. "href='".$link."'>" .
  101. "<span>&gt;</span>" .
  102. "</a>";
  103. api::callHooks(API_HOOK_AFTER,
  104. 'paging::displayNextPage', $this, $link);
  105. }
  106. function displayMorePages($link) {
  107. $handled = api::callHooks(API_HOOK_BEFORE,
  108. 'paging::displayMorePages', $this, $link);
  109. if (isset($handled)) {
  110. api::callHooks(API_HOOK_AFTER,
  111. 'paging::displayMorePages', $this, $link, $handled);
  112. return $handled;
  113. }
  114. echo
  115. "<span class='comment'" .
  116. (JCORE_VERSION < '0.5'?
  117. " style='float: left;'":
  118. null) .
  119. ">&nbsp;...&nbsp;</span>";
  120. api::callHooks(API_HOOK_AFTER,
  121. 'paging::displayMorePages', $this, $link);
  122. }
  123. function displayPage($link, $page) {
  124. $handled = api::callHooks(API_HOOK_BEFORE,
  125. 'paging::displayPage', $this, $link, $page);
  126. if (isset($handled)) {
  127. api::callHooks(API_HOOK_AFTER,
  128. 'paging::displayPage', $this, $link, $page, $handled);
  129. return $handled;
  130. }
  131. echo
  132. "<a title='".htmlchars(sprintf(__("Page (%s)"), $page), ENT_QUOTES)."' " .
  133. "href='".$link."'>" .
  134. "<span>".$page."</span>" .
  135. "</a>";
  136. api::callHooks(API_HOOK_AFTER,
  137. 'paging::displayPage', $this, $link, $page);
  138. }
  139. function displayLessPages($link) {
  140. $handled = api::callHooks(API_HOOK_BEFORE,
  141. 'paging::displayLessPages', $this, $link);
  142. if (isset($handled)) {
  143. api::callHooks(API_HOOK_AFTER,
  144. 'paging::displayLessPages', $this, $link, $handled);
  145. return $handled;
  146. }
  147. echo
  148. "<span class='comment'" .
  149. (JCORE_VERSION < '0.5'?
  150. " style='float: left;'":
  151. null) .
  152. ">&nbsp;...&nbsp;</span>";
  153. api::callHooks(API_HOOK_AFTER,
  154. 'paging::displayLessPages', $this, $link);
  155. }
  156. function displayPrevPage($link) {
  157. $handled = api::callHooks(API_HOOK_BEFORE,
  158. 'paging::displayPrevPage', $this, $link);
  159. if (isset($handled)) {
  160. api::callHooks(API_HOOK_AFTER,
  161. 'paging::displayPrevPage', $this, $link, $handled);
  162. return $handled;
  163. }
  164. echo
  165. "<a title='".htmlchars(__("Previous page"), ENT_QUOTES)."' " .
  166. "href='".$link."'>" .
  167. "<span>&lt;</span>" .
  168. "</a>";
  169. api::callHooks(API_HOOK_AFTER,
  170. 'paging::displayPrevPage', $this, $link);
  171. }
  172. function displayFirstPage($link) {
  173. $handled = api::callHooks(API_HOOK_BEFORE,
  174. 'paging::displayFirstPage', $this, $link);
  175. if (isset($handled)) {
  176. api::callHooks(API_HOOK_AFTER,
  177. 'paging::displayFirstPage', $this, $link, $handled);
  178. return $handled;
  179. }
  180. echo
  181. "<a title='".htmlchars(__("First page"), ENT_QUOTES)."' " .
  182. "href='".$link."'>" .
  183. "<span>&lt;&lt;</span>" .
  184. "</a>";
  185. api::callHooks(API_HOOK_AFTER,
  186. 'paging::displayFirstPage', $this, $link);
  187. }
  188. function displayTitle($selectedpage = 0, $totalpages = 0) {
  189. $handled = api::callHooks(API_HOOK_BEFORE,
  190. 'paging::displayTitle', $this, $selectedpage, $totalpages);
  191. if (isset($handled)) {
  192. api::callHooks(API_HOOK_AFTER,
  193. 'paging::displayTitle', $this, $selectedpage, $totalpages, $handled);
  194. return $handled;
  195. }
  196. if (JCORE_VERSION >= '1.0')
  197. echo
  198. sprintf(__("Page %s of %s"), $selectedpage, $totalpages).":";
  199. else
  200. echo __("Pages").":";
  201. api::callHooks(API_HOOK_AFTER,
  202. 'paging::displayTitle', $this, $selectedpage, $totalpages);
  203. }
  204. function display() {
  205. $args = null;
  206. $exp_args = preg_split('/(&amp;|&)/', $this->otherArgs);
  207. for ($i = 0; $i < sizeof($exp_args); $i++) {
  208. $exp_arg = explode("=", $exp_args[$i]);
  209. $args .= ", ".$exp_arg[0];
  210. }
  211. if ($this->ignoreArgs)
  212. $args .= ", ".$this->ignoreArgs;
  213. if ($this->ajax)
  214. $args .= ", ajax";
  215. $limit = $this->parse($this->limit);
  216. $totalpagenum = ceil($this->items/$limit['Limit']);
  217. $currentpagenum = round($limit['End']/$limit['Limit']);
  218. if ($totalpagenum < 2)
  219. return;
  220. $handled = api::callHooks(API_HOOK_BEFORE,
  221. 'paging::display', $this);
  222. if (isset($handled)) {
  223. api::callHooks(API_HOOK_AFTER,
  224. 'paging::display', $this, $handled);
  225. return $handled;
  226. }
  227. $startpagenum = 1;
  228. if ($currentpagenum > round($this->pageNumbers/2))
  229. $startpagenum = $currentpagenum-round($this->pageNumbers/2)+1;
  230. $endpagenum = $totalpagenum;
  231. if ($endpagenum > $this->pageNumbers) {
  232. $endpagenum = $startpagenum+$this->pageNumbers-1;
  233. if ($endpagenum > $totalpagenum) {
  234. $startpagenum -= $endpagenum-$totalpagenum;
  235. $endpagenum = $totalpagenum;
  236. }
  237. }
  238. echo
  239. "<div class='paging-outer ".
  240. ($this->ajax?
  241. "paging-ajax":
  242. null) .
  243. "'>" .
  244. "<div class='paging'>" .
  245. "<div class='paging-text'>";
  246. $this->displayTitle($currentpagenum, $totalpagenum);
  247. echo
  248. "</div>";
  249. if ($currentpagenum > 1) {
  250. $link = url::uri($this->variable.$args).
  251. "&amp;".$this->variable."=" .
  252. "0-".$limit['Limit'] .
  253. $this->otherArgs;
  254. echo
  255. "<div class='pagenumber pagenumber-first-page'>";
  256. $this->displayFirstPage($link);
  257. echo
  258. "</div>";
  259. $link = url::uri($this->variable.$args).
  260. "&amp;".$this->variable."=" .
  261. round(($currentpagenum-1)*$limit['Limit']-$limit['Limit'])."-".
  262. round(($currentpagenum-1)*$limit['Limit']) .
  263. $this->otherArgs;
  264. echo
  265. "<div class='pagenumber pagenumber-prev-page'>";
  266. $this->displayPrevPage($link);
  267. echo
  268. "</div>";
  269. }
  270. if ($startpagenum > 1) {
  271. $link = url::uri($this->variable.$args).
  272. "&amp;".$this->variable."=".
  273. round(($startpagenum-1)*$limit['Limit']-$limit['Limit'])."-".
  274. round(($startpagenum-1)*$limit['Limit']) .
  275. $this->otherArgs;
  276. echo
  277. "<div class='pagenumber pagenumber-lest-pages'>";
  278. $this->displayLessPages($link);
  279. echo
  280. "</div>";
  281. }
  282. for ($i = $startpagenum; $i <= $endpagenum; $i++) {
  283. $link = url::uri($this->variable.$args).
  284. "&amp;".$this->variable."=".
  285. round($i*$limit['Limit']-$limit['Limit'])."-".
  286. round($i*$limit['Limit']) .
  287. $this->otherArgs;
  288. echo
  289. "<div class='pagenumber pagenumber-page ".
  290. ($i == $currentpagenum?
  291. "pagenumber-selected":
  292. null).
  293. "'>";
  294. $this->displayPage($link, $i);
  295. echo
  296. "</div>";
  297. }
  298. if ($endpagenum < $totalpagenum) {
  299. $link = url::uri($this->variable.$args).
  300. "&amp;".$this->variable."=" .
  301. round(($endpagenum+1)*$limit['Limit']-$limit['Limit'])."-".
  302. round(($endpagenum+1)*$limit['Limit']) .
  303. $this->otherArgs;
  304. echo
  305. "<div class='pagenumber pagenumber-more-pages'>";
  306. $this->displayMorePages($link);
  307. echo
  308. "</div>";
  309. }
  310. if ($currentpagenum < $totalpagenum) {
  311. $link = url::uri($this->variable.$args).
  312. "&amp;".$this->variable."=" .
  313. round(($currentpagenum+1)*$limit['Limit']-$limit['Limit'])."-".
  314. round(($currentpagenum+1)*$limit['Limit']) .
  315. $this->otherArgs;
  316. echo
  317. "<div class='pagenumber pagenumber-next-page'>";
  318. $this->displayNextPage($link);
  319. echo
  320. "</div>";
  321. $link = url::uri($this->variable.$args).
  322. "&amp;".$this->variable."=" .
  323. round($totalpagenum*$limit['Limit']-$limit['Limit'])."-".
  324. round($totalpagenum*$limit['Limit']) .
  325. $this->otherArgs;
  326. echo
  327. "<div class='pagenumber pagenumber-last-page'>";
  328. $this->displayLastPage($link);
  329. echo
  330. "</div>";
  331. }
  332. echo
  333. "<div class='clear-both'></div>" .
  334. "</div>" .
  335. "</div>";
  336. api::callHooks(API_HOOK_AFTER,
  337. 'paging::display', $this);
  338. }
  339. }
  340. ?>