Skip to content

Commit

Permalink
Add missing model parameters.
Browse files Browse the repository at this point in the history
These parameters will make it easier to implement multiple pagination in
the future as the URL generation will account for different models.
  • Loading branch information
markstory committed Oct 2, 2013
1 parent b49b3e1 commit 9505727
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 25 deletions.
94 changes: 94 additions & 0 deletions Cake/Test/TestCase/View/Helper/PaginatorHelperTest.php
Expand Up @@ -1400,6 +1400,39 @@ public function testNumbersRouting() {
$this->assertTags($result, $expected);
}

/**
* Test that numbers() works with the non default model.
*
* @return void
*/
public function testNumbersNonDefaultModel() {
$this->Paginator->request->params['paging'] = array(
'Client' => array(
'page' => 1,
'current' => 3,
'count' => 13,
'prevPage' => false,
'nextPage' => true,
'pageCount' => 5,
),
'Server' => array(
'page' => 5,
'current' => 1,
'count' => 5,
'prevPage' => true,
'nextPage' => false,
'pageCount' => 5,
)
);
$result = $this->Paginator->numbers(['model' => 'Server']);
$this->assertContains('<li class="active"><span>5</span></li>', $result);
$this->assertNotContains('<li class="active"><span>1</span></li>', $result);

$result = $this->Paginator->numbers(['model' => 'Client']);
$this->assertContains('<li class="active"><span>1</span></li>', $result);
$this->assertNotContains('<li class="active"><span>5</span></li>', $result);
}

/**
* test first() and last() with tag options
*
Expand Down Expand Up @@ -1443,6 +1476,37 @@ public function testLastNoOutput() {
$this->assertEquals($expected, $result);
}

/**
* test first() with a the model parameter.
*
* @return void
*/
public function testFirstNonDefaultModel() {
$this->Paginator->request->params['paging']['Article']['page'] = 1;
$this->Paginator->request->params['paging']['Client'] = array(
'page' => 3,
'current' => 3,
'count' => 13,
'prevPage' => false,
'nextPage' => true,
'pageCount' => 5,
);


$result = $this->Paginator->first('first', ['model' => 'Article:']);
$this->assertEquals('', $result);

$result = $this->Paginator->first('first', ['model' => 'Client']);
$expected = array(
'li' => array('class' => 'first'),
'a' => array('href' => '/index', 'rel' => 'first'),
'first',
'/a',
'/li'
);
$this->assertTags($result, $expected);
}

/**
* test first() on the first page.
*
Expand Down Expand Up @@ -1632,6 +1696,36 @@ public function testLastOptions() {
$this->assertTags($result, $expected);
}

/**
* test last() with a the model parameter.
*
* @return void
*/
public function testLastNonDefaultModel() {
$this->Paginator->request->params['paging']['Article']['page'] = 7;
$this->Paginator->request->params['paging']['Client'] = array(
'page' => 3,
'current' => 3,
'count' => 13,
'prevPage' => false,
'nextPage' => true,
'pageCount' => 5,
);

$result = $this->Paginator->last('last', ['model' => 'Article:']);
$this->assertEquals('', $result);

$result = $this->Paginator->last('last', ['model' => 'Client']);
$expected = array(
'li' => array('class' => 'last'),
'a' => array('href' => '/index?page=5', 'rel' => 'last'),
'last',
'/a',
'/li'
);
$this->assertTags($result, $expected);
}

/**
* testCounter method
*
Expand Down
40 changes: 15 additions & 25 deletions Cake/View/Helper/PaginatorHelper.php
Expand Up @@ -275,19 +275,19 @@ protected function _toggledLink($text, $enabled, $options, $templates) {
return '';
}
$text = $options['escape'] ? h($text) : $text;
$paging = $this->params($options['model']);

if (!$enabled) {
return $this->_templater->format($template, [
'text' => $text,
]);
}
$paging = $this->params($options['model']);

$url = array_merge(
$options['url'],
['page' => $paging['page'] + $options['step']]
);
$url = $this->url($url);
$url = $this->url($url, $options['model']);
return $this->_templater->format($template, [
'url' => $url,
'text' => $text,
Expand Down Expand Up @@ -426,7 +426,7 @@ public function sort($key, $title = null, $options = []) {
);
$vars = [
'text' => $options['escape'] ? h($title) : $title,
'url' => $this->url($url),
'url' => $this->url($url, $options['model']),
];
return $this->_templater->format($template, $vars);
}
Expand All @@ -435,7 +435,6 @@ public function sort($key, $title = null, $options = []) {
* Merges passed URL options with current pagination state to generate a pagination URL.
*
* @param array $options Pagination/URL options array
* @param boolean $asArray Return the url as an array, or a URI string
* @param string $model Which model to paginate on
* @return mixed By default, returns a full pagination URL string for use in non-standard contexts (i.e. JavaScript)
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::url
Expand Down Expand Up @@ -631,7 +630,6 @@ public function numbers($options = array()) {
$options += $defaults;

$params = (array)$this->params($options['model']) + array('page' => 1);
unset($options['model']);

if ($params['pageCount'] <= 1) {
return false;
Expand Down Expand Up @@ -666,29 +664,29 @@ public function numbers($options = array()) {
for ($i = $start; $i < $params['page']; $i++) {
$vars = [
'text' => $i,
'url' => $this->url(['page' => $i]),
'url' => $this->url(['page' => $i], $options['model']),
];
$out .= $this->_templater->format('number', $vars);
}

$out .= $this->_templater->format('current', [
'text' => $params['page'],
'url' => $this->url(['page' => $params['page']]),
'url' => $this->url(['page' => $params['page']], $options['model']),
]);

$start = $params['page'] + 1;
for ($i = $start; $i < $end; $i++) {
$vars = [
'text' => $i,
'url' => $this->url(['page' => $i]),
'url' => $this->url(['page' => $i], $options['model']),
];
$out .= $this->_templater->format('number', $vars);
}

if ($end != $params['page']) {
$vars = [
'text' => $i,
'url' => $this->url(['page' => $end]),
'url' => $this->url(['page' => $end], $options['model']),
];
$out .= $this->_templater->format('number', $vars);
}
Expand All @@ -710,12 +708,12 @@ public function numbers($options = array()) {
if ($i == $params['page']) {
$out .= $this->_templater->format('current', [
'text' => $params['page'],
'url' => $this->url(['page' => $params['page']]),
'url' => $this->url(['page' => $params['page']], $options['model']),
]);
} else {
$vars = [
'text' => $i,
'url' => $this->url(['page' => $i]),
'url' => $this->url(['page' => $i], $options['model']),
];
$out .= $this->_templater->format('number', $vars);
}
Expand Down Expand Up @@ -756,11 +754,7 @@ public function first($first = '<< first', $options = []) {
(array)$options
);

$params = array_merge(
['page' => 1],
(array)$this->params($options['model'])
);
unset($options['model']);
$params = $this->params($options['model']);

if ($params['pageCount'] <= 1) {
return false;
Expand All @@ -771,14 +765,14 @@ public function first($first = '<< first', $options = []) {
if (is_int($first) && $params['page'] >= $first) {
for ($i = 1; $i <= $first; $i++) {
$out .= $this->_templater->format('number', [
'url' => $this->url(['page' => $i]),
'url' => $this->url(['page' => $i], $options['model']),
'text' => $i
]);
}
} elseif ($params['page'] > 1 && is_string($first)) {
$first = $options['escape'] ? h($first) : $first;
$out .= $this->_templater->format('first', [
'url' => $this->url(['page' => 1]),
'url' => $this->url(['page' => 1], $options['model']),
'text' => $first
]);
}
Expand Down Expand Up @@ -812,11 +806,7 @@ public function last($last = 'last >>', $options = array()) {
(array)$options
);

$params = array_merge(
['page' => 1],
(array)$this->params($options['model'])
);
unset($options['model']);
$params = $this->params($options['model']);

if ($params['pageCount'] <= 1) {
return false;
Expand All @@ -828,14 +818,14 @@ public function last($last = 'last >>', $options = array()) {
if (is_int($last) && $params['page'] <= $lower) {
for ($i = $lower; $i <= $params['pageCount']; $i++) {
$out .= $this->_templater->format('number', [
'url' => $this->url(['page' => $i]),
'url' => $this->url(['page' => $i], $options['model']),
'text' => $i
]);
}
} elseif ($params['page'] < $params['pageCount'] && is_string($last)) {
$last = $options['escape'] ? h($last) : $last;
$out .= $this->_templater->format('last', [
'url' => $this->url(['page' => $params['pageCount']]),
'url' => $this->url(['page' => $params['pageCount']], $options['model']),
'text' => $last
]);
}
Expand Down

0 comments on commit 9505727

Please sign in to comment.