/** * @todo What does this do? If it's still useful, figure out where it should live and what it should be named. * * @deprecated deprecated since version 2.2.3 */ function civicrm_replace_contact_formatted($contactId, &$params, &$fields) { //$contact = civcrm_get_contact(array('contact_id' => $contactId)); $delContact = array('contact_id' => $contactId); civicrm_contact_delete($delContact); $cid = CRM_Contact_BAO_Contact::createProfileContact($params, $fields, NULL, NULL, NULL, $params['contact_type']); return civicrm_create_success($cid); }
/** * Test civicrm_contact_search_count() */ public function testContactSearchEmail() { $params = array('email' => '*****@*****.**', 'contact_type' => 'Individual', 'location_type_id' => 1); $contact =& civicrm_contact_create($params); $this->assertEquals(0, $contact['is_error'], "In line " . __LINE__ . " error message: " . $contact['error_message']); $this->assertEquals(1, $contact['contact_id'], "In line " . __LINE__); $params = array('email' => 'man2'); $result = civicrm_contact_search($params); $this->assertEquals(1, $result[1]['contact_id']); $this->assertEquals('*****@*****.**', $result[1]['email']); // delete the contact civicrm_contact_delete($contact); }
public function postProcess() { $formValues = $this->exportValues(); // user can't choose to move cases without activities (CRM-3778) if ($formValues['move_rel_table_cases'] == '1' && array_key_exists('move_rel_table_activities', $formValues)) { $formValues['move_rel_table_activities'] = '1'; } // reset all selected contact ids from session // when we came from search context, CRM-3526 $session =& CRM_Core_Session::singleton(); if ($session->get('selectedSearchContactIds')) { $session->resetScope('selectedSearchContactIds'); } $relTables =& CRM_Dedupe_Merger::relTables(); $moveTables = $locBlocks = array(); foreach ($formValues as $key => $value) { if ($value == $this->_qfZeroBug) { $value = '0'; } if ((in_array(substr($key, 5), CRM_Dedupe_Merger::$validFields) or substr($key, 0, 12) == 'move_custom_') and $value != null) { $submitted[substr($key, 5)] = $value; } elseif (substr($key, 0, 14) == 'move_location_' and $value != null) { $locField = explode('_', $key); $fieldName = $locField[2]; $fieldCount = $locField[3]; $operation = CRM_Utils_Array::value('operation', $formValues['location'][$fieldName][$fieldCount]); // default operation is overwrite. if (!$operation) { $operation = 2; } $locBlocks[$fieldName][$fieldCount]['operation'] = $operation; $locBlocks[$fieldName][$fieldCount]['locTypeId'] = CRM_Utils_Array::value('locTypeId', $formValues['location'][$fieldName][$fieldCount]); } elseif (substr($key, 0, 15) == 'move_rel_table_' and $value == '1') { $moveTables = array_merge($moveTables, $relTables[substr($key, 5)]['tables']); } } // process location blocks. if (!empty($locBlocks)) { $locComponent = array('email' => 'Email', 'phone' => 'Phone', 'im' => 'IM', 'openid' => 'OpenID', 'address' => 'Address'); require_once 'CRM/Contact/BAO/Contact.php'; $primaryBlockIds = CRM_Contact_BAO_Contact::getLocBlockIds($this->_cid, array('is_primary' => 1)); $billingBlockIds = CRM_Contact_BAO_Contact::getLocBlockIds($this->_cid, array('is_billing' => 1)); foreach ($locBlocks as $name => $block) { if (!is_array($block) || CRM_Utils_System::isNull($block)) { continue; } $daoName = $locComponent[$name]; $primaryDAOId = array_key_exists($name, $primaryBlockIds) ? array_pop($primaryBlockIds[$name]) : null; $billingDAOId = array_key_exists($name, $billingBlockIds) ? array_pop($billingBlockIds[$name]) : null; foreach ($block as $blkCount => $values) { $locTypeId = CRM_Utils_Array::value('locTypeId', $values, 1); $operation = CRM_Utils_Array::value('operation', $values, 2); $updateBlockId = CRM_Utils_Array::value($blkCount, $this->_locBlockIds['other'][$name]); // keep 1-1 mapping for address - loc type. $idKey = $blkCount; if ($name == 'address') { $idKey = $locTypeId; } $deleteBlockId = CRM_Utils_Array::value($idKey, $this->_locBlockIds['main'][$name]); if (!$updateBlockId) { continue; } require_once "CRM/Core/DAO/{$daoName}.php"; eval("\$updateDAO =& new CRM_Core_DAO_{$daoName}();"); $updateDAO->id = $updateBlockId; $updateDAO->contact_id = $this->_cid; $updateDAO->location_type_id = $locTypeId; // contact having primary block. if ($primaryDAOId) { $updateDAO->is_primary = 0; } if ($billingDAOId) { $updateDAO->is_billing = 0; } // overwrite - need to delete block from main contact. if ($deleteBlockId && $operation == 2) { eval("\$deleteDAO =& new CRM_Core_DAO_{$daoName}();"); $deleteDAO->id = $deleteBlockId; $deleteDAO->find(true); // since we overwrite primary block. if ($primaryDAOId && $primaryDAOId == $deleteDAO->id) { $updateDAO->is_primary = 1; } if ($billingDAOId && $billingDAOId == $deleteDAO->id) { $updateDAO->is_billing = 1; } $deleteDAO->delete(); $deleteDAO->free(); } $updateDAO->update(); $updateDAO->free(); } } } // FIXME: fix gender, prefix and postfix, so they're edible by createProfileContact() $names['gender'] = array('newName' => 'gender_id', 'groupName' => 'gender'); $names['individual_prefix'] = array('newName' => 'prefix_id', 'groupName' => 'individual_prefix'); $names['individual_suffix'] = array('newName' => 'suffix_id', 'groupName' => 'individual_suffix'); $names['addressee'] = array('newName' => 'addressee_id', 'groupName' => 'addressee'); $names['email_greeting'] = array('newName' => 'email_greeting_id', 'groupName' => 'email_greeting'); $names['postal_greeting'] = array('newName' => 'postal_greeting_id', 'groupName' => 'postal_greeting'); CRM_Core_OptionGroup::lookupValues($submitted, $names, true); // FIXME: fix custom fields so they're edible by createProfileContact() $cgTree =& CRM_Core_BAO_CustomGroup::getTree($this->_contactType, $this, null, -1); foreach ($cgTree as $key => $group) { if (!isset($group['fields'])) { continue; } foreach ($group['fields'] as $fid => $field) { $cFields[$fid]['attributes'] = $field; } } if (!isset($submitted)) { $submitted = array(); } foreach ($submitted as $key => $value) { if (substr($key, 0, 7) == 'custom_') { $fid = (int) substr($key, 7); $htmlType = $cFields[$fid]['attributes']['html_type']; switch ($htmlType) { case 'File': $customFiles[] = $fid; unset($submitted["custom_{$fid}"]); break; case 'Select Country': case 'Select State/Province': $submitted[$key] = CRM_Core_BAO_CustomField::getDisplayValue($value, $fid, $cFields); break; case 'CheckBox': case 'AdvMulti-Select': case 'Multi-Select': case 'Multi-Select Country': case 'Multi-Select State/Province': // Merge values from both contacts for multivalue fields, CRM-4385 // get the existing custom values from db. require_once 'CRM/Core/BAO/CustomValueTable.php'; $customParams = array('entityID' => $this->_cid, $key => true); $customfieldValues = CRM_Core_BAO_CustomValueTable::getValues($customParams); if (CRM_Utils_array::value($key, $customfieldValues)) { $existingValue = explode(CRM_Core_DAO::VALUE_SEPARATOR, $customfieldValues[$key]); if (is_array($existingValue) && !empty($existingValue)) { $mergeValue = $submmtedCustomValue = array(); if ($value) { $submmtedCustomValue = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value); } //hack to remove null and duplicate values from array. foreach (array_merge($submmtedCustomValue, $existingValue) as $k => $v) { if ($v != '' && !in_array($v, $mergeValue)) { $mergeValue[] = $v; } } //keep state and country as array format. //for checkbox and m-select format w/ VALUE_SEPERATOR if (in_array($htmlType, array('CheckBox', 'Multi-Select', 'AdvMulti-Select'))) { $submitted[$key] = CRM_Core_BAO_CustomOption::VALUE_SEPERATOR . implode(CRM_Core_BAO_CustomOption::VALUE_SEPERATOR, $mergeValue) . CRM_Core_BAO_CustomOption::VALUE_SEPERATOR; } else { $submitted[$key] = $mergeValue; } } } else { if (in_array($htmlType, array('Multi-Select Country', 'Multi-Select State/Province'))) { //we require submitted values should be in array format if ($value) { $mergeValueArray = explode(CRM_Core_DAO::VALUE_SEPARATOR, $value); //hack to remove null values from array. $mergeValue = array(); foreach ($mergeValueArray as $k => $v) { if ($v != '') { $mergeValue[] = $v; } } $submitted[$key] = $mergeValue; } } } break; default: break; } } } // handle the related tables if (isset($moveTables)) { CRM_Dedupe_Merger::moveContactBelongings($this->_cid, $this->_oid, $moveTables); } // move file custom fields // FIXME: move this someplace else (one of the BAOs) after discussing // where to, and whether CRM_Core_BAO_File::delete() shouldn't actually, // like, delete a file... require_once 'CRM/Core/BAO/File.php'; require_once 'CRM/Core/DAO/CustomField.php'; require_once 'CRM/Core/DAO/CustomGroup.php'; require_once 'CRM/Core/DAO/EntityFile.php'; require_once 'CRM/Core/Config.php'; if (!isset($customFiles)) { $customFiles = array(); } foreach ($customFiles as $customId) { list($tableName, $columnName, $groupID) = CRM_Core_BAO_CustomField::getTableColumnGroup($customId); // get the contact_id -> file_id mapping $fileIds = array(); $sql = "SELECT entity_id, {$columnName} AS file_id FROM {$tableName} WHERE entity_id IN ({$this->_cid}, {$this->_oid})"; $dao =& CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray); while ($dao->fetch()) { $fileIds[$dao->entity_id] = $dao->file_id; } $dao->free(); // delete the main contact's file CRM_Core_BAO_File::delete($fileIds[$this->_cid], $this->_cid, $customId); // move the other contact's file to main contact $sql = "UPDATE {$tableName} SET {$columnName} = {$fileIds[$this->_oid]} WHERE entity_id = {$this->_cid}"; CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray); $sql = "UPDATE civicrm_entity_file SET entity_id = {$this->_cid} WHERE entity_table = '{$tableName}' AND file_id = {$fileIds[$this->_oid]}"; CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray); } // move other's belongings and delete the other contact CRM_Dedupe_Merger::moveContactBelongings($this->_cid, $this->_oid); $otherParams = array('contact_id' => $this->_oid); if (CRM_Core_Permission::check('delete contacts')) { civicrm_contact_delete($otherParams); } else { CRM_Core_Session::setStatus(ts('Do not have sufficient permission to delete duplicate contact.')); } if (isset($submitted)) { $submitted['contact_id'] = $this->_cid; CRM_Contact_BAO_Contact::createProfileContact($submitted, CRM_Core_DAO::$_nullArray, $this->_cid); } CRM_Core_Session::setStatus(ts('The contacts have been merged.')); $url = CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$this->_cid}"); CRM_Utils_System::redirect($url); }
function contactDelete($contactID) { require_once 'api/v2/Contact.php'; $params['contact_id'] = $contactID; $result = civicrm_contact_delete($params); if (CRM_Utils_Array::value('is_error', $result)) { var_dump($result); throw new Exception('Could not delete contact: ' . $result['error_message']); } return; }