Skip to content

Commit 060f149

Browse files
author
Jeremy Harris
committed
Added configurable ellipsis on Paginator::numbers(), Paginator::first(), Paginator::last(). Fixes #1086
1 parent b5deb41 commit 060f149

File tree

2 files changed

+75
-8
lines changed

2 files changed

+75
-8
lines changed

cake/libs/view/helpers/paginator.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,7 @@ public function counter($options = array()) {
582582
* links to generate
583583
* - `last` Whether you want last links generated, set to an integer to define the number of 'last'
584584
* links to generate
585+
* - `ellipsis` Ellipsis content, defaults to '...'
585586
*
586587
* @param mixed $options Options for the numbers, (before, after, model, modulus, separator)
587588
* @return string numbers string.
@@ -595,7 +596,7 @@ public function numbers($options = array()) {
595596

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

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

609610
extract($options);
610611
unset($options['tag'], $options['before'], $options['after'], $options['model'],
611-
$options['modulus'], $options['separator'], $options['first'], $options['last']);
612+
$options['modulus'], $options['separator'], $options['first'], $options['last'],
613+
$options['ellipsis']
614+
);
612615

613616
$out = '';
614617

@@ -628,7 +631,7 @@ public function numbers($options = array()) {
628631
if ($first && $start > 1) {
629632
$offset = ($start <= (int)$first) ? $start - 1 : $first;
630633
if ($offset < $start - 1) {
631-
$out .= $this->first($offset, array('tag' => $tag, 'separator' => $separator));
634+
$out .= $this->first($offset, array('tag' => $tag, 'separator' => $separator, 'ellipsis' => $ellipsis));
632635
} else {
633636
$out .= $this->first($offset, array('tag' => $tag, 'after' => $separator, 'separator' => $separator));
634637
}
@@ -661,7 +664,7 @@ public function numbers($options = array()) {
661664
if ($last && $end < $params['pageCount']) {
662665
$offset = ($params['pageCount'] < $end + (int)$last) ? $params['pageCount'] - $end : $last;
663666
if ($offset <= $last && $params['pageCount'] - $end > $offset) {
664-
$out .= $this->last($offset, array('tag' => $tag, 'separator' => $separator));
667+
$out .= $this->last($offset, array('tag' => $tag, 'separator' => $separator, 'ellipsis' => $ellipsis));
665668
} else {
666669
$out .= $this->last($offset, array('tag' => $tag, 'before' => $separator, 'separator' => $separator));
667670
}
@@ -696,6 +699,7 @@ public function numbers($options = array()) {
696699
* - `before` Content to insert before the link/tag
697700
* - `model` The model to use defaults to PaginatorHelper::defaultModel()
698701
* - `separator` Content between the generated links, defaults to ' | '
702+
* - `ellipsis` Content for ellipsis, defaults to '...'
699703
*
700704
* @param mixed $first if string use as label for the link, if numeric print page numbers
701705
* @param mixed $options
@@ -708,6 +712,7 @@ public function first($first = '<< first', $options = array()) {
708712
'after'=> null,
709713
'model' => $this->defaultModel(),
710714
'separator' => ' | ',
715+
'ellipsis' => '...',
711716
),
712717
(array)$options);
713718

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

723728
$out = '';
724729

725730
if (is_int($first) && $params['page'] > $first) {
726731
if ($after === null) {
727-
$after = '...';
732+
$after = $ellipsis;
728733
}
729734
for ($i = 1; $i <= $first; $i++) {
730735
$out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options));
@@ -749,6 +754,7 @@ public function first($first = '<< first', $options = array()) {
749754
* - `before` Content to insert before the link/tag
750755
* - `model` The model to use defaults to PaginatorHelper::defaultModel()
751756
* - `separator` Content between the generated links, defaults to ' | '
757+
* - `ellipsis` Content for ellipsis, defaults to '...'
752758
*
753759
* @param mixed $last if string use as label for the link, if numeric print page numbers
754760
* @param mixed $options Array of options
@@ -761,6 +767,7 @@ public function last($last = 'last >>', $options = array()) {
761767
'before'=> null,
762768
'model' => $this->defaultModel(),
763769
'separator' => ' | ',
770+
'ellipsis' => '...',
764771
),
765772
(array)$options);
766773

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

774781
extract($options);
775-
unset($options['tag'], $options['before'], $options['model'], $options['separator']);
782+
unset($options['tag'], $options['before'], $options['model'], $options['separator'], $options['ellipsis']);
776783

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

780787
if (is_int($last) && $params['page'] < $lower) {
781788
if ($before === null) {
782-
$before = '...';
789+
$before = $ellipsis;
783790
}
784791
for ($i = $lower; $i <= $params['pageCount']; $i++) {
785792
$out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options));

cake/tests/cases/libs/view/helpers/paginator.test.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1573,6 +1573,40 @@ function testNumbers() {
15731573
array('span' => array()), array('a' => array('href' => '/index/page:4897')), '4897', '/a', '/span',
15741574
);
15751575
$this->assertTags($result, $expected);
1576+
1577+
$this->Paginator->params['paging']['Client']['page'] = 3;
1578+
$result = $this->Paginator->numbers(array('first' => 2, 'modulus' => 2, 'last' => 2, 'separator' => ' - ', 'ellipsis' => ' ~~~ '));
1579+
$expected = array(
1580+
array('span' => array()), array('a' => array('href' => '/index/page:1')), '1', '/a', '/span',
1581+
' - ',
1582+
array('span' => array()), array('a' => array('href' => '/index/page:2')), '2', '/a', '/span',
1583+
' - ',
1584+
array('span' => array('class' => 'current')), '3', '/span',
1585+
' - ',
1586+
array('span' => array()), array('a' => array('href' => '/index/page:4')), '4', '/a', '/span',
1587+
' ~~~ ',
1588+
array('span' => array()), array('a' => array('href' => '/index/page:4896')), '4896', '/a', '/span',
1589+
' - ',
1590+
array('span' => array()), array('a' => array('href' => '/index/page:4897')), '4897', '/a', '/span',
1591+
);
1592+
$this->assertTags($result, $expected);
1593+
1594+
$this->Paginator->params['paging']['Client']['page'] = 3;
1595+
$result = $this->Paginator->numbers(array('first' => 2, 'modulus' => 2, 'last' => 2, 'separator' => ' - ', 'ellipsis' => '<span class="ellipsis">...</span>'));
1596+
$expected = array(
1597+
array('span' => array()), array('a' => array('href' => '/index/page:1')), '1', '/a', '/span',
1598+
' - ',
1599+
array('span' => array()), array('a' => array('href' => '/index/page:2')), '2', '/a', '/span',
1600+
' - ',
1601+
array('span' => array('class' => 'current')), '3', '/span',
1602+
' - ',
1603+
array('span' => array()), array('a' => array('href' => '/index/page:4')), '4', '/a', '/span',
1604+
array('span' => array('class' => 'ellipsis')), '...', '/span',
1605+
array('span' => array()), array('a' => array('href' => '/index/page:4896')), '4896', '/a', '/span',
1606+
' - ',
1607+
array('span' => array()), array('a' => array('href' => '/index/page:4897')), '4897', '/a', '/span',
1608+
);
1609+
$this->assertTags($result, $expected);
15761610
}
15771611

15781612
/**
@@ -1717,6 +1751,32 @@ function testFirstAndLast() {
17171751
'/span',
17181752
);
17191753
$this->assertTags($result, $expected);
1754+
1755+
$result = $this->Paginator->last(2, array('ellipsis' => '~~~'));
1756+
$expected = array(
1757+
'~~~',
1758+
'<span',
1759+
array('a' => array('href' => '/index/page:14/sort:Client.name/direction:DESC')), '14', '/a',
1760+
'/span',
1761+
' | ',
1762+
'<span',
1763+
array('a' => array('href' => '/index/page:15/sort:Client.name/direction:DESC')), '15', '/a',
1764+
'/span',
1765+
);
1766+
$this->assertTags($result, $expected);
1767+
1768+
$result = $this->Paginator->last(2, array('ellipsis' => '<span class="ellipsis">...</span>'));
1769+
$expected = array(
1770+
array('span' => array('class' => 'ellipsis')), '...', '/span',
1771+
'<span',
1772+
array('a' => array('href' => '/index/page:14/sort:Client.name/direction:DESC')), '14', '/a',
1773+
'/span',
1774+
' | ',
1775+
'<span',
1776+
array('a' => array('href' => '/index/page:15/sort:Client.name/direction:DESC')), '15', '/a',
1777+
'/span',
1778+
);
1779+
$this->assertTags($result, $expected);
17201780
}
17211781

17221782
/**

0 commit comments

Comments
 (0)