CRM-13179 use api function to get billing defaults in a sharable way
authoreileen <eileen@fuzion.co.nz>
Fri, 23 Aug 2013 00:32:20 +0000 (12:32 +1200)
committereileen <eileen@fuzion.co.nz>
Fri, 23 Aug 2013 00:32:20 +0000 (12:32 +1200)
CRM/Contribute/Form/Contribution/Main.php
CRM/Core/Form.php

index f28cccee90e60c8ebfff64f9782f7750e5e55576..3c497b9254a00415dd543ea5108adacad7d83726 100644 (file)
@@ -188,33 +188,8 @@ class CRM_Contribute_Form_Contribution_Main extends CRM_Contribute_Form_Contribu
         }
         $fields[$name] = 1;
       }
-
-      $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 (!empty($this->_defaults[$name])) {
-          $this->_defaults['billing_' . $name] = $this->_defaults[$name];
-        }
-      }
+      $billingDefaults = $this->getProfileDefaults('Billing', $contactID);
+      $this->_defaults = array_merge($this->_defaults, $billingDefaults);
     }
 
     //set custom field defaults set by admin if value is not set
index 9baa88a8d4457b4db02f697b657b49afab2ffd41..fe80bfc03fb12d873ff9f441fde3ff116d6b4040 100644 (file)
@@ -1434,5 +1434,25 @@ class CRM_Core_Form extends HTML_QuickForm_Page {
       $this->addAutoSelector($profiles);
     }
   }
+
+  /**
+   * Set default values on form for given contact (or no contact defaults)
+   * @param mixed $profile_id (can be id, or profile name)
+   * @param integer $contactID
+   */
+  function getProfileDefaults($profile_id = 'Billing', $contactID = NULL) {
+    try{
+      $defaults = civicrm_api3('profile', 'getsingle', array(
+        'profile_id' => (array) $profile_id,
+        'contact_id' => $contactID,
+      ));
+      return $defaults;
+    }
+    catch (Exception $e) {
+      echo $e->getMessage();
+      // the try catch block gives us silent failure -not 100% sure this is a good idea
+      // as silent failures are often worse than noisy ones
+    }
+  }
 }