$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);
}
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;
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
$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)];
}
}
}
$name = ucfirst($blockName);
- $contactId = NULL;
$isPrimary = $isBilling = TRUE;
$entityElements = $blocks = array();
$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;
$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;
}