Duplicate code cleanup
authorColeman Watts <coleman@civicrm.org>
Fri, 5 Apr 2013 03:27:14 +0000 (20:27 -0700)
committerColeman Watts <coleman@civicrm.org>
Fri, 5 Apr 2013 16:16:25 +0000 (09:16 -0700)
CRM/Contribute/Form/Contribution/Main.php
CRM/Contribute/Form/UpdateBilling.php
CRM/Core/Form.php
CRM/Core/Payment/Form.php
CRM/Event/Cart/Form/Checkout/Payment.php
CRM/Event/Form/Participant.php
CRM/Event/Form/Registration/AdditionalParticipant.php
CRM/Event/Form/Registration/Register.php
CRM/Member/Form/Membership.php

index 6c82532b3366c6ab16a6eee3f1d46d4cd761507b..0f31fbae117e1d6ce3208e591830f7e871c869fd 100644 (file)
@@ -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');
index 00530309008553fa37c8121ca3472f4f89075574..625e278d475738046ac17f2c6e7207bade9b2a28 100644 (file)
@@ -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;
   }
 
index 33362d1ddc145b5182549f0e4989b419363f17ca..649c3464a25aacde8afc072a790efa7e56937da4 100644 (file)
@@ -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']));
+      }
+    }
+  }
 }
 
index cd768186584da96cf70908458f472782c0d459ac..09ac4ef7098356b2ad209149aeebec0100ff0f51 100644 (file)
@@ -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
    *
index 5f2226152222972ac8367c4ed72ccfe83e078f03..ddc7940c467f03604d73a2f2236a7f6f244a7b5f 100644 (file)
@@ -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;
index 13919346afacb910fd8d5d8116ab53504b31e0f4..757be992e014ff17ca9fbcb3b4a7347f1e045758 100644 (file)
@@ -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'.
index da52b7fa9099cd2e6d9b1f0069981bc27619b714..ed4d73528495b9b52e63cbf617f3ff64f6857911 100644 (file)
@@ -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');
index fe7d17ad6f0d0358186e8c9d1867cd2cdda671a3..b9f97a3640f6c1b33559a1a75601f52bb9da14ed 100644 (file)
@@ -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');
index b3ac0da9dd3f5d54ba34e1c75132e1d704944455..b6e7e2b8e6444f3b162d490e25550adb2c0ddfe0 100644 (file)
@@ -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;