From: jitendrapurohit Date: Fri, 21 Aug 2015 12:27:29 +0000 (+0530) Subject: CRM-16877 -- Include ID when editing 'block' fields (+ allow deletion of block values... X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=85c882c565ea940ee878c9cbc11dae3e2657c628;p=civicrm-core.git CRM-16877 -- Include ID when editing 'block' fields (+ allow deletion of block values via Profiles) --- diff --git a/CRM/Contact/BAO/Contact.php b/CRM/Contact/BAO/Contact.php index 6cfb4af7c9..e292a376cc 100644 --- a/CRM/Contact/BAO/Contact.php +++ b/CRM/Contact/BAO/Contact.php @@ -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; diff --git a/CRM/Contact/Form/Contact.php b/CRM/Contact/Form/Contact.php index 2283c49779..a78af9be64 100644 --- a/CRM/Contact/Form/Contact.php +++ b/CRM/Contact/Form/Contact.php @@ -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; diff --git a/CRM/Contact/Form/Inline/Email.php b/CRM/Contact/Form/Inline/Email.php index 4385946184..8d11adc63f 100644 --- a/CRM/Contact/Form/Inline/Email.php +++ b/CRM/Contact/Form/Inline/Email.php @@ -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 diff --git a/CRM/Contact/Form/Inline/IM.php b/CRM/Contact/Form/Inline/IM.php index db17a2876b..11bd83ce8e 100644 --- a/CRM/Contact/Form/Inline/IM.php +++ b/CRM/Contact/Form/Inline/IM.php @@ -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(); diff --git a/CRM/Contact/Form/Inline/OpenID.php b/CRM/Contact/Form/Inline/OpenID.php index 81987882e0..0167838969 100644 --- a/CRM/Contact/Form/Inline/OpenID.php +++ b/CRM/Contact/Form/Inline/OpenID.php @@ -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(); diff --git a/CRM/Contact/Form/Inline/Phone.php b/CRM/Contact/Form/Inline/Phone.php index 57fcc78c0e..24a3dcabf2 100644 --- a/CRM/Contact/Form/Inline/Phone.php +++ b/CRM/Contact/Form/Inline/Phone.php @@ -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(); diff --git a/CRM/Core/BAO/Address.php b/CRM/Core/BAO/Address.php index d824bec5d1..c6a691f2c6 100644 --- a/CRM/Core/BAO/Address.php +++ b/CRM/Core/BAO/Address.php @@ -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)]; } } diff --git a/CRM/Core/BAO/Block.php b/CRM/Core/BAO/Block.php index a393568fb9..320f484e09 100644 --- a/CRM/Core/BAO/Block.php +++ b/CRM/Core/BAO/Block.php @@ -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; }