Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Ability to use PaginatorHelper::sort() with only one direction.

  • Loading branch information...
commit def015108d7e2bf08de5af4fc058ed930be8ac71 1 parent 5a394c3
@dereuromark dereuromark authored
View
47 lib/Cake/Test/Case/View/Helper/PaginatorHelperTest.php
@@ -234,6 +234,53 @@ public function testSortLinks() {
}
/**
+ * 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.
*
* @return void
View
15 lib/Cake/View/Helper/PaginatorHelper.php
@@ -315,9 +315,10 @@ public function next($title = 'Next >>', $options = array(), $disabledTitle = nu
*
* ### 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 @@ public function sort($key, $title = null, $options = array()) {
$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 @@ public function sort($key, $title = null, $options = array()) {
$key === $defaultModel . '.' . $sortKey
);
+ $dir = $defaultDir;
if ($isSorted) {
$dir = $this->sortDir($options['model']) === 'asc' ? 'desc' : 'asc';
$class = $dir === 'asc' ? 'desc' : 'asc';
@@ -360,6 +365,10 @@ public function sort($key, $title = null, $options = array()) {
} else {
$options['class'] = $class;
}
+ if ($locked) {
+ $dir = $defaultDir;
+ $options['class'] .= ' locked';
+ }
}
if (is_array($title) && array_key_exists($dir, $title)) {
$title = $title[$dir];
Please sign in to comment.
Something went wrong with that request. Please try again.