1 <?php /* vim: set binary expandtab tabstop=4 shiftwidth=4 textwidth=80:
2 -------------------------------------------------------------------------------
4 -------------------------------------------------------------------------------
5 This file is part of mlib.
7 mlib is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2 of the License, or (at your option)
12 mlib is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License; if not,
17 write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 Boston, MA 02111-1307 USA
19 -------------------------------------------------------------------------------
22 -------------------------------------------------------------------------------
24 -----------------------------------------------------------------------------*/
27 * Paginador de tablas, a partir de un array
28 * Basado en MLIB_DB_Pager
30 class MLIB_Array_Pager
39 * @param array $res Contenido de la taba
40 * @param int $from Nro. de fila 'desde'
41 * @param int $limit Cantidad de resultados por página
42 * @param int $maxpages Cantidad máxima de páginas a mostrar
43 * @param int $numrows Nro. de filas de la tabla
45 function MLIB_Array_Pager (&$res, $from = 0, $limit = 10, $maxpages = 21, $numrows = null)
47 $this->dbh = $res->dbh;
49 $this->row_counter = $res->row_counter;
50 $this->limit_from = $res->limit_from;
52 $this->limit = $limit;
53 $this->numrows = $numrows;
54 $this->maxpages = $maxpages;
59 * Calcula los datos necesarios para que funcione el paginador
60 * (es igual a MLIB_DB_Pager)
61 * @return mixed PEAR_Error if error.
65 // if there is no numrows given, calculate it
66 if ($this->numrows === null) {
67 $this->numrows = $this->numRows();
68 if (DB::isError($this->numrows)) {
69 return $this->numrows;
72 // Si no hay resultados no se hace nada.
73 if (empty($this->numrows) or ($this->numrows < 0)) {
76 $this->from = empty($this->from) ? 0 : $this->from;
78 if ($this->limit <= 0) {
79 return PEAR::raiseError (null, 'wrong "limit" param', null,
80 null, null, 'DB_Error', true);
83 // Total number of pages
84 $this->numpages = ceil($this->numrows/$this->limit);
88 $this->lastpage = $this->numpages;
91 $this->pages = array();
92 for ($i = 1; $i <= $this->numpages; $i++) {
93 $offset = $this->limit * ($i - 1);
94 $this->pages[$i] = $offset;
95 // $from must point to one page
96 if ($this->from == $offset) {
97 // The current page we are
98 $this->currentpage = $i;
101 if (!isset($this->currentpage)) {
102 return PEAR::raiseError (null, 'wrong "from" param', null,
103 null, null, 'DB_Error', true);
106 // Limit number of pages (goole algoritm)
107 if ($this->maxpages) {
108 $radio = floor($this->maxpages/2);
109 $this->firstpage = $this->currentpage - $radio;
110 if (!($this->maxpages % 2)) {
113 if ($this->firstpage < 1) {
114 $this->firstpage = 1;
116 $this->lastpage = $this->currentpage + $radio;
117 if ($this->lastpage > $this->numpages) {
118 $this->lastpage = $this->numpages;
120 foreach (range($this->firstpage, $this->lastpage) as $page) {
121 $tmp[$page] = $this->pages[$page];
127 $this->prev = $this->from - $this->limit;
128 $this->prev = ($this->prev >= 0) ? $this->prev : null;
131 $this->next = $this->from + $this->limit;
132 $this->next = ($this->next < $this->numrows) ? $this->next : null;
134 // Results remaining in next page & Last row to fetch
135 if ($this->currentpage == $this->numpages) {
137 $this->to = $this->numrows;
139 if ($this->currentpage == ($this->numpages - 1)) {
140 $this->remain = $this->numrows - ($this->limit * ($this->numpages - 1));
142 $this->remain = $this->limit;
144 $this->to = $this->currentpage * $this->limit;
147 // Current item (when fetching).
148 $this->current = $this->from - 1;
152 * Obtiene la siguiente fila de la tabla
155 function fetchRow($mode=null, $rownum=null)
158 if ($this->current >= $this->to) {
161 return $this->result[$this->current];
165 * Devuelve la cantidad de filas de la tabla
169 return count($this->result);