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

/upload/plugins/slug/function.pager.php

https://github.com/sahilbabu/phpb2b
PHP | 205 lines | 161 code | 13 blank | 31 comment | 43 complexity | f6e6148970764a002051051a9a06b7ee MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * [PHPB2B] Copyright (C) 2007-2099, Ualink Inc. All Rights Reserved.
  4. * The contents of this file are subject to the License; you may not use this file except in compliance with the License.
  5. *
  6. * @version $Revision: 2075 $
  7. */
  8. function smarty_function_pager($params, &$smarty)
  9. {
  10. // START
  11. $show = 'page';
  12. $posvar = 'pos';
  13. $limit = 10;//default you can set 10.
  14. $separator = ' &laquo;&laquo; ';
  15. $class_text = 'nav';
  16. $class_num = 'small';
  17. $class_numon = 'big';
  18. $txt_pos = 'middle';
  19. $txt_prev = '&laquo;'; // previous
  20. $txt_next = '&raquo;'; // next
  21. $txt_first = 'More'; // archive, more articles
  22. $shift = 0;
  23. foreach($params as $key=>$value) {
  24. $tmps[strtolower($key)] = $value;
  25. $tmp = strtolower($key);
  26. if (!(${$tmp} = $value)) {
  27. ${$tmp} = '';
  28. }
  29. }
  30. settype($shift, 'integer');
  31. // data check
  32. //$minVars = array('limit');
  33. $minVars = array();
  34. if (!empty($minVars)) {
  35. foreach($minVars as $tmp) {
  36. if (empty($params[$tmp])) {
  37. $smarty->trigger_error('plugin "pager": missing or empty parameter: "'.$tmp.'"');
  38. }
  39. }
  40. }
  41. // END data check
  42. if ($txt_pos == 'middle') {
  43. $txt_pos = 'side';
  44. }
  45. if (!in_array($txt_pos, array('side', 'top', 'bottom'))) {
  46. $smarty->trigger_error('plugin "pager": bad value for : "txt_pos"');
  47. }
  48. // if there is no need for paging at all
  49. if (is_array($rowcount)) {
  50. $rowcount = count($rowcount);
  51. } elseif (!is_int($rowcount)) {
  52. ceil($rowcount);
  53. }
  54. if ($rowcount <= $limit) {
  55. return '';
  56. }
  57. if ($limit < 1) {
  58. $limit = $rowcount + 1;
  59. }
  60. if (!empty($no_first)) {
  61. unset($txt_first);
  62. }
  63. // determine the real position if the diplayed numbers were shifted (eg: showing 1 instead of 0)
  64. if ($shift > 0) {
  65. $pos = $_REQUEST[$posvar] - $shift;
  66. if ($pos < 0) {
  67. $pos = 0;
  68. }
  69. } else {
  70. $pos = $_REQUEST[$posvar];
  71. }
  72. // END INIT
  73. // remove these vars from the request_uri - only for beauty
  74. $removeVars = array($posvar, '_rc');
  75. // START remove the unwanted variables from the query string
  76. parse_str($_SERVER['QUERY_STRING'], $urlVars);
  77. // add cache total count
  78. $urlVars['total_count'] = $rowcount;
  79. // add the forward vars
  80. if (!is_array($forwardvars)) {
  81. $forwardvars = preg_split('/[,;\s]/', $forwardvars, -1, PREG_SPLIT_NO_EMPTY);
  82. }
  83. $urlVars = array_merge($urlVars, $forwardvars);
  84. foreach($urlVars as $key=>$value) {
  85. if (in_array($key, $removeVars)) {
  86. unset($urlVars[$key]);
  87. }
  88. }
  89. // END remove the unwanted variables from the query string
  90. // START build up the link
  91. $tmp = '';
  92. foreach($urlVars as $key=>$value) {
  93. if (is_array($value)) {
  94. foreach($value as $val) {
  95. $tmp .= '&'.$key.'[]='.urlencode($val);
  96. }
  97. } elseif(!empty($value)) {
  98. $tmp .= '&'.$key.'='.urlencode($value);
  99. } else {
  100. $tmp .= '&'.$key;
  101. }
  102. }
  103. if (!empty($tmp)) {
  104. $url = pb_getenv('PHP_SELF').'?'.substr($tmp, 1);;
  105. $link = '&';
  106. } else {
  107. $url = pb_getenv('PHP_SELF');
  108. $link = '?';
  109. }
  110. // END build up the link
  111. // if there is no position (or 0) prepare the link for the second page
  112. if ((empty($pos) OR ($pos < 1)) AND ($rowcount > $limit)) {
  113. if (!empty($firstpos)) {
  114. $short['first'] .= $url.$link.$posvar.'='.$firstpos;
  115. } elseif ($pos == -1) {
  116. $short['first'] .= $url.$link.$posvar.'='.(1 + $shift);
  117. } else {
  118. $short['first'] = $url.$link.$posvar.'='.($limit+$shift);
  119. }
  120. }
  121. // START create data to print
  122. if ($rowcount > $limit) {
  123. for ($i=1; $i < $rowcount+1; $i+=$limit) {
  124. $pages[$i] = $url.$link.$posvar.'='.($i - 1 + $shift);
  125. }
  126. // previous - next stepping
  127. if ($pos >= $limit) {
  128. $short['prev'] = $url.$link.$posvar.'='.($pos - $limit + $shift);
  129. }
  130. if ( ($pos) < ($rowcount-$limit)) {
  131. $short['next'] = $url.$link.$posvar.'='.($pos + $limit + $shift);
  132. }
  133. }
  134. // END preparing the arrays to print
  135. if ($pos >= $limit) {
  136. $cache['prev'] = '<a href="'.$short['prev'].'">'.$txt_prev.'</a>';
  137. } else {
  138. $cache['prev'] = '';
  139. }
  140. // next
  141. if ($pos < ($rowcount-$limit)) {
  142. $cache['next'] = '<a href="'.$short['next'].'">'.$txt_next.'</a>';
  143. } else {
  144. $cache['next'] = '';
  145. }
  146. // END prepare the prev and next string/image, make it a link ....
  147. $pagenav = null;
  148. // START PRININT
  149. if ($txt_pos == 'top') {
  150. $pagenav.= $cache['prev'].$cache['next']."\n";
  151. }
  152. if (($txt_pos == 'side') AND (!empty($cache['prev']))) {
  153. $pagenav.= $cache['prev'];
  154. }
  155. $total_record = $rowcount;
  156. $total_page = ceil($total_record / $limit)-1;// the last page is not the pos section,should out.
  157. $page = floor($pos/$limit);
  158. $group_pages = 3;
  159. if($page>1){ // show page number for paging left
  160. $prev_begin = ($page-$group_pages)<=0?1:($page-$group_pages);
  161. $prev_end = ($page-1)<=0?1:($page-1);
  162. $prevs = range($prev_begin, $prev_end);
  163. if ($prev_begin>1) {
  164. $pagenav.="<a href='".$pages[1]."' title='".L('first_page', 'tpl')."'>1</a>... ";
  165. }
  166. foreach ($prevs as $val) {
  167. $pagenav.="<a href='".($pages[$val*$limit+1])."'>$val</a>";
  168. }
  169. }
  170. if($page>0)
  171. $pagenav.="<span class='current'>{$page}</span>";
  172. if($page<$total_page){ // page right
  173. $next_begin = ($page+1)>$total_page?$total_page:($page+1);
  174. $next_end = ($page+$group_pages)>$total_page?$total_page:($page+$group_pages);
  175. $nexts = range($next_begin, $next_end);
  176. foreach ($nexts as $val) {
  177. $pagenav.="<a href='".($pages[$val*$limit+1])."'>{$val}</a>";
  178. }
  179. }
  180. if (($txt_pos == 'side') AND (!empty($cache['next']))) {
  181. $pagenav.= $cache['next'];
  182. }
  183. // END NUMBERS
  184. // START PREVIOUS, NEXT paging
  185. if ($txt_pos == 'bottom') {
  186. $pagenav.= $cache['prev'].$cache['next']."\n";
  187. }
  188. // END PREVIOUS, NEXT paging
  189. // END DISPLAY
  190. return $pagenav;
  191. }
  192. ?>