From 23de8776c8a088c19efec7ea3ea95706265448ae Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Thu, 2 Dec 2021 22:36:38 -0500 Subject: [PATCH] MembershipStatus - Use standard delete function which calls hooks --- CRM/Member/BAO/MembershipStatus.php | 42 ++++++++++++++++------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/CRM/Member/BAO/MembershipStatus.php b/CRM/Member/BAO/MembershipStatus.php index 0f43afc73f..7fa4a1248a 100644 --- a/CRM/Member/BAO/MembershipStatus.php +++ b/CRM/Member/BAO/MembershipStatus.php @@ -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'); } /** -- 2.25.1