CRM-13179 use api function so get billing defaults in order to use 'sensible fallback...
[civicrm-core.git] / CRM / Event / Form / Registration / Register.php
index 6d9fef410ad38853d81e3e4b0a850cba131bcdaf..7947fdb1ea20b5cee09134cee0431ead438cb9ec 100644 (file)
@@ -140,38 +140,12 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
       // see function comment block for explanation of this
       return;
     }
-
+    $this->_defaults = array();
     $contactID = $this->getContactID();
     if ($contactID) {
       //@todo CRM-11915 I observed that even when the billing block is not present the routine to retrieve the billing defaults is still called - which seems a bit redundant.
-      $names = array(
-        'first_name', 'middle_name', 'last_name',
-        "street_address-{$this->_bltID}", "city-{$this->_bltID}", "postal_code-{$this->_bltID}",
-        "country_id-{$this->_bltID}", "state_province_id-{$this->_bltID}",
-      );
-
-      foreach ($names as $name) {
-        $fields[$name] = 1;
-      }
-      $fields["state_province-{$this->_bltID}"] = 1;
-      $fields["country-{$this->_bltID}"] = 1;
-      $fields["email-{$this->_bltID}"] = 1;
-      $fields['email-Primary'] = 1;
-
-      CRM_Core_BAO_UFGroup::setProfileDefaults($contactID, $fields, $this->_defaults);
-
-      // use primary email address if billing email address is empty
-      if (empty($this->_defaults["email-{$this->_bltID}"]) &&
-        !empty($this->_defaults['email-Primary'])
-      ) {
-        $this->_defaults["email-{$this->_bltID}"] = $this->_defaults['email-Primary'];
-      }
-
-      foreach ($names as $name) {
-        if (isset($this->_defaults[$name])) {
-          $this->_defaults['billing_' . $name] = $this->_defaults[$name];
-        }
-      }
+      $billingDefaults = $this->getProfileDefaults('Billing', $contactID);
+      $this->_defaults = array_merge($this->_defaults, $billingDefaults);
     }
     $config = CRM_Core_Config::singleton();
     // set default country from config if no country set
@@ -858,6 +832,9 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
     }
 
     if ($self->_values['event']['is_monetary']) {
+      if (($fields['amount'] > 0) && !isset($fields['payment_processor'])) {
+        $errors['payment_processor'] = ts('Please select a Payment Method');
+      }
       if (is_array($self->_paymentProcessor)) {
         $payment = CRM_Core_Payment::singleton($self->_mode, $self->_paymentProcessor, $this);
         $error = $payment->checkConfig($self->_mode);
@@ -877,7 +854,7 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
         }
       }
 
-      $isZeroAmount = $skipPayementValidation = FALSE;
+      $isZeroAmount = $skipPaymentValidation = FALSE;
       if (CRM_Utils_Array::value('priceSetId', $fields)) {
         if (CRM_Utils_Array::value('amount', $fields) == 0) {
           $isZeroAmount = TRUE;
@@ -899,13 +876,13 @@ class CRM_Event_Form_Registration_Register extends CRM_Event_Form_Registration {
       }
 
       if ($isZeroAmount && !($self->_forcePayement && CRM_Utils_Array::value('additional_participants', $fields))) {
-        $skipPayementValidation = TRUE;
+        $skipPaymentValidation = TRUE;
       }
 
       // also return if paylater mode or zero fees for valid members
       if (CRM_Utils_Array::value('is_pay_later', $fields) ||
         CRM_Utils_Array::value('bypass_payment', $fields) ||
-        $skipPayementValidation ||
+        $skipPaymentValidation ||
         (!$self->_allowConfirmation && ($self->_requireApproval || $self->_allowWaitlist))
       ) {
         return empty($errors) ? TRUE : $errors;