MembershipStatus - Use standard delete function which calls hooks
authorColeman Watts <coleman@civicrm.org>
Fri, 3 Dec 2021 03:36:38 +0000 (22:36 -0500)
committerColeman Watts <coleman@civicrm.org>
Sat, 4 Dec 2021 13:49:50 +0000 (08:49 -0500)
CRM/Member/BAO/MembershipStatus.php

index 0f43afc73fffd6f97b2d720f3e14ac01250f465e..7fa4a1248aadb1e30d96df72a49a7d2774df4f9e 100644 (file)
@@ -14,7 +14,7 @@
  * @package CRM
  * @copyright CiviCRM LLC https://civicrm.org/licensing
  */
-class CRM_Member_BAO_MembershipStatus extends CRM_Member_DAO_MembershipStatus {
+class CRM_Member_BAO_MembershipStatus extends CRM_Member_DAO_MembershipStatus implements \Civi\Test\HookInterface {
 
   /**
    * Fetch object based on array of properties.
@@ -150,29 +150,33 @@ class CRM_Member_BAO_MembershipStatus extends CRM_Member_DAO_MembershipStatus {
    * Delete membership status.
    *
    * @param int $membershipStatusId
-   *
+   * @deprecated
    * @throws CRM_Core_Exception
    */
   public static function del($membershipStatusId) {
-    // Check if any membership records are assigned this membership status
-    $dependency = ['Membership'];
-    foreach ($dependency as $name) {
-      $baoString = 'CRM_Member_BAO_' . $name;
-      $dao = new $baoString();
-      $dao->status_id = $membershipStatusId;
-      if ($dao->find(TRUE)) {
-        throw new CRM_Core_Exception(ts('This membership status cannot be deleted. Memberships exist with this status.'));
+    static::deleteRecord(['id' => $membershipStatusId]);
+  }
+
+  /**
+   * 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') {
+      // Check if any membership records are assigned this membership status
+      $dependency = ['Membership'];
+      foreach ($dependency as $name) {
+        $baoString = 'CRM_Member_BAO_' . $name;
+        $dao = new $baoString();
+        $dao->status_id = $event->id;
+        if ($dao->find(TRUE)) {
+          throw new CRM_Core_Exception(ts('This membership status cannot be deleted. Memberships exist with this status.'));
+        }
       }
+      CRM_Utils_Weight::delWeight('CRM_Member_DAO_MembershipStatus', $event->id);
+      CRM_Member_PseudoConstant::flush('membershipStatus');
     }
-    CRM_Utils_Weight::delWeight('CRM_Member_DAO_MembershipStatus', $membershipStatusId);
-    // Delete from Membership Status table
-    $membershipStatus = new CRM_Member_DAO_MembershipStatus();
-    $membershipStatus->id = $membershipStatusId;
-    if (!$membershipStatus->find()) {
-      throw new CRM_Core_Exception(ts('Cannot delete membership status ' . $membershipStatusId));
-    }
-    $membershipStatus->delete();
-    CRM_Member_PseudoConstant::flush('membershipStatus');
   }
 
   /**