Browse Source

Add URL option to first/last as well.

This makes all the link generating methods in PaginatorHelper support
the `url` option. This also fixes the tests provided in #6576
Mark Story 11 years ago
parent
commit
30f6d35f11

+ 30 - 14
src/View/Helper/PaginatorHelper.php

@@ -269,7 +269,7 @@ class PaginatorHelper extends Helper
      *   this method to return ''.
      * - `escape` Whether you want the contents html entity encoded, defaults to true
      * - `model` The model to use, defaults to PaginatorHelper::defaultModel()
-     * - `url` Additional URL parameters to use in the generated URL.
+     * - `url` An array of additional URL options to use for link generation.
      *
      * @param string $title Title for the link. Defaults to '<< Previous'.
      * @param array $options Options for pagination link. See above for list of keys.
@@ -305,7 +305,7 @@ class PaginatorHelper extends Helper
      *   this method to return ''.
      * - `escape` Whether you want the contents html entity encoded, defaults to true
      * - `model` The model to use, defaults to PaginatorHelper::defaultModel()
-     * - `url` Additional URL parameters to use in the generated URL.
+     * - `url` An array of additional URL options to use for link generation.
      *
      * @param string $title Title for the link. Defaults to 'Next >>'.
      * @param array $options Options for pagination link. See above for list of keys.
@@ -604,6 +604,7 @@ class PaginatorHelper extends Helper
      * - `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.
+     * - `url` An array of additional URL options to use for link generation.
      *
      * The generated number links will include the 'ellipsis' template when the `first` and `last` options
      * and the number of pages exceed the modulus. For example if you have 25 pages, and use the first/last
@@ -617,7 +618,7 @@ class PaginatorHelper extends Helper
     {
         $defaults = [
             'before' => null, 'after' => null, 'model' => $this->defaultModel(),
-            'modulus' => 8, 'first' => null, 'last' => null,
+            'modulus' => 8, 'first' => null, 'last' => null, 'url' => []
         ];
         $options += $defaults;
 
@@ -678,9 +679,10 @@ class PaginatorHelper extends Helper
      */
     protected function _formatNumber($templater, $options)
     {
+        $url = array_merge($options['url'], ['page' => $options['page']]);
         $vars = [
             'text' => $options['text'],
-            'url' => $this->generateUrl(['page' => $options['page']], $options['model']),
+            'url' => $this->generateUrl($url, $options['model']),
         ];
         return $templater->format('number', $vars);
     }
@@ -701,14 +703,14 @@ class PaginatorHelper extends Helper
         list($start, $end) = $this->_getNumbersStartAndEnd($params, $options);
 
         $out .= $this->_firstNumber($ellipsis, $params, $start, $options);
-
         $out .= $options['before'];
 
         for ($i = $start; $i < $params['page']; $i++) {
             $out .= $this->_formatNumber($templater, [
                 'text' => $i,
                 'page' => $i,
-                'model' => $options['model']
+                'model' => $options['model'],
+                'url' => $options['url'],
             ]);
         }
 
@@ -723,7 +725,8 @@ class PaginatorHelper extends Helper
             $out .= $this->_formatNumber($templater, [
                 'text' => $i,
                 'page' => $i,
-                'model' => $options['model']
+                'model' => $options['model'],
+                'url' => $options['url'],
             ]);
         }
 
@@ -731,7 +734,8 @@ class PaginatorHelper extends Helper
             $out .= $this->_formatNumber($templater, [
                 'text' => $i,
                 'page' => $end,
-                'model' => $options['model']
+                'model' => $options['model'],
+                'url' => $options['url'],
             ]);
         }
 
@@ -754,7 +758,7 @@ class PaginatorHelper extends Helper
         $out = '';
         if ($options['first'] && $start > 1) {
             $offset = ($start <= (int)$options['first']) ? $start - 1 : $options['first'];
-            $out .= $this->first($offset);
+            $out .= $this->first($offset, $options);
             if ($offset < $start - 1) {
                 $out .= $ellipsis;
             }
@@ -779,7 +783,7 @@ class PaginatorHelper extends Helper
             if ($offset <= $options['last'] && $params['pageCount'] - $end > $offset) {
                 $out .= $ellipsis;
             }
-            $out .= $this->last($offset);
+            $out .= $this->last($offset, $options);
         }
         return $out;
     }
@@ -835,6 +839,7 @@ class PaginatorHelper extends Helper
      *
      * - `model` The model to use defaults to PaginatorHelper::defaultModel()
      * - `escape` Whether or not to HTML escape the text.
+     * - `url` An array of additional URL options to use for link generation.
      *
      * @param string|int $first if string use as label for the link. If numeric, the number of page links
      *   you want at the beginning of the range.
@@ -844,7 +849,11 @@ class PaginatorHelper extends Helper
      */
     public function first($first = '<< first', array $options = [])
     {
-        $options += ['model' => $this->defaultModel(), 'escape' => true];
+        $options += [
+            'url' => [],
+            'model' => $this->defaultModel(),
+            'escape' => true
+        ];
 
         $params = $this->params($options['model']);
 
@@ -856,8 +865,9 @@ class PaginatorHelper extends Helper
 
         if (is_int($first) && $params['page'] >= $first) {
             for ($i = 1; $i <= $first; $i++) {
+                $url = array_merge($options['url'], ['page' => $i]);
                 $out .= $this->templater()->format('number', [
-                    'url' => $this->generateUrl(['page' => $i], $options['model']),
+                    'url' => $this->generateUrl($url, $options['model']),
                     'text' => $i
                 ]);
             }
@@ -890,6 +900,7 @@ class PaginatorHelper extends Helper
      *
      * - `model` The model to use defaults to PaginatorHelper::defaultModel()
      * - `escape` Whether or not to HTML escape the text.
+     * - `url` An array of additional URL options to use for link generation.
      *
      * @param string|int $last if string use as label for the link, if numeric print page numbers
      * @param array $options Array of options
@@ -898,7 +909,11 @@ class PaginatorHelper extends Helper
      */
     public function last($last = 'last >>', array $options = [])
     {
-        $options += ['model' => $this->defaultModel(), 'escape' => true];
+        $options += [
+            'model' => $this->defaultModel(),
+            'escape' => true,
+            'url' => []
+        ];
         $params = $this->params($options['model']);
 
         if ($params['pageCount'] <= 1) {
@@ -910,8 +925,9 @@ class PaginatorHelper extends Helper
 
         if (is_int($last) && $params['page'] <= $lower) {
             for ($i = $lower; $i <= $params['pageCount']; $i++) {
+                $url = array_merge($options['url'], ['page' => $i]);
                 $out .= $this->templater()->format('number', [
-                    'url' => $this->generateUrl(['page' => $i], $options['model']),
+                    'url' => $this->generateUrl($url, $options['model']),
                     'text' => $i
                 ]);
             }

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

@@ -1593,7 +1593,6 @@ class PaginatorHelperTest extends TestCase
             ['li' => []], ['a' => ['href' => '/index?page=4897&amp;foo=bar']], '4897', '/a', '/li',
         ];
         $this->assertHtml($expected, $result);
-        $this->assertHtml($expected, $result);
     }
 
     /**