From f145179e2046eff60bb1c0c8c5b953da8847720a Mon Sep 17 00:00:00 2001 From: colemanw Date: Wed, 6 Sep 2023 10:41:11 -0400 Subject: [PATCH] RecurringEntity - Cleanup godawful form code Before - Form code is terrible After - Form code still deeply flawed but a bit easier to read This form code belongs in a BAO where it can have an api and proper tests but this at least cleans it up a bit to make it legible. --- CRM/Core/BAO/RecurringEntity.php | 16 ------ CRM/Core/Form/RecurringEntity.php | 72 ++++++++------------------- CRM/Event/Form/ManageEvent/Repeat.php | 5 +- 3 files changed, 23 insertions(+), 70 deletions(-) diff --git a/CRM/Core/BAO/RecurringEntity.php b/CRM/Core/BAO/RecurringEntity.php index 9a8aa74492..cc1d7c1f8e 100644 --- a/CRM/Core/BAO/RecurringEntity.php +++ b/CRM/Core/BAO/RecurringEntity.php @@ -39,24 +39,8 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity implemen protected $recursion = NULL; protected $recursion_start_date = NULL; - public static $_entitiesToBeDeleted = []; - public static $status = NULL; - public static $_recurringEntityHelper - = [ - 'civicrm_event' => [ - 'helper_class' => 'CRM_Event_DAO_Event', - 'delete_func' => 'delete', - 'pre_delete_func' => 'CRM_Event_Form_ManageEvent_Repeat::checkRegistrationForEvents', - ], - 'civicrm_activity' => [ - 'helper_class' => 'CRM_Activity_DAO_Activity', - 'delete_func' => 'delete', - 'pre_delete_func' => '', - ], - ]; - public static $_dateColumns = [ 'civicrm_event' => [ diff --git a/CRM/Core/Form/RecurringEntity.php b/CRM/Core/Form/RecurringEntity.php index 6390726a75..ce86c7a42a 100644 --- a/CRM/Core/Form/RecurringEntity.php +++ b/CRM/Core/Form/RecurringEntity.php @@ -20,6 +20,12 @@ * This class generates form components for processing Entity. */ class CRM_Core_Form_RecurringEntity { + + private static $preDeleteFunction = [ + 'Event' => 'CRM_Event_Form_ManageEvent_Repeat::checkRegistrationForEvents', + 'Activity' => NULL, + ]; + /** * Current entity id * @var int @@ -317,6 +323,7 @@ class CRM_Core_Form_RecurringEntity { * * @param array $params * @param string $type + * Redundant - always the same as `$params['entity_table']` * @param array $linkedEntities * * @throws \CRM_Core_Exception @@ -393,59 +400,22 @@ class CRM_Core_Form_RecurringEntity { } } - // FIXME: This is the worst way possible to convert a table name to an api entity name - $apiEntityType = explode("_", $type); - if (!empty($apiEntityType[1])) { - $apiType = $apiEntityType[1]; - } - //Delete relations if any from recurring entity tables before inserting new relations for this entity id + // Delete relations if any from recurring entity tables before inserting new relations for this entity id if ($params['entity_id']) { - //If entity has any pre delete function, consider that first - if (!empty(CRM_Core_BAO_RecurringEntity::$_recurringEntityHelper[$params['entity_table']]['pre_delete_func']) && - !empty(CRM_Core_BAO_RecurringEntity::$_recurringEntityHelper[$params['entity_table']]['helper_class']) - ) { - // FIXME: This calls `CRM_Event_Form_ManageEvent_Repeat::checkRegistrationForEvents` - // which then sets the static variable `CRM_Core_BAO_RecurringEntity::$_entitiesToBeDeleted` - // which is then accessed below and used to delete events with no registrations. - // I can't think of a worse way to pass a variable back from a function. - call_user_func_array(CRM_Core_BAO_RecurringEntity::$_recurringEntityHelper[$params['entity_table']]['pre_delete_func'], [$params['entity_id']]); + $entityType = CRM_Core_DAO_AllCoreTables::getEntityNameForTable($type); + // Use pre-delete function for events to exclude those with registered participants + if (!empty(self::$preDeleteFunction[$entityType])) { + $itemsToDelete = call_user_func_array(self::$preDeleteFunction[$entityType], [$params['entity_id']]); } - //Ready to execute delete on entities if it has delete function set - if (!empty(CRM_Core_BAO_RecurringEntity::$_recurringEntityHelper[$params['entity_table']]['delete_func']) && - !empty(CRM_Core_BAO_RecurringEntity::$_recurringEntityHelper[$params['entity_table']]['helper_class']) - ) { - //Check if pre delete function has some ids to be deleted - if (!empty(CRM_Core_BAO_RecurringEntity::$_entitiesToBeDeleted)) { - foreach (CRM_Core_BAO_RecurringEntity::$_entitiesToBeDeleted as $eid) { - $result = civicrm_api3( - ucfirst(strtolower($apiType)), - CRM_Core_BAO_RecurringEntity::$_recurringEntityHelper[$params['entity_table']]['delete_func'], - [ - 'sequential' => 1, - 'id' => $eid, - ] - ); - if ($result['error']) { - CRM_Core_Error::statusBounce(ts('Error creating recurring list')); - } - } - } - else { - $getRelatedEntities = CRM_Core_BAO_RecurringEntity::getEntitiesFor($params['entity_id'], $params['entity_table'], FALSE); - foreach ($getRelatedEntities as $key => $value) { - $result = civicrm_api3( - ucfirst(strtolower($apiType)), - CRM_Core_BAO_RecurringEntity::$_recurringEntityHelper[$params['entity_table']]['delete_func'], - [ - 'sequential' => 1, - 'id' => $value['id'], - ] - ); - if ($result['error']) { - CRM_Core_Error::statusBounce(ts('Error creating recurring list')); - } - } - } + else { + $getRelatedEntities = CRM_Core_BAO_RecurringEntity::getEntitiesFor($params['entity_id'], $params['entity_table'], FALSE); + $itemsToDelete = array_column($getRelatedEntities, 'id'); + } + if ($itemsToDelete) { + civicrm_api4($entityType, 'delete', [ + 'checkPermissions' => FALSE, + 'where' => [['id', 'IN', $itemsToDelete]], + ]); } // find all entities from the recurring set. At this point we 'll get entities which were not deleted diff --git a/CRM/Event/Form/ManageEvent/Repeat.php b/CRM/Event/Form/ManageEvent/Repeat.php index f84c3de494..74c24246df 100644 --- a/CRM/Event/Form/ManageEvent/Repeat.php +++ b/CRM/Event/Form/ManageEvent/Repeat.php @@ -205,7 +205,7 @@ class CRM_Event_Form_ManageEvent_Repeat extends CRM_Event_Form_ManageEvent { * * @return array */ - public static function checkRegistrationForEvents($eventID) { + public static function checkRegistrationForEvents($eventID): array { $eventIdsWithNoRegistration = []; if ($eventID) { $getRelatedEntities = CRM_Core_BAO_RecurringEntity::getEntitiesFor($eventID, 'civicrm_event', TRUE); @@ -218,8 +218,7 @@ class CRM_Event_Form_ManageEvent_Repeat extends CRM_Event_Form_ManageEvent { } } } - CRM_Core_BAO_RecurringEntity::$_entitiesToBeDeleted = $eventIdsWithNoRegistration; - return CRM_Core_BAO_RecurringEntity::$_entitiesToBeDeleted; + return $eventIdsWithNoRegistration; } } -- 2.25.1