Browse Source

Merge pull request #7145 from cakephp/master-pagination-modulus

Allow modulus of 0 in PaginatorHelper::numbers()
Mark Story 10 years ago
parent
commit
a301fecbdf

+ 3 - 2
src/View/Helper/PaginatorHelper.php

@@ -603,7 +603,8 @@ class PaginatorHelper extends Helper
      * - `before` Content to be inserted before the numbers, but after the first links.
      * - `after` Content to be inserted after the numbers, but before the last links.
      * - `model` Model to create numbers for, defaults to PaginatorHelper::defaultModel()
-     * - `modulus` how many numbers to include on either side of the current page, defaults to 8.
+     * - `modulus` How many numbers to include on either side of the current page, defaults to 8.
+     *    Set to `false` to disable and to show all numbers.
      * - `first` Whether you want first links generated, set to an integer to define the number of 'first'
      *    links to generate.
      * - `last` Whether you want last links generated, set to an integer to define the number of 'last'
@@ -641,7 +642,7 @@ class PaginatorHelper extends Helper
             $templater->{$method}($options['templates']);
         }
 
-        if ($options['modulus'] && $params['pageCount'] > $options['modulus']) {
+        if ($options['modulus'] !== false && $params['pageCount'] > $options['modulus']) {
             $out = $this->_modulusNumbers($templater, $params, $options);
         } else {
             $out = $this->_numbers($templater, $params, $options);

+ 44 - 2
tests/TestCase/View/Helper/PaginatorHelperTest.php

@@ -1379,8 +1379,8 @@ class PaginatorHelperTest extends TestCase
                 'pageCount' => 3,
             ]
         ];
-        $options = ['modulus' => 10];
-        $result = $this->Paginator->numbers($options);
+
+        $result = $this->Paginator->numbers(['modulus' => 10]);
         $expected = [
             ['li' => ['class' => 'active']], '<a href=""', '1', '/a', '/li',
             ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
@@ -1535,6 +1535,48 @@ class PaginatorHelperTest extends TestCase
             ['li' => []], ['a' => ['href' => '/index?page=4897']], '4897', '/a', '/li',
         ];
         $this->assertHtml($expected, $result);
+
+        $this->Paginator->request->params['paging']['Client']['page'] = 3;
+        $result = $this->Paginator->numbers(['first' => 2, 'modulus' => 0, 'last' => 2]);
+        $expected = [
+            ['li' => []], ['a' => ['href' => '/index']], '1', '/a', '/li',
+            ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
+            ['li' => ['class' => 'active']], '<a href=""', '3', '/a', '/li',
+            ['li' => ['class' => 'ellipsis']], '...', '/li',
+            ['li' => []], ['a' => ['href' => '/index?page=4896']], '4896', '/a', '/li',
+            ['li' => []], ['a' => ['href' => '/index?page=4897']], '4897', '/a', '/li',
+        ];
+        $this->assertHtml($expected, $result);
+    }
+
+    /**
+     * Tests that disabling modulus displays all page links.
+     *
+     * @return void
+     */
+    public function testModulusDisabled()
+    {
+        $this->Paginator->request->params['paging'] = [
+            'Client' => [
+                'page' => 4,
+                'current' => 2,
+                'count' => 30,
+                'prevPage' => 1,
+                'nextPage' => 1,
+                'pageCount' => 6,
+            ]
+        ];
+
+        $result = $this->Paginator->numbers(['modulus' => false]);
+        $expected = [
+            ['li' => []], '<a href="/index"', '1', '/a', '/li',
+            ['li' => []], ['a' => ['href' => '/index?page=2']], '2', '/a', '/li',
+            ['li' => []], ['a' => ['href' => '/index?page=3']], '3', '/a', '/li',
+            ['li' => ['class' => 'active']], ['a' => ['href' => '']], '4', '/a', '/li',
+            ['li' => []], ['a' => ['href' => '/index?page=5']], '5', '/a', '/li',
+            ['li' => []], ['a' => ['href' => '/index?page=6']], '6', '/a', '/li',
+        ];
+        $this->assertHtml($expected, $result);
     }
 
     /**