PageRenderTime 65ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Admin/Lib/ORG/Page.class.php

http://nblog-thinkphp.googlecode.com/
PHP | 180 lines | 110 code | 10 blank | 60 comment | 22 complexity | 7f54701de5acdafb510a8d1867943f34 MD5 | raw file
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. // $Id: Page.class.php 3 2011-02-03 16:24:00Z nicholasinlove1986@gmail.com $
  12. // +----------------------------------------------------------------------
  13. // | @author nicholas <nicholasinlove@126.com>
  14. // +----------------------------------------------------------------------
  15. // | @link ( http://www.yurnero.net )
  16. // +----------------------------------------------------------------------
  17. // | ??????????? $rollPage???????
  18. // +----------------------------------------------------------------------
  19. // | ????&nbsp;???? ????$upPage,$downPage???
  20. // +----------------------------------------------------------------------
  21. class Page extends Think {
  22. // ????
  23. public $firstRow ;
  24. // ????????
  25. public $listRows ;
  26. // ??????????
  27. public $parameter ;
  28. // ??????
  29. protected $totalPages ;
  30. // ???
  31. protected $totalRows ;
  32. // ????
  33. protected $nowPage ;
  34. // ????????
  35. protected $coolPages ;
  36. // ??????????
  37. protected $rollPage ;
  38. // ??????
  39. protected $config = array('header'=>'records','prev'=>'‹‹','next'=>'››','first'=>'1...','last'=>'????','theme'=>'Total %totalRow% %header%, Pages: %nowPage%/%totalPage%, %first% %upPage% %linkPage% %downPage% %end% %input%'); //?? %select% %prePage% ?$rollPage? %nextPage% ?$rollPage? ???
  40. /**
  41. +----------------------------------------------------------
  42. * ????
  43. +----------------------------------------------------------
  44. * @access public
  45. +----------------------------------------------------------
  46. * @param array $totalRows ?????
  47. * @param array $listRows ???????
  48. * @param array $parameter ???????
  49. +----------------------------------------------------------
  50. */
  51. public function __construct($totalRows,$listRows,$parameter='') {
  52. $this->totalRows = $totalRows;
  53. $this->parameter = $parameter;
  54. $this->rollPage = C('PAGE_ROLLPAGE');
  55. $this->listRows = !empty($listRows)?$listRows:C('PAGE_LISTROWS');
  56. $this->totalPages = ceil($this->totalRows/$this->listRows); //???
  57. $this->coolPages = ceil($this->totalPages/$this->rollPage);
  58. $this->nowPage = !empty($_GET[C('VAR_PAGE')])?$_GET[C('VAR_PAGE')]:1;
  59. if(!empty($this->totalPages) && $this->nowPage>$this->totalPages) {
  60. $this->nowPage = $this->totalPages;
  61. }
  62. $this->firstRow = $this->listRows*($this->nowPage-1);
  63. }
  64. public function setConfig($name,$value) {
  65. if(isset($this->config[$name])) {
  66. $this->config[$name] = $value;
  67. }
  68. }
  69. /**
  70. +----------------------------------------------------------
  71. * ??????
  72. +----------------------------------------------------------
  73. * @access public
  74. +----------------------------------------------------------
  75. */
  76. public function show() {
  77. if(0 == $this->totalRows) return '';
  78. $p = C('VAR_PAGE');
  79. $nowCoolPage = ceil($this->nowPage/$this->rollPage);
  80. $url = $_SERVER['REQUEST_URI'].(strpos($_SERVER['REQUEST_URI'],'?')?'':"?").$this->parameter;
  81. $parse = parse_url($url);
  82. if(isset($parse['query'])) {
  83. parse_str($parse['query'],$params);
  84. unset($params[$p]);
  85. $url = $parse['path'].'?'.http_build_query($params);
  86. }
  87. //???????
  88. $upRow = $this->nowPage-1;
  89. $downRow = $this->nowPage+1;
  90. if ($upRow>0){
  91. $upPage="<a href='".$url."&".$p."=$upRow'>".$this->config['prev']."</a>";
  92. }else{
  93. //$upPage="";
  94. $upPage="<span class='disabled'>".$this->config['prev']."</span>";
  95. }
  96. if ($downRow <= $this->totalPages){
  97. $downPage="<a href='".$url."&".$p."=$downRow'>".$this->config['next']."</a>";
  98. }else{
  99. //$downPage="";
  100. $downPage="<span class='disabled'>".$this->config['next']."</span>";
  101. }
  102. // << < > >>
  103. if($nowCoolPage == 1){
  104. $theFirst = "";
  105. $prePage = "";
  106. }else{
  107. $preRow = $this->nowPage-$this->rollPage;
  108. $prePage = "<a href='".$url."&".$p."=$preRow' >?".$this->rollPage."?</a>";
  109. }
  110. //??3?
  111. if($this->nowPage - 3 > 0){
  112. $theFirst = "<a href='".$url."&".$p."=1' >".$this->config['first']."</a>";
  113. }
  114. if($nowCoolPage == $this->coolPages){
  115. $nextPage = "";
  116. $theEnd="";
  117. }else{
  118. $nextRow = $this->nowPage+$this->rollPage;
  119. $theEndRow = $this->totalPages;
  120. $nextPage = "<a href='".$url."&".$p."=$nextRow' >?".$this->rollPage."?/a>";
  121. $theEnd = "<a href='".$url."&".$p."=$theEndRow' >...".$theEndRow."</a>";
  122. }
  123. // 1 2 3 4 5
  124. $linkPage = "";
  125. //??????
  126. $init = 1;//?????
  127. $max = $this->totalPages;//?????
  128. $pageoffset = ($this->rollPage-1)/2;//?????????
  129. //??????????????
  130. if($this->totalPages>$this->rollPage){
  131. //????????????
  132. if($this->nowPage<=$pageoffset){
  133. $init=1;
  134. $max = $this->rollPage;
  135. }else{//??????????
  136. //????????????????
  137. if($this->nowPage+$pageoffset>=$this->totalPages+1){
  138. $init = $this->totalPages-$this->rollPage+1;
  139. }else{
  140. //???????????
  141. $init = $this->nowPage-$pageoffset;
  142. $max = $this->nowPage+$pageoffset;
  143. }
  144. }
  145. }
  146. //echo $init.';'.$max;
  147. for($i=$init;$i<=$max;$i++){
  148. $page=$this->nowPage;
  149. if($i==$page){
  150. $linkPage .= "<span class='current'>".$page."</span>";
  151. } else {
  152. $linkPage .= "<a href='".$url."&".$p."=$i'>".$i."</a>";
  153. }
  154. }
  155. /*???*/
  156. $input = 'Go <input style="width:20px; padding:0;font:10px Verdana;background:#aaa;" onkeypress="var keyChar=event.keyCode;if(keyChar == 13){location.href=\''.$url.'&'.$p.'=\'+this.value}" />';
  157. /*???*/
  158. $select.='Go&nbsp;&nbsp;<select name="topage" size="1" onchange="window.location.href=\''.$url.'&'.$p.'=\'+this.value">\n';
  159. for($i=1;$i<=$this->totalPages;$i++){
  160. if($i==$page) $select.="<option value='".$url."&".$p."=$i' selected>".$i."</option>\n";
  161. else $select.="<option value='".$url."&".$p."=$i'>".$i."</option>\n";
  162. }
  163. $select.="</select>";
  164. $pageStr = str_replace(
  165. array('%header%','%nowPage%','%totalRow%','%totalPage%','%upPage%','%downPage%','%first%','%prePage%','%linkPage%','%nextPage%','%end%','%input%','%select%'),
  166. array($this->config['header'],$this->nowPage,$this->totalRows,$this->totalPages,$upPage,$downPage,$theFirst,$prePage,$linkPage,$nextPage,$theEnd,$input,$select),$this->config['theme']);
  167. return $pageStr;
  168. }
  169. }
  170. ?>