From 10d38ff0392b26d78e75572c8e3966bc98262587 Mon Sep 17 00:00:00 2001 From: Aidan Saunders Date: Mon, 21 Nov 2022 16:40:11 +0000 Subject: [PATCH] Deprecate `CRM_Contact_BAO_Relationship::del()` in favour of `deleteRecord()` Move status message out of business-layer --- CRM/Contact/BAO/Relationship.php | 65 +++++++++++++++----------- CRM/Contact/Form/Relationship.php | 1 + CRM/Contact/Page/View/Relationship.php | 2 + 3 files changed, 42 insertions(+), 26 deletions(-) diff --git a/CRM/Contact/BAO/Relationship.php b/CRM/Contact/BAO/Relationship.php index 40315dd334..4b6b86605f 100644 --- a/CRM/Contact/BAO/Relationship.php +++ b/CRM/Contact/BAO/Relationship.php @@ -12,7 +12,7 @@ /** * Class CRM_Contact_BAO_Relationship. */ -class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship { +class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship implements \Civi\Core\HookInterface { /** * Various constants to indicate different type of relationships. @@ -623,37 +623,50 @@ class CRM_Contact_BAO_Relationship extends CRM_Contact_DAO_Relationship { * @return CRM_Contact_DAO_Relationship * * @throws \CRM_Core_Exception + * + * @deprecated */ public static function del($id) { - // delete from relationship table - CRM_Utils_Hook::pre('delete', 'Relationship', $id); - - $relationship = self::clearCurrentEmployer($id, CRM_Core_Action::DELETE); - $relationship->delete(); - if (CRM_Core_Permission::access('CiviMember')) { - // create $params array which isrequired to delete memberships - // of the related contacts. - $params = [ - 'relationship_type_id' => "{$relationship->relationship_type_id}_a_b", - 'contact_check' => [$relationship->contact_id_b => 1], - ]; + return static::deleteRecord(['id' => $id]); + } - $ids = []; - // calling relatedMemberships to delete the memberships of - // related contacts. - self::relatedMemberships($relationship->contact_id_a, - $params, - $ids, - CRM_Core_Action::DELETE, - FALSE - ); + /** + * Callback for hook_civicrm_pre(). + * @param \Civi\Core\Event\PreEvent $event + * @throws CRM_Core_Exception + */ + public static function self_hook_civicrm_pre(\Civi\Core\Event\PreEvent $event) { + if ($event->action === 'delete' && $event->id) { + self::clearCurrentEmployer($event->id, CRM_Core_Action::DELETE); } + } - CRM_Core_Session::setStatus(ts('Selected relationship has been deleted successfully.'), ts('Record Deleted'), 'success'); - - CRM_Utils_Hook::post('delete', 'Relationship', $id, $relationship); + /** + * Callback for hook_civicrm_post(). + * @param \Civi\Core\Event\PostEvent $event + * @throws CRM_Core_Exception + */ + public static function self_hook_civicrm_post(\Civi\Core\Event\PostEvent $event) { + if ($event->action === 'delete' && $event->id) { + if (CRM_Core_Permission::access('CiviMember')) { + // create $params array which isrequired to delete memberships + // of the related contacts. + $params = [ + 'relationship_type_id' => "{$event->object->relationship_type_id}_a_b", + 'contact_check' => [$event->object->contact_id_b => 1], + ]; - return $relationship; + $ids = []; + // calling relatedMemberships to delete the memberships of + // related contacts. + self::relatedMemberships($event->object->contact_id_a, + $params, + $ids, + CRM_Core_Action::DELETE, + FALSE + ); + } + } } /** diff --git a/CRM/Contact/Form/Relationship.php b/CRM/Contact/Form/Relationship.php index 4119d11725..41c11d221a 100644 --- a/CRM/Contact/Form/Relationship.php +++ b/CRM/Contact/Form/Relationship.php @@ -509,6 +509,7 @@ class CRM_Contact_Form_Relationship extends CRM_Core_Form { */ private function deleteAction($id) { CRM_Contact_BAO_Relationship::del($id); + CRM_Core_Session::setStatus(ts('Selected relationship has been deleted successfully.'), ts('Record Deleted'), 'success'); // reload all blocks to reflect this change on the user interface. $this->ajaxResponse['reloadBlocks'] = ['#crm-contactinfo-content']; diff --git a/CRM/Contact/Page/View/Relationship.php b/CRM/Contact/Page/View/Relationship.php index faa48927c7..0255f4137c 100644 --- a/CRM/Contact/Page/View/Relationship.php +++ b/CRM/Contact/Page/View/Relationship.php @@ -181,6 +181,7 @@ class CRM_Contact_Page_View_Relationship extends CRM_Core_Page { // delete relationship CRM_Contact_BAO_Relationship::del($this->getEntityId()); + CRM_Core_Session::setStatus(ts('Selected relationship has been deleted successfully.'), ts('Record Deleted'), 'success'); CRM_Utils_System::redirect($url); } @@ -239,6 +240,7 @@ class CRM_Contact_Page_View_Relationship extends CRM_Core_Page { public function delete() { // calls a function to delete relationship CRM_Contact_BAO_Relationship::del($this->getEntityId()); + CRM_Core_Session::setStatus(ts('Selected relationship has been deleted successfully.'), ts('Record Deleted'), 'success'); } /** -- 2.25.1