Add a method for generating pagination meta links.

This commit is contained in:
euromark 2014-11-02 22:29:02 +01:00
parent fb90362121
commit e7f76a983a
2 changed files with 152 additions and 0 deletions

View file

@ -2763,4 +2763,110 @@ class PaginatorHelperTest extends CakeTestCase {
$expected = '0 of 1';
$this->assertEquals($expected, $result);
}
/**
* Verify that no next and prev links are created for single page results
*
* @return void
*/
public function testMetaPage0() {
$this->Paginator->request['paging'] = array(
'Article' => array(
'page' => 1,
'prevPage' => false,
'nextPage' => false,
'pageCount' => 1,
)
);
$expected = '';
$result = $this->Paginator->meta();
$this->assertSame($expected, $result);
}
/**
* Verify that page 1 only has a next link
*
* @return void
*/
public function testMetaPage1() {
$this->Paginator->request['paging'] = array(
'Article' => array(
'page' => 1,
'prevPage' => false,
'nextPage' => true,
'pageCount' => 2,
'options' => array(),
'paramType' => 'querystring'
)
);
$expected = '<link href="/?page=2" rel="next" />';
$result = $this->Paginator->meta();
$this->assertSame($expected, $result);
}
/**
* Verify that the method will append to a block
*
* @return void
*/
public function testMetaPage1InlineFalse() {
$this->Paginator->request['paging'] = array(
'Article' => array(
'page' => 1,
'prevPage' => false,
'nextPage' => true,
'pageCount' => 2,
'options' => array(),
'paramType' => 'querystring'
)
);
$expected = '<link href="/?page=2" rel="next" />';
$this->Paginator->meta(array('block' => true));
$result = $this->View->fetch('meta');
$this->assertSame($expected, $result);
}
/**
* Verify that the last page only has a prev link
*
* @return void
*/
public function testMetaPage1Last() {
$this->Paginator->request['paging'] = array(
'Article' => array(
'page' => 2,
'prevPage' => true,
'nextPage' => false,
'pageCount' => 2,
'options' => array(),
'paramType' => 'querystring'
)
);
$expected = '<link href="/" rel="prev" />';
$result = $this->Paginator->meta();
$this->assertSame($expected, $result);
}
/**
* Verify that a page in the middle has both links
*
* @return void
*/
public function testMetaPage10Last() {
$this->Paginator->request['paging'] = array(
'Article' => array(
'page' => 5,
'prevPage' => true,
'nextPage' => true,
'pageCount' => 10,
'options' => array(),
'paramType' => 'querystring'
)
);
$expected = '<link href="/?page=4" rel="prev" />';
$expected .= '<link href="/?page=6" rel="next" />';
$result = $this->Paginator->meta();
$this->assertSame($expected, $result);
}
}

View file

@ -958,4 +958,50 @@ class PaginatorHelper extends AppHelper {
return $out;
}
/**
* Returns the meta-links for a paginated result set
*
* `echo $this->Paginator->meta();`
*
* Echos the links directly, will output nothing of there is neither a previous nor next page.
*
* `$this->Paginator->meta(array('block' => true));`
*
* Will append the output of the meta function to the named block - if true is passed the "meta"
* block is used
*
* ### Options:
*
* - `model` The model to use defaults to PaginatorHelper::defaultModel()
* - `block` The block name to append the output to, or false/absenst to return as a string
*
* @param array $options Array of options
* @return string|null meta links
*/
public function meta($options = array()) {
$model = isset($options['model']) ? $options['model'] : null;
$params = $this->params($model);
$links = array();
if ($this->hasPrev()) {
$links[] = $this->Html->meta(array(
'rel' => 'prev',
'link' => $this->url(array('page' => $params['page'] - 1), true)
));
}
if ($this->hasNext()) {
$links[] = $this->Html->meta(array(
'rel' => 'next',
'link' => $this->url(array('page' => $params['page'] + 1), true)
));
}
$out = implode($links);
if (empty($options['block'])) {
return $out;
}
if ($options['block'] === true) {
$options['block'] = __FUNCTION__;
}
$this->_View->append($options['block'], $out);
}
}