CRM-16877 -- Include ID when editing 'block' fields (+ allow deletion of block values...
authorjitendrapurohit <jitendra.purohit@webaccessglobal.com>
Fri, 21 Aug 2015 12:27:29 +0000 (17:57 +0530)
committerjitendrapurohit <jitendra.purohit@webaccessglobal.com>
Fri, 21 Aug 2015 12:27:29 +0000 (17:57 +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/Address.php
CRM/Core/BAO/Block.php

index 6cfb4af7c989edc940fa4276400217f4747f8cea..e292a376ccd1cc2d57aa6f002fef2cb418385ae1 100644 (file)
@@ -1864,6 +1864,14 @@ ORDER BY civicrm_email.is_primary DESC";
       $params['uf_group_id'] = $ufGroupId;
     }
 
+    // If a user has logged in, or accessed via a checksum
+    // Then deliberately 'blanking' a value in the profile should remove it from their record
+    $session = CRM_Core_Session::singleton();
+    $params['updateBlankLocInfo'] = TRUE;
+    if (($session->get('authSrc') & (CRM_Core_Permission::AUTH_SRC_CHECKSUM + CRM_Core_Permission::AUTH_SRC_LOGIN)) == 0) {
+      $params['updateBlankLocInfo'] = FALSE;
+    }
+
     if ($contactID) {
       $editHook = TRUE;
       CRM_Utils_Hook::pre('edit', 'Profile', $contactID, $params);
@@ -2097,6 +2105,11 @@ 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 2283c49779381ffe954bbaf4889525ab556c3f57..a78af9be64ad5a8504f75603951416c5bb313d92 100644 (file)
@@ -988,6 +988,15 @@ class CRM_Contact_Form_Contact extends CRM_Core_Form {
       $parseStatusMsg = self::parseAddressStatusMsg($parseResult);
     }
 
+    $blocks = array('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'];
+        }
+      }
+    }
+
     // Allow un-setting of location info, CRM-5969
     $params['updateBlankLocInfo'] = TRUE;
 
index 4385946184a73bc6950a6d16d73fa366d41188e9..8d11adc63f8fcad0ec9e7534d4ea00071ecf5dfe 100644 (file)
@@ -182,6 +182,11 @@ class CRM_Contact_Form_Inline_Email extends CRM_Contact_Form_Inline {
     // Process / save emails
     $params['contact_id'] = $this->_contactId;
     $params['updateBlankLocInfo'] = TRUE;
+    foreach ($this->_emails as $count => $value) {
+      if (!empty($value['id']) && isset($params['email'][$count])) {
+        $params['email'][$count]['id'] = $value['id'];
+      }
+    }
     CRM_Core_BAO_Block::create('email', $params);
 
     // If contact has no name, set primary email as display name
index db17a2876bc4dfb821b116b67737df929b49cd91..11bd83ce8ee0e27d6c7315a52725685767c8e0cf 100644 (file)
@@ -164,6 +164,11 @@ class CRM_Contact_Form_Inline_IM extends CRM_Contact_Form_Inline {
     // Process / save IMs
     $params['contact_id'] = $this->_contactId;
     $params['updateBlankLocInfo'] = TRUE;
+    foreach ($this->_ims as $count => $value) {
+      if (!empty($value['id']) && isset($params['im'][$count])) {
+        $params['im'][$count]['id'] = $value['id'];
+      }
+    }
     CRM_Core_BAO_Block::create('im', $params);
 
     $this->log();
index 81987882e0822cc7838875656aad49ae250d0c29..016783896937eb0ae19d36f76e24bfe7b20224bb 100644 (file)
@@ -164,6 +164,11 @@ class CRM_Contact_Form_Inline_OpenID extends CRM_Contact_Form_Inline {
     // Process / save openID
     $params['contact_id'] = $this->_contactId;
     $params['updateBlankLocInfo'] = TRUE;
+    foreach ($this->_openids as $count => $value) {
+      if (!empty($value['id']) && isset($params['openid'][$count])) {
+        $params['openid'][$count]['id'] = $value['id'];
+      }
+    }
     CRM_Core_BAO_Block::create('openid', $params);
 
     $this->log();
index 57fcc78c0e7d2a052e23b122bc876c2b696c6996..24a3dcabf27cacaab4f079739f8e6c9317c32358 100644 (file)
@@ -165,6 +165,11 @@ class CRM_Contact_Form_Inline_Phone extends CRM_Contact_Form_Inline {
     // Process / save phones
     $params['contact_id'] = $this->_contactId;
     $params['updateBlankLocInfo'] = TRUE;
+    foreach ($this->_phones as $count => $value) {
+      if (!empty($value['id']) && isset($params['phone'][$count])) {
+        $params['phone'][$count]['id'] = $value['id'];
+      }
+    }
     CRM_Core_BAO_Block::create('phone', $params);
 
     $this->log();
index d824bec5d1ef30f8ed9f37b94d5b7c68056962bc..c6a691f2c6b4a86c26b2add6ff9c8396e6adab89 100644 (file)
@@ -64,7 +64,7 @@ class CRM_Core_BAO_Address extends CRM_Core_DAO_Address {
     if (!$entity) {
       $contactId = $params['contact_id'];
       //get all the addresses for this contact
-      $addresses = self::allAddress($contactId, $updateBlankLocInfo);
+      $addresses = self::allAddress($contactId);
     }
     else {
       // get all address from location block
@@ -84,15 +84,8 @@ class CRM_Core_BAO_Address extends CRM_Core_DAO_Address {
 
       $addressExists = self::dataExists($value);
       if (empty($value['id'])) {
-        if ($updateBlankLocInfo) {
-          if ((!empty($addresses) || !$addressExists) && array_key_exists($key, $addresses)) {
-            $value['id'] = $addresses[$key];
-          }
-        }
-        else {
-          if (!empty($addresses) && array_key_exists(CRM_Utils_Array::value('location_type_id', $value), $addresses)) {
-            $value['id'] = $addresses[CRM_Utils_Array::value('location_type_id', $value)];
-          }
+        if (!empty($addresses) && array_key_exists(CRM_Utils_Array::value('location_type_id', $value), $addresses)) {
+          $value['id'] = $addresses[CRM_Utils_Array::value('location_type_id', $value)];
         }
       }
 
index a393568fb980dc3eb2e7ae7dc2188d46e2c19a98..320f484e0975c6fa4b3431f427d74aaab342ece9 100644 (file)
@@ -228,7 +228,6 @@ class CRM_Core_BAO_Block {
     }
 
     $name = ucfirst($blockName);
-    $contactId = NULL;
     $isPrimary = $isBilling = TRUE;
     $entityElements = $blocks = array();
 
@@ -244,119 +243,40 @@ class CRM_Core_BAO_Block {
 
     $updateBlankLocInfo = CRM_Utils_Array::value('updateBlankLocInfo', $params, FALSE);
 
-    //get existsing block ids.
-    $blockIds = self::getBlockIds($blockName, $contactId, $entityElements, $updateBlankLocInfo);
-
-    if (!$updateBlankLocInfo) {
-      $resetPrimaryId = NULL;
-      $primaryId = FALSE;
-      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();
-          }
-        }
-      }
-    }
+    //get existing block ids.
+    $blockIds = self::getBlockIds($blockName, $contactId, $entityElements);
 
     foreach ($params[$blockName] as $count => $value) {
       if (!is_array($value)) {
         continue;
       }
-      $contactFields = array(
-        'contact_id' => $contactId,
-        'location_type_id' => CRM_Utils_Array::value('location_type_id', $value),
-      );
-
-      //check for update
-      if (empty($value['id']) &&
-        is_array($blockIds) && !empty($blockIds)
-      ) {
-        foreach ($blockIds as $blockId => $blockValue) {
-          if ($updateBlankLocInfo) {
-            if (!empty($blockIds[$count])) {
-              $value['id'] = $blockIds[$count]['id'];
-              unset($blockIds[$count]);
-            }
-          }
-          else {
-            if ($blockValue['locationTypeId'] == CRM_Utils_Array::value('location_type_id', $value)) {
-              $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 ($providerBlockValue == $value['provider_id']) {
-                  $valueId = TRUE;
-                }
-              }
-              else {
-                $valueId = TRUE;
-              }
-
-              if ($valueId) {
-                //assigned id as first come first serve basis
-                $value['id'] = $blockValue['id'];
-                if (!$primaryId && !empty($blockValue['is_primary'])) {
-                  $value['is_primary'] = $blockValue['is_primary'];
-                }
-                unset($blockIds[$blockId]);
-                break;
-              }
-            }
-          }
+      // if in some case (eg. email used in Online Conribution Page) 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;
         }
       }
-
-      $dataExits = self::dataExists(self::$requiredBlockFields[$blockName], $value);
-
+      $dataExists = self::dataExists(self::$requiredBlockFields[$blockName], $value);
       // Note there could be cases when block 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 (!empty($value['id']) && !$dataExits && $updateBlankLocInfo) {
+      if (!empty($value['id']) && !$dataExists && $updateBlankLocInfo) {
         //delete the existing record
         self::blockDelete($blockName, array('id' => $value['id']));
         continue;
       }
-      elseif (!$dataExits) {
+      elseif (!$dataExists) {
         continue;
       }
 
+      $contactFields = array(
+        'contact_id' => $contactId,
+        'location_type_id' => CRM_Utils_Array::value('location_type_id', $value),
+      );
+
       if ($isPrimary && !empty($value['is_primary'])) {
         $contactFields['is_primary'] = $value['is_primary'];
         $isPrimary = FALSE;
@@ -378,16 +298,6 @@ class CRM_Core_BAO_Block {
       $blocks[] = $baoString::add($blockFields);
     }
 
-    // we need to delete blocks that were deleted during update
-    if ($updateBlankLocInfo && !empty($blockIds)) {
-      foreach ($blockIds as $deleteBlock) {
-        if (empty($deleteBlock['id'])) {
-          continue;
-        }
-        self::blockDelete($blockName, array('id' => $deleteBlock['id']));
-      }
-    }
-
     return $blocks;
   }