Created
March 14, 2012 17:04
-
-
Save joonty/2037902 to your computer and use it in GitHub Desktop.
Custom paginator helper for CakePHP
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 | |
App::uses('PaginatorHelper','View/Helper'); | |
class CustomPaginatorHelper extends PaginatorHelper { | |
public function numbers($options = array()) { | |
if ($options === true) { | |
$options = array( | |
'before' => ' | ', 'after' => ' | ', 'first' => 'first', 'last' => 'last' | |
); | |
} | |
$defaults = array( | |
'tag' => 'span', 'before' => null, 'after' => null, 'model' => $this->defaultModel(), 'class' => null, | |
'modulus' => '8', 'separator' => ' | ', 'first' => null, 'last' => null, 'ellipsis' => '...', | |
); | |
$options += $defaults; | |
$params = (array)$this->params($options['model']) + array('page' => 1); | |
unset($options['model']); | |
if ($params['pageCount'] <= 1) { | |
return false; | |
} | |
extract($options); | |
unset($options['tag'], $options['before'], $options['after'], $options['model'], | |
$options['modulus'], $options['separator'], $options['first'], $options['last'], | |
$options['ellipsis'], $options['class'] | |
); | |
$out = ''; | |
if ($modulus && $params['pageCount'] > $modulus) { | |
$half = intval($modulus / 2); | |
$end = $params['page'] + $half; | |
if ($end > $params['pageCount']) { | |
$end = $params['pageCount']; | |
} | |
$start = $params['page'] - ($modulus - ($end - $params['page'])); | |
if ($start <= 1) { | |
$start = 1; | |
$end = $params['page'] + ($modulus - $params['page']) + 1; | |
} | |
if ($first && $start > 1) { | |
$offset = ($start <= (int)$first) ? $start - 1 : $first; | |
if ($offset < $start - 1) { | |
$out .= $this->first($offset, compact('tag', 'separator', 'ellipsis', 'class')); | |
} else { | |
$out .= $this->first($offset, compact('tag', 'separator', 'class') + array('after' => $separator)); | |
} | |
} | |
$out .= $before; | |
$pagemax = $params['limit']; | |
for ($i = $start; $i < $params['page']; $i++) { | |
$display = (($i - 1)*$pagemax + 1). '-'.($i*$pagemax); | |
$out .= $this->Html->tag($tag, $this->link($display, array('page' => $i), $options), compact('class')) | |
. $separator; | |
} | |
$currentClass = 'current'; | |
if ($class) { | |
$currentClass .= ' ' . $class; | |
} | |
$out .= $this->Html->tag($tag, $params['page'], array('class' => $currentClass)); | |
if ($i != $params['pageCount']) { | |
$out .= $separator; | |
} | |
$start = $params['page'] + 1; | |
for ($i = $start; $i < $end; $i++) { | |
$out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class')) | |
. $separator; | |
} | |
if ($end != $params['page']) { | |
$out .= $this->Html->tag($tag, $this->link($i, array('page' => $end), $options), compact('class')); | |
} | |
$out .= $after; | |
if ($last && $end < $params['pageCount']) { | |
$offset = ($params['pageCount'] < $end + (int)$last) ? $params['pageCount'] - $end : $last; | |
if ($offset <= $last && $params['pageCount'] - $end > $offset) { | |
$out .= $this->last($offset, compact('tag', 'separator', 'ellipsis', 'class')); | |
} else { | |
$out .= $this->last($offset, compact('tag', 'separator', 'class') + array('before' => $separator)); | |
} | |
} | |
} else { | |
$out .= $before; | |
$pagemax = $params['limit']; | |
for ($i = 1; $i <= $params['pageCount']; $i++) { | |
$display = (($i - 1)*$pagemax + 1). '-'.($i*$pagemax); | |
if ($i == $params['page']) { | |
$currentClass = 'current'; | |
if ($class) { | |
$currentClass .= ' ' . $class; | |
} | |
$out .= $this->Html->tag($tag, $display, array('class' => $currentClass)); | |
} else { | |
$out .= $this->Html->tag($tag, $this->link($display, array('page' => $i), $options), compact('class')); | |
} | |
if ($i != $params['pageCount']) { | |
$out .= $separator; | |
} | |
} | |
$out .= $after; | |
} | |
return $out; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To use, save this to app/View/Helper/CustomPaginatorHelper.php, and add something like this in the controller:
You can then call
$this->CustomPaginator->numbers()
and all other methods on the originalPaginator
class in the view templates.