Skip to content

Commit

Permalink
Move first() to use StringTemplate.
Browse files Browse the repository at this point in the history
Add templates for first() and last(). Migrate first() over.
  • Loading branch information
markstory committed Sep 28, 2013
1 parent 8819b85 commit a578ec5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 35 deletions.
16 changes: 8 additions & 8 deletions Cake/Test/TestCase/View/Helper/PaginatorHelperTest.php
Expand Up @@ -1810,13 +1810,13 @@ public function testFirstFullBaseUrl() {

$result = $this->Paginator->first();
$expected = array(
'<span',
'li' => ['class' => 'first'],
array('a' => array(
'href' => Configure::read('App.fullBaseUrl') . '/index?sort=Article.title&amp;direction=DESC', 'rel' => 'first'
)),
'&lt;&lt; first',
'/a',
'/span',
'/li',
);
$this->assertTags($result, $expected);
}
Expand All @@ -1830,23 +1830,23 @@ public function testFirstBoundaries() {
$this->Paginator->request->params['paging']['Article']['page'] = 3;
$result = $this->Paginator->first();
$expected = array(
'<span',
'li' => ['class' => 'first'],
'a' => array('href' => '/index', 'rel' => 'first'),
'&lt;&lt; first',
'/a',
'/span'
'/li'
);
$this->assertTags($result, $expected);

$result = $this->Paginator->first(2);
$expected = array(
'<span',
'<li',
array('a' => array('href' => '/index')), '1', '/a',
'/span',
'/li',
' | ',
'<span',
'<li',
array('a' => array('href' => '/index?page=2')), '2', '/a',
'/span'
'/li'
);
$this->assertTags($result, $expected);

Expand Down
56 changes: 29 additions & 27 deletions Cake/View/Helper/PaginatorHelper.php
Expand Up @@ -78,6 +78,11 @@ class PaginatorHelper extends Helper {
'prevDisabled' => '<li class="prev disabled"><span>{{text}}</span></li>',
'counterRange' => '{{start}} - {{end}} of {{count}}',
'counterPages' => '{{page}} of {{pages}}',
'first' => '<li class="first"><a rel="first" href="{{url}}">{{text}}</a></li>',
'last' => '<li class="last"><a rel="last" href="{{url}}">{{text}}</a></li>',
'number' => '<li><a href="{{url}}">{{text}}</a></li>',
'ellipsis' => '...',
'separator' => ' | ',
];

/**
Expand Down Expand Up @@ -276,9 +281,6 @@ protected function _toggledLink($text, $enabled, $options, $templates) {
]);
}

if (!empty($this->options['url'])) {
$options['url'] = array_merge($this->options['url'], $options['url']);
}
$url = array_merge(
$options['url'],
['page' => $paging['page'] + $options['step']]
Expand Down Expand Up @@ -469,6 +471,10 @@ public function url($options = array(), $asArray = false, $model = null) {
'sort' => $paging['sort'],
'direction' => $paging['direction'],
];

if (!empty($this->options['url'])) {
$url = array_merge($this->options['url'], $url);
}
$url = array_merge(array_filter($url), $options);

if (!empty($url['page']) && $url['page'] == 1) {
Expand Down Expand Up @@ -768,55 +774,51 @@ public function numbers($options = array()) {
*
* ### Options:
*
* - `tag` The tag wrapping tag you want to use, defaults to 'span'
* - `after` Content to insert after 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 '...'
* - `escape` Whether or not to HTML escape the text.
*
* @param string|integer $first if string use as label for the link. If numeric, the number of page links
* you want at the beginning of the range.
* @param array $options An array of options.
* @return string numbers string.
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::first
*/
public function first($first = '<< first', $options = array()) {
public function first($first = '<< first', $options = []) {
$options = array_merge(
array(
'tag' => 'span',
'after' => null,
'model' => $this->defaultModel(),
'separator' => ' | ',
'ellipsis' => '...',
'class' => null
),
(array)$options);
['model' => $this->defaultModel(), 'escape' => true],
(array)$options
);

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

if ($params['pageCount'] <= 1) {
return false;
}
extract($options);
unset($options['tag'], $options['after'], $options['model'], $options['separator'], $options['ellipsis'], $options['class']);

$out = '';

if (is_int($first) && $params['page'] >= $first) {
if ($after === null) {
$after = $ellipsis;
}
$ellipsis = $this->_templater->format('ellipsis', []);
$separator = $this->_templater->format('separator', []);
for ($i = 1; $i <= $first; $i++) {
$out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class'));
$out .= $this->_templater->format('number', [
'url' => $this->url(['page' => $i]),
'text' => $i
]);
if ($i != $first) {
$out .= $separator;
}
}
$out .= $after;
} elseif ($params['page'] > 1 && is_string($first)) {
$options += array('rel' => 'first');
$out = $this->Html->tag($tag, $this->link($first, array('page' => 1), $options), compact('class')) . $after;
$first = $options['escape'] ? h($first) : $first;
$out .= $this->_templater->format('first', [
'url' => $this->url(['page' => 1]),
'text' => $first
]);
}
return $out;
}
Expand Down

0 comments on commit a578ec5

Please sign in to comment.