Created
May 25, 2011 17:46
-
-
Save thiagosf/991459 to your computer and use it in GitHub Desktop.
Paginação simples em OOP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Paginacao de dados | |
*/ | |
class Paginator | |
{ | |
/** | |
* Total de registros para ser paginados | |
*/ | |
public $count; | |
/** | |
* Total de paginas | |
*/ | |
private $total_pages; | |
/** | |
* Pagina atual | |
*/ | |
private $page; | |
/** | |
* Maximo de registros antes e depois da pagina atual | |
*/ | |
private $range = 5; | |
/** | |
* URL para ser passada os parametros da paginacao | |
*/ | |
private $url; | |
/** | |
* Registros por pagina | |
*/ | |
private $limit; | |
/** | |
* Setando configurações | |
*/ | |
public function __construct ($config = array()) { | |
$defaults = array( | |
'range' => $this->range, | |
'page' => $this->getPage(), | |
'url' => $this->getUrl(), | |
); | |
$config = array_merge($defaults, $config); | |
$this->setConfig($config); | |
} | |
/** | |
* Resgata pagina atual | |
*/ | |
public function getPage () { | |
$page = (isset($_GET['page'])) ? (int) $_GET['page'] : 1; | |
$page = ($page < 1) ? 1 : $page; | |
return $page; | |
} | |
/** | |
* Resgata URL default | |
*/ | |
public function getUrl () { | |
$url = $_SERVER['PHP_SELF']; | |
return $url; | |
} | |
/** | |
* Setando configuracao | |
*/ | |
private function setConfig ($config) { | |
foreach($config as $key => $value) { | |
$this->$key = $value; | |
} | |
$this->total_pages = ceil($this->count / $this->limit); | |
} | |
/** | |
* Pagina anterior | |
*/ | |
public function prev ($label = 'Prev') { | |
if ($this->hasPrev()) { | |
$page_prev = ($this->page - 1); | |
$page_prev = ($page_prev < 1) ? 1 : $page_prev; | |
$page_prev = $this->getLink($page_prev); | |
return $this->prepareLink($label, $page_prev); | |
} | |
else { | |
return sprintf('<span class="off_link">%s</span>', strip_tags($label)); | |
} | |
} | |
/** | |
* Pagina anterior | |
*/ | |
public function next ($label = 'Next') { | |
if ($this->hasNext()) { | |
$page_next = ($this->page + 1); | |
$page_next = ($page_next > $this->total_pages) ? $this->total_pages : $page_next; | |
$page_next = $this->getLink($page_next); | |
return $this->prepareLink($label, $page_next); | |
} | |
else { | |
return sprintf('<span class="off_link">%s</span>', strip_tags($label)); | |
} | |
} | |
/** | |
* Primeira pagina | |
*/ | |
public function first ($label = 'First') { | |
if ($this->hasPrev()) { | |
return $this->prepareLink($label, $this->getLink(1)); | |
} | |
else { | |
return sprintf('<span class="off_link">%s</span>', strip_tags($label)); | |
} | |
} | |
/** | |
* Ultima pagina | |
*/ | |
public function last ($label = 'Last') { | |
if ($this->hasNext()) { | |
return $this->prepareLink($label, $this->getLink($this->total_pages)); | |
} | |
else { | |
return sprintf('<span class="off_link">%s</span>', strip_tags($label)); | |
} | |
} | |
/** | |
* Verifica se tem proxima pagina | |
*/ | |
public function hasNext () { | |
if ($this->page != $this->total_pages) { | |
return true; | |
} | |
return false; | |
} | |
/** | |
* Verifica se tem pagina anterior | |
*/ | |
public function hasPrev () { | |
if ($this->page != 1) { | |
return true; | |
} | |
return false; | |
} | |
/** | |
* Numeros da paginacao | |
*/ | |
public function numbers ($separator = ' | ', $container = '<span>%s</span>') { | |
$start_rest = ($this->page <= $this->range) ? $this->range - $this->page : 0; | |
$end_rest = (($this->total_pages - $this->page) <= $this->range) ? $this->range - ($this->total_pages - $this->page) : 0; | |
$start = ($this->page - ($this->range + $end_rest)); | |
$start = ($start < 1) ? 1 : $start; | |
$end = ($this->page + ($this->range + $start_rest)); | |
$end = ($end > $this->total_pages) ? $this->total_pages : $end; | |
$out = array(); | |
for ($i = $start; $i <= $end; $i++) { | |
if ($i == $this->page) { | |
$out[] = sprintf('<span class="current">%s</span>', $i); | |
} | |
else { | |
$out[] = sprintf('<span>%s</span>', $this->prepareLink($i, $this->getLink($i))); | |
} | |
} | |
$out = implode($separator, $out); | |
if (!empty($container)) { | |
$out = sprintf($container, $out); | |
} | |
return $out; | |
} | |
/** | |
* Prepara link para paginacao | |
*/ | |
public function prepareLink ($label, $link) { | |
if (preg_match('/%s/', $label)) { | |
$label = preg_replace('/%s/', $link, $label); | |
} | |
else if (!empty($label)) { | |
$label = sprintf('<a href="%s">%s</a>', $link, $label); | |
} | |
else { | |
$label = sprintf('<a href="%s">%s</a>', $link, $label); | |
} | |
return $label."\n"; | |
} | |
/** | |
* Resgata link para ser utilizado em todos links | |
*/ | |
public function getLink ($page) { | |
$query_string = $_SERVER['QUERY_STRING']; | |
$query_string = preg_replace('/&?page=[^&]*&?/i', '', $query_string); | |
$query_string = (!empty($query_string)) ? '&'.$query_string : null; | |
$url = sprintf('%s?page=%s%s', $this->url, $page, $query_string); | |
return $url; | |
} | |
/** | |
* Infomrações da paginação | |
*/ | |
public function info ($string) { | |
$infos = array( | |
'page' => $this->page, | |
'pages' => $this->total_pages, | |
'count' => (int) $this->count | |
); | |
foreach($infos as $key => $value) { | |
$string = preg_replace('/%'.$key.'%/i', $value, $string); | |
} | |
return $string; | |
} | |
/** | |
* Facilitador para mostrar paginação | |
*/ | |
public function display ($config = array()) { | |
$defaults = array( | |
'first' => true, | |
'prev' => true, | |
'numbers' => true, | |
'next' => true, | |
'last' => true, | |
'separator' => '', | |
'info' => true, | |
); | |
$config = array_merge($defaults, $config); | |
$separator = (($config['separator']) ? ' | ' : ''); | |
$out = array(); | |
if (($config['info'])) $out[] = $this->info('<span class="count">%count%</span>'); | |
if (($config['info'])) $out[] = $this->info('<span class="info_page">Página <b>%page%</b> de <b>%pages%</b></span>'); | |
if (($config['first'])) $out[] = $this->first('<span class="first"><a href="%s">Primeira</a></span>'); | |
if (($config['prev'])) $out[] = $this->prev('<span class="prev"><a href="%s">Anterior</a></span>'); | |
if (($config['numbers'])) $out[] = $this->numbers($separator, '<span class="numbers">%s</span>'); | |
if (($config['next'])) $out[] = $this->next('<span class="next"><a href="%s">Próxima</a></span>'); | |
if (($config['last'])) $out[] = $this->last('<span class="last"><a href="%s">Última</a></span>'); | |
$out = implode($separator, $out); | |
return '<div class="paginator">'.$out.'</div>'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment