PageRenderTime 42ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/webphp/lib/recordsetpager.php

http://webpy-php-port.googlecode.com/
PHP | 236 lines | 205 code | 28 blank | 3 comment | 41 complexity | 5663408d9a73015da0ee7a3e83f61422 MD5 | raw file
  1. <?php
  2. /**
  3. * RecordSetPager
  4. */
  5. class RecordSetPager
  6. {
  7. private $rs = null;
  8. public $pager_id = 'pager_cp'; // pager id sent in url
  9. public $more_links = '&hellip;';
  10. public $start_links = '&hellip;';
  11. public $first = '|&laquo;';
  12. public $previous = '&laquo;';
  13. public $next = '&raquo;';
  14. public $last = '&raquo;|';
  15. public $page = 'Page'; // displays Page 1 of n
  16. public $cache = 0; #secs to cache with CachePageExecute()
  17. public $show_page_links = true;
  18. private $total_rows;
  19. private $_get_string = '';
  20. private $current_page;
  21. private $links_per_page = 10;
  22. public $rows;
  23. public function __construct(&$db, $sql, $numrows=30, $location=null, $pager_id='pager_cp', $use_session=false)
  24. {
  25. if (empty($location)) {
  26. $this->location = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI']
  27. : $_SERVER['REDIRECT_URL'];
  28. }
  29. $pos = strpos($this->location,'?');
  30. if($pos !== false){
  31. $this->location = substr($this->location,0,$pos);
  32. }
  33. $this->location = htmlspecialchars($this->location) ;
  34. $this->rows = $numrows;
  35. $this->sql = $sql;
  36. $this->db = $db;
  37. $this->pager_id = $pager_id;
  38. $next_page = $pager_id.'_next_page';
  39. $current_page = $pager_id.'_current_page';
  40. if ($use_session) {
  41. if (isset($_GET[$next_page])) {
  42. $_SESSION[$current_page] = (int) $_GET[$next_page];
  43. }
  44. if (empty($_SESSION[$current_page])) $_SESSION[$current_page] = 1; ## at first page
  45. $this->current_page = $_SESSION[$current_page];
  46. } else {
  47. if (isset($_GET[$next_page])) {
  48. $_GET[$current_page] = (int) $_GET[$next_page];
  49. }
  50. if (empty($_GET[$current_page])) $_GET[$current_page] = 1; ## at first page
  51. $this->current_page = (int) $_GET[$current_page];
  52. }
  53. }
  54. public function first($anchor=true)
  55. {
  56. echo '<span class="first-page page-nav">';
  57. if ($anchor) {
  58. echo "<a href='{$this->location}?{$this->pager_id}_next_page=1{$this->_get_string}'>{$this->first}</a>";
  59. } else {
  60. echo "{$this->first}";
  61. }
  62. echo "</span>";
  63. }
  64. public function next($anchor=true)
  65. {
  66. echo '<span class="next-page page-nav">';
  67. if ($anchor) {
  68. $extras = $this->buildQuery($_GET);
  69. echo "<a href='{$this->location}?{$this->pager_id}_next_page=".
  70. ($this->rs->AbsolutePage() + 1).
  71. "{$this->_get_string}'>{$this->next}</a>";
  72. } else {
  73. print "{$this->next}";
  74. }
  75. echo '</span>';
  76. }
  77. private function buildQuery($a)
  78. {
  79. unset($a[$this->pager_id . "_next_page"]);
  80. $this->_get_string = '&'.http_build_query($a);
  81. }
  82. function previous($anchor=true)
  83. {
  84. echo '<span class="previous-page page-nav">';
  85. if ($anchor) {
  86. echo "<a href='{$this->location}?{$this->pager_id}_next_page=".
  87. ($this->rs->AbsolutePage() - 1).
  88. "{$this->_get_string}'>{$this->previous}</a>";
  89. } else {
  90. print "{$this->previous}";
  91. }
  92. echo '</span>';
  93. }
  94. function pageLinks()
  95. {
  96. $pages = $this->rs->LastPageNo();
  97. $links_per_page = $this->links_per_page ? $this->links_per_page : $pages;
  98. $start = 1;
  99. for($i=1; $i <= $pages; $i+=$links_per_page)
  100. {
  101. if($this->rs->AbsolutePage() >= $i)
  102. {
  103. $start = $i;
  104. }
  105. }
  106. $numbers = '';
  107. $end = $start+$links_per_page-1;
  108. $link = $this->pager_id . "_next_page";
  109. if($end > $pages) $end = $pages;
  110. if ($this->start_links && $start > 1) {
  111. $pos = $start - 1;
  112. $numbers .= '<span class="page-nav">';
  113. $numbers .= "<a href={$this->location}?$link=$pos{$this->_get_string}>$this->start_links</a>";
  114. $numbers .= '</span>';
  115. }
  116. for($i=$start; $i <= $end; $i++) {
  117. $numbers .= '<span class="page-nav">';
  118. if ($this->rs->AbsolutePage() == $i){
  119. $numbers .= "<b>$i</b>";
  120. } else {
  121. $numbers .= "<a href={$this->location}?$link=$i{$this->_get_string}>$i</a>";
  122. }
  123. $numbers .= '</span>';
  124. }
  125. if ($this->more_links && $end < $pages) {
  126. $numbers .= '<span class="page-nav">';
  127. $numbers .= "<a href={$this->location}?$link=$i{$this->_get_string}>$this->more_links</a>";
  128. }
  129. echo $numbers;
  130. }
  131. function last($anchor=true)
  132. {
  133. if (!$this->db->pageExecuteCountRows) return;
  134. echo '<span class="last-page page-nav">';
  135. if ($anchor) {
  136. $extras = $this->buildQuery($_GET);
  137. echo "<a href='{$this->location}?{$this->pager_id}_next_page=".
  138. $this->rs->LastPageNo().
  139. "{$this->_get_string}'>{$this->last}</a>";
  140. } else {
  141. print "{$this->last}";
  142. }
  143. echo '</span>';
  144. }
  145. function navigation()
  146. {
  147. $this->buildQuery($_GET);
  148. ob_start();
  149. if (!$this->rs->AtFirstPage()) {
  150. $this->first();
  151. $this->previous();
  152. } else {
  153. $this->first(false);
  154. $this->previous(false);
  155. }
  156. if ($this->show_page_links){
  157. $this->pageLinks();
  158. }
  159. if (!$this->rs->AtLastPage()) {
  160. $this->next();
  161. $this->last();
  162. } else {
  163. $this->next(false);
  164. $this->last(false);
  165. }
  166. $s = ob_get_contents();
  167. ob_end_clean();
  168. return $s;
  169. }
  170. function pageCount()
  171. {
  172. if (!$this->db->pageExecuteCountRows) return '';
  173. $lastPage = $this->rs->LastPageNo();
  174. if ($lastPage == -1) $lastPage = 1; // check for empty rs.
  175. if ($this->current_page > $lastPage) $this->current_page = 1;
  176. return $this->page." ".$this->current_page."/".$lastPage;
  177. }
  178. function &getRecords()
  179. {
  180. global $ADODB_COUNTRECS;
  181. $savec = $ADODB_COUNTRECS;
  182. if ($this->db->pageExecuteCountRows) {
  183. $ADODB_COUNTRECS = true;
  184. }
  185. if ($this->cache){
  186. $rs = &$this->db->CachePageExecute($this->cache,
  187. $this->sql,
  188. $this->rows,
  189. $this->current_page);
  190. } else {
  191. $rs = &$this->db->PageExecute($this->sql,
  192. $this->rows,
  193. $this->current_page);
  194. }
  195. $ADODB_COUNTRECS = $savec;
  196. $this->rs = &$rs;
  197. if ($rs === false) {
  198. throw new Exception("Query Failed");
  199. return;
  200. }
  201. $this->total_rows = $rs->MaxRecordCount();
  202. return $rs;
  203. }
  204. public function totalRows()
  205. {
  206. return (int) $this->total_rows;
  207. }
  208. }