Merge pull request #2348 from dlobo/CRM-13278
[civicrm-core.git] / CRM / Core / BAO / Email.php
index bd99c13940c3c6209dbdf242de7ba674386f89e5..d2e5c859d85d94790e57f32ce86e5cb29f118206 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.3                                                |
+ | CiviCRM version 4.4                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2013                                |
  +--------------------------------------------------------------------+
  */
 class CRM_Core_BAO_Email extends CRM_Core_DAO_Email {
 
-  /*
+  /**
    * Create email address - note that the create function calls 'add' but
    * has more business logic
    *
    * @param array $params input parameters
    */
   static function create($params) {
-    if (is_numeric(CRM_Utils_Array::value('is_primary', $params)) ||
-      // if id is set & is_primary isn't we can assume no change
-      empty($params['id'])
-    ) {
+    // if id is set & is_primary isn't we can assume no change
+    if (is_numeric(CRM_Utils_Array::value('is_primary', $params)) || empty($params['id'])) {
       CRM_Core_BAO_Block::handlePrimary($params, get_class());
     }
+
     $email = CRM_Core_BAO_Email::add($params);
 
     return $email;
@@ -76,14 +75,21 @@ class CRM_Core_BAO_Email extends CRM_Core_DAO_Email {
     $strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower';
     $email->email = $strtolower($email->email);
 
-    // since we're setting bulkmail for 1 of this contact's emails, first reset all their emails to is_bulkmail false
-    // (only 1 email address can have is_bulkmail = true)
+    /*
+    * since we're setting bulkmail for 1 of this contact's emails, first reset all their other emails to is_bulkmail false
+    *  We shouldn't not set the current email to false even though we
+    *  are about to reset it to avoid contaminating the changelog if logging is enabled
+    * (only 1 email address can have is_bulkmail = true)
+    */
     if ($email->is_bulkmail != 'null' && $params['contact_id'] && !self::isMultipleBulkMail()) {
       $sql = "
 UPDATE civicrm_email
 SET    is_bulkmail = 0
 WHERE  contact_id = {$params['contact_id']}
 ";
+    if($hook == 'edit'){
+      $sql .= " AND id <> {$params['id']}";
+    }
       CRM_Core_DAO::executeQuery($sql);
     }
 
@@ -92,6 +98,11 @@ WHERE  contact_id = {$params['contact_id']}
 
     $email->save();
 
+    if ($email->is_primary) {
+      // update the UF user email if that has changed
+      CRM_Core_BAO_UFMatch::updateUFName($email->contact_id);
+    }
+
     CRM_Utils_Hook::post($hook, 'Email', $email->id, $email);
     return $email;
   }