adding Paginator::first() and last() methods, also adding them to numbers, tests added, closes #3709, closes #3208, closes #3465, closes #2412, closes #2954

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6195 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
gwoo 2007-12-22 06:03:48 +00:00
parent 194ab8364a
commit 81b074cf1f
4 changed files with 217 additions and 26 deletions

View file

@ -182,6 +182,13 @@ div.paging div.disabled {
color: #ddd; color: #ddd;
display: inline; display: inline;
} }
div.paging span {
}
div.paging span.current {
color: #000;
}
div.paging span a {
}
/* Scaffold View */ /* Scaffold View */
dl { dl {
@ -312,7 +319,7 @@ p.error {
font-size: 120%; font-size: 120%;
line-height: 140%; line-height: 140%;
padding: 0.8em; padding: 0.8em;
margin: 1em 0; margin: 1em 0;
} }
p.error em { p.error em {
color: #000; color: #000;

View file

@ -183,6 +183,13 @@ div.paging div.disabled {
color: #ddd; color: #ddd;
display: inline; display: inline;
} }
div.paging span {
}
div.paging span.current {
color: #000;
}
div.paging span a {
}
/* Scaffold View */ /* Scaffold View */
dl { dl {

View file

@ -217,7 +217,7 @@ class PaginatorHelper extends AppHelper {
if (empty($key)) { if (empty($key)) {
$key = $title; $key = $title;
$title = Inflector::humanize(preg_replace('/_id$/', '', $title)); $title = __(Inflector::humanize(preg_replace('/_id$/', '', $title)), true);
} }
$dir = 'asc'; $dir = 'asc';
@ -423,19 +423,25 @@ class PaginatorHelper extends AppHelper {
} }
/** /**
* Returns a set of numbers for the paged result set * Returns a set of numbers for the paged result set
* uses a modulus to decide how many numbers to show on each side of the current page (defautl: 8) * uses a modulus to decide how many numbers to show on each side of the current page (default: 8)
* *
* @param mixed $options Options for the counter string. See #options for list of keys. * @param mixed $options Options for the numbers, (before, after, model, modulus, separator)
* @return string numbers string. * @return string numbers string.
*/ */
function numbers($options = array()) { function numbers($options = array()) {
if ($options === true) {
$options = array(
'before' => ' | ', 'after' => ' | ',
'first' => 'first', 'last' => 'last',
);
}
$options = array_merge( $options = array_merge(
array( array(
'before'=> null, 'before'=> null, 'after'=> null,
'after'=> null,
'model' => $this->defaultModel(), 'model' => $this->defaultModel(),
'modulus' => '8', 'modulus' => '8', 'separator' => ' | ',
'separator' => ' | ' 'first' => null, 'last' => null,
), ),
(array)$options); (array)$options);
@ -445,22 +451,22 @@ class PaginatorHelper extends AppHelper {
if ($params['pageCount'] <= 1) { if ($params['pageCount'] <= 1) {
return false; return false;
} }
$before = $options['before'];
unset($options['before']);
$after = $options['after'];
unset($options['after']);
$modulus = $options['modulus']; extract($options);
unset($options['modulus']); unset($options['before'], $options['after'], $options['model'], $options['modulus'], $options['separator'], $options['first'], $options['last']);
$separator = $options['separator']; $out = '';
unset($options['separator']);
$out = $before;
if ($modulus && $params['pageCount'] > $modulus) { if ($modulus && $params['pageCount'] > $modulus) {
$half = intval($modulus / 2); $half = intval($modulus / 2);
$end = $params['page'] + $half; $end = $params['page'] + $half;
if($first && $end > (int)$first) {
$out .= $this->first($first);
}
$out .= $before;
if ($end > $params['pageCount']) { if ($end > $params['pageCount']) {
$end = $params['pageCount']; $end = $params['pageCount'];
} }
@ -470,33 +476,128 @@ class PaginatorHelper extends AppHelper {
$end = $params['page'] + ($modulus - $params['page']) + 1; $end = $params['page'] + ($modulus - $params['page']) + 1;
} }
for ($i = $start; $i < $params['page']; $i++) { for ($i = $start; $i < $params['page']; $i++) {
$out .= $this->link($i, array('page' => $i), $options) . $separator; $out .= '<span>' . $this->link($i, array('page' => $i), $options) . '</span>' . $separator;
} }
$out .= $params['page'] . $separator; $out .= '<span class="current">' . $params['page'] . '</span>' . $separator;
$start = $params['page'] + 1; $start = $params['page'] + 1;
for ($i = $start; $i < $end; $i++) { for ($i = $start; $i < $end; $i++) {
$out .= $this->link($i, array('page' => $i), $options) . $separator; $out .= '<span>' .$this->link($i, array('page' => $i), $options) . '</span>'. $separator;
} }
if ($end != $params['page']) { if ($end != $params['page']) {
$out .= $this->link($i, array('page' => $end), $options); $out .= '<span>' .$this->link($i, array('page' => $end), $options) . '</span>';
} }
$out .= $after;
if($last && $end < $params['pageCount'] - (int)$last) {
$out .= $this->last($last);
}
} else { } else {
for ($i = 1; $i <= $params['pageCount']; $i++) { for ($i = 1; $i <= $params['pageCount']; $i++) {
if ($i == $params['page']) { if ($i == $params['page']) {
$out .= $i; $out .= '<span class="current">' . $i . '</span>';
} else { } else {
$out .= $this->link($i, array('page' => $i), $options); $out .= '<span>' .$this->link($i, array('page' => $i), $options) . '</span>';
} }
if($i != $params['pageCount']) { if($i != $params['pageCount']) {
$out .= $separator; $out .= $separator;
} }
} }
} }
$out .= $after;
return $this->output($out); return $this->output($out);
} }
/**
* Returns a first or set of numbers for the first pages
*
* @param mixed $first if string use as label for the link, if numeric print page numbers
* @param mixed $options
* @return string numbers string.
*/
function first($first = '<< first', $options = array()) {
$options = array_merge(
array(
'after'=> null,
'model' => $this->defaultModel(),
'separator' => ' | ',
),
(array)$options);
$params = array_merge(array('page'=> 1), (array)$this->params($options['model']));
unset($options['model']);
if ($params['pageCount'] <= 1) {
return false;
}
extract($options);
unset($options['after'], $options['model'], $options['separator']);
$out = '';
if (is_int($first) && $params['page'] > $first) {
if ($after === null) {
$after = '...';
}
for ($i = 1; $i <= $first; $i++) {
$out .= '<span>' . $this->link($i, array('page' => $i), $options) . '</span>';
if($i != $first) {
$out .= $separator;
}
}
$out .= $after;
} elseif ($params['page'] > 1) {
$out = '<span>' . $this->link($first, array('page' => 1), $options) . '</span>' . $after;
}
return $out;
}
/**
* Returns a last or set of numbers for the last pages
*
* @param mixed $last if string use as label for the link, if numeric print page numbers
* @param mixed $options
* @return string numbers string.
*/
function last($last = 'last >>', $options = array()) {
$options = array_merge(
array(
'before'=> null,
'model' => $this->defaultModel(),
'separator' => ' | ',
),
(array)$options);
$params = array_merge(array('page'=> 1), (array)$this->params($options['model']));
unset($options['model']);
if ($params['pageCount'] <= 1) {
return false;
}
extract($options);
unset($options['before'], $options['model'], $options['separator']);
$out = '';
$lower = $params['pageCount'] - $last + 1;
if (is_int($last) && $params['page'] < $lower) {
if ($before === null) {
$before = '...';
}
for ($i = $lower; $i <= $params['pageCount']; $i++) {
$out .= '<span>' . $this->link($i, array('page' => $i), $options) . '</span>';
if($i != $params['pageCount']) {
$out .= $separator;
}
}
$out = $before . $out;
} elseif ($params['page'] < $params['pageCount']) {
$out = $before . '<span>' . $this->link($last, array('page' => $params['pageCount']), $options) . '</span>';
}
return $out;
}
} }
?> ?>

View file

@ -171,9 +171,85 @@ class PaginatorTest extends UnitTestCase {
$this->assertPattern('/\/direction:desc/', $result); $this->assertPattern('/\/direction:desc/', $result);
} }
function testNumbers() {
$this->Paginator->params['paging'] = array('Client' => array(
'page' => 8, 'current' => 3, 'count' => 30, 'prevPage' => false, 'nextPage' => 2, 'pageCount' => 15,
'defaults' => array('limit' => 3, 'step' => 1, 'order' => array('Client.name' => 'DESC'), 'conditions' => array()),
'options' => array('page' => 1, 'limit' => 3, 'order' => array('Client.name' => 'DESC'), 'conditions' => array()))
);
$result = $this->Paginator->numbers();
$expected = '<span><a href="/index/page:4">4</a></span> | <span><a href="/index/page:5">5</a></span> | <span><a href="/index/page:6">6</a></span> | <span><a href="/index/page:7">7</a></span> | <span class="current">8</span> | <span><a href="/index/page:9">9</a></span> | <span><a href="/index/page:10">10</a></span> | <span><a href="/index/page:11">11</a></span> | <span><a href="/index/page:12">12</a></span>';
$this->assertEqual($result, $expected);
$result = $this->Paginator->numbers(true);
$expected = '<span><a href="/index/page:1">first</a></span> | <span><a href="/index/page:4">4</a></span> | <span><a href="/index/page:5">5</a></span> | <span><a href="/index/page:6">6</a></span> | <span><a href="/index/page:7">7</a></span> | <span class="current">8</span> | <span><a href="/index/page:9">9</a></span> | <span><a href="/index/page:10">10</a></span> | <span><a href="/index/page:11">11</a></span> | <span><a href="/index/page:12">12</a></span> | <span><a href="/index/page:15">last</a></span>';
$this->assertEqual($result, $expected);
$this->Paginator->params['paging'] = array('Client' => array(
'page' => 1, 'current' => 3, 'count' => 30, 'prevPage' => false, 'nextPage' => 2, 'pageCount' => 15,
'defaults' => array('limit' => 3, 'step' => 1, 'order' => array('Client.name' => 'DESC'), 'conditions' => array()),
'options' => array('page' => 1, 'limit' => 3, 'order' => array('Client.name' => 'DESC'), 'conditions' => array()))
);
$result = $this->Paginator->numbers();
$expected = '<span class="current">1</span> | <span><a href="/index/page:2">2</a></span> | <span><a href="/index/page:3">3</a></span> | <span><a href="/index/page:4">4</a></span> | <span><a href="/index/page:5">5</a></span> | <span><a href="/index/page:6">6</a></span> | <span><a href="/index/page:7">7</a></span> | <span><a href="/index/page:8">8</a></span> | <span><a href="/index/page:9">9</a></span>';
$this->assertEqual($result, $expected);
$this->Paginator->params['paging'] = array('Client' => array(
'page' => 14, 'current' => 3, 'count' => 30, 'prevPage' => false, 'nextPage' => 2, 'pageCount' => 15,
'defaults' => array('limit' => 3, 'step' => 1, 'order' => array('Client.name' => 'DESC'), 'conditions' => array()),
'options' => array('page' => 1, 'limit' => 3, 'order' => array('Client.name' => 'DESC'), 'conditions' => array()))
);
$result = $this->Paginator->numbers();
$expected = '<span><a href="/index/page:7">7</a></span> | <span><a href="/index/page:8">8</a></span> | <span><a href="/index/page:9">9</a></span> | <span><a href="/index/page:10">10</a></span> | <span><a href="/index/page:11">11</a></span> | <span><a href="/index/page:12">12</a></span> | <span><a href="/index/page:13">13</a></span> | <span class="current">14</span> | <span><a href="/index/page:15">15</a></span>';
$this->assertEqual($result, $expected);
$this->Paginator->params['paging'] = array('Client' => array(
'page' => 1, 'current' => 3, 'count' => 30, 'prevPage' => false, 'nextPage' => 2, 'pageCount' => 15,
'defaults' => array('limit' => 3, 'step' => 1, 'order' => array('Client.name' => 'DESC'), 'conditions' => array()),
'options' => array('page' => 1, 'limit' => 3, 'order' => array('Client.name' => 'DESC'), 'conditions' => array()))
);
$result = $this->Paginator->first();
$expected = '';
$this->assertEqual($result, $expected);
$this->Paginator->params['paging'] = array('Client' => array(
'page' => 4, 'current' => 3, 'count' => 30, 'prevPage' => false, 'nextPage' => 2, 'pageCount' => 15,
'defaults' => array('limit' => 3, 'step' => 1, 'order' => array('Client.name' => 'DESC'), 'conditions' => array()),
'options' => array('page' => 1, 'limit' => 3, 'order' => array('Client.name' => 'DESC'), 'conditions' => array()))
);
$result = $this->Paginator->first();
$expected = '<span><a href="/index/page:1">&lt;&lt; first</a></span>';
$this->assertEqual($result, $expected);
$result = $this->Paginator->last();
$expected = '<span><a href="/index/page:15">last &gt;&gt;</a></span>';
$this->assertEqual($result, $expected);
$result = $this->Paginator->last(1);
$expected = '...<span><a href="/index/page:15">15</a></span>';
$this->assertEqual($result, $expected);
$result = $this->Paginator->last(2);
$expected = '...<span><a href="/index/page:14">14</a></span> | <span><a href="/index/page:15">15</a></span>';
$this->assertEqual($result, $expected);
$this->Paginator->params['paging'] = array('Client' => array(
'page' => 15, 'current' => 3, 'count' => 30, 'prevPage' => false, 'nextPage' => 2, 'pageCount' => 15,
'defaults' => array('limit' => 3, 'step' => 1, 'order' => array('Client.name' => 'DESC'), 'conditions' => array()),
'options' => array('page' => 1, 'limit' => 3, 'order' => array('Client.name' => 'DESC'), 'conditions' => array()))
);
$result = $this->Paginator->last();
$expected = '';
$this->assertEqual($result, $expected);
}
function tearDown() { function tearDown() {
unset($this->Paginator); unset($this->Paginator);
} }
} }
?> ?>