PaymentProcessorType - Use standard delete function which calls hooks
authorColeman Watts <coleman@civicrm.org>
Fri, 3 Dec 2021 04:51:22 +0000 (23:51 -0500)
committerColeman Watts <coleman@civicrm.org>
Sat, 4 Dec 2021 20:38:25 +0000 (15:38 -0500)
CRM/Financial/BAO/PaymentProcessorType.php

index c0f49c2b10bfe17b862267e198c496bdba872347..796e5cfd37e0e3fb237bd74e1c9868befb89e69f 100644 (file)
@@ -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.<br/>'), '', '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.<br/>'), '', '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.'));
+      }
     }
   }