PageRenderTime 42ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/pageNavigation.php

https://bitbucket.org/dgough/annamaria-daneswood-25102012
PHP | 185 lines | 114 code | 18 blank | 53 comment | 22 complexity | 7bdbd0353a59a73c5a83c6f3e2d45901 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. /**
  3. * @version $Id: pageNavigation.php 9847 2008-01-04 04:10:37Z eddieajau $
  4. * @package Joomla
  5. * @copyright Copyright (C) 2005 Open Source Matters. All rights reserved.
  6. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
  7. * Joomla! is free software. This version may have been modified pursuant
  8. * to the GNU General Public License, and as distributed it includes or
  9. * is derivative of works licensed under the GNU General Public License or
  10. * other free or open source software licenses.
  11. * See COPYRIGHT.php for copyright notices and details.
  12. */
  13. // no direct access
  14. defined( '_VALID_MOS' ) or die( 'Restricted access' );
  15. /**
  16. * Page navigation support class
  17. * @package Joomla
  18. */
  19. class mosPageNav {
  20. /** @var int The record number to start dislpaying from */
  21. var $limitstart = null;
  22. /** @var int Number of rows to display per page */
  23. var $limit = null;
  24. /** @var int Total number of rows */
  25. var $total = null;
  26. function mosPageNav( $total, $limitstart, $limit ) {
  27. $this->total = (int) $total;
  28. $this->limitstart = (int) max( $limitstart, 0 );
  29. $this->limit = (int) max( $limit, 0 );
  30. }
  31. /**
  32. * Returns the html limit # input box
  33. * @param string The basic link to include in the href
  34. * @return string
  35. */
  36. function getLimitBox ( $link ) {
  37. $limits = array();
  38. for ($i=5; $i <= 30; $i+=5) {
  39. $limits[] = mosHTML::makeOption( "$i" );
  40. }
  41. $limits[] = mosHTML::makeOption( "50" );
  42. // build the html select list
  43. $link = $link ."&amp;limit=' + this.options[selectedIndex].value + '&amp;limitstart=". $this->limitstart;
  44. $link = sefRelToAbs( $link );
  45. return mosHTML::selectList( $limits, 'limit', 'class="inputbox" size="1" onchange="document.location.href=\''. $link .'\';"', 'value', 'text', $this->limit );
  46. }
  47. /**
  48. * Writes the html limit # input box
  49. * @param string The basic link to include in the href
  50. */
  51. function writeLimitBox ( $link ) {
  52. echo mosPageNav::getLimitBox( $link );
  53. }
  54. /**
  55. * Writes the html for the pages counter, eg, Results 1-10 of x
  56. */
  57. function writePagesCounter() {
  58. $txt = '';
  59. $from_result = $this->limitstart+1;
  60. if ($this->limitstart + $this->limit < $this->total) {
  61. $to_result = $this->limitstart + $this->limit;
  62. } else {
  63. $to_result = $this->total;
  64. }
  65. if ($this->total > 0) {
  66. $txt .= _PN_RESULTS." $from_result - $to_result "._PN_OF." $this->total";
  67. }
  68. return $to_result ? $txt : '';
  69. }
  70. /**
  71. * Writes the html for the leafs counter, eg, Page 1 of x
  72. */
  73. function writeLeafsCounter() {
  74. $txt = '';
  75. $page = ceil( ($this->limitstart + 1) / $this->limit );
  76. if ($this->total > 0) {
  77. $total_pages = ceil( $this->total / $this->limit );
  78. $txt .= _PN_PAGE." $page "._PN_OF." $total_pages";
  79. }
  80. return $txt;
  81. }
  82. /**
  83. * Writes the html links for pages, eg, previous, next, 1 2 3 ... x
  84. * @param string The basic link to include in the href
  85. */
  86. function writePagesLinks( $link ) {
  87. $txt = '';
  88. $displayed_pages = 10;
  89. $total_pages = $this->limit ? ceil( $this->total / $this->limit ) : 0;
  90. $this_page = $this->limit ? ceil( ($this->limitstart+1) / $this->limit ) : 1;
  91. $start_loop = (floor(($this_page-1)/$displayed_pages))*$displayed_pages+1;
  92. if ($start_loop + $displayed_pages - 1 < $total_pages) {
  93. $stop_loop = $start_loop + $displayed_pages - 1;
  94. } else {
  95. $stop_loop = $total_pages;
  96. }
  97. //Organic Mod:
  98. //Get the query string
  99. $parts = explode('?',$link);
  100. if(isset($parts[1])){
  101. //Replace &amp; with &
  102. $parts[1] = str_replace('&amp;','&',$parts[1]);
  103. //Split query string into parts
  104. parse_str($parts[1], $pieces);
  105. //Rebuild link but remove some elements
  106. $link = strstr($link, 'index2.php') ? 'index2.php' : 'index.php';
  107. $i = 0;
  108. foreach($pieces as $key => $value){
  109. $sep = ($i > 0) ? '&' : '?';
  110. if($key != 'search_id' ) $link .= $sep.$key.'='.$value;
  111. $i++;
  112. }
  113. //Make sef friendly
  114. $link = sefRelToAbs($link);
  115. //Attach limit var and search id
  116. $link .= (strstr($link,'?')) ? "&limit=". $this->limit : "?limit=". $this->limit;
  117. $link .= isset($pieces['search_id']) ? "&amp;search_id=".$pieces['search_id'] : '';
  118. }else{
  119. //Make sef friendly
  120. $link = sefRelToAbs($link);
  121. }
  122. if (!defined( '_PN_LT' ) || !defined( '_PN_RT' ) ) {
  123. DEFINE('_PN_LT','&lt;');
  124. DEFINE('_PN_RT','&gt;');
  125. }
  126. $pnSpace = '';
  127. if (_PN_LT || _PN_RT) $pnSpace = "&nbsp;";
  128. if ($this_page > 1) {
  129. $page = ($this_page - 2) * $this->limit;
  130. $txt .= '<a href="'. "$link&amp;limitstart=0" .'" class="pagenav btn-first" title="'. _PN_START .'">'. _PN_LT . _PN_LT . $pnSpace . _PN_START .'</a> ';
  131. $txt .= '<a href="'. "$link&amp;limitstart=$page" .'" class="pagenav btn-prev" title="'. _PN_PREVIOUS .'">'. _PN_LT . $pnSpace . _PN_PREVIOUS .'</a> ';
  132. } else {
  133. $txt .= '<span class="pagenav btn-first">'. _PN_LT . _PN_LT . $pnSpace . _PN_START .'</span> ';
  134. $txt .= '<span class="pagenav btn-prev">'. _PN_LT . $pnSpace . _PN_PREVIOUS .'</span> ';
  135. }
  136. for ($i=$start_loop; $i <= $stop_loop; $i++) {
  137. $page = ($i - 1) * $this->limit;
  138. if ($i == $this_page) {
  139. $txt .= '<span class="pagenav">'. $i .'</span> ';
  140. } else {
  141. $txt .= '<a href="'. $link .'&amp;limitstart='. $page .'" class="pagenav"><strong>'. $i .'</strong></a> ';
  142. }
  143. }
  144. if ($this_page < $total_pages) {
  145. $page = $this_page * $this->limit;
  146. $end_page = ($total_pages-1) * $this->limit;
  147. $txt .= '<a href="'. $link .'&amp;limitstart='. $page .' " class="pagenav btn-next" title="'. _PN_NEXT .'">'. _PN_NEXT . $pnSpace . _PN_RT .'</a> ';
  148. $txt .= '<a href="'. $link .'&amp;limitstart='. $end_page .' " class="pagenav btn-last" title="'. _PN_END .'">'. _PN_END . $pnSpace . _PN_RT . _PN_RT .'</a>';
  149. } else {
  150. $txt .= '<span class="pagenav btn-next">'. _PN_NEXT . $pnSpace . _PN_RT .'</span> ';
  151. $txt .= '<span class="pagenav btn-last">'. _PN_END . $pnSpace . _PN_RT . _PN_RT .'</span>';
  152. }
  153. return $txt;
  154. }
  155. /**
  156. * Sets the vars {PAGE_LINKS}, {PAGE_LIST_OPTIONS} and {PAGE_COUNTER} for the page navigation template
  157. * @param object The patTemplate object
  158. * @param string The full link to be used in the nav link, eg index.php?option=com_content
  159. * @param string The name of the template to add the variables
  160. */
  161. function setTemplateVars( &$tmpl, $link = '', $name = 'admin-list-footer' ) {
  162. $tmpl->addVar( $name, 'PAGE_LINKS', $this->writePagesLinks( $link ) );
  163. $tmpl->addVar( $name, 'PAGE_LIST_OPTIONS', $this->getLimitBox( $link ) );
  164. $tmpl->addVar( $name, 'PAGE_COUNTER', $this->writePagesCounter() );
  165. }
  166. }
  167. ?>