Skip to content

Commit

Permalink
fixes #6189, paginator sort direction for associated fields of the sa…
Browse files Browse the repository at this point in the history
…me name

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@8092 3807eeeb-6ff5-0310-8944-8be069107fe0
  • Loading branch information
gwoo committed Mar 12, 2009
1 parent 149995c commit de6541f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 27 deletions.
24 changes: 7 additions & 17 deletions cake/libs/view/helpers/paginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ class PaginatorHelper extends AppHelper {
* Holds the default options for pagination links
*
* The values that may be specified are:
*
*
* - <i>$options['format']</i> Format of the counter. Supported formats are 'range' and 'pages'
* and custom (default). In the default mode the supplied string is parsed and constants are replaced
* and custom (default). In the default mode the supplied string is parsed and constants are replaced
* by their actual values.
* Constants: %page%, %pages%, %current%, %count%, %start%, %end% .
* - <i>$options['separator']</i> The separator of the actual page and number of pages (default: ' of ').
Expand Down Expand Up @@ -84,7 +84,7 @@ function params($model = null) {
/**
* Sets default options for all pagination links
*
* @param mixed $options Default options for pagination links. If a string is supplied - it
* @param mixed $options Default options for pagination links. If a string is supplied - it
* is used as the DOM id element to update. See #options for list of keys.
*/
function options($options = array()) {
Expand Down Expand Up @@ -144,7 +144,7 @@ function sortKey($model = null, $options = array()) {
}
return $options['sort'];
} elseif (isset($options['order']) && is_array($options['order'])) {
return preg_replace('/.*\./', '', key($options['order']));
return key($options['order']);
} elseif (isset($options['order']) && is_string($options['order'])) {
if (preg_match('/(?:\w+\.)?(\w+)/', $options['order'], $result) && isset($result[1])) {
return $result[1];
Expand Down Expand Up @@ -209,7 +209,7 @@ function next($title = 'Next >>', $options = array(), $disabledTitle = null, $di
* @param string $title Title for the link.
* @param string $key The name of the key that the recordset should be sorted.
* @param array $options Options for sorting link. See #options for list of keys.
* @return string A link sorting default by 'asc'. If the resultset is sorted 'asc' by the specified
* @return string A link sorting default by 'asc'. If the resultset is sorted 'asc' by the specified
* key the returned link will sort by 'desc'.
*/
function sort($title, $key = null, $options = array()) {
Expand All @@ -222,19 +222,15 @@ function sort($title, $key = null, $options = array()) {
$title = __(Inflector::humanize(preg_replace('/_id$/', '', $title)), true);
}
$dir = 'asc';
$model = null;

if (strpos($key, '.') !== false) {
list($model, $key) = explode('.', $key);
$model = $model . '.';
}
if ($this->sortKey($options['model']) == $key && $this->sortDir($options['model']) == 'asc') {
$dir = 'desc';
}
if (is_array($title) && array_key_exists($dir, $title)) {
$title = $title[$dir];
}
$url = array_merge(array('sort' => $model . $key, 'direction' => $dir), $url, array('order' => null));

$url = array_merge(array('sort' => $key, 'direction' => $dir), $url, array('order' => null));
return $this->link($title, $url, $options);
}
/**
Expand Down Expand Up @@ -280,12 +276,6 @@ function url($options = array(), $asArray = false, $model = null) {
$sort = $direction = null;
if (is_array($url['order'])) {
list($sort, $direction) = array($this->sortKey($model, $url), current($url['order']));
$key = array_keys($url['order']);

if (strpos($key[0], '.') !== false) {
list($model) = explode('.', $key[0]);
$sort = $model . '.' . $sort;
}
}
unset($url['order']);
$url = array_merge($url, compact('sort', 'direction'));
Expand Down
39 changes: 29 additions & 10 deletions cake/tests/cases/libs/view/helpers/paginator.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,25 @@ function testSortLinksUsingDotNotation() {
$result = $this->Paginator->sort('Title','Article.title');
$this->assertPattern('/\/accounts\/index\/page:1\/sort:Article.title\/direction:desc">Title<\/a>$/', $result);

$this->Paginator->params['paging']['Article']['options']['order'] = array('Article.title' => 'asc');
$result = $this->Paginator->sort('title');
$this->assertPattern('/\/accounts\/index\/page:1\/sort:title\/direction:asc">Title<\/a>$/', $result);

}
/**
* testSortKey method
*
* @access public
* @return void
*/
function testSortKey() {

$result = $this->Paginator->sortKey(null, array(
'order' => array('Article.title' => 'desc'
)));
$this->assertEqual('Article.title', $result);


}
/**
* testSortAdminLinks method
Expand Down Expand Up @@ -349,29 +368,29 @@ function testPagingLinks() {
$result = $this->Paginator->prev('<< Previous', null, null, array('class' => 'disabled'));
$this->assertPattern('/^<a[^<>]+>&lt;&lt; Previous<\/a>$/', $result);
$this->assertPattern('/href="\/index\/page:1"/', $result);

$result = $this->Paginator->next('Next');
$this->assertPattern('/^<a[^<>]+>Next<\/a>$/', $result);
$this->assertPattern('/href="\/index\/page:3"/', $result);

$result = $this->Paginator->prev('<< Previous', array('escape' => true));
$this->assertPattern('/^<a[^<>]+>&lt;&lt; Previous<\/a>$/', $result);

$result = $this->Paginator->prev('<< Previous', array('escape' => false));
$this->assertPattern('/^<a[^<>]+><< Previous<\/a>$/', $result);

$this->Paginator->params['paging'] = array('Client' => array(
'page' => 1, 'current' => 1, 'count' => 13, 'prevPage' => false, 'nextPage' => true, 'pageCount' => 5,
'defaults' => array(),
'options' => array('page' => 1, 'limit' => 3, 'order' => array('Client.name' => 'DESC'), 'conditions' => array()))
);

$result = $this->Paginator->prev('<< Previous', null, '<strong>Disabled</strong>');
$this->assertPattern('/^<div>&lt;strong&gt;Disabled&lt;\/strong&gt;<\/div>$/', $result);

$result = $this->Paginator->prev('<< Previous', null, '<strong>Disabled</strong>', array('escape' => true));
$this->assertPattern('/^<div>&lt;strong&gt;Disabled&lt;\/strong&gt;<\/div>$/', $result);

$result = $this->Paginator->prev('<< Previous', null, '<strong>Disabled</strong>', array('escape' => false));
$this->assertPattern('/^<div><strong>Disabled<\/strong><\/div>$/', $result);

Expand Down Expand Up @@ -404,7 +423,7 @@ function testPagingLinks() {
}
/**
* testPagingLinksNotDefaultModel
*
*
* Test the creation of paging links when the non default model is used.
*
* @access public
Expand All @@ -427,7 +446,7 @@ function testPagingLinksNotDefaultModel() {
$result = $this->Paginator->next('Next', array('model' => 'Client'));
$this->assertPattern('/^<a[^<>]+>Next<\/a>$/', $result);
$this->assertPattern('/href="\/index\/page:2"/', $result); // These is passed.

$result = $this->Paginator->next('Next', array('model' => 'Server'), 'No Next', array('model' => 'Server'));
$this->assertPattern('/^<div>No Next<\/div>$/', $result);
}
Expand Down Expand Up @@ -792,7 +811,7 @@ function testWithPlugin() {
Router::setRequestInfo(array(
array(
'pass' => array(), 'named' => array(), 'prefix' => null, 'form' => array(),
'controller' => 'magazines', 'plugin' => 'my_plugin', 'action' => 'index',
'controller' => 'magazines', 'plugin' => 'my_plugin', 'action' => 'index',
'url' => array('ext' => 'html', 'url' => 'my_plugin/magazines')),
array('base' => '', 'here' => '/my_plugin/magazines', 'webroot' => '/')
));
Expand Down

0 comments on commit de6541f

Please sign in to comment.