/** * Delete a contact and all its associated records. * * @param int $id * Id of the contact to delete. * @param bool $restore * Whether to actually restore, not delete. * @param bool $skipUndelete * Whether to force contact delete or not. * * @return bool * Was contact deleted? */ public static function deleteContact($id, $restore = FALSE, $skipUndelete = FALSE) { if (!$id) { return FALSE; } // If trash is disabled in system settings then we always skip if (!CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME, 'contact_undelete', NULL, 1)) { $skipUndelete = TRUE; } // make sure we have edit permission for this contact // before we delete if ($skipUndelete && !CRM_Core_Permission::check('delete contacts') || $restore && !CRM_Core_Permission::check('access deleted contacts')) { return FALSE; } // CRM-12929 // Restrict contact to be delete if contact has financial trxns $error = NULL; if ($skipUndelete && CRM_Financial_BAO_FinancialItem::checkContactPresent(array($id), $error)) { return FALSE; } // make sure this contact_id does not have any membership types $membershipTypeID = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $id, 'id', 'member_of_contact_id'); if ($membershipTypeID) { return FALSE; } $contact = new CRM_Contact_DAO_Contact(); $contact->id = $id; if (!$contact->find(TRUE)) { return FALSE; } $contactType = $contact->contact_type; // currently we only clear employer cache. // we are now deleting inherited membership if any. if ($contact->contact_type == 'Organization') { $action = $restore ? CRM_Core_Action::ENABLE : CRM_Core_Action::DISABLE; $relationshipDtls = CRM_Contact_BAO_Relationship::getRelationship($id); if (!empty($relationshipDtls)) { foreach ($relationshipDtls as $rId => $details) { CRM_Contact_BAO_Relationship::disableEnableRelationship($rId, $action); } } CRM_Contact_BAO_Contact_Utils::clearAllEmployee($id); } if ($restore) { return self::contactTrashRestore($contact, TRUE); } // start a new transaction $transaction = new CRM_Core_Transaction(); if ($skipUndelete) { CRM_Utils_Hook::pre('delete', $contactType, $id, CRM_Core_DAO::$_nullArray); //delete billing address if exists. CRM_Contribute_BAO_Contribution::deleteAddress(NULL, $id); // delete the log entries since we dont have triggers enabled as yet $logDAO = new CRM_Core_DAO_Log(); $logDAO->entity_table = 'civicrm_contact'; $logDAO->entity_id = $id; $logDAO->delete(); // delete contact participants CRM-12155 CRM_Event_BAO_Participant::deleteContactParticipant($id); // delete contact contributions CRM-12155 CRM_Contribute_BAO_Contribution::deleteContactContribution($id); // do activity cleanup, CRM-5604 CRM_Activity_BAO_Activity::cleanupActivity($id); // delete all notes related to contact CRM_Core_BAO_Note::cleanContactNotes($id); // delete cases related to contact $contactCases = CRM_Case_BAO_Case::retrieveCaseIdsByContactId($id); if (!empty($contactCases)) { foreach ($contactCases as $caseId) { //check if case is associate with other contact or not. $caseContactId = CRM_Case_BAO_Case::getCaseClients($caseId); if (count($caseContactId) <= 1) { CRM_Case_BAO_Case::deleteCase($caseId); } } } $contact->delete(); } else { self::contactTrashRestore($contact); } //delete the contact id from recently view CRM_Utils_Recent::delContact($id); // Update the group contact cache if ($restore) { CRM_Contact_BAO_GroupContactCache::remove(); } else { CRM_Contact_BAO_GroupContactCache::removeContact($id); } // delete any dupe cache entry CRM_Core_BAO_PrevNextCache::deleteItem($id); $transaction->commit(); if ($skipUndelete) { CRM_Utils_Hook::post('delete', $contactType, $contact->id, $contact); } // also reset the DB_DO global array so we can reuse the memory // http://issues.civicrm.org/jira/browse/CRM-4387 CRM_Core_DAO::freeResult(); return TRUE; }
/** * Test case for retrieve( ). * * Test with all values. */ public function testRetrieve() { //take the common contact params $params = $this->contactParams(); $params['note'] = 'test note'; //create the contact with given params. $contact = CRM_Contact_BAO_Contact::create($params); //Now check $contact is object of contact DAO.. $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object'); $contactId = $contact->id; //create the organization contact with the given params. $orgParams = array('organization_name' => 'Test Organization ' . substr(sha1(rand()), 0, 4), 'contact_type' => 'Organization'); $orgContact = CRM_Contact_BAO_Contact::add($orgParams); $this->assertInstanceOf('CRM_Contact_DAO_Contact', $orgContact, 'Check for created object'); //create employee of relationship. CRM_Contact_BAO_Contact_Utils::createCurrentEmployerRelationship($contactId, $orgContact->id); //retrieve the contact values from database. $values = array(); $searchParams = array('contact_id' => $contactId); $retrieveContact = CRM_Contact_BAO_Contact::retrieve($searchParams, $values); //Now check $retrieveContact is object of contact DAO.. $this->assertInstanceOf('CRM_Contact_DAO_Contact', $retrieveContact, 'Check for retrieve object'); //Now check the ids. $this->assertEquals($contactId, $retrieveContact->id, 'Check for contact id'); //Now check values retrieve from database with params. $this->assertEquals($params['first_name'], $values['first_name'], 'Check for first name creation.'); $this->assertEquals($params['last_name'], $values['last_name'], 'Check for last name creation.'); $this->assertEquals($params['contact_type'], $values['contact_type'], 'Check for contact type creation.'); //Now check values of address $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['address']), CRM_Utils_Array::value('1', $values['address'])); //Now check values of email $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['email']), CRM_Utils_Array::value('1', $values['email'])); //Now check values of phone $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['phone']), CRM_Utils_Array::value('1', $values['phone'])); //Now check values of mobile $this->assertAttributesEquals(CRM_Utils_Array::value('2', $params['phone']), CRM_Utils_Array::value('2', $values['phone'])); //Now check values of openid $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['openid']), CRM_Utils_Array::value('1', $values['openid'])); //Now check values of im $this->assertAttributesEquals(CRM_Utils_Array::value('1', $params['im']), CRM_Utils_Array::value('1', $values['im'])); //Now check values of Note Count. $this->assertEquals(1, $values['noteTotalCount'], 'Check for total note count'); foreach ($values['note'] as $key => $val) { $retrieveNote = CRM_Utils_Array::value('note', $val); //check the note value $this->assertEquals($params['note'], $retrieveNote, 'Check for note'); } //Now check values of Relationship Count. $this->assertEquals(1, $values['relationship']['totalCount'], 'Check for total relationship count'); foreach ($values['relationship']['data'] as $key => $val) { //Now check values of Relationship organization. $this->assertEquals($orgContact->id, $val['contact_id_b'], 'Check for organization'); //Now check values of Relationship type. $this->assertEquals('Employee of', $val['relation'], 'Check for relationship type'); //delete the organization. $this->contactDelete(CRM_Utils_Array::value('contact_id_b', $val)); } //delete all notes related to contact CRM_Core_BAO_Note::cleanContactNotes($contactId); //cleanup DB by deleting the contact $this->contactDelete($contactId); $this->quickCleanup(array('civicrm_contact')); }
/** * Delete a contact and all its associated records * * @param int $id id of the contact to delete * @param bool $restore whether to actually restore, not delete * @param bool $skipUndelete whether to force contact delete or not * * @return boolean true if contact deleted, false otherwise * @access public * @static */ static function deleteContact($id, $restore = FALSE, $skipUndelete = FALSE) { if (!$id) { return FALSE; } // make sure we have edit permission for this contact // before we delete if ($skipUndelete && !CRM_Core_Permission::check('delete contacts') || $restore && !CRM_Core_Permission::check('access deleted contacts')) { return FALSE; } // make sure this contact_id does not have any membership types $membershipTypeID = CRM_Core_DAO::getFieldValue('CRM_Member_DAO_MembershipType', $id, 'id', 'member_of_contact_id'); if ($membershipTypeID) { return FALSE; } $contact = new CRM_Contact_DAO_Contact(); $contact->id = $id; if (!$contact->find(TRUE)) { return FALSE; } $contactType = $contact->contact_type; $action = $restore ? 'restore' : 'delete'; CRM_Utils_Hook::pre($action, $contactType, $id, CRM_Core_DAO::$_nullArray); if ($restore) { self::contactTrashRestore($contact, TRUE); CRM_Utils_Hook::post($action, $contactType, $contact->id, $contact); return TRUE; } // currently we only clear employer cache. // we are not deleting inherited membership if any. if ($contact->contact_type == 'Organization') { CRM_Contact_BAO_Contact_Utils::clearAllEmployee($id); } // start a new transaction $transaction = new CRM_Core_Transaction(); $config = CRM_Core_Config::singleton(); if ($skipUndelete or !$config->contactUndelete) { //delete billing address if exists. CRM_Contribute_BAO_Contribution::deleteAddress(NULL, $id); // delete the log entries since we dont have triggers enabled as yet $logDAO = new CRM_Core_DAO_Log(); $logDAO->entity_table = 'civicrm_contact'; $logDAO->entity_id = $id; $logDAO->delete(); // do activity cleanup, CRM-5604 CRM_Activity_BAO_Activity::cleanupActivity($id); // delete all notes related to contact CRM_Core_BAO_Note::cleanContactNotes($id); $contact->delete(); } else { self::contactTrashRestore($contact); } //delete the contact id from recently view CRM_Utils_Recent::delContact($id); // reset the group contact cache for this group CRM_Contact_BAO_GroupContactCache::remove(); // delete any dupe cache entry CRM_Core_BAO_PrevNextCache::deleteItem($id); $transaction->commit(); CRM_Utils_Hook::post('delete', $contactType, $contact->id, $contact); // also reset the DB_DO global array so we can reuse the memory // http://issues.civicrm.org/jira/browse/CRM-4387 CRM_Core_DAO::freeResult(); return TRUE; }