From ba933cebad71a273590db2b50798187918e40841 Mon Sep 17 00:00:00 2001 From: Eileen Date: Sat, 28 Sep 2013 05:07:54 -0400 Subject: [PATCH] CRM-13379 prevent double click on confirm button on main contribution page to prevent double payment or error --- CRM/Contact/Form/Task/EmailCommon.php | 2 +- CRM/Contribute/Form/Contribution/Confirm.php | 3 ++- CRM/Contribute/Form/Contribution/Main.php | 1 + CRM/Core/Form.php | 13 ++++++++++--- js/Common.js | 9 +++++++-- templates/CRM/Contact/Form/Task/EmailCommon.js | 3 +++ templates/CRM/Core/Form.js | 15 +++++++++++++++ 7 files changed, 39 insertions(+), 7 deletions(-) create mode 100644 templates/CRM/Core/Form.js diff --git a/CRM/Contact/Form/Task/EmailCommon.php b/CRM/Contact/Form/Task/EmailCommon.php index ca503ddb90..d90e7ac469 100644 --- a/CRM/Contact/Form/Task/EmailCommon.php +++ b/CRM/Contact/Form/Task/EmailCommon.php @@ -270,7 +270,7 @@ class CRM_Contact_Form_Task_EmailCommon { } $form->addFormRule(array('CRM_Contact_Form_Task_EmailCommon', 'formRule'), $form); - CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'templates/CRM/Contact/Form/Task/EmailCommon.js'); + CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'templates/CRM/Core/Form.js'); } /** diff --git a/CRM/Contribute/Form/Contribution/Confirm.php b/CRM/Contribute/Form/Contribution/Confirm.php index 33e67ccccf..8e7ee1e63d 100644 --- a/CRM/Contribute/Form/Contribution/Confirm.php +++ b/CRM/Contribute/Form/Contribution/Confirm.php @@ -494,7 +494,6 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr 'name' => $contribButton, 'spacing' => '         ', 'isDefault' => TRUE, - 'js' => array('onclick' => "return submitOnce(this,'" . $this->_name . "','" . ts('Processing') . "');"), ), array( 'type' => 'back', @@ -557,6 +556,8 @@ class CRM_Contribute_Form_Contribution_Confirm extends CRM_Contribute_Form_Contr $this->setDefaults($defaults); $this->freeze(); + + parent::buildQuickForm(); } /** diff --git a/CRM/Contribute/Form/Contribution/Main.php b/CRM/Contribute/Form/Contribution/Main.php index 358a90de47..190ccdc8f3 100644 --- a/CRM/Contribute/Form/Contribution/Main.php +++ b/CRM/Contribute/Form/Contribution/Main.php @@ -556,6 +556,7 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu } $this->addFormRule(array('CRM_Contribute_Form_Contribution_Main', 'formRule'), $this); + parent::buildQuickForm(); } /** diff --git a/CRM/Core/Form.php b/CRM/Core/Form.php index 5167ab339d..e81c6f42ba 100644 --- a/CRM/Core/Form.php +++ b/CRM/Core/Form.php @@ -285,7 +285,14 @@ class CRM_Core_Form extends HTML_QuickForm_Page { * @return void * */ - function buildQuickForm() {} + function buildQuickForm() { + // @todo less cautious approach would be to add this to addButtons. + // it would be good to make this switch at the early stage of a release cycle. + // @ the moment of the 106 uses of parent::buildQuickForm only a handle are direct descendents + // of this class - including the contribution forms per CRM-13397 & the (tested) sms provider form. + //(I checked maybe another 20 directly that weren't & was also able to rule out about 40 more by virtue of common patterns + CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'templates/CRM/Core/Form.js'); + } /** * This virtual function is used to set the default values of @@ -398,10 +405,10 @@ class CRM_Core_Form extends HTML_QuickForm_Page { $js = CRM_Utils_Array::value('js', $button); $isDefault = CRM_Utils_Array::value('isDefault', $button, FALSE); if ($isDefault) { - $attrs = array('class' => 'form-submit default'); + $attrs = array('class' => 'form-submit default crm-form-button-' . $button['type']); } else { - $attrs = array('class' => 'form-submit'); + $attrs = array('class' => 'form-submit crm-form-button-' . $button['type']); } if ($js) { diff --git a/js/Common.js b/js/Common.js index efd5140b2c..8770917b03 100644 --- a/js/Common.js +++ b/js/Common.js @@ -396,8 +396,13 @@ function unselectRadio(fieldName, form) { * @return null */ var submitcount = 0; -/* Changes button label on submit, and disables button after submit for newer browsers. - Puts up alert for older browsers. */ +/** + * @deprecated + * Changes button label on submit, and disables button after submit for newer browsers. + * Puts up alert for older browsers. + * @todo CRM-13397 replaces this with an alternate (jquery) mechanism - to use the jquery mechanism add + * parent::buildQuickForm to make buttons 'protected' + * */ function submitOnce(obj, formId, procText) { // if named button clicked, change text if (obj.value != null) { diff --git a/templates/CRM/Contact/Form/Task/EmailCommon.js b/templates/CRM/Contact/Form/Task/EmailCommon.js index bb7610e4c9..acbb4c49d6 100644 --- a/templates/CRM/Contact/Form/Task/EmailCommon.js +++ b/templates/CRM/Contact/Form/Task/EmailCommon.js @@ -2,6 +2,7 @@ cj(function ($) { //do not copy & paste this - find a way to generalise it 'use strict'; $().crmAccordions(); +<<<<<<< HEAD // NOTE: Might be safer to say $('[name=_qf_Email_upload]') $('.form-submit').not('.cancel').on("click", function() { $('.form-submit').not('.cancel').attr({value: ts('Processing')}); @@ -12,4 +13,6 @@ cj(function ($) { $('.form-submit').not('.cancel').attr({disabled: 'disabled'}); }, 0); }); +======= +>>>>>>> CRM-13379 js, contribution form - generalise button double submit protection and add to contribution.main to prevent double submits where people have no confirm page }); diff --git a/templates/CRM/Core/Form.js b/templates/CRM/Core/Form.js new file mode 100644 index 0000000000..4b27202a6e --- /dev/null +++ b/templates/CRM/Core/Form.js @@ -0,0 +1,15 @@ +cj(function ($) { + 'use strict'; + $('.form-submit').on("click", function() { + $('.form-submit').attr({value: ts('Processing'), disabled : 'disabled'}); + $('.crm-form-button-back ').closest('span').hide(); + $('.crm-form-button-cancel').closest('span').hide(); + // CRM-13449 : setting a 0 ms timeout is needed + // for some browsers like chrome. Used for purpose of + // submit the form and stop accidental multiple clicks + setTimeout(function(){ + $('.form-submit').not('.cancel').attr({disabled: 'disabled'}); + }, 0); + }); +}); + -- 2.25.1