dev/core#2046 Migrate BAO_Address::create towards standardisation
authoreileen <emcnaughton@wikimedia.org>
Thu, 1 Oct 2020 22:57:07 +0000 (11:57 +1300)
committereileen <emcnaughton@wikimedia.org>
Fri, 2 Oct 2020 02:03:55 +0000 (15:03 +1300)
Our standard expectation is that each BAO will have a create action that handles a single entity. For legacy
reasons Address.create has special handling for multiple addresses. This extracts that handling
into a separate function (legacyCreate) and updates the (very few) places currently calling create to
call that.

With this merged apis v3 & v4 can call Address.create  - currently v3 is hard coded to call add and
v4 bypasses the BAO altogher. The v4 api bypass means we are not managing is_primay integrity on v4 api calls

CRM/Contact/Form/Inline/Address.php
CRM/Core/BAO/Address.php
CRM/Core/BAO/Location.php
tests/phpunit/CRM/Core/BAO/AddressTest.php

index e33118c9133fea7b35abb60675251187026e2f1a..4170a36f0bd14c39a1aa58217f02591f98ce861c 100644 (file)
@@ -169,7 +169,7 @@ class CRM_Contact_Form_Inline_Address extends CRM_Contact_Form_Inline {
     }
 
     // save address changes
-    $address = CRM_Core_BAO_Address::create($params, TRUE);
+    $address = CRM_Core_BAO_Address::legacyCreate($params, TRUE);
 
     $this->log();
     $this->ajaxResponse['addressId'] = $address[0]->id;
index 329940adbdf9c9896b627cd0af9c222c769f5068..fdbf08fe6b383c75be73211fabcc80e88567faab 100644 (file)
@@ -29,69 +29,15 @@ class CRM_Core_BAO_Address extends CRM_Core_DAO_Address {
    *   True if you need to fix (format) address values.
    *                               before inserting in db
    *
-   * @return array|NULL
+   * @return array|NULL|self
    *   array of created address
    */
   public static function create(&$params, $fixAddress = TRUE) {
     if (!isset($params['address']) || !is_array($params['address'])) {
-      return NULL;
-    }
-    CRM_Core_BAO_Block::sortPrimaryFirst($params['address']);
-    $contactId = NULL;
-
-    $updateBlankLocInfo = CRM_Utils_Array::value('updateBlankLocInfo', $params, FALSE);
-    $contactId = $params['contact_id'];
-    //get all the addresses for this contact
-    $addresses = self::allAddress($contactId);
-
-    $isPrimary = $isBilling = TRUE;
-    $blocks = [];
-    foreach ($params['address'] as $key => $value) {
-      if (!is_array($value)) {
-        continue;
-      }
-
-      $addressExists = self::dataExists($value);
-      if (empty($value['id'])) {
-        if (!empty($addresses) && !empty($value['location_type_id']) && array_key_exists($value['location_type_id'], $addresses)) {
-          $value['id'] = $addresses[$value['location_type_id']];
-        }
-      }
-
-      // Note there could be cases when address info already exist ($value[id] is set) for a contact/entity
-      // BUT info is not present at this time, and therefore we should be really careful when deleting the block.
-      // $updateBlankLocInfo will help take appropriate decision. CRM-5969
-      if (isset($value['id']) && !$addressExists && $updateBlankLocInfo) {
-        //delete the existing record
-        CRM_Core_BAO_Block::blockDelete('Address', ['id' => $value['id']]);
-        continue;
-      }
-      elseif (!$addressExists) {
-        continue;
-      }
-
-      if ($isPrimary && !empty($value['is_primary'])) {
-        $isPrimary = FALSE;
-      }
-      else {
-        $value['is_primary'] = 0;
-      }
-
-      if ($isBilling && !empty($value['is_billing'])) {
-        $isBilling = FALSE;
-      }
-      else {
-        $value['is_billing'] = 0;
-      }
-
-      if (empty($value['manual_geo_code'])) {
-        $value['manual_geo_code'] = 0;
-      }
-      $value['contact_id'] = $contactId;
-      $blocks[] = self::add($value, $fixAddress);
+      return self::add($params, $fixAddress);
     }
-
-    return $blocks;
+    CRM_Core_Error::deprecatedFunctionWarning('Use legacyCreate if not doing a single crud action');
+    return self::legacyCreate($params, $fixAddress);
   }
 
   /**
@@ -1366,4 +1312,73 @@ SELECT is_primary,
     return TRUE;
   }
 
+  /**
+   * Create multiple addresses using legacy methodology.
+   *
+   * @param array $params
+   * @param bool $fixAddress
+   *
+   * @return array|null
+   */
+  public static function legacyCreate(array $params, bool $fixAddress) {
+    if (!isset($params['address']) || !is_array($params['address'])) {
+      return NULL;
+    }
+    CRM_Core_BAO_Block::sortPrimaryFirst($params['address']);
+    $contactId = NULL;
+
+    $updateBlankLocInfo = CRM_Utils_Array::value('updateBlankLocInfo', $params, FALSE);
+    $contactId = $params['contact_id'];
+    //get all the addresses for this contact
+    $addresses = self::allAddress($contactId);
+
+    $isPrimary = $isBilling = TRUE;
+    $blocks = [];
+    foreach ($params['address'] as $key => $value) {
+      if (!is_array($value)) {
+        continue;
+      }
+
+      $addressExists = self::dataExists($value);
+      if (empty($value['id'])) {
+        if (!empty($addresses) && !empty($value['location_type_id']) && array_key_exists($value['location_type_id'], $addresses)) {
+          $value['id'] = $addresses[$value['location_type_id']];
+        }
+      }
+
+      // Note there could be cases when address info already exist ($value[id] is set) for a contact/entity
+      // BUT info is not present at this time, and therefore we should be really careful when deleting the block.
+      // $updateBlankLocInfo will help take appropriate decision. CRM-5969
+      if (isset($value['id']) && !$addressExists && $updateBlankLocInfo) {
+        //delete the existing record
+        CRM_Core_BAO_Block::blockDelete('Address', ['id' => $value['id']]);
+        continue;
+      }
+      elseif (!$addressExists) {
+        continue;
+      }
+
+      if ($isPrimary && !empty($value['is_primary'])) {
+        $isPrimary = FALSE;
+      }
+      else {
+        $value['is_primary'] = 0;
+      }
+
+      if ($isBilling && !empty($value['is_billing'])) {
+        $isBilling = FALSE;
+      }
+      else {
+        $value['is_billing'] = 0;
+      }
+
+      if (empty($value['manual_geo_code'])) {
+        $value['manual_geo_code'] = 0;
+      }
+      $value['contact_id'] = $contactId;
+      $blocks[] = self::add($value, $fixAddress);
+    }
+    return $blocks;
+  }
+
 }
index 911862b8fc18a35506b49edc82edba44cb9907fd..fd1545efe9117f8d9b0917df1050dcd2f975a745 100644 (file)
@@ -45,11 +45,11 @@ class CRM_Core_BAO_Location extends CRM_Core_DAO {
 
     // create location blocks.
     foreach (self::$blocks as $block) {
-      if ($block != 'address') {
+      if ($block !== 'address') {
         $location[$block] = CRM_Core_BAO_Block::create($block, $params);
       }
-      else {
-        $location[$block] = CRM_Core_BAO_Address::create($params, $fixAddress);
+      elseif (is_array($params['address'] ?? NULL)) {
+        $location[$block] = CRM_Core_BAO_Address::legacyCreate($params, $fixAddress);
       }
     }
 
index 8e8ddcfe7701b8b34c311655aee9cfece7850028..707df1e1489e080b00fbb892ff7dfde5cb4b4c5c 100644 (file)
@@ -50,7 +50,7 @@ class CRM_Core_BAO_AddressTest extends CiviUnitTestCase {
 
     $fixAddress = TRUE;
 
-    CRM_Core_BAO_Address::create($params, $fixAddress);
+    CRM_Core_BAO_Address::legacyCreate($params, $fixAddress);
     $addressId = $this->assertDBNotNull('CRM_Core_DAO_Address', 'Oberoi Garden', 'id', 'street_address',
       'Database check for created address.'
     );
@@ -76,7 +76,7 @@ class CRM_Core_BAO_AddressTest extends CiviUnitTestCase {
     ];
     $params['contact_id'] = $contactId;
 
-    $block = CRM_Core_BAO_Address::create($params, $fixAddress);
+    $block = CRM_Core_BAO_Address::legacyCreate($params, $fixAddress);
 
     $this->assertDBNotNull('CRM_Core_DAO_Address', $contactId, 'id', 'contact_id',
       'Database check for updated address by contactId.'
@@ -253,7 +253,7 @@ class CRM_Core_BAO_AddressTest extends CiviUnitTestCase {
 
     $fixAddress = TRUE;
 
-    CRM_Core_BAO_Address::create($params, $fixAddress);
+    CRM_Core_BAO_Address::legacyCreate($params, $fixAddress);
 
     $addressId = $this->assertDBNotNull('CRM_Core_DAO_Address', $contactId, 'id', 'contact_id',
       'Database check for created address.'