Backport paginator changes for string integers.

Backport the intent of #7845 into 2.x. The implementation differs a bit
from 3.x but paginator helper internals are pretty different in both
branches.

Refs #7092
This commit is contained in:
mark_story 2015-12-26 22:52:17 -05:00
parent 430612f1df
commit 72b98f58a8
2 changed files with 66 additions and 9 deletions

View file

@ -2210,7 +2210,54 @@ class PaginatorHelperTest extends CakeTestCase {
array('span' => array()), array('a' => array('href' => '/index/page:4897')), '4897', '/a', '/span',
);
$this->assertTags($result, $expected);
}
/**
* Test first/last options as strings.
*
* @return void
*/
public function testNumbersStringFirstAndLast() {
$this->Paginator->request->params['paging'] = array(
'Client' => array(
'page' => 10,
'current' => 3,
'count' => 30,
'prevPage' => false,
'nextPage' => 2,
'pageCount' => 15,
'options' => array(
'page' => 1,
),
'paramType' => 'named'
)
);
$result = $this->Paginator->numbers(array('first' => '1', 'last' => '1'));
$expected = array(
array('span' => array()), array('a' => array('href' => '/')), '1', '/a', '/span',
'...',
array('span' => array()), array('a' => array('href' => '/index/page:6')), '6', '/a', '/span',
' | ',
array('span' => array()), array('a' => array('href' => '/index/page:7')), '7', '/a', '/span',
' | ',
array('span' => array()), array('a' => array('href' => '/index/page:8')), '8', '/a', '/span',
' | ',
array('span' => array()), array('a' => array('href' => '/index/page:9')), '9', '/a', '/span',
' | ',
array('span' => array('class' => 'current')), '10', '/span',
' | ',
array('span' => array()), array('a' => array('href' => '/index/page:11')), '11', '/a', '/span',
' | ',
array('span' => array()), array('a' => array('href' => '/index/page:12')), '12', '/a', '/span',
' | ',
array('span' => array()), array('a' => array('href' => '/index/page:13')), '13', '/a', '/span',
' | ',
array('span' => array()), array('a' => array('href' => '/index/page:14')), '14', '/a', '/span',
' | ',
array('span' => array()), array('a' => array('href' => '/index/page:15')), '15', '/a', '/span',
);
$this->assertTags($result, $expected);
}
/**
@ -2241,6 +2288,9 @@ class PaginatorHelperTest extends CakeTestCase {
'/li',
);
$this->assertTags($result, $expected);
$result = $this->Paginator->last('2', array('tag' => 'li', 'class' => 'last'));
$this->assertTags($result, $expected);
}
/**
@ -2392,6 +2442,10 @@ class PaginatorHelperTest extends CakeTestCase {
);
$this->assertTags($result, $expected);
// Test stringy number.
$result = $this->Paginator->last('2');
$this->assertTags($result, $expected);
$result = $this->Paginator->last(3);
$this->assertEquals('', $result, 'When inside the last links range, no links should be made');
}

View file

@ -705,9 +705,11 @@ class PaginatorHelper extends AppHelper {
* - `separator` Separator content defaults to ' | '
* - `tag` The tag to wrap links in, defaults to 'span'
* - `first` Whether you want first links generated, set to an integer to define the number of 'first'
* links to generate.
* links to generate. If a string is set a link to the first page will be generated with the value
* as the title.
* - `last` Whether you want last links generated, set to an integer to define the number of 'last'
* links to generate.
* links to generate. If a string is set a link to the last page will be generated with the value
* as the title.
* - `ellipsis` Ellipsis content, defaults to '...'
* - `class` Class for wrapper tag
* - `currentClass` Class for wrapper tag on current active page, defaults to 'current'
@ -743,7 +745,6 @@ class PaginatorHelper extends AppHelper {
$options['modulus'], $options['separator'], $options['first'], $options['last'],
$options['ellipsis'], $options['class'], $options['currentClass'], $options['currentTag']
);
$out = '';
if ($modulus && $params['pageCount'] > $modulus) {
@ -759,9 +760,10 @@ class PaginatorHelper extends AppHelper {
$end = $params['page'] + ($modulus - $params['page']) + 1;
}
$firstPage = is_int($first) ? $first : 0;
if ($first && $start > 1) {
$offset = ($start <= (int)$first) ? $start - 1 : $first;
if ($offset < $start - 1) {
$offset = ($start <= $firstPage) ? $start - 1 : $first;
if ($firstPage < $start - 1) {
$out .= $this->first($offset, compact('tag', 'separator', 'ellipsis', 'class'));
} else {
$out .= $this->first($offset, compact('tag', 'separator', 'class', 'ellipsis') + array('after' => $separator));
@ -798,8 +800,9 @@ class PaginatorHelper extends AppHelper {
$out .= $after;
if ($last && $end < $params['pageCount']) {
$offset = ($params['pageCount'] < $end + (int)$last) ? $params['pageCount'] - $end : $last;
if ($offset <= $last && $params['pageCount'] - $end > $offset) {
$lastPage = is_int($last) ? $last : 0;
$offset = ($params['pageCount'] < $end + $lastPage) ? $params['pageCount'] - $end : $last;
if ($offset <= $lastPage && $params['pageCount'] - $end > $lastPage) {
$out .= $this->last($offset, compact('tag', 'separator', 'ellipsis', 'class'));
} else {
$out .= $this->last($offset, compact('tag', 'separator', 'class', 'ellipsis') + array('before' => $separator));
@ -880,7 +883,7 @@ class PaginatorHelper extends AppHelper {
$out = '';
if (is_int($first) && $params['page'] >= $first) {
if ((is_int($first) || ctype_digit($first)) && $params['page'] >= $first) {
if ($after === null) {
$after = $ellipsis;
}
@ -945,7 +948,7 @@ class PaginatorHelper extends AppHelper {
$out = '';
$lower = $params['pageCount'] - $last + 1;
if (is_int($last) && $params['page'] <= $lower) {
if ((is_int($last) || ctype_digit($last)) && $params['page'] <= $lower) {
if ($before === null) {
$before = $ellipsis;
}