Merge pull request #14793 from eileenmcnaughton/update_test
[civicrm-core.git] / CRM / Core / BAO / Block.php
index f462b7ffca97e434b1136ff8cd0342d04882a133..048614246464dc0ff891b1727f215314eb0775ef 100644 (file)
@@ -36,13 +36,14 @@ class CRM_Core_BAO_Block {
 
   /**
    * Fields that are required for a valid block.
+   * @var array
    */
-  static $requiredBlockFields = array(
-    'email' => array('email'),
-    'phone' => array('phone'),
-    'im' => array('name'),
-    'openid' => array('openid'),
-  );
+  public static $requiredBlockFields = [
+    'email' => ['email'],
+    'phone' => ['phone'],
+    'im' => ['name'],
+    'openid' => ['openid'],
+  ];
 
   /**
    * Given the list of params in the params array, fetch the object
@@ -63,7 +64,7 @@ class CRM_Core_BAO_Block {
     $BAOString = 'CRM_Core_BAO_' . $blockName;
     $block = new $BAOString();
 
-    $blocks = array();
+    $blocks = [];
     if (!isset($params['entity_table'])) {
       $block->contact_id = $params['contact_id'];
       if (!$block->contact_id) {
@@ -108,7 +109,7 @@ class CRM_Core_BAO_Block {
     $block->find();
 
     $count = 1;
-    $blocks = array();
+    $blocks = [];
     while ($block->fetch()) {
       CRM_Core_DAO::storeValues($block, $blocks[$count]);
       //unset is_primary after first block. Due to some bug in earlier version
@@ -178,7 +179,7 @@ class CRM_Core_BAO_Block {
    *
    */
   public static function getBlockIds($blockName, $contactId = NULL, $entityElements = NULL, $updateBlankLocInfo = FALSE) {
-    $allBlocks = array();
+    $allBlocks = [];
 
     $name = ucfirst($blockName);
     if ($blockName == 'im') {
@@ -226,15 +227,15 @@ class CRM_Core_BAO_Block {
 
     $name = ucfirst($blockName);
     $isPrimary = $isBilling = TRUE;
-    $entityElements = $blocks = array();
+    $entityElements = $blocks = [];
     $resetPrimaryId = NULL;
     $primaryId = FALSE;
 
     if ($entity) {
-      $entityElements = array(
+      $entityElements = [
         'entity_table' => $params['entity_table'],
         'entity_id' => $params['entity_id'],
-      );
+      ];
     }
     else {
       $contactId = $params['contact_id'];
@@ -276,7 +277,6 @@ class CRM_Core_BAO_Block {
             $block->is_primary = FALSE;
             $block->save();
           }
-          $block->free();
         }
       }
     }
@@ -320,16 +320,16 @@ class CRM_Core_BAO_Block {
       // $updateBlankLocInfo will help take appropriate decision. CRM-5969
       if (!empty($value['id']) && !$dataExists && $updateBlankLocInfo) {
         //delete the existing record
-        self::blockDelete($blockName, array('id' => $value['id']));
+        self::blockDelete($blockName, ['id' => $value['id']]);
         continue;
       }
       elseif (!$dataExists) {
         continue;
       }
-      $contactFields = array(
+      $contactFields = [
         'contact_id' => $contactId,
         'location_type_id' => CRM_Utils_Array::value('location_type_id', $value),
-      );
+      ];
 
       $contactFields['is_primary'] = 0;
       if ($isPrimary && !empty($value['is_primary'])) {
@@ -344,8 +344,18 @@ class CRM_Core_BAO_Block {
       }
 
       $blockFields = array_merge($value, $contactFields);
-      $baoString = 'CRM_Core_BAO_' . $name;
-      $blocks[] = $baoString::add($blockFields);
+      if ($name === 'Email') {
+        // @todo ideally all would call the api which is our main tested function,
+        // and towards that call the create rather than add which is preferred by the
+        // api. In order to be careful with change only email is swapped over here because it
+        // is specifically tested in testImportParserWithUpdateWithContactID
+        // and the primary handling is otherwise bypassed on importing an email update.
+        $blocks[] = CRM_Core_BAO_Email::create($blockFields);
+      }
+      else {
+        $baoString = 'CRM_Core_BAO_' . $name;
+        $blocks[] = $baoString::add($blockFields);
+      }
     }
 
     return $blocks;
@@ -422,11 +432,11 @@ class CRM_Core_BAO_Block {
     // if is_primary = 1
     if (!empty($params['is_primary'])) {
       $sql = "UPDATE $table SET is_primary = 0 WHERE contact_id = %1";
-      $sqlParams = array(1 => array($contactId, 'Integer'));
+      $sqlParams = [1 => [$contactId, 'Integer']];
       // we don't want to create unnecessary entries in the log_ tables so exclude the one we are working on
       if (!empty($params['id'])) {
         $sql .= " AND id <> %2";
-        $sqlParams[2] = array($params['id'], 'Integer');
+        $sqlParams[2] = [$params['id'], 'Integer'];
       }
       CRM_Core_DAO::executeQuery($sql, $sqlParams);
       return;