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

/components/libraries/cmslib/libraries/pagination.php

https://bitbucket.org/dgough/annamaria-daneswood-25102012
PHP | 322 lines | 180 code | 53 blank | 89 comment | 31 complexity | 8af4d97e0757231d4d843850c6fae68c MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. (defined('_VALID_MOS') OR defined('_JEXEC')) or die('Direct Access to this location is not allowed.');
  3. /**
  4. * CodeIgniter
  5. *
  6. * An open source application development framework for PHP 4.3.2 or newer
  7. *
  8. * @package CodeIgniter
  9. * @author Rick Ellis
  10. * @copyright Copyright (c) 2006, EllisLab, Inc.
  11. * @license http://www.codeignitor.com/user_guide/license.html
  12. * @link http://www.codeigniter.com
  13. * @since Version 1.0
  14. * @filesource
  15. *
  16. * @modified Azrul Rahim
  17. * @purpose Change class naming and remove CI dependecies
  18. */
  19. // ------------------------------------------------------------------------
  20. /**
  21. * Pagination Class
  22. *
  23. * @package CodeIgniter
  24. * @subpackage Libraries
  25. * @category Pagination
  26. * @author Rick Ellis
  27. * @link http://www.codeigniter.com/user_guide/libraries/pagination.html
  28. */
  29. class CMSPagination {
  30. var $base_url = ''; // The page we are linking to
  31. var $total_rows = ''; // Total number of items (database results)
  32. var $per_page = 4; // Max number of items you want shown per page
  33. var $num_links = 4; // Number of "digit" links to show before/after the currently viewed page
  34. var $cur_page = 0; // The current page being viewed
  35. var $first_link = '&lsaquo; First';
  36. var $next_link = '&gt;';
  37. var $prev_link = '&lt;';
  38. var $last_link = 'Last &rsaquo;';
  39. var $full_tag_open = '';
  40. var $full_tag_close = '';
  41. var $first_tag_open = '';
  42. var $first_tag_close = '&nbsp;';
  43. var $last_tag_open = '&nbsp;';
  44. var $last_tag_close = '';
  45. var $cur_tag_open = '&nbsp;<b>';
  46. var $cur_tag_close = '</b>';
  47. var $next_tag_open = '&nbsp;';
  48. var $next_tag_close = '&nbsp;';
  49. var $prev_tag_open = '&nbsp;';
  50. var $prev_tag_close = '';
  51. var $num_tag_open = '&nbsp;';
  52. var $num_tag_close = '';
  53. var $item_id = "1";
  54. var $num_pages = 0;
  55. var $last_page = "";
  56. var $cms = null;
  57. /**
  58. * Constructor
  59. *
  60. * @access public
  61. * @param array initialization parameters
  62. */
  63. function CMSPagination($params = array())
  64. {
  65. if (count($params) > 0)
  66. {
  67. $this->initialize($params);
  68. }
  69. //log_message('debug', "Pagination Class Initialized");
  70. }
  71. // --------------------------------------------------------------------
  72. /**
  73. * Initialize Preferences
  74. *
  75. * @access public
  76. * @param array initialization parameters
  77. * @return void
  78. */
  79. function initialize($params = array())
  80. {
  81. $this->cms =& cmsInstance('CMSCore');
  82. $this->cms->load('helper','url');
  83. if (count($params) > 0)
  84. {
  85. foreach ($params as $key => $val)
  86. {
  87. if (isset($this->$key))
  88. {
  89. $this->$key = $val;
  90. }
  91. }
  92. }
  93. // Need to clean up any previous &cpage
  94. $this->base_url = preg_replace('/\&cpage=[0-9]*/', '', $this->base_url);
  95. $this->base_url = preg_replace('/\&amp;cpage=[0-9]*/', '', $this->base_url);
  96. // Remove the url and set it to start with index.php if SEF ext exist
  97. if(function_exists('function_exists'))
  98. $this->base_url = "index.php" . substr($this->base_url, strpos($this->base_url, '?'));
  99. }
  100. // --------------------------------------------------------------------
  101. /**
  102. * Return the current limist start value
  103. *
  104. * @access public
  105. * @return number, current limitstart
  106. */
  107. function get_page()
  108. {
  109. // For build-in SEF ext for com_content, we need to handle it in a special way
  110. if($p = strpos($_SERVER['REQUEST_URI'], 'content/view/cpage,'))
  111. {
  112. return substr($_SERVER['REQUEST_URI'], $p+19);
  113. } else
  114. if(isset($_GET['cpage']))
  115. return $_GET['cpage'];
  116. else
  117. return 0;
  118. }
  119. // --------------------------------------------------------------------
  120. /**
  121. * Generate the pagination links
  122. *
  123. * @access public
  124. * @return string
  125. */
  126. function create_links()
  127. {
  128. // If our item count or per-page total is zero there is no need to continue.
  129. if ($this->total_rows == 0 OR $this->per_page == 0)
  130. {
  131. return '';
  132. }
  133. // Calculate the total number of pages
  134. $num_pages = ceil($this->total_rows / $this->per_page);
  135. $this->num_pages = $num_pages;
  136. // Is there only one page? Hm... nothing more to do here then.
  137. if ($num_pages == 1)
  138. {
  139. return '';
  140. }
  141. // Determine the current page number.
  142. // $CI =& get_instance();
  143. if (isset($_GET['cpage']))
  144. {
  145. $this->cur_page = intval($_GET['cpage']);
  146. // Prep the current page - no funny business!
  147. $this->cur_page = (int) $this->cur_page;
  148. }
  149. if ( ! is_numeric($this->cur_page))
  150. {
  151. $this->cur_page = 0;
  152. }
  153. // Is the page number beyond the result range?
  154. // If so we show the last page
  155. if ($this->cur_page > $this->total_rows)
  156. {
  157. $this->cur_page = ($num_pages - 1) * $this->per_page;
  158. }
  159. $uri_page_number = $this->cur_page;
  160. $this->cur_page = floor(($this->cur_page/$this->per_page) + 1);
  161. // Calculate the start and end numbers. These determine
  162. // which number to start and end the digit links with
  163. $start = (($this->cur_page - $this->num_links) > 0) ? $this->cur_page - ($this->num_links - 1) : 1;
  164. $end = (($this->cur_page + $this->num_links) < $num_pages) ? $this->cur_page + $this->num_links : $num_pages;
  165. // Add a trailing slash to the base URL if needed
  166. $this->base_url = rtrim($this->base_url); //rtrim($this->base_url, '/') .'/';
  167. // And here we go...
  168. $output = '';
  169. // Render the "First" link
  170. if ($this->cur_page > $this->num_links)
  171. {
  172. $output .= $this->first_tag_open.'<a href="'.$this->base_url.'">'.$this->first_link.'</a>'.$this->first_tag_close;
  173. }
  174. // Render the "previous" link
  175. if (($this->cur_page - $this->num_links) >= 0)
  176. {
  177. $i = $uri_page_number - $this->per_page;
  178. if ($i == 0) $i = '';
  179. $tl = $this->base_url.'&cpage='.$i;
  180. if(function_exists('function_exists'))
  181. $tl = cmsSefAmpReplace($tl);
  182. if(strpos($tl, 'content/view'))
  183. {
  184. $tl .= "cpage,$i";
  185. }
  186. $output .= $this->prev_tag_open.'<a href="'.$tl.'">'.$this->prev_link.'</a>'.$this->prev_tag_close;
  187. }
  188. // Write the digit links
  189. for ($loop = $start -1; $loop <= $end; $loop++)
  190. {
  191. $i = ($loop * $this->per_page) - $this->per_page;
  192. if ($i >= 0)
  193. {
  194. if ($this->cur_page == $loop)
  195. {
  196. $output .= $this->cur_tag_open.$loop.$this->cur_tag_close; // Current page
  197. }
  198. else
  199. {
  200. //$n = ($i == 0) ? '' : "&amp;cpage=$i";
  201. $n = "&cpage=$i";
  202. $tl = $this->base_url.$n;
  203. $tl = cmsSefAmpReplace($tl);
  204. // For com_content view, we need to specifically add the cpage,xxx
  205. // since it doesn't gets added by Joomla default SEF ext
  206. if(strpos($tl, 'content/view'))
  207. {
  208. $tl .= "cpage,$i";
  209. }
  210. $output .= $this->num_tag_open.'<a href="'.$tl.'">'.$loop.'</a>'.$this->num_tag_close;
  211. }
  212. }
  213. }
  214. // Render the "next" link
  215. if ($this->cur_page < $num_pages)
  216. {
  217. $i = ($this->cur_page * $this->per_page);
  218. $tl = $this->base_url.'&cpage='.$i;
  219. $tl = cmsSefAmpReplace($tl);
  220. // For com_content view, we need to specifically add the cpage,xxx
  221. // since it doesn't gets added by Joomla default SEF ext
  222. if(strpos($tl, 'content/view'))
  223. {
  224. $tl .= "cpage,$i";
  225. }
  226. //$output .= $this->next_tag_open.'<a href="'.$this->base_url.'&amp;cpage='.($this->cur_page * $this->per_page).'">'.$this->next_link.'</a>'.$this->next_tag_close;
  227. $output .= $this->next_tag_open.'<a href="'.$tl.'">'.$this->next_link.'</a>'.$this->next_tag_close;
  228. }
  229. // Render the "Last" link
  230. if (($this->cur_page + $this->num_links) < $num_pages)
  231. {
  232. $i = (($num_pages * $this->per_page) - $this->per_page);
  233. $tl = $this->base_url.'&cpage='.$i;
  234. $tl = cmsSefAmpReplace($tl);
  235. // For com_content view, we need to specifically add the cpage,xxx
  236. // since it doesn't gets added by Joomla default SEF ext
  237. if(strpos($tl, 'content/view'))
  238. {
  239. $tl .= "cpage,$i";
  240. }
  241. $this->last_page = $tl;
  242. $output .= $this->last_tag_open.'<a href="'.$tl.'">'.$this->last_link.'</a>'.$this->last_tag_close;
  243. }
  244. // Kill double slashes. Note: Sometimes we can end up with a double slash
  245. // in the penultimate link so we'll kill all double slashes.
  246. $output = preg_replace("#([^:])//+#", "\\1/", $output);
  247. // Add the wrapper HTML if exists
  248. $output = $this->full_tag_open.$output.$this->full_tag_close;
  249. return $output;
  250. }
  251. function last_link()
  252. {
  253. if (($this->num_pages > 1))
  254. {
  255. $i = (($this->num_pages * $this->per_page) - $this->per_page);
  256. $tl = $this->base_url.'&cpage='.$i;
  257. $tl = cmsSefAmpReplace($tl);
  258. // For com_content view, we need to specifically add the cpage,xxx
  259. // since it doesn't gets added by Joomla default SEF ext
  260. if(strpos($tl, 'content/view'))
  261. {
  262. $tl .= "cpage,$i";
  263. }
  264. return $tl;
  265. }
  266. }
  267. }
  268. // END Pagination Class
  269. ?>