Skip to content

Commit

Permalink
Merge pull request #16824 from eileenmcnaughton/cont_restore
Browse files Browse the repository at this point in the history
Deprecate calling contactTrashRestore function
  • Loading branch information
colemanw authored Mar 17, 2020
2 parents 7035e06 + 844bdf3 commit 6ae069e
Showing 1 changed file with 52 additions and 20 deletions.
72 changes: 52 additions & 20 deletions CRM/Contact/BAO/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,36 @@ protected static function getLocationEntityForKey($fieldName) {
return 'address';
}

/**
* Soft delete a contact.
*
* Call this via the api, not directly.
*
* @param \CRM_Contact_BAO_Contact $contact
*
* @return bool
* @throws \CRM_Core_Exception
*/
protected static function contactTrash($contact): bool {
$updateParams = [
'id' => $contact->id,
'is_deleted' => 1,
];
CRM_Utils_Hook::pre('update', $contact->contact_type, $contact->id, $updateParams);

$params = [1 => [$contact->id, 'Integer']];
$query = 'DELETE FROM civicrm_uf_match WHERE contact_id = %1';
CRM_Core_DAO::executeQuery($query, $params);

$contact->copyValues($updateParams);
$contact->save();
CRM_Core_BAO_Log::register($contact->id, 'civicrm_contact', $contact->id);

CRM_Utils_Hook::post('update', $contact->contact_type, $contact->id, $contact);

return TRUE;
}

/**
* Create last viewed link to recently updated contact.
*
Expand Down Expand Up @@ -966,7 +996,13 @@ public static function deleteContact($id, $restore = FALSE, $skipUndelete = FALS

$contactType = $contact->contact_type;
if ($restore) {
return self::contactTrashRestore($contact, TRUE);
// @todo deprecate calling contactDelete with the intention to restore.
$updateParams = [
'id' => $contact->id,
'is_deleted' => FALSE,
];
self::create($updateParams);
return TRUE;
}

// start a new transaction
Expand Down Expand Up @@ -1011,7 +1047,7 @@ public static function deleteContact($id, $restore = FALSE, $skipUndelete = FALS
$contact->delete();
}
else {
self::contactTrashRestore($contact);
self::contactTrash($contact);
}
// currently we only clear employer cache.
// we are now deleting inherited membership if any.
Expand Down Expand Up @@ -1203,29 +1239,25 @@ public static function processImage() {
* True to set the is_delete = 1 else false to restore deleted contact,
* i.e. is_delete = 0
*
* @deprecated
*
* @return bool
* @throws \CRM_Core_Exception
*/
public static function contactTrashRestore($contact, $restore = FALSE) {
$updateParams = [
'id' => $contact->id,
'is_deleted' => $restore ? 0 : 1,
];
CRM_Core_Error::deprecatedFunctionWarning('Use the api');

CRM_Utils_Hook::pre('update', $contact->contact_type, $contact->id, $updateParams);

$params = [1 => [$contact->id, 'Integer']];
if (!$restore) {
$query = "DELETE FROM civicrm_uf_match WHERE contact_id = %1";
CRM_Core_DAO::executeQuery($query, $params);
if ($restore) {
CRM_Core_Error::deprecatedFunctionWarning('Use contact.create to restore - this does nothing much');
// @todo deprecate calling contactDelete with the intention to restore.
$updateParams = [
'id' => $contact->id,
'is_deleted' => FALSE,
];
self::create($updateParams);
return TRUE;
}

$contact->copyValues($updateParams);
$contact->save();
CRM_Core_BAO_Log::register($contact->id, 'civicrm_contact', $contact->id);

CRM_Utils_Hook::post('update', $contact->contact_type, $contact->id, $contact);

return TRUE;
return self::contactTrash($contact);
}

/**
Expand Down

0 comments on commit 6ae069e

Please sign in to comment.