Merge branch 'master' of git://github.com/openemr/openemr
[openemr.git] / library / adodb / adodb-pager.inc.php
blob072f0b10f0b953c60ee029cd957bb52fc21ba0b9
1 <?php
2 /*
3 V4.20 22 Feb 2004 (c) 2000-2004 John Lim (jlim@natsoft.com.my). 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.
9 This class provides recordset pagination with
10 First/Prev/Next/Last links.
12 Feel free to modify this class for your own use as
13 it is very basic. To learn how to use it, see the
14 example in adodb/tests/testpaging.php.
16 "Pablo Costa" <pablo@cbsp.com.br> implemented Render_PageLinks().
18 Please note, this class is entirely unsupported,
19 and no free support requests except for bug reports
20 will be entertained by the author.
23 class ADODB_Pager {
24 var $id; // unique id for pager (defaults to 'adodb')
25 var $qs; // extra parameters to pass on the query line
26 var $db; // ADODB connection object
27 var $sql; // sql used
28 var $rs; // recordset generated
29 var $curr_page; // current page number before Render() called, calculated in constructor
30 var $rows; // number of rows per page
31 var $linksPerPage=10; // number of links per page in navigation bar
32 var $showPageLinks;
34 var $gridAttributes = 'width=100% border=1 bgcolor=white';
36 // Localize text strings here
37 var $first = '<code>|&lt;</code>';
38 var $prev = '<code>&lt;&lt;</code>';
39 var $next = '<code>>></code>';
40 var $last = '<code>>|</code>';
41 var $moreLinks = '...';
42 var $startLinks = '...';
43 var $gridHeader = false;
44 var $htmlSpecialChars = true;
45 var $page = 'Page';
46 var $linkSelectedColor = 'red';
47 var $cache = 0; #secs to cache with CachePageExecute()
49 //----------------------------------------------
50 // constructor
52 // $db adodb connection object
53 // $sql sql statement
54 // $id optional id to identify which pager,
55 // if you have multiple on 1 page.
56 // $id should be only be [a-z0-9]*
58 function ADODB_Pager(&$db,$sql,$id = 'adodb', $showPageLinks = false, $qs="")
60 global $HTTP_SERVER_VARS,$PHP_SELF,$HTTP_SESSION_VARS,$HTTP_GET_VARS;
62 $curr_page = $id.'_curr_page';
63 if (empty($PHP_SELF)) $PHP_SELF = $HTTP_SERVER_VARS['PHP_SELF'];
65 $this->sql = $sql;
66 //hack to allow extra junk onto the query string
67 $this->qs = $qs;
68 $this->id = $id;
69 $this->db = $db;
70 $this->showPageLinks = $showPageLinks;
72 $next_page = $id.'_next_page';
73 if (isset($HTTP_GET_VARS[$next_page])) {
74 $HTTP_SESSION_VARS[$curr_page] = $HTTP_GET_VARS[$next_page];
76 if (empty($HTTP_SESSION_VARS[$curr_page])) $HTTP_SESSION_VARS[$curr_page] = 1; ## at first page
78 $this->curr_page = $HTTP_SESSION_VARS[$curr_page];
82 //---------------------------
83 // Display link to first page
84 function Render_First($anchor=true)
86 global $PHP_SELF;
87 if ($anchor) {
89 <a href="<?php echo $PHP_SELF,'?',$this->id;?>_next_page=1&<?=$this->qs?>"><?php echo $this->first;?></a> &nbsp;
90 <?php
91 } else {
92 print "$this->first &nbsp; ";
96 //--------------------------
97 // Display link to next page
98 function render_next($anchor=true)
100 global $PHP_SELF;
102 if ($anchor) {
104 <a href="<?php echo $PHP_SELF,'?',$this->id,'_next_page=',$this->rs->AbsolutePage() + 1 ?>&<?=$this->qs?>"><?php echo $this->next;?></a> &nbsp;
105 <?php
106 } else {
107 print "$this->next &nbsp; ";
111 //------------------
112 // Link to last page
114 // for better performance with large recordsets, you can set
115 // $this->db->pageExecuteCountRows = false, which disables
116 // last page counting.
117 function render_last($anchor=true)
119 global $PHP_SELF;
121 if (!$this->db->pageExecuteCountRows) return;
123 if ($anchor) {
125 <a href="<?php echo $PHP_SELF,'?',$this->id,'_next_page=',$this->rs->LastPageNo() ?>&<?=$this->qs?>"><?php echo $this->last;?></a> &nbsp;
126 <?php
127 } else {
128 print "$this->last &nbsp; ";
132 //---------------------------------------------------
133 // original code by "Pablo Costa" <pablo@cbsp.com.br>
134 function render_pagelinks()
136 global $PHP_SELF;
137 $pages = $this->rs->LastPageNo();
138 $linksperpage = $this->linksPerPage ? $this->linksPerPage : $pages;
139 for($i=1; $i <= $pages; $i+=$linksperpage)
141 if($this->rs->AbsolutePage() >= $i)
143 $start = $i;
146 $numbers = '';
147 $end = $start+$linksperpage-1;
148 $link = $this->id . "_next_page";
149 if($end > $pages) $end = $pages;
152 if ($this->startLinks && $start > 1) {
153 $pos = $start - 1;
154 $numbers .= "<a href=$PHP_SELF?$link=$pos>$this->startLinks</a> ";
157 for($i=$start; $i <= $end; $i++) {
158 if ($this->rs->AbsolutePage() == $i)
159 $numbers .= "<font color=$this->linkSelectedColor><b>$i</b></font> ";
160 else
161 $numbers .= "<a href=$PHP_SELF?$link=$i>$i</a> ";
164 if ($this->moreLinks && $end < $pages)
165 $numbers .= "<a href=$PHP_SELF?$link=$i>$this->moreLinks</a> ";
166 print $numbers . ' &nbsp; ';
168 // Link to previous page
169 function render_prev($anchor=true)
171 global $PHP_SELF;
172 if ($anchor) {
174 <a href="<?php echo $PHP_SELF,'?',$this->id,'_next_page=',$this->rs->AbsolutePage() - 1 ?>&<?=$this->qs?>"><?php echo $this->prev;?></a> &nbsp;
175 <?php
176 } else {
177 print "$this->prev &nbsp; ";
181 //--------------------------------------------------------
182 // Simply rendering of grid. You should override this for
183 // better control over the format of the grid
185 // We use output buffering to keep code clean and readable.
186 function RenderGrid()
188 global $gSQLBlockRows; // used by rs2html to indicate how many rows to display
189 include_once(ADODB_DIR.'/tohtml.inc.php');
190 ob_start();
191 $gSQLBlockRows = $this->rows;
192 rs2html($this->rs,$this->gridAttributes,$this->gridHeader,$this->htmlSpecialChars);
193 $s = ob_get_contents();
194 ob_end_clean();
195 return $s;
198 //-------------------------------------------------------
199 // Navigation bar
201 // we use output buffering to keep the code easy to read.
202 function RenderNav()
204 ob_start();
205 if (!$this->rs->AtFirstPage()) {
206 $this->Render_First();
207 $this->Render_Prev();
208 } else {
209 $this->Render_First(false);
210 $this->Render_Prev(false);
212 if ($this->showPageLinks){
213 $this->Render_PageLinks();
215 if (!$this->rs->AtLastPage()) {
216 $this->Render_Next();
217 $this->Render_Last();
218 } else {
219 $this->Render_Next(false);
220 $this->Render_Last(false);
222 $s = ob_get_contents();
223 ob_end_clean();
224 return $s;
227 //-------------------
228 // This is the footer
229 function RenderPageCount()
231 if (!$this->db->pageExecuteCountRows) return '';
232 $lastPage = $this->rs->LastPageNo();
233 if ($lastPage == -1) $lastPage = 1; // check for empty rs.
234 return "<font size=-1>$this->page ".$this->curr_page."/".$lastPage."</font>";
237 //-----------------------------------
238 // Call this class to draw everything.
239 function Render($rows=10)
241 global $ADODB_COUNTRECS;
243 $this->rows = $rows;
245 $savec = $ADODB_COUNTRECS;
247 if ($this->db->pageExecuteCountRows) $ADODB_COUNTRECS = true;
248 if ($this->cache)
249 $rs = &$this->db->CachePageExecute($this->cache,$this->sql,$rows,$this->curr_page);
250 else {
251 $sqls = split(";",$this->sql);
252 foreach ($sqls as $sql) {
253 if (strlen($sql) > 1) {
254 $rs = &$this->db->PageExecute($sql,$rows,$this->curr_page);
258 $ADODB_COUNTRECS = $savec;
260 $this->rs = &$rs;
261 if (!$rs) {
262 print "<h3>Query failed: $this->sql, " . $this->db->ErrorMsg() . "</h3>";
263 return;
266 if (!$rs->EOF && (!$rs->AtFirstPage() || !$rs->AtLastPage()))
267 $header = $this->RenderNav();
268 else
269 $header = "&nbsp;";
271 $grid = $this->RenderGrid();
272 $footer = $this->RenderPageCount();
273 $rs->Close();
274 $this->rs = false;
276 $this->RenderLayout($header,$grid,$footer);
279 //------------------------------------------------------
280 // override this to control overall layout and formating
281 function RenderLayout($header,$grid,$footer,$attributes='border=1')
283 echo "<table ".$attributes."><tr><td>",
284 $header,
285 "</td></tr><tr><td>",
286 $grid,
287 "</td></tr><tr><td>",
288 $footer,
289 "</td></tr></table>";