PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/concreteOLD/libraries/3rdparty/adodb/adodb-pager.inc.php

https://bitbucket.org/selfeky/xclusivescardwebsite
PHP | 290 lines | 195 code | 36 blank | 59 comment | 40 complexity | ed462c8da81301b2bf4bcd54aea9a1b2 MD5 | raw file
  1. <?php
  2. /*
  3. V5.10 10 Nov 2009 (c) 2000-2009 John Lim (jlim#natsoft.com). All rights reserved.
  4. Released under both BSD license and Lesser GPL library license.
  5. Whenever there is any discrepancy between the two licenses,
  6. the BSD license will take precedence.
  7. Set tabs to 4 for best viewing.
  8. This class provides recordset pagination with
  9. First/Prev/Next/Last links.
  10. Feel free to modify this class for your own use as
  11. it is very basic. To learn how to use it, see the
  12. example in adodb/tests/testpaging.php.
  13. "Pablo Costa" <pablo@cbsp.com.br> implemented Render_PageLinks().
  14. Please note, this class is entirely unsupported,
  15. and no free support requests except for bug reports
  16. will be entertained by the author.
  17. */
  18. class ADODB_Pager {
  19. var $id; // unique id for pager (defaults to 'adodb')
  20. var $db; // ADODB connection object
  21. var $sql; // sql used
  22. var $rs; // recordset generated
  23. var $curr_page; // current page number before Render() called, calculated in constructor
  24. var $rows; // number of rows per page
  25. var $linksPerPage=10; // number of links per page in navigation bar
  26. var $showPageLinks;
  27. var $gridAttributes = 'width=100% border=1 bgcolor=white';
  28. // Localize text strings here
  29. var $first = '<code>|&lt;</code>';
  30. var $prev = '<code>&lt;&lt;</code>';
  31. var $next = '<code>>></code>';
  32. var $last = '<code>>|</code>';
  33. var $moreLinks = '...';
  34. var $startLinks = '...';
  35. var $gridHeader = false;
  36. var $htmlSpecialChars = true;
  37. var $page = 'Page';
  38. var $linkSelectedColor = 'red';
  39. var $cache = 0; #secs to cache with CachePageExecute()
  40. //----------------------------------------------
  41. // constructor
  42. //
  43. // $db adodb connection object
  44. // $sql sql statement
  45. // $id optional id to identify which pager,
  46. // if you have multiple on 1 page.
  47. // $id should be only be [a-z0-9]*
  48. //
  49. function ADODB_Pager(&$db,$sql,$id = 'adodb', $showPageLinks = false)
  50. {
  51. global $PHP_SELF;
  52. $curr_page = $id.'_curr_page';
  53. if (!empty($PHP_SELF)) $PHP_SELF = htmlspecialchars($_SERVER['PHP_SELF']); // htmlspecialchars() to prevent XSS attacks
  54. $this->sql = $sql;
  55. $this->id = $id;
  56. $this->db = $db;
  57. $this->showPageLinks = $showPageLinks;
  58. $next_page = $id.'_next_page';
  59. if (isset($_GET[$next_page])) {
  60. $_SESSION[$curr_page] = (integer) $_GET[$next_page];
  61. }
  62. if (empty($_SESSION[$curr_page])) $_SESSION[$curr_page] = 1; ## at first page
  63. $this->curr_page = $_SESSION[$curr_page];
  64. }
  65. //---------------------------
  66. // Display link to first page
  67. function Render_First($anchor=true)
  68. {
  69. global $PHP_SELF;
  70. if ($anchor) {
  71. ?>
  72. <a href="<?php echo $PHP_SELF,'?',$this->id;?>_next_page=1"><?php echo $this->first;?></a> &nbsp;
  73. <?php
  74. } else {
  75. print "$this->first &nbsp; ";
  76. }
  77. }
  78. //--------------------------
  79. // Display link to next page
  80. function render_next($anchor=true)
  81. {
  82. global $PHP_SELF;
  83. if ($anchor) {
  84. ?>
  85. <a href="<?php echo $PHP_SELF,'?',$this->id,'_next_page=',$this->rs->AbsolutePage() + 1 ?>"><?php echo $this->next;?></a> &nbsp;
  86. <?php
  87. } else {
  88. print "$this->next &nbsp; ";
  89. }
  90. }
  91. //------------------
  92. // Link to last page
  93. //
  94. // for better performance with large recordsets, you can set
  95. // $this->db->pageExecuteCountRows = false, which disables
  96. // last page counting.
  97. function render_last($anchor=true)
  98. {
  99. global $PHP_SELF;
  100. if (!$this->db->pageExecuteCountRows) return;
  101. if ($anchor) {
  102. ?>
  103. <a href="<?php echo $PHP_SELF,'?',$this->id,'_next_page=',$this->rs->LastPageNo() ?>"><?php echo $this->last;?></a> &nbsp;
  104. <?php
  105. } else {
  106. print "$this->last &nbsp; ";
  107. }
  108. }
  109. //---------------------------------------------------
  110. // original code by "Pablo Costa" <pablo@cbsp.com.br>
  111. function render_pagelinks()
  112. {
  113. global $PHP_SELF;
  114. $pages = $this->rs->LastPageNo();
  115. $linksperpage = $this->linksPerPage ? $this->linksPerPage : $pages;
  116. for($i=1; $i <= $pages; $i+=$linksperpage)
  117. {
  118. if($this->rs->AbsolutePage() >= $i)
  119. {
  120. $start = $i;
  121. }
  122. }
  123. $numbers = '';
  124. $end = $start+$linksperpage-1;
  125. $link = $this->id . "_next_page";
  126. if($end > $pages) $end = $pages;
  127. if ($this->startLinks && $start > 1) {
  128. $pos = $start - 1;
  129. $numbers .= "<a href=$PHP_SELF?$link=$pos>$this->startLinks</a> ";
  130. }
  131. for($i=$start; $i <= $end; $i++) {
  132. if ($this->rs->AbsolutePage() == $i)
  133. $numbers .= "<font color=$this->linkSelectedColor><b>$i</b></font> ";
  134. else
  135. $numbers .= "<a href=$PHP_SELF?$link=$i>$i</a> ";
  136. }
  137. if ($this->moreLinks && $end < $pages)
  138. $numbers .= "<a href=$PHP_SELF?$link=$i>$this->moreLinks</a> ";
  139. print $numbers . ' &nbsp; ';
  140. }
  141. // Link to previous page
  142. function render_prev($anchor=true)
  143. {
  144. global $PHP_SELF;
  145. if ($anchor) {
  146. ?>
  147. <a href="<?php echo $PHP_SELF,'?',$this->id,'_next_page=',$this->rs->AbsolutePage() - 1 ?>"><?php echo $this->prev;?></a> &nbsp;
  148. <?php
  149. } else {
  150. print "$this->prev &nbsp; ";
  151. }
  152. }
  153. //--------------------------------------------------------
  154. // Simply rendering of grid. You should override this for
  155. // better control over the format of the grid
  156. //
  157. // We use output buffering to keep code clean and readable.
  158. function RenderGrid()
  159. {
  160. global $gSQLBlockRows; // used by rs2html to indicate how many rows to display
  161. include_once(ADODB_DIR.'/tohtml.inc.php');
  162. ob_start();
  163. $gSQLBlockRows = $this->rows;
  164. rs2html($this->rs,$this->gridAttributes,$this->gridHeader,$this->htmlSpecialChars);
  165. $s = ob_get_contents();
  166. ob_end_clean();
  167. return $s;
  168. }
  169. //-------------------------------------------------------
  170. // Navigation bar
  171. //
  172. // we use output buffering to keep the code easy to read.
  173. function RenderNav()
  174. {
  175. ob_start();
  176. if (!$this->rs->AtFirstPage()) {
  177. $this->Render_First();
  178. $this->Render_Prev();
  179. } else {
  180. $this->Render_First(false);
  181. $this->Render_Prev(false);
  182. }
  183. if ($this->showPageLinks){
  184. $this->Render_PageLinks();
  185. }
  186. if (!$this->rs->AtLastPage()) {
  187. $this->Render_Next();
  188. $this->Render_Last();
  189. } else {
  190. $this->Render_Next(false);
  191. $this->Render_Last(false);
  192. }
  193. $s = ob_get_contents();
  194. ob_end_clean();
  195. return $s;
  196. }
  197. //-------------------
  198. // This is the footer
  199. function RenderPageCount()
  200. {
  201. if (!$this->db->pageExecuteCountRows) return '';
  202. $lastPage = $this->rs->LastPageNo();
  203. if ($lastPage == -1) $lastPage = 1; // check for empty rs.
  204. if ($this->curr_page > $lastPage) $this->curr_page = 1;
  205. return "<font size=-1>$this->page ".$this->curr_page."/".$lastPage."</font>";
  206. }
  207. //-----------------------------------
  208. // Call this class to draw everything.
  209. function Render($rows=10)
  210. {
  211. global $ADODB_COUNTRECS;
  212. $this->rows = $rows;
  213. if ($this->db->dataProvider == 'informix') $this->db->cursorType = IFX_SCROLL;
  214. $savec = $ADODB_COUNTRECS;
  215. if ($this->db->pageExecuteCountRows) $ADODB_COUNTRECS = true;
  216. if ($this->cache)
  217. $rs = $this->db->CachePageExecute($this->cache,$this->sql,$rows,$this->curr_page);
  218. else
  219. $rs = $this->db->PageExecute($this->sql,$rows,$this->curr_page);
  220. $ADODB_COUNTRECS = $savec;
  221. $this->rs = $rs;
  222. if (!$rs) {
  223. print "<h3>Query failed: $this->sql</h3>";
  224. return;
  225. }
  226. if (!$rs->EOF && (!$rs->AtFirstPage() || !$rs->AtLastPage()))
  227. $header = $this->RenderNav();
  228. else
  229. $header = "&nbsp;";
  230. $grid = $this->RenderGrid();
  231. $footer = $this->RenderPageCount();
  232. $this->RenderLayout($header,$grid,$footer);
  233. $rs->Close();
  234. $this->rs = false;
  235. }
  236. //------------------------------------------------------
  237. // override this to control overall layout and formating
  238. function RenderLayout($header,$grid,$footer,$attributes='border=1 bgcolor=beige')
  239. {
  240. echo "<table ".$attributes."><tr><td>",
  241. $header,
  242. "</td></tr><tr><td>",
  243. $grid,
  244. "</td></tr><tr><td>",
  245. $footer,
  246. "</td></tr></table>";
  247. }
  248. }
  249. ?>