Merge pull request #15833 from yashodha/participant_edit
[civicrm-core.git] / CRM / Core / BAO / Block.php
index 780d00acdade23e0710a08796498ebecbe696440..6c37470377d71e9d0844175dbe1fa5c709565ae6 100644 (file)
@@ -3,7 +3,7 @@
  +--------------------------------------------------------------------+
  | CiviCRM version 5                                                  |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2019                                |
+ | Copyright CiviCRM LLC (c) 2004-2020                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -28,7 +28,7 @@
 /**
  *
  * @package CRM
- * @copyright CiviCRM LLC (c) 2004-2019
+ * @copyright CiviCRM LLC (c) 2004-2020
  *
  * Add static functions to include some common functionality used across location sub object BAO classes.
  */
@@ -36,8 +36,9 @@ class CRM_Core_BAO_Block {
 
   /**
    * Fields that are required for a valid block.
+   * @var array
    */
-  static $requiredBlockFields = [
+  public static $requiredBlockFields = [
     'email' => ['email'],
     'phone' => ['phone'],
     'im' => ['name'],
@@ -276,7 +277,6 @@ class CRM_Core_BAO_Block {
             $block->is_primary = FALSE;
             $block->save();
           }
-          $block->free();
         }
       }
     }
@@ -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;