PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/class_paginate.php

https://bitbucket.org/ideagital-united/monex-insights
PHP | 202 lines | 144 code | 20 blank | 38 comment | 55 complexity | 3690dbf52eb72cc7de7b46719508d436 MD5 | raw file
Possible License(s): GPL-2.0, MIT, BSD-3-Clause, Apache-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception
  1. <?php
  2. /**
  3. * Class Pagination
  4. *
  5. * @package CMS pro
  6. * @author prolificscripts.com
  7. * @copyright 2014
  8. * @version $Id: class_paginate.php, v1.00 2012-03-05 10:12:05 gewa Exp $
  9. */
  10. if (!defined("_VALID_PHP"))
  11. die('Direct access to this location is not allowed.');
  12. class Paginator
  13. {
  14. public $items_per_page;
  15. public $items_total;
  16. public $num_pages = 1;
  17. public $limit;
  18. public $current_page;
  19. public $default_ipp;
  20. public $path = 0;
  21. public $path_after;
  22. private $mid_range;
  23. private $low;
  24. private $high;
  25. private $retdata;
  26. private $querystring;
  27. private static $instance;
  28. /**
  29. * Paginator::__construct()
  30. *
  31. * @return
  32. */
  33. private function __construct()
  34. {
  35. $this->current_page = 1;
  36. $this->mid_range = 7;
  37. $this->items_per_page = (isset($_GET['ipp']) and !empty($_GET['ipp'])) ? sanitize($_GET['ipp']) : $this->default_ipp;
  38. }
  39. /**
  40. * Paginator::instance()
  41. *
  42. * @return
  43. */
  44. public static function instance(){
  45. if (!self::$instance){
  46. self::$instance = new Paginator();
  47. }
  48. return self::$instance;
  49. }
  50. /**
  51. * Paginator::paginate()
  52. *
  53. * @return
  54. */
  55. public function paginate()
  56. {
  57. $this->items_per_page = (isset($_GET['ipp']) and !empty($_GET['ipp'])) ? intval($_GET['ipp']) : $this->default_ipp;
  58. $this->num_pages = ceil($this->items_total / $this->items_per_page);
  59. $this->current_page = intval(sanitize(get('pg')));
  60. if ($this->current_page < 1 or !is_numeric($this->current_page))
  61. $this->current_page = 1;
  62. if ($this->current_page > $this->num_pages)
  63. $this->current_page = $this->num_pages;
  64. $prev_page = $this->current_page - 1;
  65. $next_page = $this->current_page + 1;
  66. if (isset($_GET)) {
  67. $args = explode("&", $_SERVER['QUERY_STRING']);
  68. foreach ($args as $arg) {
  69. $keyval = explode("=", $arg);
  70. if ($keyval[0] != "pg" && $keyval[0] != "ipp")
  71. $this->querystring .= "&" . sanitize($arg);
  72. }
  73. }
  74. if (isset($_POST)) {
  75. foreach ($_POST as $key => $val) {
  76. if ($key != "pg" && $key != "ipp")
  77. $this->querystring .= "&amp;$key=" . sanitize($val);
  78. }
  79. }
  80. if ($this->num_pages > 1) {
  81. if ($this->current_page != 1 && $this->items_total >= $this->default_ipp) {
  82. if ($this->path) {
  83. $this->retdata = "<a class=\"item\" href=\"".$this->path."pg=".$prev_page."{$this->path_after}\"><i class=\"icon left arrow\"></i></a>";
  84. } else {
  85. $this->retdata = "<a class=\"item\" href=\"" . phpself() . "?pg=$prev_page&amp;ipp=$this->items_per_page$this->querystring\"><i class=\"icon left arrow\"></i></a>";
  86. }
  87. } else {
  88. $this->retdata = "<a class=\"disabled item\"><i class=\"icon left arrow\"></i></a>";
  89. }
  90. $this->start_range = $this->current_page - floor($this->mid_range / 2);
  91. $this->end_range = $this->current_page + floor($this->mid_range / 2);
  92. if ($this->start_range <= 0) {
  93. $this->end_range += abs($this->start_range) + 1;
  94. $this->start_range = 1;
  95. }
  96. if ($this->end_range > $this->num_pages) {
  97. $this->start_range -= $this->end_range - $this->num_pages;
  98. $this->end_range = $this->num_pages;
  99. }
  100. $this->range = range($this->start_range, $this->end_range);
  101. for ($i = 1; $i <= $this->num_pages; $i++) {
  102. if ($this->range[0] > 2 && $i == $this->range[0])
  103. $this->retdata .= "<a class=\"disabled item\"> ... </a>";
  104. if ($i == 1 or $i == $this->num_pages or in_array($i, $this->range)) {
  105. if ($i == $this->current_page) {
  106. $this->retdata .= "<a title=\"" . Lang::$word->_PAG_GOTO . $i . Lang::$word->_PAG_OF . $this->num_pages . "\" class=\"active item\">$i</a>";
  107. } else {
  108. if ($this->path) {
  109. $this->retdata .= "<a class=\"item\" title=\"Go To $i of $this->num_pages\" href=\"".$this->path."pg=$i{$this->path_after}\">$i</a>";
  110. } else {
  111. $this->retdata .= "<a class=\"item\" title=\"Go To $i of $this->num_pages\" href=\"" . phpself() . "?pg=$i&amp;ipp=$this->items_per_page$this->querystring\">$i</a>";
  112. }
  113. }
  114. }
  115. if ($this->range[$this->mid_range - 1] < $this->num_pages - 1 && $i == $this->range[$this->mid_range - 1])
  116. $this->retdata .= "<a class=\"disabled item\"> ... </a>";
  117. }
  118. if ($this->current_page != $this->num_pages && $this->items_total >= $this->default_ipp) {
  119. if ($this->path) {
  120. $this->retdata .= "<a class=\"item\" href=\"".$this->path."pg=".$next_page."{$this->path_after}\"><i class=\"icon right arrow\"></i></a>";
  121. } else {
  122. $this->retdata .= "<a class=\"item\" href=\"" . phpself() . "?pg=$next_page&amp;ipp=$this->items_per_page$this->querystring\"><i class=\"icon right arrow\"></i></a>\n";
  123. }
  124. } else {
  125. $this->retdata .= "<a class=\"disabled item\"><i class=\"icon right arrow\"></i></a>";
  126. }
  127. } else {
  128. for ($i = 1; $i <= $this->num_pages; $i++) {
  129. if ($i == $this->current_page) {
  130. $this->retdata .= "<a class=\"active item\">$i</a>";
  131. } else {
  132. if ($this->path) {
  133. $this->retdata .= "<a class=\"item\" href=\"".$this->path . "pg=$i{$this->path_after}\">$i</a>";
  134. } else {
  135. $this->retdata .= "<a class=\"item\" href=\"" . phpself() . "?pg=$i&amp;ipp=$this->items_per_page$this->querystring\">$i</a>";
  136. }
  137. }
  138. }
  139. }
  140. $this->low = ($this->current_page - 1) * $this->items_per_page;
  141. $this->high = $this->current_page * $this->items_per_page - 1;
  142. $this->limit = ($this->items_total == 0) ? '' : " LIMIT $this->low,$this->items_per_page";
  143. }
  144. /**
  145. * Paginator::items_per_page()
  146. *
  147. * @return
  148. */
  149. public function items_per_page()
  150. {
  151. $items = '';
  152. $ipp_array = array(10, 25, 50, 75, 100);
  153. $items .= "<option value=\"\">" . Lang::$word->_PAG_IPP . "</option>";
  154. foreach ($ipp_array as $ipp_opt)
  155. $items .= ($ipp_opt == $this->items_per_page) ? "<option selected=\"selected\" value=\"$ipp_opt\">$ipp_opt</option>\n" : "<option value=\"$ipp_opt\">$ipp_opt</option>\n";
  156. return ($this->num_pages >= 1) ? "<select class=\"selectbox\" onchange=\"window.location='" . phpself() . "?pg=1&amp;ipp='+this[this.selectedIndex].value+'$this->querystring';return false\">$items</select>\n" : '';
  157. }
  158. /**
  159. * Paginator::jump_menu()
  160. *
  161. * @return
  162. */
  163. public function jump_menu()
  164. {
  165. $option = '';
  166. $option .= "<option value=\"\">" . Lang::$word->_PAG_GOTO . "</option>";
  167. for ($i = 1; $i <= $this->num_pages; $i++) {
  168. $option .= ($i == $this->current_page) ? "<option value=\"$i\" selected=\"selected\">$i</option>\n" : "<option value=\"$i\">$i</option>\n";
  169. }
  170. return ($this->num_pages >= 1) ? "<select class=\"selectbox\" onchange=\"window.location='" . phpself() . "?pg='+this[this.selectedIndex].value+'&amp;ipp=$this->items_per_page$this->querystring';return false\">$option</select>\n" : '';
  171. }
  172. /**
  173. * Paginator::display_pages()
  174. *
  175. * @return
  176. */
  177. public function display_pages()
  178. {
  179. return($this->items_total > $this->items_per_page) ? '<div class="prolific pagination menu">' . $this->retdata . '</div>' : "";
  180. }
  181. }
  182. ?>