RecurringEntity - Cleanup godawful form code
authorcolemanw <coleman@civicrm.org>
Wed, 6 Sep 2023 14:41:11 +0000 (10:41 -0400)
committercolemanw <coleman@civicrm.org>
Wed, 6 Sep 2023 14:41:11 +0000 (10:41 -0400)
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
CRM/Core/Form/RecurringEntity.php
CRM/Event/Form/ManageEvent/Repeat.php

index 9a8aa744927a5aca2b1e8987ccdcbe6c827269e0..cc1d7c1f8ecabf962bff39fe1c79739a86bd9cba 100644 (file)
@@ -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' => [
index 6390726a755f03cb287224ba0f5c831656acdead..ce86c7a42ab2ce7d1fc07b38fe6889e0343384aa 100644 (file)
  * 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
index f84c3de49420d86c849ae529644e556daf4a3a28..74c24246df7c3002fce308aa990ce2d8359236a8 100644 (file)
@@ -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;
   }
 
 }