Merge branch 'MDL-58454-master' of git://github.com/junpataleta/moodle
[moodle.git] / lib / adodb / adodb-pager.inc.php
blobfa77d55c7a04aa0256d53c440e7a0a90df436f2a
1 <?php
3 /*
4 @version v5.20.9 21-Dec-2016
5 @copyright (c) 2000-2013 John Lim (jlim#natsoft.com). All rights reserved.
6 @copyright (c) 2014 Damien Regad, Mark Newnham and the ADOdb community
7 Released under both BSD license and Lesser GPL library license.
8 Whenever there is any discrepancy between the two licenses,
9 the BSD license will take precedence.
10 Set tabs to 4 for best viewing.
12 This class provides recordset pagination with
13 First/Prev/Next/Last links.
15 Feel free to modify this class for your own use as
16 it is very basic. To learn how to use it, see the
17 example in adodb/tests/testpaging.php.
19 "Pablo Costa" <pablo@cbsp.com.br> implemented Render_PageLinks().
21 Please note, this class is entirely unsupported,
22 and no free support requests except for bug reports
23 will be entertained by the author.
26 class ADODB_Pager {
27 var $id; // unique id for pager (defaults to 'adodb')
28 var $db; // ADODB connection object
29 var $sql; // sql used
30 var $rs; // recordset generated
31 var $curr_page; // current page number before Render() called, calculated in constructor
32 var $rows; // number of rows per page
33 var $linksPerPage=10; // number of links per page in navigation bar
34 var $showPageLinks;
36 var $gridAttributes = 'width=100% border=1 bgcolor=white';
38 // Localize text strings here
39 var $first = '<code>|&lt;</code>';
40 var $prev = '<code>&lt;&lt;</code>';
41 var $next = '<code>>></code>';
42 var $last = '<code>>|</code>';
43 var $moreLinks = '...';
44 var $startLinks = '...';
45 var $gridHeader = false;
46 var $htmlSpecialChars = true;
47 var $page = 'Page';
48 var $linkSelectedColor = 'red';
49 var $cache = 0; #secs to cache with CachePageExecute()
51 //----------------------------------------------
52 // constructor
54 // $db adodb connection object
55 // $sql sql statement
56 // $id optional id to identify which pager,
57 // if you have multiple on 1 page.
58 // $id should be only be [a-z0-9]*
60 function __construct(&$db,$sql,$id = 'adodb', $showPageLinks = false)
62 global $PHP_SELF;
64 $curr_page = $id.'_curr_page';
65 if (!empty($PHP_SELF)) $PHP_SELF = htmlspecialchars($_SERVER['PHP_SELF']); // htmlspecialchars() to prevent XSS attacks
67 $this->sql = $sql;
68 $this->id = $id;
69 $this->db = $db;
70 $this->showPageLinks = $showPageLinks;
72 $next_page = $id.'_next_page';
74 if (isset($_GET[$next_page])) {
75 $_SESSION[$curr_page] = (integer) $_GET[$next_page];
77 if (empty($_SESSION[$curr_page])) $_SESSION[$curr_page] = 1; ## at first page
79 $this->curr_page = $_SESSION[$curr_page];
83 //---------------------------
84 // Display link to first page
85 function Render_First($anchor=true)
87 global $PHP_SELF;
88 if ($anchor) {
90 <a href="<?php echo $PHP_SELF,'?',$this->id;?>_next_page=1"><?php echo $this->first;?></a> &nbsp;
91 <?php
92 } else {
93 print "$this->first &nbsp; ";
97 //--------------------------
98 // Display link to next page
99 function render_next($anchor=true)
101 global $PHP_SELF;
103 if ($anchor) {
105 <a href="<?php echo $PHP_SELF,'?',$this->id,'_next_page=',$this->rs->AbsolutePage() + 1 ?>"><?php echo $this->next;?></a> &nbsp;
106 <?php
107 } else {
108 print "$this->next &nbsp; ";
112 //------------------
113 // Link to last page
115 // for better performance with large recordsets, you can set
116 // $this->db->pageExecuteCountRows = false, which disables
117 // last page counting.
118 function render_last($anchor=true)
120 global $PHP_SELF;
122 if (!$this->db->pageExecuteCountRows) return;
124 if ($anchor) {
126 <a href="<?php echo $PHP_SELF,'?',$this->id,'_next_page=',$this->rs->LastPageNo() ?>"><?php echo $this->last;?></a> &nbsp;
127 <?php
128 } else {
129 print "$this->last &nbsp; ";
133 //---------------------------------------------------
134 // original code by "Pablo Costa" <pablo@cbsp.com.br>
135 function render_pagelinks()
137 global $PHP_SELF;
138 $pages = $this->rs->LastPageNo();
139 $linksperpage = $this->linksPerPage ? $this->linksPerPage : $pages;
140 for($i=1; $i <= $pages; $i+=$linksperpage)
142 if($this->rs->AbsolutePage() >= $i)
144 $start = $i;
147 $numbers = '';
148 $end = $start+$linksperpage-1;
149 $link = $this->id . "_next_page";
150 if($end > $pages) $end = $pages;
153 if ($this->startLinks && $start > 1) {
154 $pos = $start - 1;
155 $numbers .= "<a href=$PHP_SELF?$link=$pos>$this->startLinks</a> ";
158 for($i=$start; $i <= $end; $i++) {
159 if ($this->rs->AbsolutePage() == $i)
160 $numbers .= "<font color=$this->linkSelectedColor><b>$i</b></font> ";
161 else
162 $numbers .= "<a href=$PHP_SELF?$link=$i>$i</a> ";
165 if ($this->moreLinks && $end < $pages)
166 $numbers .= "<a href=$PHP_SELF?$link=$i>$this->moreLinks</a> ";
167 print $numbers . ' &nbsp; ';
169 // Link to previous page
170 function render_prev($anchor=true)
172 global $PHP_SELF;
173 if ($anchor) {
175 <a href="<?php echo $PHP_SELF,'?',$this->id,'_next_page=',$this->rs->AbsolutePage() - 1 ?>"><?php echo $this->prev;?></a> &nbsp;
176 <?php
177 } else {
178 print "$this->prev &nbsp; ";
182 //--------------------------------------------------------
183 // Simply rendering of grid. You should override this for
184 // better control over the format of the grid
186 // We use output buffering to keep code clean and readable.
187 function RenderGrid()
189 global $gSQLBlockRows; // used by rs2html to indicate how many rows to display
190 include_once(ADODB_DIR.'/tohtml.inc.php');
191 ob_start();
192 $gSQLBlockRows = $this->rows;
193 rs2html($this->rs,$this->gridAttributes,$this->gridHeader,$this->htmlSpecialChars);
194 $s = ob_get_contents();
195 ob_end_clean();
196 return $s;
199 //-------------------------------------------------------
200 // Navigation bar
202 // we use output buffering to keep the code easy to read.
203 function RenderNav()
205 ob_start();
206 if (!$this->rs->AtFirstPage()) {
207 $this->Render_First();
208 $this->Render_Prev();
209 } else {
210 $this->Render_First(false);
211 $this->Render_Prev(false);
213 if ($this->showPageLinks){
214 $this->Render_PageLinks();
216 if (!$this->rs->AtLastPage()) {
217 $this->Render_Next();
218 $this->Render_Last();
219 } else {
220 $this->Render_Next(false);
221 $this->Render_Last(false);
223 $s = ob_get_contents();
224 ob_end_clean();
225 return $s;
228 //-------------------
229 // This is the footer
230 function RenderPageCount()
232 if (!$this->db->pageExecuteCountRows) return '';
233 $lastPage = $this->rs->LastPageNo();
234 if ($lastPage == -1) $lastPage = 1; // check for empty rs.
235 if ($this->curr_page > $lastPage) $this->curr_page = 1;
236 return "<font size=-1>$this->page ".$this->curr_page."/".$lastPage."</font>";
239 //-----------------------------------
240 // Call this class to draw everything.
241 function Render($rows=10)
243 global $ADODB_COUNTRECS;
245 $this->rows = $rows;
247 if ($this->db->dataProvider == 'informix') $this->db->cursorType = IFX_SCROLL;
249 $savec = $ADODB_COUNTRECS;
250 if ($this->db->pageExecuteCountRows) $ADODB_COUNTRECS = true;
251 if ($this->cache)
252 $rs = $this->db->CachePageExecute($this->cache,$this->sql,$rows,$this->curr_page);
253 else
254 $rs = $this->db->PageExecute($this->sql,$rows,$this->curr_page);
255 $ADODB_COUNTRECS = $savec;
257 $this->rs = $rs;
258 if (!$rs) {
259 print "<h3>Query failed: $this->sql</h3>";
260 return;
263 if (!$rs->EOF && (!$rs->AtFirstPage() || !$rs->AtLastPage()))
264 $header = $this->RenderNav();
265 else
266 $header = "&nbsp;";
268 $grid = $this->RenderGrid();
269 $footer = $this->RenderPageCount();
271 $this->RenderLayout($header,$grid,$footer);
273 $rs->Close();
274 $this->rs = false;
277 //------------------------------------------------------
278 // override this to control overall layout and formating
279 function RenderLayout($header,$grid,$footer,$attributes='border=1 bgcolor=beige')
281 echo "<table ".$attributes."><tr><td>",
282 $header,
283 "</td></tr><tr><td>",
284 $grid,
285 "</td></tr><tr><td>",
286 $footer,
287 "</td></tr></table>";