Browse Source

Merge pull request #7845 from Xety/fix-paginator-helper

Fixed helper `Paginator::numbers()` to get it working with a string number
Mark Story 10 years ago
parent
commit
2464c71f65

+ 10 - 6
src/View/Helper/PaginatorHelper.php

@@ -640,9 +640,11 @@ class PaginatorHelper extends Helper
      * - `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.
+     *    links to generate. If a string is set a link to the first page will be generated with the value
+     *    as the title.
      * - `last` Whether you want last links generated, set to an integer to define the number of 'last'
-     *    links to generate.
+     *    links to generate. If a string is set a link to the last page will be generated with the value
+     *    as the title.
      * - `templates` An array of templates, or template file name containing the templates you'd like to
      *    use when generating the numbers. The helper's original templates will be restored once
      *    numbers() is done.
@@ -798,10 +800,11 @@ class PaginatorHelper extends Helper
     protected function _firstNumber($ellipsis, $params, $start, $options)
     {
         $out = '';
+        $first = is_int($options['first']) ? $options['first'] : 0;
         if ($options['first'] && $start > 1) {
-            $offset = ($start <= (int)$options['first']) ? $start - 1 : $options['first'];
+            $offset = ($start <= $first) ? $start - 1 : $options['first'];
             $out .= $this->first($offset, $options);
-            if ($offset < $start - 1) {
+            if ($first < $start - 1) {
                 $out .= $ellipsis;
             }
         }
@@ -820,9 +823,10 @@ class PaginatorHelper extends Helper
     protected function _lastNumber($ellipsis, $params, $end, $options)
     {
         $out = '';
+        $last = is_int($options['last']) ? $options['last'] : 0;
         if ($options['last'] && $end < $params['pageCount']) {
-            $offset = ($params['pageCount'] < $end + (int)$options['last']) ? $params['pageCount'] - $end : $options['last'];
-            if ($offset <= $options['last'] && $params['pageCount'] - $end > $offset) {
+            $offset = ($params['pageCount'] < $end + $last) ? $params['pageCount'] - $end : $options['last'];
+            if ($offset <= $options['last'] && $params['pageCount'] - $end > $last) {
                 $out .= $ellipsis;
             }
             $out .= $this->last($offset, $options);

+ 56 - 0
tests/TestCase/View/Helper/PaginatorHelperTest.php

@@ -1147,6 +1147,42 @@ class PaginatorHelperTest extends TestCase
         ];
         $this->assertHtml($expected, $result);
 
+        $result = $this->Paginator->numbers(['first' => '2', 'last' => '8']);
+        $expected = [
+            ['li' => ['class' => 'first']], ['a' => ['href' => '/index']], '2', '/a', '/li',
+            ['li' => ['class' => 'ellipsis']], '...', '/li',
+            ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
+            ['li' => []], ['a' => ['href' => '/index?page=5']], '5', '/a', '/li',
+            ['li' => []], ['a' => ['href' => '/index?page=6']], '6', '/a', '/li',
+            ['li' => []], ['a' => ['href' => '/index?page=7']], '7', '/a', '/li',
+            ['li' => ['class' => 'active']], '<a href=""', '8', '/a', '/li',
+            ['li' => []], ['a' => ['href' => '/index?page=9']], '9', '/a', '/li',
+            ['li' => []], ['a' => ['href' => '/index?page=10']], '10', '/a', '/li',
+            ['li' => []], ['a' => ['href' => '/index?page=11']], '11', '/a', '/li',
+            ['li' => []], ['a' => ['href' => '/index?page=12']], '12', '/a', '/li',
+            ['li' => ['class' => 'ellipsis']], '...', '/li',
+            ['li' => ['class' => 'last']], ['a' => ['href' => '/index?page=15']], '8', '/a', '/li',
+        ];
+        $this->assertHtml($expected, $result);
+
+        $result = $this->Paginator->numbers(['first' => '8', 'last' => '8']);
+        $expected = [
+            ['li' => ['class' => 'first']], ['a' => ['href' => '/index']], '8', '/a', '/li',
+            ['li' => ['class' => 'ellipsis']], '...', '/li',
+            ['li' => []], ['a' => ['href' => '/index?page=4']], '4', '/a', '/li',
+            ['li' => []], ['a' => ['href' => '/index?page=5']], '5', '/a', '/li',
+            ['li' => []], ['a' => ['href' => '/index?page=6']], '6', '/a', '/li',
+            ['li' => []], ['a' => ['href' => '/index?page=7']], '7', '/a', '/li',
+            ['li' => ['class' => 'active']], '<a href=""', '8', '/a', '/li',
+            ['li' => []], ['a' => ['href' => '/index?page=9']], '9', '/a', '/li',
+            ['li' => []], ['a' => ['href' => '/index?page=10']], '10', '/a', '/li',
+            ['li' => []], ['a' => ['href' => '/index?page=11']], '11', '/a', '/li',
+            ['li' => []], ['a' => ['href' => '/index?page=12']], '12', '/a', '/li',
+            ['li' => ['class' => 'ellipsis']], '...', '/li',
+            ['li' => ['class' => 'last']], ['a' => ['href' => '/index?page=15']], '8', '/a', '/li',
+        ];
+        $this->assertHtml($expected, $result);
+
         $this->Paginator->request->params['paging'] = [
             'Client' => [
                 'page' => 1,
@@ -1750,6 +1786,16 @@ class PaginatorHelperTest extends TestCase
         ];
         $this->assertHtml($expected, $result);
 
+        $result = $this->Paginator->first('5');
+        $expected = [
+            'li' => ['class' => 'first'],
+            'a' => ['href' => '/index'],
+            '5',
+            '/a',
+            '/li'
+        ];
+        $this->assertHtml($expected, $result);
+
         $result = $this->Paginator->last(2);
         $expected = [
             '<li',
@@ -1760,6 +1806,16 @@ class PaginatorHelperTest extends TestCase
             '/li',
         ];
         $this->assertHtml($expected, $result);
+
+        $result = $this->Paginator->last('9');
+        $expected = [
+            'li' => ['class' => 'last'],
+            'a' => ['href' => '/index?page=7'],
+            '9',
+            '/a',
+            '/li'
+        ];
+        $this->assertHtml($expected, $result);
     }
 
     /**