Deprecate `CRM_Contact_BAO_Relationship::del()` in favour of
authorAidan Saunders <aidan.saunders@squiffle.uk>
Mon, 21 Nov 2022 16:40:11 +0000 (16:40 +0000)
committerAidan Saunders <aidan.saunders@squiffle.uk>
Mon, 21 Nov 2022 17:14:15 +0000 (17:14 +0000)
`deleteRecord()`

Move status message out of business-layer

CRM/Contact/BAO/Relationship.php
CRM/Contact/Form/Relationship.php
CRM/Contact/Page/View/Relationship.php

index 40315dd33464593545d8e2b2ce5c61eee18293e3..4b6b86605f4a442d78224fd27346d06685085902 100644 (file)
@@ -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
+        );
+      }
+    }
   }
 
   /**
index 4119d117252ca3ec482555ccbc80c612dce49ab9..41c11d221ad8bbd0d3ed46b4adad5cc2caf0e7cd 100644 (file)
@@ -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'];
index faa48927c74415d135c8901a2500710aedc6715d..0255f4137c50f708dfd13cbb7fb9cd275b2cdbc7 100644 (file)
@@ -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');
   }
 
   /**