Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Ability to use PaginatorHelper::sort() with only one direction.
  • Loading branch information
euromark committed Oct 19, 2013
1 parent 5a394c3 commit def0151
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
47 changes: 47 additions & 0 deletions lib/Cake/Test/Case/View/Helper/PaginatorHelperTest.php
Expand Up @@ -233,6 +233,53 @@ public function testSortLinks() {
$this->assertRegExp('/\/accounts\/index\/param\/sort:title\/direction:desc" class="foo asc">Title<\/a>$/', $result); $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. * test that sort() works with virtual field order options.
* *
Expand Down
15 changes: 12 additions & 3 deletions lib/Cake/View/Helper/PaginatorHelper.php
Expand Up @@ -315,9 +315,10 @@ public function next($title = 'Next >>', $options = array(), $disabledTitle = nu
* *
* ### Options: * ### Options:
* *
* - `escape` Whether you want the contents html entity encoded, defaults to true * - `escape` Whether you want the contents html entity encoded, defaults to true.
* - `model` The model to use, defaults to PaginatorHelper::defaultModel() * - `model` The model to use, defaults to PaginatorHelper::defaultModel().
* - `direction` The default direction to use when this link isn't active. * - `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 $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 * @param string $title Title for the link. If $title is null $key will be used
Expand All @@ -341,9 +342,12 @@ public function sort($key, $title = null, $options = array()) {


$title = __(Inflector::humanize(preg_replace('/_id$/', '', $title))); $title = __(Inflector::humanize(preg_replace('/_id$/', '', $title)));
} }
$dir = isset($options['direction']) ? $options['direction'] : 'asc'; $defaultDir = isset($options['direction']) ? $options['direction'] : 'asc';
unset($options['direction']); unset($options['direction']);


$locked = isset($options['lock']) ? $options['lock'] : false;
unset($options['lock']);

$sortKey = $this->sortKey($options['model']); $sortKey = $this->sortKey($options['model']);
$defaultModel = $this->defaultModel(); $defaultModel = $this->defaultModel();
$isSorted = ( $isSorted = (
Expand All @@ -352,6 +356,7 @@ public function sort($key, $title = null, $options = array()) {
$key === $defaultModel . '.' . $sortKey $key === $defaultModel . '.' . $sortKey
); );


$dir = $defaultDir;
if ($isSorted) { if ($isSorted) {
$dir = $this->sortDir($options['model']) === 'asc' ? 'desc' : 'asc'; $dir = $this->sortDir($options['model']) === 'asc' ? 'desc' : 'asc';
$class = $dir === 'asc' ? 'desc' : 'asc'; $class = $dir === 'asc' ? 'desc' : 'asc';
Expand All @@ -360,6 +365,10 @@ public function sort($key, $title = null, $options = array()) {
} else { } else {
$options['class'] = $class; $options['class'] = $class;
} }
if ($locked) {
$dir = $defaultDir;
$options['class'] .= ' locked';
}
} }
if (is_array($title) && array_key_exists($dir, $title)) { if (is_array($title) && array_key_exists($dir, $title)) {
$title = $title[$dir]; $title = $title[$dir];
Expand Down

0 comments on commit def0151

Please sign in to comment.