From 921ad3b36967e8ecf9ad5296a2adadb50710e336 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Thu, 2 Dec 2021 23:51:22 -0500 Subject: [PATCH] PaymentProcessorType - Use standard delete function which calls hooks --- CRM/Financial/BAO/PaymentProcessorType.php | 43 ++++++++++++++-------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/CRM/Financial/BAO/PaymentProcessorType.php b/CRM/Financial/BAO/PaymentProcessorType.php index c0f49c2b10..796e5cfd37 100644 --- a/CRM/Financial/BAO/PaymentProcessorType.php +++ b/CRM/Financial/BAO/PaymentProcessorType.php @@ -14,7 +14,7 @@ * @package CRM * @copyright CiviCRM LLC https://civicrm.org/licensing */ -class CRM_Financial_BAO_PaymentProcessorType extends CRM_Financial_DAO_PaymentProcessorType { +class CRM_Financial_BAO_PaymentProcessorType extends CRM_Financial_DAO_PaymentProcessorType implements \Civi\Test\HookInterface { /** * Static holder for the default payment processor. @@ -153,29 +153,40 @@ class CRM_Financial_BAO_PaymentProcessorType extends CRM_Financial_DAO_PaymentPr * Delete payment processor. * * @param int $paymentProcessorTypeId - * ID of the processor to be deleted. - * + * @deprecated * @return bool|NULL */ public static function del($paymentProcessorTypeId) { - $query = " + try { + static::deleteRecord(['id' => $paymentProcessorTypeId]); + // This message is bad on so many levels + CRM_Core_Session::setStatus(ts('Selected Payment Processor type has been deleted.
'), '', 'success'); + return TRUE; + } + catch (CRM_Core_Exception $e) { + CRM_Core_Session::setStatus($e->getMessage(), ts('Deletion Error'), 'error'); + return NULL; + } + } + + /** + * 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') { + $query = " SELECT pp.id processor_id FROM civicrm_payment_processor pp, civicrm_payment_processor_type ppt WHERE pp.payment_processor_type_id = ppt.id AND ppt.id = %1"; - $params = [1 => [$paymentProcessorTypeId, 'Integer']]; - $dao = CRM_Core_DAO::executeQuery($query, $params); - - if ($dao->fetch()) { - CRM_Core_Session::setStatus(ts('There is a Payment Processor associated with selected Payment Processor type, hence it can not be deleted.'), ts('Deletion Error'), 'error'); - return NULL; - } + $params = [1 => [$event->id, 'Integer']]; + $dao = CRM_Core_DAO::executeQuery($query, $params); - $paymentProcessorType = new CRM_Financial_DAO_PaymentProcessorType(); - $paymentProcessorType->id = $paymentProcessorTypeId; - if ($paymentProcessorType->delete()) { - CRM_Core_Session::setStatus(ts('Selected Payment Processor type has been deleted.
'), '', 'success'); - return TRUE; + if ($dao->fetch()) { + throw new CRM_Core_Exception(ts('There is a Payment Processor associated with selected Payment Processor type, hence it can not be deleted.')); + } } } -- 2.25.1