Skip to content

Commit

Permalink
Added configurable ellipsis on Paginator::numbers(), Paginator::first…
Browse files Browse the repository at this point in the history
…(), Paginator::last(). Fixes #1086
  • Loading branch information
jeremyharris committed Nov 8, 2010
1 parent b5deb41 commit 060f149
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 8 deletions.
23 changes: 15 additions & 8 deletions cake/libs/view/helpers/paginator.php
Expand Up @@ -582,6 +582,7 @@ public function counter($options = array()) {
* links to generate
* - `last` Whether you want last links generated, set to an integer to define the number of 'last'
* links to generate
* - `ellipsis` Ellipsis content, defaults to '...'
*
* @param mixed $options Options for the numbers, (before, after, model, modulus, separator)
* @return string numbers string.
Expand All @@ -595,7 +596,7 @@ public function numbers($options = array()) {

$defaults = array(
'tag' => 'span', 'before' => null, 'after' => null, 'model' => $this->defaultModel(),
'modulus' => '8', 'separator' => ' | ', 'first' => null, 'last' => null,
'modulus' => '8', 'separator' => ' | ', 'first' => null, 'last' => null, 'ellipsis' => '...',
);
$options += $defaults;

Expand All @@ -608,7 +609,9 @@ public function numbers($options = array()) {

extract($options);
unset($options['tag'], $options['before'], $options['after'], $options['model'],
$options['modulus'], $options['separator'], $options['first'], $options['last']);
$options['modulus'], $options['separator'], $options['first'], $options['last'],
$options['ellipsis']
);

$out = '';

Expand All @@ -628,7 +631,7 @@ public function numbers($options = array()) {
if ($first && $start > 1) {
$offset = ($start <= (int)$first) ? $start - 1 : $first;
if ($offset < $start - 1) {
$out .= $this->first($offset, array('tag' => $tag, 'separator' => $separator));
$out .= $this->first($offset, array('tag' => $tag, 'separator' => $separator, 'ellipsis' => $ellipsis));
} else {
$out .= $this->first($offset, array('tag' => $tag, 'after' => $separator, 'separator' => $separator));
}
Expand Down Expand Up @@ -661,7 +664,7 @@ public function numbers($options = array()) {
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, array('tag' => $tag, 'separator' => $separator));
$out .= $this->last($offset, array('tag' => $tag, 'separator' => $separator, 'ellipsis' => $ellipsis));
} else {
$out .= $this->last($offset, array('tag' => $tag, 'before' => $separator, 'separator' => $separator));
}
Expand Down Expand Up @@ -696,6 +699,7 @@ public function numbers($options = array()) {
* - `before` Content to insert before the link/tag
* - `model` The model to use defaults to PaginatorHelper::defaultModel()
* - `separator` Content between the generated links, defaults to ' | '
* - `ellipsis` Content for ellipsis, defaults to '...'
*
* @param mixed $first if string use as label for the link, if numeric print page numbers
* @param mixed $options
Expand All @@ -708,6 +712,7 @@ public function first($first = '<< first', $options = array()) {
'after'=> null,
'model' => $this->defaultModel(),
'separator' => ' | ',
'ellipsis' => '...',
),
(array)$options);

Expand All @@ -718,13 +723,13 @@ public function first($first = '<< first', $options = array()) {
return false;
}
extract($options);
unset($options['tag'], $options['after'], $options['model'], $options['separator']);
unset($options['tag'], $options['after'], $options['model'], $options['separator'], $options['ellipsis']);

$out = '';

if (is_int($first) && $params['page'] > $first) {
if ($after === null) {
$after = '...';
$after = $ellipsis;
}
for ($i = 1; $i <= $first; $i++) {
$out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options));
Expand All @@ -749,6 +754,7 @@ public function first($first = '<< first', $options = array()) {
* - `before` Content to insert before the link/tag
* - `model` The model to use defaults to PaginatorHelper::defaultModel()
* - `separator` Content between the generated links, defaults to ' | '
* - `ellipsis` Content for ellipsis, defaults to '...'
*
* @param mixed $last if string use as label for the link, if numeric print page numbers
* @param mixed $options Array of options
Expand All @@ -761,6 +767,7 @@ public function last($last = 'last >>', $options = array()) {
'before'=> null,
'model' => $this->defaultModel(),
'separator' => ' | ',
'ellipsis' => '...',
),
(array)$options);

Expand All @@ -772,14 +779,14 @@ public function last($last = 'last >>', $options = array()) {
}

extract($options);
unset($options['tag'], $options['before'], $options['model'], $options['separator']);
unset($options['tag'], $options['before'], $options['model'], $options['separator'], $options['ellipsis']);

$out = '';
$lower = $params['pageCount'] - $last + 1;

if (is_int($last) && $params['page'] < $lower) {
if ($before === null) {
$before = '...';
$before = $ellipsis;
}
for ($i = $lower; $i <= $params['pageCount']; $i++) {
$out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options));
Expand Down
60 changes: 60 additions & 0 deletions cake/tests/cases/libs/view/helpers/paginator.test.php
Expand Up @@ -1573,6 +1573,40 @@ function testNumbers() {
array('span' => array()), array('a' => array('href' => '/index/page:4897')), '4897', '/a', '/span',
);
$this->assertTags($result, $expected);

$this->Paginator->params['paging']['Client']['page'] = 3;
$result = $this->Paginator->numbers(array('first' => 2, 'modulus' => 2, 'last' => 2, 'separator' => ' - ', 'ellipsis' => ' ~~~ '));
$expected = array(
array('span' => array()), array('a' => array('href' => '/index/page:1')), '1', '/a', '/span',
' - ',
array('span' => array()), array('a' => array('href' => '/index/page:2')), '2', '/a', '/span',
' - ',
array('span' => array('class' => 'current')), '3', '/span',
' - ',
array('span' => array()), array('a' => array('href' => '/index/page:4')), '4', '/a', '/span',
' ~~~ ',
array('span' => array()), array('a' => array('href' => '/index/page:4896')), '4896', '/a', '/span',
' - ',
array('span' => array()), array('a' => array('href' => '/index/page:4897')), '4897', '/a', '/span',
);
$this->assertTags($result, $expected);

$this->Paginator->params['paging']['Client']['page'] = 3;
$result = $this->Paginator->numbers(array('first' => 2, 'modulus' => 2, 'last' => 2, 'separator' => ' - ', 'ellipsis' => '<span class="ellipsis">...</span>'));
$expected = array(
array('span' => array()), array('a' => array('href' => '/index/page:1')), '1', '/a', '/span',
' - ',
array('span' => array()), array('a' => array('href' => '/index/page:2')), '2', '/a', '/span',
' - ',
array('span' => array('class' => 'current')), '3', '/span',
' - ',
array('span' => array()), array('a' => array('href' => '/index/page:4')), '4', '/a', '/span',
array('span' => array('class' => 'ellipsis')), '...', '/span',
array('span' => array()), array('a' => array('href' => '/index/page:4896')), '4896', '/a', '/span',
' - ',
array('span' => array()), array('a' => array('href' => '/index/page:4897')), '4897', '/a', '/span',
);
$this->assertTags($result, $expected);
}

/**
Expand Down Expand Up @@ -1717,6 +1751,32 @@ function testFirstAndLast() {
'/span',
);
$this->assertTags($result, $expected);

$result = $this->Paginator->last(2, array('ellipsis' => '~~~'));
$expected = array(
'~~~',
'<span',
array('a' => array('href' => '/index/page:14/sort:Client.name/direction:DESC')), '14', '/a',
'/span',
' | ',
'<span',
array('a' => array('href' => '/index/page:15/sort:Client.name/direction:DESC')), '15', '/a',
'/span',
);
$this->assertTags($result, $expected);

$result = $this->Paginator->last(2, array('ellipsis' => '<span class="ellipsis">...</span>'));
$expected = array(
array('span' => array('class' => 'ellipsis')), '...', '/span',
'<span',
array('a' => array('href' => '/index/page:14/sort:Client.name/direction:DESC')), '14', '/a',
'/span',
' | ',
'<span',
array('a' => array('href' => '/index/page:15/sort:Client.name/direction:DESC')), '15', '/a',
'/span',
);
$this->assertTags($result, $expected);
}

/**
Expand Down

0 comments on commit 060f149

Please sign in to comment.