From: Coleman Watts Date: Fri, 3 Dec 2021 03:36:38 +0000 (-0500) Subject: MembershipStatus - Use standard delete function which calls hooks X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=23de8776c8a088c19efec7ea3ea95706265448ae;p=civicrm-core.git MembershipStatus - Use standard delete function which calls hooks --- 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'); } /**