Browse Source

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

euromark 12 years ago
parent
commit
def015108d

+ 47 - 0
lib/Cake/Test/Case/View/Helper/PaginatorHelperTest.php

@@ -234,6 +234,53 @@ class PaginatorHelperTest extends CakeTestCase {
 	}
 
 /**
+ * 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

+ 12 - 3
lib/Cake/View/Helper/PaginatorHelper.php

@@ -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];