Browse Source

FormHelper::postLink(), and HtmlHelper::link() updated to allow users to supply their own javascript for displaying a confirm window. Milestone #11924

Eugene Ritter 7 years ago
parent
commit
626c0a0e59

+ 12 - 1
src/View/Helper.php

@@ -176,7 +176,7 @@ class Helper implements EventListenerInterface
      */
      */
     protected function _confirm($message, $okCode, $cancelCode = '', $options = [])
     protected function _confirm($message, $okCode, $cancelCode = '', $options = [])
     {
     {
-        $message = str_replace('\\\n', '\n', json_encode($message));
+        $message = $this->_cleanConfirmMessage($message);
         $confirm = "if (confirm({$message})) { {$okCode} } {$cancelCode}";
         $confirm = "if (confirm({$message})) { {$okCode} } {$cancelCode}";
         // We cannot change the key here in 3.x, but the behavior is inverted in this case
         // We cannot change the key here in 3.x, but the behavior is inverted in this case
         $escape = isset($options['escape']) && $options['escape'] === false;
         $escape = isset($options['escape']) && $options['escape'] === false;
@@ -189,6 +189,17 @@ class Helper implements EventListenerInterface
     }
     }
 
 
     /**
     /**
+     * Returns a string read to be used in confirm()
+     *
+     * @param string $message The message to clean
+     * @return mixed
+     */
+    protected function _cleanConfirmMessage($message)
+    {
+        return str_replace('\\\n', '\n', json_encode($message));
+    }
+
+    /**
      * Adds the given class to the element options
      * Adds the given class to the element options
      *
      *
      * @param array $options Array options/attributes to add a class to
      * @param array $options Array options/attributes to add a class to

+ 10 - 3
src/View/Helper/FormHelper.php

@@ -163,6 +163,8 @@ class FormHelper extends Helper
             'textarea' => '<textarea name="{{name}}"{{attrs}}>{{value}}</textarea>',
             'textarea' => '<textarea name="{{name}}"{{attrs}}>{{value}}</textarea>',
             // Container for submit buttons.
             // Container for submit buttons.
             'submitContainer' => '<div class="submit">{{content}}</div>',
             'submitContainer' => '<div class="submit">{{content}}</div>',
+            //Confirm javascript template for postLink()
+            'confirmJs' => '{{confirm}}',
         ]
         ]
     ];
     ];
 
 
@@ -1950,11 +1952,16 @@ class FormHelper extends Helper
         $url = '#';
         $url = '#';
         $onClick = 'document.' . $formName . '.submit();';
         $onClick = 'document.' . $formName . '.submit();';
         if ($confirmMessage) {
         if ($confirmMessage) {
-            $options['onclick'] = $this->_confirm($confirmMessage, $onClick, '', $options);
+            $confirm = $this->_confirm($confirmMessage, $onClick, '', $options);
         } else {
         } else {
-            $options['onclick'] = $onClick . ' ';
+            $confirm = $onClick . ' ';
         }
         }
-        $options['onclick'] .= 'event.returnValue = false; return false;';
+        $confirm .= 'event.returnValue = false; return false;';
+        $options['onclick'] = $this->templater()->format('confirmJs', [
+            'confirmMessage' => $this->_cleanConfirmMessage($confirmMessage),
+            'formName' => $formName,
+            'confirm' => $confirm
+        ]);
 
 
         $out .= $this->Html->link($title, $url, $options);
         $out .= $this->Html->link($title, $url, $options);
 
 

+ 8 - 4
src/View/Helper/HtmlHelper.php

@@ -81,7 +81,8 @@ class HtmlHelper extends Helper
             'javascriptblock' => '<script{{attrs}}>{{content}}</script>',
             'javascriptblock' => '<script{{attrs}}>{{content}}</script>',
             'javascriptstart' => '<script>',
             'javascriptstart' => '<script>',
             'javascriptlink' => '<script src="{{url}}"{{attrs}}></script>',
             'javascriptlink' => '<script src="{{url}}"{{attrs}}></script>',
-            'javascriptend' => '</script>'
+            'javascriptend' => '</script>',
+            'confirmJs' => '{{confirm}}'
         ]
         ]
     ];
     ];
 
 
@@ -373,17 +374,20 @@ class HtmlHelper extends Helper
             $title = htmlentities($title, ENT_QUOTES, $escapeTitle);
             $title = htmlentities($title, ENT_QUOTES, $escapeTitle);
         }
         }
 
 
+        $templater = $this->templater();
         $confirmMessage = null;
         $confirmMessage = null;
         if (isset($options['confirm'])) {
         if (isset($options['confirm'])) {
             $confirmMessage = $options['confirm'];
             $confirmMessage = $options['confirm'];
             unset($options['confirm']);
             unset($options['confirm']);
         }
         }
         if ($confirmMessage) {
         if ($confirmMessage) {
-            $options['onclick'] = $this->_confirm($confirmMessage, 'return true;', 'return false;', $options);
+            $confirm = $this->_confirm($confirmMessage, 'return true;', 'return false;', $options);
+            $options['onclick'] = $templater->format('confirmJs', [
+                'confirmMessage' => $this->_cleanConfirmMessage($confirmMessage),
+                'confirm' => $confirm
+            ]);
         }
         }
 
 
-        $templater = $this->templater();
-
         return $templater->format('link', [
         return $templater->format('link', [
             'url' => $url,
             'url' => $url,
             'attrs' => $templater->formatAttributes($options),
             'attrs' => $templater->formatAttributes($options),

+ 20 - 0
tests/TestCase/View/Helper/FormHelperTest.php

@@ -7654,6 +7654,26 @@ class FormHelperTest extends TestCase
             '/a'
             '/a'
         ];
         ];
         $this->assertHtml($expected, $result);
         $this->assertHtml($expected, $result);
+
+        $this->Form->setTemplates(['confirmJs' => 'if (confirm({{confirmMessage}})) { $(\'form[name="{{formName}}"]\').submit();};']);
+        $result = $this->Form->postLink(
+            'Delete',
+            '/posts/delete/1',
+            ['escape' => false, 'confirm' => 'Confirm this deletion?']
+        );
+        $expected = [
+            'form' => [
+                'method' => 'post', 'action' => '/posts/delete/1',
+                'name' => 'preg:/post_\w+/', 'style' => 'display:none;'
+            ],
+            'input' => ['type' => 'hidden', 'name' => '_method', 'value' => 'POST'],
+            '/form',
+            'a' => ['href' => '#', 'onclick' => 'preg:/if \(confirm\("Confirm this deletion\?"\)\) \{ \$\(\'form\[name="post_\w+"\]\'\)\.submit\(\);\};/'],
+            'Delete',
+            '/a'
+        ];
+
+        $this->assertHtml($expected, $result);
     }
     }
 
 
     /**
     /**

+ 11 - 0
tests/TestCase/View/Helper/HtmlHelperTest.php

@@ -147,6 +147,17 @@ class HtmlHelperTest extends TestCase
         ];
         ];
         $this->assertHtml($expected, $result);
         $this->assertHtml($expected, $result);
 
 
+        $this->Html->setTemplates(['confirmJs' => 'if (confirm({{confirmMessage}})) { window.location="/";};']);
+        $result = $this->Html->link('Home', '/home', ['confirm' => 'Are you sure you want to do this?']);
+        $expected = [
+            'a' => ['href' => '/home', 'onclick' => 'preg:/if \(confirm\(&quot;Are you sure you want to do this\?&quot;\)\) \{ window\.location=&quot;\/&quot;;\};/'],
+            'Home',
+            '/a'
+        ];
+
+        $this->assertHtml($expected, $result);
+        $this->Html->setTemplates(['confirmJs' => '{{confirm}}']);
+
         $result = $this->Html->link('Home', '/home', ['escape' => false, 'confirm' => 'Confirm\'s "nightmares"']);
         $result = $this->Html->link('Home', '/home', ['escape' => false, 'confirm' => 'Confirm\'s "nightmares"']);
         $expected = [
         $expected = [
             'a' => ['href' => '/home', 'onclick' => 'if (confirm(&quot;Confirm&#039;s \&quot;nightmares\&quot;&quot;)) { return true; } return false;'],
             'a' => ['href' => '/home', 'onclick' => 'if (confirm(&quot;Confirm&#039;s \&quot;nightmares\&quot;&quot;)) { return true; } return false;'],