CaseType - Use standard delete function which calls hooks
authorColeman Watts <coleman@civicrm.org>
Thu, 2 Dec 2021 16:08:10 +0000 (11:08 -0500)
committerColeman Watts <coleman@civicrm.org>
Sat, 4 Dec 2021 13:49:50 +0000 (08:49 -0500)
CRM/Case/BAO/CaseType.php

index e6a2510c32fd647d4749754a97390c4a952ea940..3cc8291001dd405643589e525adf7d400c2b0b6b 100644 (file)
@@ -18,7 +18,7 @@
 /**
  * This class contains the functions for Case Type management.
  */
-class CRM_Case_BAO_CaseType extends CRM_Case_DAO_CaseType {
+class CRM_Case_BAO_CaseType extends CRM_Case_DAO_CaseType implements \Civi\Test\HookInterface {
 
   /**
    * Static field for all the case information that we can potentially export.
@@ -65,7 +65,6 @@ class CRM_Case_BAO_CaseType extends CRM_Case_DAO_CaseType {
 
     $caseTypeDAO->copyValues($params);
     $result = $caseTypeDAO->save();
-    CRM_Case_XMLRepository::singleton()->flush();
     return $result;
   }
 
@@ -407,13 +406,10 @@ class CRM_Case_BAO_CaseType extends CRM_Case_DAO_CaseType {
       $transaction->rollback();
       return $caseType;
     }
+    $transaction->commit();
 
     CRM_Utils_Hook::post($action, 'CaseType', $caseType->id, $case);
 
-    $transaction->commit();
-    CRM_Case_XMLRepository::singleton(TRUE);
-    CRM_Core_OptionGroup::flushAll();
-
     return $caseType;
   }
 
@@ -438,20 +434,40 @@ class CRM_Case_BAO_CaseType extends CRM_Case_DAO_CaseType {
   /**
    * @param int $caseTypeId
    *
+   * @deprecated
    * @throws CRM_Core_Exception
-   * @return mixed
+   * @return CRM_Case_DAO_CaseType
    */
   public static function del($caseTypeId) {
-    $caseType = new CRM_Case_DAO_CaseType();
-    $caseType->id = $caseTypeId;
-    $refCounts = $caseType->getReferenceCounts();
-    $total = array_sum(CRM_Utils_Array::collect('count', $refCounts));
-    if ($total) {
-      throw new CRM_Core_Exception(ts("You can not delete this case type -- it is assigned to %1 existing case record(s). If you do not want this case type to be used going forward, consider disabling it instead.", [1 => $total]));
+    return static::deleteRecord(['id' => $caseTypeId]);
+  }
+
+  /**
+   * 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) {
+    // Before deleting a caseType, check references
+    if ($event->action === 'delete') {
+      $caseType = new CRM_Case_DAO_CaseType();
+      $caseType->id = $event->id;
+      $refCounts = $caseType->getReferenceCounts();
+      $total = array_sum(CRM_Utils_Array::collect('count', $refCounts));
+      if (array_sum(CRM_Utils_Array::collect('count', $refCounts))) {
+        throw new CRM_Core_Exception(ts("You can not delete this case type -- it is assigned to %1 existing case record(s). If you do not want this case type to be used going forward, consider disabling it instead.", [1 => $total]));
+      }
     }
-    $result = $caseType->delete();
-    CRM_Case_XMLRepository::singleton(TRUE);
-    return $result;
+  }
+
+  /**
+   * Callback for hook_civicrm_post().
+   * @param \Civi\Core\Event\PostEvent $event
+   */
+  public static function self_hook_civicrm_post(\Civi\Core\Event\PostEvent $event) {
+    // When a caseType is saved or deleted, flush xml and optionGroup cache
+    CRM_Case_XMLRepository::singleton()->flush();
+    CRM_Core_OptionGroup::flushAll();
   }
 
   /**