From 8601e0078a8242fda63d4ce0023602e1b80feda1 Mon Sep 17 00:00:00 2001 From: Alexander Hofbauer Date: Mon, 5 Aug 2013 16:20:53 +0200 Subject: [PATCH] Implement a Helper method to generate confirm() links This allows for overriding the default behavior of showing confirm()-dialogs in Html and Form helpers. --- lib/Cake/Test/Case/View/Helper/FormHelperTest.php | 2 +- lib/Cake/Test/Case/View/Helper/HtmlHelperTest.php | 2 +- lib/Cake/View/Helper.php | 13 +++++++++++++ lib/Cake/View/Helper/FormHelper.php | 7 +++---- lib/Cake/View/Helper/HtmlHelper.php | 9 ++++----- 5 files changed, 22 insertions(+), 11 deletions(-) diff --git a/lib/Cake/Test/Case/View/Helper/FormHelperTest.php b/lib/Cake/Test/Case/View/Helper/FormHelperTest.php index 93a18a24d..ee0807313 100644 --- a/lib/Cake/Test/Case/View/Helper/FormHelperTest.php +++ b/lib/Cake/Test/Case/View/Helper/FormHelperTest.php @@ -7018,7 +7018,7 @@ class FormHelperTest extends CakeTestCase { ), 'input' => array('type' => 'hidden', 'name' => '_method', 'value' => 'POST'), '/form', - 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\('Confirm\?'\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'), + 'a' => array('href' => '#', 'onclick' => 'preg:/if \(confirm\("Confirm\?"\)\) \{ document\.post_\w+\.submit\(\); \} event\.returnValue = false; return false;/'), 'Delete', '/a' )); diff --git a/lib/Cake/Test/Case/View/Helper/HtmlHelperTest.php b/lib/Cake/Test/Case/View/Helper/HtmlHelperTest.php index 054b09641..20dba237a 100644 --- a/lib/Cake/Test/Case/View/Helper/HtmlHelperTest.php +++ b/lib/Cake/Test/Case/View/Helper/HtmlHelperTest.php @@ -215,7 +215,7 @@ class HtmlHelperTest extends CakeTestCase { $result = $this->Html->link('Home', '/home', array('confirm' => 'Are you sure you want to do this?')); $expected = array( - 'a' => array('href' => '/home', 'onclick' => 'return confirm('Are you sure you want to do this?');'), + 'a' => array('href' => '/home', 'onclick' => 'if (confirm("Are you sure you want to do this?")) { return true; } return false;'), 'Home', '/a' ); diff --git a/lib/Cake/View/Helper.php b/lib/Cake/View/Helper.php index 4db342f5f..cc7e1ed00 100644 --- a/lib/Cake/View/Helper.php +++ b/lib/Cake/View/Helper.php @@ -496,6 +496,19 @@ class Helper extends Object { return sprintf($this->_attributeFormat, $key, ($escape ? h($value) : $value)); } +/** + * Returns a string to be used as onclick handler for confirm dialogs. + * + * @param string $message Message to be displayed + * @param string $okCode Code to be executed after user chose 'OK' + * @param string $cancelCode Code to be executed after user chose 'Cancel' + * @return string onclick JS code + */ + protected function _confirm($message, $okCode, $cancelCode = '') { + $message = json_encode($message); + return "if (confirm({$message})) { {$okCode} } {$cancelCode}"; + } + /** * Sets this helper's model and field properties to the dot-separated value-pair in $entity. * diff --git a/lib/Cake/View/Helper/FormHelper.php b/lib/Cake/View/Helper/FormHelper.php index 6f404896b..f2809fca8 100644 --- a/lib/Cake/View/Helper/FormHelper.php +++ b/lib/Cake/View/Helper/FormHelper.php @@ -1784,12 +1784,11 @@ class FormHelper extends AppHelper { $url = '#'; $onClick = 'document.' . $formName . '.submit();'; if ($confirmMessage) { - $confirmMessage = str_replace(array("'", '"'), array("\'", '\"'), $confirmMessage); - $options['onclick'] = "if (confirm('{$confirmMessage}')) { {$onClick} }"; + $options['onclick'] = $this->_confirm($confirmMessage, $onClick); } else { - $options['onclick'] = $onClick; + $options['onclick'] = $onClick . ' '; } - $options['onclick'] .= ' event.returnValue = false; return false;'; + $options['onclick'] .= 'event.returnValue = false; return false;'; $out .= $this->Html->link($title, $url, $options); return $out; diff --git a/lib/Cake/View/Helper/HtmlHelper.php b/lib/Cake/View/Helper/HtmlHelper.php index 247cd6020..4b19f0c45 100644 --- a/lib/Cake/View/Helper/HtmlHelper.php +++ b/lib/Cake/View/Helper/HtmlHelper.php @@ -359,15 +359,14 @@ class HtmlHelper extends AppHelper { unset($options['confirm']); } if ($confirmMessage) { - $confirmMessage = str_replace("'", "\'", $confirmMessage); - $confirmMessage = str_replace('"', '\"', $confirmMessage); - $options['onclick'] = "return confirm('{$confirmMessage}');"; + $options['onclick'] = $this->_confirm($confirmMessage, 'return true;', 'return false;'); } elseif (isset($options['default']) && !$options['default']) { if (isset($options['onclick'])) { - $options['onclick'] .= ' event.returnValue = false; return false;'; + $options['onclick'] .= ' '; } else { - $options['onclick'] = 'event.returnValue = false; return false;'; + $options['onclick'] = ''; } + $options['onclick'] .= 'event.returnValue = false; return false;'; unset($options['default']); } return sprintf($this->_tags['link'], $url, $this->_parseAttributes($options), $title);