From d858668c47e56e55bfee6f9c753f6c634ce9983a Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Sat, 13 Aug 2022 14:56:06 -0400 Subject: [PATCH] Activity BAO - Decouple CiviCase logic from create function A big chunk of civicase code was jammed into the Activity create function. This moves it to the Case BAO. --- CRM/Activity/BAO/Activity.php | 27 +-------------------------- CRM/Case/BAO/Case.php | 35 ++++++++++++++++++++++++++++++++++- Civi/Core/HookInterface.php | 6 +++--- 3 files changed, 38 insertions(+), 30 deletions(-) diff --git a/CRM/Activity/BAO/Activity.php b/CRM/Activity/BAO/Activity.php index 965e91d291..1c80354a30 100644 --- a/CRM/Activity/BAO/Activity.php +++ b/CRM/Activity/BAO/Activity.php @@ -521,37 +521,12 @@ class CRM_Activity_BAO_Activity extends CRM_Activity_DAO_Activity { CRM_Contact_BAO_GroupContactCache::opportunisticCacheFlush(); - // if the subject contains a ‘[case #…]’ string, file that activity on the related case (CRM-5916) - $matches = []; - $subjectToMatch = $params['subject'] ?? ''; - if (preg_match('/\[case #([0-9a-h]{7})\]/', $subjectToMatch, $matches)) { - $key = CRM_Core_DAO::escapeString(CIVICRM_SITE_KEY); - $hash = $matches[1]; - $query = "SELECT id FROM civicrm_case WHERE SUBSTR(SHA1(CONCAT('$key', id)), 1, 7) = '" . CRM_Core_DAO::escapeString($hash) . "'"; - } - elseif (preg_match('/\[case #(\d+)\]/', $subjectToMatch, $matches)) { - $query = "SELECT id FROM civicrm_case WHERE id = '" . CRM_Core_DAO::escapeString($matches[1]) . "'"; - } - if (!empty($matches)) { - $caseParams = [ - 'activity_id' => $activity->id, - 'case_id' => CRM_Core_DAO::singleValueQuery($query), - ]; - if ($caseParams['case_id']) { - CRM_Case_BAO_Case::processCaseActivity($caseParams); - } - else { - self::logActivityAction($activity, "Case details for {$matches[1]} not found while recording an activity on case."); - } - } CRM_Utils_Hook::post($action, 'Activity', $activity->id, $activity); return $result; } /** - * Create an activity. - * - * @todo elaborate on what this does. + * Adds an entry to the log table about an activity * * @param CRM_Activity_DAO_Activity $activity * @param string $logMessage diff --git a/CRM/Case/BAO/Case.php b/CRM/Case/BAO/Case.php index b2a0db357b..5cf18562d8 100644 --- a/CRM/Case/BAO/Case.php +++ b/CRM/Case/BAO/Case.php @@ -19,7 +19,7 @@ /** * This class contains the functions for Case Management. */ -class CRM_Case_BAO_Case extends CRM_Case_DAO_Case { +class CRM_Case_BAO_Case extends CRM_Case_DAO_Case implements \Civi\Core\HookInterface { /** * Static field for all the case information that we can potentially export. @@ -58,6 +58,39 @@ class CRM_Case_BAO_Case extends CRM_Case_DAO_Case { return $result; } + /** + * @param \Civi\Core\Event\PostEvent $e + */ + public static function on_hook_civicrm_post(\Civi\Core\Event\PostEvent $e): void { + if ($e->entity === 'Activity' && in_array($e->action, ['create', 'edit'])) { + // If subject contains a ‘[case #…]’ string, file activity on the related case (CRM-5916) + /** @var CRM_Activity_DAO_Activity $activity */ + $activity = $e->object; + $matches = []; + $subjectToMatch = $activity->subject ?? ''; + if (preg_match('/\[case #([0-9a-h]{7})\]/', $subjectToMatch, $matches)) { + $key = CRM_Core_DAO::escapeString(CIVICRM_SITE_KEY); + $hash = $matches[1]; + $query = "SELECT id FROM civicrm_case WHERE SUBSTR(SHA1(CONCAT('$key', id)), 1, 7) = '" . CRM_Core_DAO::escapeString($hash) . "'"; + } + elseif (preg_match('/\[case #(\d+)\]/', $subjectToMatch, $matches)) { + $query = "SELECT id FROM civicrm_case WHERE id = '" . CRM_Core_DAO::escapeString($matches[1]) . "'"; + } + if (!empty($matches)) { + $caseParams = [ + 'activity_id' => $activity->id, + 'case_id' => CRM_Core_DAO::singleValueQuery($query), + ]; + if ($caseParams['case_id']) { + CRM_Case_BAO_Case::processCaseActivity($caseParams); + } + else { + CRM_Activity_BAO_Activity::logActivityAction($activity, "Case details for {$matches[1]} not found while recording an activity on case."); + } + } + } + } + /** * Takes an associative array and creates a case object. * diff --git a/Civi/Core/HookInterface.php b/Civi/Core/HookInterface.php index 24592b5035..3ef70d6bac 100644 --- a/Civi/Core/HookInterface.php +++ b/Civi/Core/HookInterface.php @@ -11,7 +11,7 @@ namespace Civi\Core; * * ``` * class CRM_Foo_BAO_Bar implements \Civi\Core\HookInterface { - * public function hook_civicrm_post($op, $objectName, $objectId, &$objectRef) { + * public static function hook_civicrm_post($op, $objectName, $objectId, &$objectRef) { * echo "Running hook_civicrm_post\n"; * } * } @@ -21,10 +21,10 @@ namespace Civi\Core; * * ``` * class CRM_Foo_BAO_Bar implements \Civi\Core\HookInterface { - * public function on_civi_api_authorize(AuthorizeEvent $e): void { + * public static function on_civi_api_authorize(AuthorizeEvent $e): void { * echo "Running civi.api.authorize\n"; * } - * public function on_hook_civicrm_post(GenericHookEvent $e): void { + * public static function on_hook_civicrm_post(PostEvent $e): void { * echo "Running hook_civicrm_post\n"; * } * } -- 2.25.1