From 11a1236cf36ca49443af1d644bdb42fd58ae180c Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Thu, 2 Dec 2021 11:08:10 -0500 Subject: [PATCH] CaseType - Use standard delete function which calls hooks --- CRM/Case/BAO/CaseType.php | 48 ++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/CRM/Case/BAO/CaseType.php b/CRM/Case/BAO/CaseType.php index e6a2510c32..3cc8291001 100644 --- a/CRM/Case/BAO/CaseType.php +++ b/CRM/Case/BAO/CaseType.php @@ -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(); } /** -- 2.25.1