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

/system/core/pagination.php

https://github.com/sony88/answion
PHP | 270 lines | 176 code | 39 blank | 55 comment | 41 complexity | f3f49e6040e8ac042ed4f51e96226250 MD5 | raw file
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------------
  4. | Anwsion [#RELEASE_VERSION#]
  5. | ========================================
  6. | by Anwsion dev team
  7. | (c) 2011 - 2012 Anwsion Software
  8. | http://www.anwsion.com
  9. | ========================================
  10. | Support: zhengqiang@gmail.com
  11. |
  12. +---------------------------------------------------------------------------
  13. */
  14. class core_pagination
  15. {
  16. var $base_url = ''; // The page we are linking to
  17. var $prefix = ''; // A custom prefix added to the path.
  18. var $suffix = ''; // A custom suffix added to the path.
  19. var $total_rows = ''; // Total number of items (database results)
  20. var $per_page = 10; // Max number of items you want shown per page
  21. var $num_links = 2; // Number of "digit" links to show before/after the currently viewed page
  22. var $cur_page = 0; // The current page being viewed
  23. var $first_link = '&lsaquo; First';
  24. var $next_link = '&gt;';
  25. var $prev_link = '&lt;';
  26. var $last_link = 'Last &rsaquo;';
  27. var $uri_segment = 3;
  28. var $full_tag_open = '';
  29. var $full_tag_close = '';
  30. var $first_tag_open = '';
  31. var $first_tag_close = '&nbsp;';
  32. var $last_tag_open = '&nbsp;';
  33. var $last_tag_close = '';
  34. var $first_url = ''; // Alternative URL for the First Page.
  35. var $cur_tag_open = '&nbsp;<strong>';
  36. var $cur_tag_close = '</strong>';
  37. var $next_tag_open = '&nbsp;';
  38. var $next_tag_close = '&nbsp;';
  39. var $prev_tag_open = '&nbsp;';
  40. var $prev_tag_close = '';
  41. var $num_tag_open = '&nbsp;';
  42. var $num_tag_close = '';
  43. var $query_string_segment = 'page';
  44. var $display_pages = TRUE;
  45. var $direct_page = FALSE;
  46. /**
  47. * Constructor
  48. *
  49. * @access public
  50. * @param array initialization parameters
  51. */
  52. public function __construct($params = array())
  53. {
  54. if (file_exists(ROOT_PATH . 'views/' . get_setting('ui_style') . '/config/pagination.php'))
  55. {
  56. include(ROOT_PATH . 'views/' . get_setting('ui_style') . '/config/pagination.php');
  57. }
  58. else
  59. {
  60. include(ROOT_PATH . 'views/default/config/pagination.php');
  61. }
  62. $this->initialize($config);
  63. if (count($params) > 0)
  64. {
  65. $this->initialize($params);
  66. }
  67. if ($this->anchor_class != '')
  68. {
  69. $this->anchor_class = 'class="'.$this->anchor_class.'" ';
  70. }
  71. }
  72. // --------------------------------------------------------------------
  73. /**
  74. * Initialize Preferences
  75. *
  76. * @access public
  77. * @param array initialization parameters
  78. * @return void
  79. */
  80. public function initialize($params = array())
  81. {
  82. if (count($params) > 0)
  83. {
  84. foreach ($params as $key => $val)
  85. {
  86. if (isset($this->$key))
  87. {
  88. $this->$key = $val;
  89. }
  90. }
  91. }
  92. return $this;
  93. }
  94. // --------------------------------------------------------------------
  95. /**
  96. * Generate the pagination links
  97. *
  98. * @access public
  99. * @return string
  100. */
  101. public function create_links()
  102. {
  103. // If our item count or per-page total is zero there is no need to continue.
  104. if ($this->total_rows == 0 OR $this->per_page == 0)
  105. {
  106. return '';
  107. }
  108. // Calculate the total number of pages
  109. $num_pages = ceil($this->total_rows / $this->per_page);
  110. // Is there only one page? Hm... nothing more to do here then.
  111. if ($num_pages == 1)
  112. {
  113. return '';
  114. }
  115. if ($_GET[$this->query_string_segment] != 0)
  116. {
  117. $this->cur_page = $_GET[$this->query_string_segment];
  118. // Prep the current page - no funny business!
  119. $this->cur_page = (int) $this->cur_page;
  120. }
  121. else
  122. {
  123. $this->cur_page = 1;
  124. }
  125. $this->num_links = (int)$this->num_links;
  126. if ($this->num_links < 1)
  127. {
  128. die('Your number of links must be a positive number.');
  129. }
  130. if ( ! is_numeric($this->cur_page))
  131. {
  132. $this->cur_page = 0;
  133. }
  134. // Is the page number beyond the result range?
  135. // If so we show the last page
  136. if ($this->cur_page > $this->total_rows)
  137. {
  138. $this->cur_page = ($num_pages - 1) * $this->per_page;
  139. }
  140. $uri_page_number = $this->cur_page;
  141. //$this->cur_page = floor(($this->cur_page/$this->per_page) + 1);
  142. // Calculate the start and end numbers. These determine
  143. // which number to start and end the digit links with
  144. $start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1;
  145. $end = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages;
  146. if (substr($this->base_url, -1, 1) == '/')
  147. {
  148. $this->base_url = rtrim($this->base_url).$this->query_string_segment.'-';
  149. }
  150. else
  151. {
  152. $this->base_url = rtrim($this->base_url).'__'.$this->query_string_segment.'-';
  153. }
  154. // And here we go...
  155. $output = '';
  156. // Render the "First" link
  157. if ($this->first_link !== FALSE AND $this->cur_page > ($this->num_links + 1))
  158. {
  159. $first_url = ($this->first_url == '') ? $this->base_url : $this->first_url;
  160. $output .= $this->first_tag_open.'<a '.$this->anchor_class.'href="'.$first_url.'">'.$this->first_link.'</a>'.$this->first_tag_close;
  161. }
  162. // Render the "previous" link
  163. if ($this->prev_link !== FALSE AND $this->cur_page != 1)
  164. {
  165. //$i = $uri_page_number - $this->per_page;
  166. $i = $uri_page_number - 1;
  167. if ($i == 0 && $this->first_url != '')
  168. {
  169. $output .= $this->prev_tag_open.'<a '.$this->anchor_class.'href="'.$this->first_url.'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
  170. }
  171. else
  172. {
  173. $i = ($i == 0) ? '' : $this->prefix.$i.$this->suffix;
  174. $output .= $this->prev_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$i.'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
  175. }
  176. }
  177. // Render the pages
  178. if ($this->display_pages !== FALSE)
  179. {
  180. // Write the digit links
  181. for ($loop = $start -1; $loop <= $end; $loop++)
  182. {
  183. $i = ($loop * $this->per_page) - $this->per_page;
  184. if ($i >= 0)
  185. {
  186. if ($this->cur_page == $loop)
  187. {
  188. $output .= $this->cur_tag_open.$loop.$this->cur_tag_close; // Current page
  189. }
  190. else
  191. {
  192. $n = ($i == 0) ? '' : $i;
  193. $n = $n / $this->per_page + 1; // by Anwsion
  194. if ($n == '' && $this->first_url != '')
  195. {
  196. $output .= $this->num_tag_open.'<a '.$this->anchor_class.'href="'.$this->first_url.'">'.$loop.'</a>'.$this->num_tag_close;
  197. }
  198. else
  199. {
  200. $n = ($n == '') ? '' : $this->prefix.$n.$this->suffix;
  201. $output .= $this->num_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$n.'">'.$loop.'</a>'.$this->num_tag_close;
  202. }
  203. }
  204. }
  205. }
  206. }
  207. // Render the "next" link
  208. if ($this->next_link !== FALSE AND $this->cur_page < $num_pages)
  209. {
  210. //$output .= $this->next_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$this->prefix.($this->cur_page * $this->per_page).$this->suffix.'">'.$this->next_link.'</a>'.$this->next_tag_close;
  211. $output .= $this->next_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$this->prefix.($this->cur_page + 1).$this->suffix.'">'.$this->next_link.'</a>'.$this->next_tag_close;
  212. }
  213. // Render the "Last" link
  214. if ($this->last_link !== FALSE AND ($this->cur_page + $this->num_links) < $num_pages)
  215. {
  216. //$i = (($num_pages * $this->per_page) - $this->per_page);
  217. $i = $num_pages;
  218. $output .= $this->last_tag_open.'<a '.$this->anchor_class.'href="'.$this->base_url.$this->prefix.$i.$this->suffix.'">'.$this->last_link.'</a>'.$this->last_tag_close;
  219. }
  220. if ($this->direct_page)
  221. {
  222. $output .= '<input name="page_jumper" type="text" style="width:30px;"/> <input type="button" value="Go" onClick="window.location = \'' . $this->base_url.$this->prefix . '\' + $(this).parent().find(\'[name=page_jumper]\').val() + \'\';"/>';
  223. }
  224. // Kill double slashes. Note: Sometimes we can end up with a double slash
  225. // in the penultimate link so we'll kill all double slashes.
  226. $output = preg_replace("#([^:])//+#", "\\1/", $output);
  227. // Add the wrapper HTML if exists
  228. $output = $this->full_tag_open.$output.$this->full_tag_close;
  229. return $output;
  230. }
  231. }