additional fixes for CRM-16877
authorjitendrapurohit <jitendra.purohit@webaccessglobal.com>
Thu, 3 Sep 2015 13:18:46 +0000 (18:48 +0530)
committerjitendrapurohit <jitendra.purohit@webaccessglobal.com>
Thu, 3 Sep 2015 13:18:46 +0000 (18:48 +0530)
CRM/Contact/BAO/Contact.php
CRM/Contact/Form/Contact.php
CRM/Contact/Form/Inline/Email.php
CRM/Contact/Form/Inline/IM.php
CRM/Contact/Form/Inline/OpenID.php
CRM/Contact/Form/Inline/Phone.php
CRM/Core/BAO/Block.php

index e292a376ccd1cc2d57aa6f002fef2cb418385ae1..c3658b59c95b39350a35f7915bbc83e7a37efa15 100644 (file)
@@ -2105,11 +2105,6 @@ ORDER BY civicrm_email.is_primary DESC";
         }
 
         if ($contactID) {
-          //CRM-16877 -- include id in params to edit `block` fields
-          if ($blockName != 'address' && !empty($entityId[$loc]['id']) && isset($data[$blockName][$loc])) {
-            $entityId = CRM_Core_BAO_Block::getBlockIds($blockName, $contactID, NULL, TRUE);
-            $data[$blockName][$loc]['id'] = $entityId[$loc]['id'];
-          }
           //get the primary location type
           if ($locTypeId == $primaryLocationType) {
             $data[$blockName][$loc]['is_primary'] = 1;
index a78af9be64ad5a8504f75603951416c5bb313d92..b0d5294eaa2fbc5c441fba45bc178716a1067a9a 100644 (file)
@@ -988,11 +988,14 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
       $parseStatusMsg = self::parseAddressStatusMsg($parseResult);
     }
 
-    $blocks = array('email', 'phone', 'im', 'openid');
+    $blocks = array('address', 'email', 'phone', 'im', 'openid');
     foreach ($blocks as $block) {
-      foreach ($this->_preEditValues[$block] as $count => $value) {
-        if (!empty($value['id'])) {
-          $params[$block][$count]['id'] = $value['id'];
+      if (!empty($this->_preEditValues[$block]) && is_array($this->_preEditValues[$block])) {
+        foreach ($this->_preEditValues[$block] as $count => $value) {
+          if (!empty($value['id'])) {
+            $params[$block][$count]['id'] = $value['id'];
+            $params[$block]['isIdSet'] = TRUE;
+          }
         }
       }
     }
index 8d11adc63f8fcad0ec9e7534d4ea00071ecf5dfe..9fb51d4bccf265a302aa5e0f04a232729bb79513 100644 (file)
@@ -182,6 +182,7 @@ class CRM_Contact_Form_Inline_Email extends CRM_Contact_Form_Inline {
     // Process / save emails
     $params['contact_id'] = $this->_contactId;
     $params['updateBlankLocInfo'] = TRUE;
+    $params['email']['isIdSet'] = TRUE;
     foreach ($this->_emails as $count => $value) {
       if (!empty($value['id']) && isset($params['email'][$count])) {
         $params['email'][$count]['id'] = $value['id'];
index 11bd83ce8ee0e27d6c7315a52725685767c8e0cf..46cbcf0ccee3f9a49e27167276e29e5f6cf52a1b 100644 (file)
@@ -164,6 +164,7 @@ class CRM_Contact_Form_Inline_IM extends CRM_Contact_Form_Inline {
     // Process / save IMs
     $params['contact_id'] = $this->_contactId;
     $params['updateBlankLocInfo'] = TRUE;
+    $params['im']['isIdSet'] = TRUE;
     foreach ($this->_ims as $count => $value) {
       if (!empty($value['id']) && isset($params['im'][$count])) {
         $params['im'][$count]['id'] = $value['id'];
index 016783896937eb0ae19d36f76e24bfe7b20224bb..f3920812f70a791e25137606ee8a2727505a331b 100644 (file)
@@ -164,6 +164,7 @@ class CRM_Contact_Form_Inline_OpenID extends CRM_Contact_Form_Inline {
     // Process / save openID
     $params['contact_id'] = $this->_contactId;
     $params['updateBlankLocInfo'] = TRUE;
+    $params['openid']['isIdSet'] = TRUE;
     foreach ($this->_openids as $count => $value) {
       if (!empty($value['id']) && isset($params['openid'][$count])) {
         $params['openid'][$count]['id'] = $value['id'];
index 24a3dcabf27cacaab4f079739f8e6c9317c32358..a2e8c1d29291ac40d3b734f2e1394c7b9e5b2b37 100644 (file)
@@ -165,6 +165,7 @@ class CRM_Contact_Form_Inline_Phone extends CRM_Contact_Form_Inline {
     // Process / save phones
     $params['contact_id'] = $this->_contactId;
     $params['updateBlankLocInfo'] = TRUE;
+    $params['phone']['isIdSet'] = TRUE;
     foreach ($this->_phones as $count => $value) {
       if (!empty($value['id']) && isset($params['phone'][$count])) {
         $params['phone'][$count]['id'] = $value['id'];
index 8bde67027b47daa1a278b0e8f49b2006d6678cc8..ff53dd91e39885f2c93d698b54bc1071c6c05c75 100644 (file)
@@ -230,6 +230,8 @@ class CRM_Core_BAO_Block {
     $name = ucfirst($blockName);
     $isPrimary = $isBilling = TRUE;
     $entityElements = $blocks = array();
+    $resetPrimaryId = NULL;
+    $primaryId = FALSE;
 
     if ($entity) {
       $entityElements = array(
@@ -242,21 +244,77 @@ class CRM_Core_BAO_Block {
     }
 
     $updateBlankLocInfo = CRM_Utils_Array::value('updateBlankLocInfo', $params, FALSE);
+    $isIdSet = CRM_Utils_Array::value('isIdSet', $params[$blockName], FALSE);
 
     //get existing block ids.
     $blockIds = self::getBlockIds($blockName, $contactId, $entityElements);
+    foreach ($params[$blockName] as $count => $value) {
+      $blockId = CRM_Utils_Array::value('id', $value);
+      if ($blockId) {
+        if (is_array($blockIds) && array_key_exists($blockId, $blockIds)) {
+          unset($blockIds[$blockId]);
+        }
+        else {
+          unset($value['id']);
+        }
+      }
+      //lets allow to update primary w/ more cleanly.
+      if (!$resetPrimaryId && !empty($value['is_primary'])) {
+        $primaryId = TRUE;
+        if (is_array($blockIds)) {
+          foreach ($blockIds as $blockId => $blockValue) {
+            if (!empty($blockValue['is_primary'])) {
+              $resetPrimaryId = $blockId;
+              break;
+            }
+          }
+        }
+        if ($resetPrimaryId) {
+          $baoString = 'CRM_Core_BAO_' . $blockName;
+          $block = new $baoString();
+          $block->selectAdd();
+          $block->selectAdd("id, is_primary");
+          $block->id = $resetPrimaryId;
+          if ($block->find(TRUE)) {
+            $block->is_primary = FALSE;
+            $block->save();
+          }
+          $block->free();
+        }
+      }
+    }
 
     foreach ($params[$blockName] as $count => $value) {
       if (!is_array($value)) {
         continue;
       }
-      // if in some case (eg. email used in Online Conribution Page) id is not set
+      // if in some cases (eg. email used in Online Conribution Page, Profiles, etc.) id is not set
       // lets try to add using the previous method to avoid any false creation of existing data.
       foreach ($blockIds as $blockId => $blockValue) {
-        if (empty($value['id']) && $blockValue['locationTypeId'] == CRM_Utils_Array::value('location_type_id', $value)) {
-          //assigned id as first come first serve basis
-          $value['id'] = $blockValue['id'];
-          break;
+        if (empty($value['id']) && $blockValue['locationTypeId'] == CRM_Utils_Array::value('location_type_id', $value) && !$isIdSet) {
+          $valueId = FALSE;
+          if ($blockName == 'phone') {
+            $phoneTypeBlockValue = CRM_Utils_Array::value('phoneTypeId', $blockValue);
+            if ($phoneTypeBlockValue == CRM_Utils_Array::value('phone_type_id', $value)) {
+              $valueId = TRUE;
+            }
+          }
+          elseif ($blockName == 'im') {
+            $providerBlockValue = CRM_Utils_Array::value('providerId', $blockValue);
+            if (!empty($value['provider_id']) && $providerBlockValue == $value['provider_id']) {
+              $valueId = TRUE;
+            }
+          }
+          else {
+            $valueId = TRUE;
+          }
+          if ($valueId) {
+            $value['id'] = $blockValue['id'];
+            if (!$primaryId && !empty($blockValue['is_primary'])) {
+              $value['is_primary'] = $blockValue['is_primary'];
+            }
+            break;
+          }
         }
       }
       $dataExists = self::dataExists(self::$requiredBlockFields[$blockName], $value);
@@ -271,27 +329,22 @@ class CRM_Core_BAO_Block {
       elseif (!$dataExists) {
         continue;
       }
-
       $contactFields = array(
         'contact_id' => $contactId,
         'location_type_id' => CRM_Utils_Array::value('location_type_id', $value),
       );
 
+      $contactFields['is_primary'] = 0;
       if ($isPrimary && !empty($value['is_primary'])) {
         $contactFields['is_primary'] = $value['is_primary'];
         $isPrimary = FALSE;
       }
-      else {
-        $contactFields['is_primary'] = 0;
-      }
 
+      $contactFields['is_billing'] = 0;
       if ($isBilling && !empty($value['is_billing'])) {
         $contactFields['is_billing'] = $value['is_billing'];
         $isBilling = FALSE;
       }
-      else {
-        $contactFields['is_billing'] = 0;
-      }
 
       $blockFields = array_merge($value, $contactFields);
       $baoString = 'CRM_Core_BAO_' . $name;