diff --git a/cake/libs/view/helpers/paginator.php b/cake/libs/view/helpers/paginator.php index 80207909b..d99bb60a6 100644 --- a/cake/libs/view/helpers/paginator.php +++ b/cake/libs/view/helpers/paginator.php @@ -140,10 +140,14 @@ class PaginatorHelper extends AppHelper { $options = am($params['defaults'], $params['options']); } - if (isset($options['sort'])) { + if (isset($options['sort']) && !empty($options['sort'])) { return $options['sort']; } elseif (isset($options['order']) && is_array($options['order'])) { return preg_replace('/.*\./', '', 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]; + } } return null; } @@ -210,9 +214,6 @@ class PaginatorHelper extends AppHelper { * key the returned link will sort by 'desc'. */ function sort($title, $key = null, $options = array()) { - if(!empty($this->options)) { - $options = am($this->options, $options); - } $options = am(array('url' => array(), 'model' => null), $options); $url = $options['url']; unset($options['url']); diff --git a/cake/tests/cases/libs/view/helpers/paginator.test.php b/cake/tests/cases/libs/view/helpers/paginator.test.php index 97e28a561..c0eb941d4 100644 --- a/cake/tests/cases/libs/view/helpers/paginator.test.php +++ b/cake/tests/cases/libs/view/helpers/paginator.test.php @@ -49,18 +49,20 @@ class PaginatorTest extends UnitTestCase { 'nextPage' => true, 'pageCount' => 7, 'defaults' => array( - 'order' => 'Article.date DESC', + 'order' => 'Article.date ASC', 'limit' => 9, 'conditions' => array() - ) - ), - - 'options' => array( - 'order' => 'Article.date DESC', - 'limit' => 9, - 'page' => 1 + ), + 'options' => array( + 'order' => 'Article.date ASC', + 'limit' => 9, + 'page' => 1, + 'conditions' => array() + ) ) ); + $this->paginator->Html =& new HtmlHelper(); + $this->paginator->Ajax =& new AjaxHelper(); } function testHasPrevious() { @@ -77,6 +79,20 @@ class PaginatorTest extends UnitTestCase { $this->paginator->params['paging']['Article']['nextPage'] = true; } + function testSortLinks() { + Router::reload(); + Router::setRequestInfo(array( + array ('plugin' => null, 'controller' => 'accounts', 'action' => 'index', 'pass' => array(), 'form' => array(), 'url' => array('url' => 'accounts/', 'mod_rewrite' => 'true'), 'bare' => 0, 'webservices' => null), + array ('plugin' => null, 'controller' => null, 'action' => null, 'base' => '/officespace', 'here' => '/officespace/accounts/', 'webroot' => '/officespace/', 'passedArgs' => array(), 'argSeparator' => ':', 'namedArgs' => array(), 'webservices' => null) + )); + $this->paginator->options(array('url' => array('param'))); + $result = $this->paginator->sort('title'); + $this->assertPattern('/\/accounts\/index\/param\/page:1\/sort:title\/direction:asc"\s*>Title<\/a>$/', $result); + + $result = $this->paginator->sort('date'); + $this->assertPattern('/\/accounts\/index\/param\/page:1\/sort:date\/direction:desc"\s*>Date<\/a>$/', $result); + } + function tearDown() { unset($this->paginator); }