Merge pull request #2362 from totten/4.4-sync-redirect
[civicrm-core.git] / CRM / Core / BAO / UFField.php
index fb779f7add740783f8e033796890ef8d1beb50c7..e4baabe332852abc74cb65d56b2b47572d5de886 100644 (file)
@@ -788,13 +788,15 @@ SELECT  id
    * http://issues.civicrm.org/jira/browse/CRM-5869
    *
    * @param string $key Field key - e.g. street_address-Primary, first_name
-   * @param $profileAddressFields
-   * @params array $profileAddressFields array of profile fields that relate to address fields
+   * @param array $profileAddressFields array of profile fields that relate to address fields
+   * @param array $profileFilter filter to apply to profile fields - expected usage is to only fill based on
+   * the bottom profile per CRM-13726
    */
-  static function assignAddressField($key, &$profileAddressFields) {
+  static function assignAddressField($key, &$profileAddressFields, $profileFilter) {
     $billing_id = CRM_Core_BAO_LocationType::getBilling();
     list($prefixName, $index) = CRM_Utils_System::explode('-', $key, 2);
 
+    $profileFields = civicrm_api3('uf_field', 'get', array_merge($profileFilter, array('is_active' => 1, 'return' => 'field_name')));
     //check for valid fields ( fields that are present in billing block )
     $validBillingFields = array(
       'first_name',
@@ -807,8 +809,15 @@ SELECT  id
       'postal_code',
       'country'
     );
+    $validProfileFields = array();
 
-    if (!in_array($prefixName, $validBillingFields)) {
+    foreach ($profileFields['values'] as $field) {
+      if(in_array($field['field_name'], $validBillingFields)) {
+        $validProfileFields[] = $field['field_name'];
+      }
+    }
+
+    if (!in_array($prefixName, $validProfileFields) ) {
       return;
     }