Autor: ------------------------------------------------------------------------------- $Id$ -----------------------------------------------------------------------------*/ /** * Paginador de tablas, a partir de un array * Basado en MLIB_DB_Pager */ class MLIB_Array_Pager { var $currentpage = 0; var $current = 0; var $to = 0; /** * Constructor * * @param array $res Contenido de la taba * @param int $from Nro. de fila 'desde' * @param int $limit Cantidad de resultados por página * @param int $maxpages Cantidad máxima de páginas a mostrar * @param int $numrows Nro. de filas de la tabla */ function MLIB_Array_Pager (&$res, $from = 0, $limit = 10, $maxpages = 21, $numrows = null) { $this->dbh = $res->dbh; $this->result = $res; $this->row_counter = $res->row_counter; $this->limit_from = $res->limit_from; $this->from = $from; $this->limit = $limit; $this->numrows = $numrows; $this->maxpages = $maxpages; $this->build(); } /** * Calcula los datos necesarios para que funcione el paginador * (es igual a MLIB_DB_Pager) * @return mixed PEAR_Error if error. */ function build() { // if there is no numrows given, calculate it if ($this->numrows === null) { $this->numrows = $this->numRows(); if (DB::isError($this->numrows)) { return $this->numrows; } } // Si no hay resultados no se hace nada. if (empty($this->numrows) or ($this->numrows < 0)) { return; } $this->from = empty($this->from) ? 0 : $this->from; if ($this->limit <= 0) { return PEAR::raiseError (null, 'wrong "limit" param', null, null, null, 'DB_Error', true); } // Total number of pages $this->numpages = ceil($this->numrows/$this->limit); // first & last page $this->firstpage = 1; $this->lastpage = $this->numpages; // Build pages array $this->pages = array(); for ($i = 1; $i <= $this->numpages; $i++) { $offset = $this->limit * ($i - 1); $this->pages[$i] = $offset; // $from must point to one page if ($this->from == $offset) { // The current page we are $this->currentpage = $i; } } if (!isset($this->currentpage)) { return PEAR::raiseError (null, 'wrong "from" param', null, null, null, 'DB_Error', true); } // Limit number of pages (goole algoritm) if ($this->maxpages) { $radio = floor($this->maxpages/2); $this->firstpage = $this->currentpage - $radio; if (!($this->maxpages % 2)) { $this->firstpage++; } if ($this->firstpage < 1) { $this->firstpage = 1; } $this->lastpage = $this->currentpage + $radio; if ($this->lastpage > $this->numpages) { $this->lastpage = $this->numpages; } foreach (range($this->firstpage, $this->lastpage) as $page) { $tmp[$page] = $this->pages[$page]; } $this->pages = $tmp; } // Prev link $this->prev = $this->from - $this->limit; $this->prev = ($this->prev >= 0) ? $this->prev : null; // Next link $this->next = $this->from + $this->limit; $this->next = ($this->next < $this->numrows) ? $this->next : null; // Results remaining in next page & Last row to fetch if ($this->currentpage == $this->numpages) { $this->remain = 0; $this->to = $this->numrows; } else { if ($this->currentpage == ($this->numpages - 1)) { $this->remain = $this->numrows - ($this->limit * ($this->numpages - 1)); } else { $this->remain = $this->limit; } $this->to = $this->currentpage * $this->limit; } // Current item (when fetching). $this->current = $this->from - 1; } /** * Obtiene la siguiente fila de la tabla * @return array */ function fetchRow($mode=null, $rownum=null) { $this->current++; if ($this->current >= $this->to) { return null; } return $this->result[$this->current]; } /** * Devuelve la cantidad de filas de la tabla * @return int. */ function numRows(){ return count($this->result); } } ?>