From 7cb3d4f01cec9b88976696dce525ae07143f207c Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Thu, 4 Apr 2013 20:27:14 -0700 Subject: [PATCH] Duplicate code cleanup --- CRM/Contribute/Form/Contribution/Main.php | 25 +++-------------- CRM/Contribute/Form/UpdateBilling.php | 22 +++------------ CRM/Core/Form.php | 12 +++++++++ CRM/Core/Payment/Form.php | 19 +++++++++++++ CRM/Event/Cart/Form/Checkout/Payment.php | 23 +++------------- CRM/Event/Form/Participant.php | 18 +++---------- .../Registration/AdditionalParticipant.php | 27 +++++-------------- CRM/Event/Form/Registration/Register.php | 24 +++-------------- CRM/Member/Form/Membership.php | 14 +--------- 9 files changed, 54 insertions(+), 130 deletions(-) diff --git a/CRM/Contribute/Form/Contribution/Main.php b/CRM/Contribute/Form/Contribution/Main.php index 6c82532b33..0f31fbae11 100644 --- a/CRM/Contribute/Form/Contribution/Main.php +++ b/CRM/Contribute/Form/Contribution/Main.php @@ -1030,30 +1030,11 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu return $errors; } - if(isset($self->_paymentFields)) { - foreach ($self->_paymentFields as $name => $fld) { - if ($fld['is_required'] && - CRM_Utils_System::isNull(CRM_Utils_Array::value($name, $fields)) - ) { - $errors[$name] = ts('%1 is a required field.', array(1 => $fld['title'])); - } - } + if (!empty($self->_paymentFields)) { + CRM_Core_Form::validateMandatoryFields($self->_paymentFields, $fields, $errors); } + CRM_Core_Payment_Form::validateCreditCard($fields, $errors); - // make sure that credit card number and cvv are valid - if (CRM_Utils_Array::value('credit_card_type', $fields)) { - if (CRM_Utils_Array::value('credit_card_number', $fields) && - !CRM_Utils_Rule::creditCardNumber($fields['credit_card_number'], $fields['credit_card_type']) - ) { - $errors['credit_card_number'] = ts('Please enter a valid Credit Card Number'); - } - - if (CRM_Utils_Array::value('cvv2', $fields) && - !CRM_Utils_Rule::cvv($fields['cvv2'], $fields['credit_card_type']) - ) { - $errors['cvv2'] = ts('Please enter a valid Credit Card Verification Number'); - } - } foreach (CRM_Contact_BAO_Contact::$_greetingTypes as $greeting) { if ($greetingType = CRM_Utils_Array::value($greeting, $fields)) { $customizedValue = CRM_Core_OptionGroup::getValue($greeting, 'Customized', 'name'); diff --git a/CRM/Contribute/Form/UpdateBilling.php b/CRM/Contribute/Form/UpdateBilling.php index 0053030900..625e278d47 100644 --- a/CRM/Contribute/Form/UpdateBilling.php +++ b/CRM/Contribute/Form/UpdateBilling.php @@ -214,28 +214,12 @@ class CRM_Contribute_Form_UpdateBilling extends CRM_Core_Form { * @static */ static function formRule($fields, $files, $self) { - foreach ($self->_fields as $name => $fld) { - if ($fld['is_required'] && - CRM_Utils_System::isNull(CRM_Utils_Array::value($name, $fields)) - ) { - $errors[$name] = ts('%1 is a required field.', array(1 => $fld['title'])); - } - } + $errors = array(); + CRM_Core_Form::validateMandatoryFields($self->_fields, $fields, $errors); // make sure that credit card number and cvv are valid - if (CRM_Utils_Array::value('credit_card_type', $fields)) { - if (CRM_Utils_Array::value('credit_card_number', $fields) && - !CRM_Utils_Rule::creditCardNumber($fields['credit_card_number'], $fields['credit_card_type']) - ) { - $errors['credit_card_number'] = ts('Please enter a valid Credit Card Number'); - } + CRM_Core_Payment_Form::validateCreditCard($fields, $errors); - if (CRM_Utils_Array::value('cvv2', $fields) && - !CRM_Utils_Rule::cvv($fields['cvv2'], $fields['credit_card_type']) - ) { - $errors['cvv2'] = ts('Please enter a valid Credit Card Verification Number'); - } - } return empty($errors) ? TRUE : $errors; } diff --git a/CRM/Core/Form.php b/CRM/Core/Form.php index 33362d1ddc..649c3464a2 100644 --- a/CRM/Core/Form.php +++ b/CRM/Core/Form.php @@ -1239,5 +1239,17 @@ class CRM_Core_Form extends HTML_QuickForm_Page { * @access public */ function cancelAction() {} + + /** + * Helper function to verify that required fields have been filled + * Typically called within the scope of a FormRule function + */ + static function validateMandatoryFields($fields, $values, &$errors) { + foreach ($fields as $name => $fld) { + if (!empty($fld['is_required']) && CRM_Utils_System::isNull(CRM_Utils_Array::value($name, $values))) { + $errors[$name] = ts('%1 is a required field.', array(1 => $fld['title'])); + } + } + } } diff --git a/CRM/Core/Payment/Form.php b/CRM/Core/Payment/Form.php index cd76818658..09ac4ef709 100644 --- a/CRM/Core/Payment/Form.php +++ b/CRM/Core/Payment/Form.php @@ -327,6 +327,25 @@ class CRM_Core_Payment_Form { } } + /** + * Make sure that credit card number and cvv are valid + * Called within the scope of a QF formRule function + */ + static function validateCreditCard($values, &$errors) { + if (!empty($values['credit_card_type'])) { + if (!empty($values['credit_card_number']) && + !CRM_Utils_Rule::creditCardNumber($values['credit_card_number'], $values['credit_card_type']) + ) { + $errors['credit_card_number'] = ts('Please enter a valid Credit Card Number'); + } + if (!empty($values['cvv2']) && + !CRM_Utils_Rule::cvv($values['cvv2'], $values['credit_card_type']) + ) { + $errors['cvv2'] = ts('Please enter a valid Credit Card Verification Number'); + } + } + } + /** * function to map address fields * diff --git a/CRM/Event/Cart/Form/Checkout/Payment.php b/CRM/Event/Cart/Form/Checkout/Payment.php index 5f22261522..ddc7940c46 100644 --- a/CRM/Event/Cart/Form/Checkout/Payment.php +++ b/CRM/Event/Cart/Form/Checkout/Payment.php @@ -363,27 +363,10 @@ class CRM_Event_Cart_Form_Checkout_Payment extends CRM_Event_Cart_Form_Cart { if ($error) { $errors['_qf_default'] = $error; } + CRM_Core_Form::validateMandatoryFields($self->_fields, $fields, $errors); - foreach ($self->_fields as $name => $field) { - if ($field['is_required'] && CRM_Utils_System::isNull(CRM_Utils_Array::value($name, $fields))) { - $errors[$name] = ts('%1 is a required field.', array(1 => $field['title'])); - } - } - - - if (CRM_Utils_Array::value('credit_card_type', $fields)) { - if (CRM_Utils_Array::value('credit_card_number', $fields) && - !CRM_Utils_Rule::creditCardNumber($fields['credit_card_number'], $fields['credit_card_type']) - ) { - $errors['credit_card_number'] = ts("Please enter a valid Credit Card Number"); - } - - if (CRM_Utils_Array::value('cvv2', $fields) && - !CRM_Utils_Rule::cvv($fields['cvv2'], $fields['credit_card_type']) - ) { - $errors['cvv2'] = ts("Please enter a valid Credit Card Verification Number"); - } - } + // make sure that credit card number and cvv are valid + CRM_Core_Payment_Form::validateCreditCard($fields, $errors); } return empty($errors) ? TRUE : $errors; diff --git a/CRM/Event/Form/Participant.php b/CRM/Event/Form/Participant.php index 13919346af..757be992e0 100644 --- a/CRM/Event/Form/Participant.php +++ b/CRM/Event/Form/Participant.php @@ -955,23 +955,11 @@ loadCampaign( {$this->_eID}, {$eventCampaigns} ); if (CRM_Utils_Array::value('payment_processor_id', $values)) { // make sure that credit card number and cvv are valid - if (CRM_Utils_Array::value('credit_card_type', $values)) { - if (CRM_Utils_Array::value('credit_card_number', $values) && - !CRM_Utils_Rule::creditCardNumber($values['credit_card_number'], $values['credit_card_type']) - ) { - $errorMsg['credit_card_number'] = ts('Please enter a valid Credit Card Number'); - } - - if (CRM_Utils_Array::value('cvv2', $values) && - !CRM_Utils_Rule::cvv($values['cvv2'], $values['credit_card_type']) - ) { - $errorMsg['cvv2'] = ts('Please enter a valid Credit Card Verification Number'); - } - } + CRM_Core_Payment_Form::validateCreditCard($values, $errorMsg); } - if ( CRM_Utils_Array::value( 'record_contribution', $values ) && ! CRM_Utils_Array::value( 'financial_type_id', $values ) ) { - $errorMsg['financial_type_id'] = ts( 'Please enter the associated Financial Type' ); + if (CRM_Utils_Array::value('record_contribution', $values) && !CRM_Utils_Array::value('financial_type_id', $values)) { + $errorMsg['financial_type_id'] = ts('Please enter the associated Financial Type'); } // validate contribution status for 'Failed'. diff --git a/CRM/Event/Form/Registration/AdditionalParticipant.php b/CRM/Event/Form/Registration/AdditionalParticipant.php index da52b7fa90..ed4d735284 100644 --- a/CRM/Event/Form/Registration/AdditionalParticipant.php +++ b/CRM/Event/Form/Registration/AdditionalParticipant.php @@ -546,33 +546,20 @@ class CRM_Event_Form_Registration_AdditionalParticipant extends CRM_Event_Form_R } if (!$validatePayement) { - return TRUE; - } - foreach ($self->_fields as $name => $fld) { - if ($fld['is_required'] && - CRM_Utils_System::isNull(CRM_Utils_Array::value($name, $fields)) - ) { - return FALSE; - } - } + $errors = array(); + + CRM_Core_Form::validateMandatoryFields($self->_fields, $fields, $errors); // make sure that credit card number and cvv are valid - if (CRM_Utils_Array::value('credit_card_type', $self->_params[0])) { - if (CRM_Utils_Array::value('credit_card_number', $self->_params[0]) && - !CRM_Utils_Rule::creditCardNumber($self->_params[0]['credit_card_number'], $self->_params[0]['credit_card_type']) - ) { - return FALSE; - } + CRM_Core_Payment_Form::validateCreditCard($self->_params[0], $errors); - if (CRM_Utils_Array::value('cvv2', $self->_params[0]) && - !CRM_Utils_Rule::cvv($self->_params[0]['cvv2'], $self->_params[0]['credit_card_type']) - ) { - return FALSE; - } + if ($errors) { + return FALSE; } + foreach (CRM_Contact_BAO_Contact::$_greetingTypes as $greeting) { if ($greetingType = CRM_Utils_Array::value($greeting, $self->_params[0])) { $customizedValue = CRM_Core_OptionGroup::getValue($greeting, 'Customized', 'name'); diff --git a/CRM/Event/Form/Registration/Register.php b/CRM/Event/Form/Registration/Register.php index fe7d17ad6f..b9f97a3640 100644 --- a/CRM/Event/Form/Registration/Register.php +++ b/CRM/Event/Form/Registration/Register.php @@ -891,30 +891,12 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration { ) { return empty($errors) ? TRUE : $errors; } - if (property_exists($self, '_paymentFields') && !empty($self->_paymentFields )) { - foreach ($self->_paymentFields as $name => $fld) { - if ($fld['is_required'] && - CRM_Utils_System::isNull(CRM_Utils_Array::value($name, $fields)) - ) { - $errors[$name] = ts('%1 is a required field.', array(1 => $fld['title'])); - } - } + if (!empty($self->_paymentFields)) { + CRM_Core_Form::validateMandatoryFields($self->_paymentFields, $fields, $errors); } + CRM_Core_Payment_Form::validateCreditCard($fields, $errors); } - // make sure that credit card number and cvv are valid - if (CRM_Utils_Array::value('credit_card_type', $fields)) { - if (CRM_Utils_Array::value('credit_card_number', $fields) && - !CRM_Utils_Rule::creditCardNumber($fields['credit_card_number'], $fields['credit_card_type']) - ) { - $errors['credit_card_number'] = ts('Please enter a valid Credit Card Number'); - } - if (CRM_Utils_Array::value('cvv2', $fields) && - !CRM_Utils_Rule::cvv($fields['cvv2'], $fields['credit_card_type']) - ) { - $errors['cvv2'] = ts('Please enter a valid Credit Card Verification Number'); - } - } foreach (CRM_Contact_BAO_Contact::$_greetingTypes as $greeting) { if ($greetingType = CRM_Utils_Array::value($greeting, $fields)) { $customizedValue = CRM_Core_OptionGroup::getValue($greeting, 'Customized', 'name'); diff --git a/CRM/Member/Form/Membership.php b/CRM/Member/Form/Membership.php index b3ac0da9dd..b6e7e2b8e6 100644 --- a/CRM/Member/Form/Membership.php +++ b/CRM/Member/Form/Membership.php @@ -895,19 +895,7 @@ WHERE id IN ( ' . implode(' , ', array_keys($membershipType)) . ' )'; if (CRM_Utils_Array::value('payment_processor_id', $params)) { // make sure that credit card number and cvv are valid - if (CRM_Utils_Array::value('credit_card_type', $params)) { - if (CRM_Utils_Array::value('credit_card_number', $params) && - !CRM_Utils_Rule::creditCardNumber($params['credit_card_number'], $params['credit_card_type']) - ) { - $errors['credit_card_number'] = ts('Please enter a valid Credit Card Number'); - } - - if (CRM_Utils_Array::value('cvv2', $params) && - !CRM_Utils_Rule::cvv($params['cvv2'], $params['credit_card_type']) - ) { - $errors['cvv2'] = ts('Please enter a valid Credit Card Verification Number'); - } - } + CRM_Core_Payment_Form::validateCreditCard($params, $errors); } $joinDate = NULL; -- 2.25.1