Skip to content

Commit

Permalink
untangle url methods
Browse files Browse the repository at this point in the history
  • Loading branch information
euromark committed Apr 2, 2014
1 parent c72db9e commit 34a0757
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 21 deletions.
29 changes: 15 additions & 14 deletions src/View/Helper/PaginatorHelper.php
Expand Up @@ -239,7 +239,7 @@ protected function _toggledLink($text, $enabled, $options, $templates) {
$options['url'],
['page' => $paging['page'] + $options['step']]
);
$url = $this->url($url, $options['model']);
$url = $this->generateUrl($url, $options['model']);
return $this->templater()->format($template, [
'url' => $url,
'text' => $text,
Expand Down Expand Up @@ -387,7 +387,7 @@ public function sort($key, $title = null, $options = []) {
);
$vars = [
'text' => $options['escape'] ? h($title) : $title,
'url' => $this->url($url, $options['model']),
'url' => $this->generateUrl($url, $options['model']),
];
return $this->templater()->format($template, $vars);
}
Expand All @@ -397,10 +397,11 @@ public function sort($key, $title = null, $options = []) {
*
* @param array $options Pagination/URL options array
* @param string $model Which model to paginate on
* @param boolean $full If true, the full base URL will be prepended to the result
* @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
*/
public function url($options = array(), $model = null) {
public function generateUrl($options = array(), $model = null, $full = false) {
$paging = $this->params($model);
$paging += ['page' => null, 'sort' => null, 'direction' => null, 'limit' => null];
$url = [
Expand All @@ -425,7 +426,7 @@ public function url($options = array(), $model = null) {
) {
$url['sort'] = $url['direction'] = null;
}
return parent::url($url);
return $this->url($url, $full);
}

/**
Expand Down Expand Up @@ -632,29 +633,29 @@ public function numbers($options = array()) {
for ($i = $start; $i < $params['page']; $i++) {
$vars = [
'text' => $i,
'url' => $this->url(['page' => $i], $options['model']),
'url' => $this->generateUrl(['page' => $i], $options['model']),
];
$out .= $this->templater()->format('number', $vars);
}

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

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

if ($end != $params['page']) {
$vars = [
'text' => $i,
'url' => $this->url(['page' => $end], $options['model']),
'url' => $this->generateUrl(['page' => $end], $options['model']),
];
$out .= $this->templater()->format('number', $vars);
}
Expand All @@ -676,12 +677,12 @@ public function numbers($options = array()) {
if ($i == $params['page']) {
$out .= $this->templater()->format('current', [
'text' => $params['page'],
'url' => $this->url(['page' => $params['page']], $options['model']),
'url' => $this->generateUrl(['page' => $params['page']], $options['model']),
]);
} else {
$vars = [
'text' => $i,
'url' => $this->url(['page' => $i], $options['model']),
'url' => $this->generateUrl(['page' => $i], $options['model']),
];
$out .= $this->templater()->format('number', $vars);
}
Expand Down Expand Up @@ -733,14 +734,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], $options['model']),
'url' => $this->generateUrl(['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], $options['model']),
'url' => $this->generateUrl(['page' => 1], $options['model']),
'text' => $first
]);
}
Expand Down Expand Up @@ -786,14 +787,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], $options['model']),
'url' => $this->generateUrl(['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']], $options['model']),
'url' => $this->generateUrl(['page' => $params['pageCount']], $options['model']),
'text' => $last
]);
}
Expand Down
14 changes: 7 additions & 7 deletions tests/TestCase/View/Helper/PaginatorHelperTest.php
Expand Up @@ -591,20 +591,20 @@ public function testUrlGeneration() {
);
$this->assertTags($result, $expected);

$result = $this->Paginator->url();
$result = $this->Paginator->generateUrl();
$this->assertEquals('/index', $result);

$this->Paginator->request->params['paging']['Article']['page'] = 2;
$result = $this->Paginator->url();
$result = $this->Paginator->generateUrl();
$this->assertEquals('/index?page=2', $result);

$options = array('sort' => 'Article', 'direction' => 'desc');
$result = $this->Paginator->url($options);
$result = $this->Paginator->generateUrl($options);
$this->assertEquals('/index?page=2&amp;sort=Article&amp;direction=desc', $result);

$this->Paginator->request->params['paging']['Article']['page'] = 3;
$options = array('sort' => 'Article.name', 'direction' => 'desc');
$result = $this->Paginator->url($options);
$result = $this->Paginator->generateUrl($options);
$this->assertEquals('/index?page=3&amp;sort=Article.name&amp;direction=desc', $result);
}

Expand All @@ -628,7 +628,7 @@ public function testUrlGenerationWithPrefixes() {
$this->Paginator->request->params['paging']['Article']['prevPage'] = true;
$options = array('prefix' => 'members');

$result = $this->Paginator->url($options);
$result = $this->Paginator->generateUrl($options);
$expected = '/members/posts/index?page=2';
$this->assertEquals($expected, $result);

Expand Down Expand Up @@ -661,12 +661,12 @@ public function testUrlGenerationWithPrefixes() {
$this->assertTags($result, $expected);

$options = array('prefix' => 'members', 'controller' => 'posts', 'sort' => 'name', 'direction' => 'desc');
$result = $this->Paginator->url($options);
$result = $this->Paginator->generateUrl($options);
$expected = '/members/posts/index?page=2&amp;sort=name&amp;direction=desc';
$this->assertEquals($expected, $result);

$options = array('controller' => 'posts', 'sort' => 'Article.name', 'direction' => 'desc');
$result = $this->Paginator->url($options);
$result = $this->Paginator->generateUrl($options);
$expected = '/posts/index?page=2&amp;sort=Article.name&amp;direction=desc';
$this->assertEquals($expected, $result);
}
Expand Down

0 comments on commit 34a0757

Please sign in to comment.