Merge pull request #2132 from dereuromark/2.5-paginator-sort

Ability to use PaginatorHelper::sort() with only one direction.
This commit is contained in:
José Lorenzo Rodríguez 2013-10-24 05:40:42 -07:00
commit 45bd01fdc0
2 changed files with 59 additions and 3 deletions

View file

@ -233,6 +233,53 @@ class PaginatorHelperTest extends CakeTestCase {
$this->assertRegExp('/\/accounts\/index\/param\/sort:title\/direction:desc" class="foo asc">Title<\/a>$/', $result);
}
/**
* testSortLinksWithLockOption method
*
* @return void
*/
public function testSortLinksWithLockOption() {
Router::reload();
Router::parse('/');
Router::setRequestInfo(array(
array('plugin' => null, 'controller' => 'accounts', 'action' => 'index', 'pass' => array(), 'url' => array('url' => 'accounts/')),
array('base' => '/officespace', 'here' => '/officespace/accounts/', 'webroot' => '/officespace/')
));
$this->Paginator->options(array('url' => array('param')));
$this->Paginator->request['paging'] = array(
'Article' => array(
'current' => 9,
'count' => 62,
'prevPage' => false,
'nextPage' => true,
'pageCount' => 7,
'options' => array(
'page' => 1,
'order' => array('date' => 'asc'),
'conditions' => array()
),
'paramType' => 'named'
)
);
$result = $this->Paginator->sort('distance', null, array('lock' => true));
$expected = array(
'a' => array('href' => '/officespace/accounts/index/param/sort:distance/direction:asc'),
'Distance',
'/a'
);
$this->assertTags($result, $expected);
$this->Paginator->request->params['paging']['Article']['options']['sort'] = 'distance';
$result = $this->Paginator->sort('distance', null, array('lock' => true));
$expected = array(
'a' => array('href' => '/officespace/accounts/index/param/sort:distance/direction:asc', 'class' => 'asc locked'),
'Distance',
'/a'
);
$this->assertTags($result, $expected);
}
/**
* test that sort() works with virtual field order options.
*

View file

@ -315,9 +315,10 @@ class PaginatorHelper extends AppHelper {
*
* ### Options:
*
* - `escape` Whether you want the contents html entity encoded, defaults to true
* - `model` The model to use, defaults to PaginatorHelper::defaultModel()
* - `escape` Whether you want the contents html entity encoded, defaults to true.
* - `model` The model to use, defaults to PaginatorHelper::defaultModel().
* - `direction` The default direction to use when this link isn't active.
* - `lock` Lock direction. Will only use the default direction then, defaults to false.
*
* @param string $key The name of the key that the recordset should be sorted.
* @param string $title Title for the link. If $title is null $key will be used
@ -341,9 +342,12 @@ class PaginatorHelper extends AppHelper {
$title = __(Inflector::humanize(preg_replace('/_id$/', '', $title)));
}
$dir = isset($options['direction']) ? $options['direction'] : 'asc';
$defaultDir = isset($options['direction']) ? $options['direction'] : 'asc';
unset($options['direction']);
$locked = isset($options['lock']) ? $options['lock'] : false;
unset($options['lock']);
$sortKey = $this->sortKey($options['model']);
$defaultModel = $this->defaultModel();
$isSorted = (
@ -352,6 +356,7 @@ class PaginatorHelper extends AppHelper {
$key === $defaultModel . '.' . $sortKey
);
$dir = $defaultDir;
if ($isSorted) {
$dir = $this->sortDir($options['model']) === 'asc' ? 'desc' : 'asc';
$class = $dir === 'asc' ? 'desc' : 'asc';
@ -360,6 +365,10 @@ class PaginatorHelper extends AppHelper {
} else {
$options['class'] = $class;
}
if ($locked) {
$dir = $defaultDir;
$options['class'] .= ' locked';
}
}
if (is_array($title) && array_key_exists($dir, $title)) {
$title = $title[$dir];