Merge pull request #18440 from civicrm/5.30
[civicrm-core.git] / CRM / Event / Form / Task / Batch.php
index bc00eccc68d1a2a637eeadb938e95dacd8e4e84e..11341b917da8b630513a870dac4c8d6aea290b72 100644 (file)
@@ -280,28 +280,21 @@ class CRM_Event_Form_Task_Batch extends CRM_Event_Form_Task {
     $negativeStatuses = CRM_Event_PseudoConstant::participantStatus(NULL, "class = 'Negative'");
     $contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
 
-    $contributionStatusId = NULL;
     if (array_key_exists($statusId, $positiveStatuses)) {
-      $contributionStatusId = array_search('Completed', $contributionStatuses);
+      $params = [
+        'component_id' => $participantId,
+        'contribution_id' => $contributionId,
+        'IAmAHorribleNastyBeyondExcusableHackInTheCRMEventFORMTaskClassThatNeedsToBERemoved' => 1,
+      ];
+
+      //change related contribution status.
+      self::updateContributionStatus($params);
     }
     if (array_key_exists($statusId, $negativeStatuses)) {
-      $contributionStatusId = array_search('Cancelled', $contributionStatuses);
-    }
-
-    if (!$contributionStatusId || !$participantId || !$contributionId) {
+      civicrm_api3('Contribution', 'create', ['id' => $contributionId, 'contribution_status_id' => 'Cancelled']);
       return;
     }
 
-    $params = [
-      'component_id' => $participantId,
-      'componentName' => 'Event',
-      'contribution_id' => $contributionId,
-      'contribution_status_id' => $contributionStatusId,
-      'IAmAHorribleNastyBeyondExcusableHackInTheCRMEventFORMTaskClassThatNeedsToBERemoved' => 1,
-    ];
-
-    //change related contribution status.
-    self::updateContributionStatus($params);
   }
 
   /**
@@ -320,15 +313,14 @@ class CRM_Event_Form_Task_Batch extends CRM_Event_Form_Task {
    */
   public static function updateContributionStatus($params) {
     // get minimum required values.
-    $statusId = $params['contribution_status_id'] ?? NULL;
     $componentId = $params['component_id'] ?? NULL;
-    $componentName = $params['componentName'] ?? NULL;
     $contributionId = $params['contribution_id'] ?? NULL;
 
     $input = $ids = $objects = [];
 
     //get the required ids.
     $ids['contribution'] = $contributionId;
+    $ids['participant'] = $params['component_id'];
 
     if (!$ids['contact'] = CRM_Utils_Array::value('contact_id', $params)) {
       $ids['contact'] = CRM_Core_DAO::getFieldValue('CRM_Contribute_DAO_Contribution',
@@ -337,25 +329,14 @@ class CRM_Event_Form_Task_Batch extends CRM_Event_Form_Task {
       );
     }
 
-    if ($componentName === 'Event') {
-      $name = 'event';
-      $ids['participant'] = $componentId;
-
-      if (!$ids['event'] = CRM_Utils_Array::value('event_id', $params)) {
-        $ids['event'] = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant',
-          $componentId,
-          'event_id'
-        );
-      }
+    if (!$ids['event'] = CRM_Utils_Array::value('event_id', $params)) {
+      $ids['event'] = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant',
+        $componentId,
+        'event_id'
+      );
     }
 
-    if ($componentName === 'Membership') {
-      $name = 'contribute';
-      $ids['membership'] = $componentId;
-    }
-    $ids['contributionPage'] = NULL;
-    $ids['contributionRecur'] = NULL;
-    $input['component'] = $name;
+    $input['component'] = 'event';
 
     $baseIPN = new CRM_Core_Payment_BaseIPN();
 
@@ -374,18 +355,6 @@ class CRM_Event_Form_Task_Batch extends CRM_Event_Form_Task {
       'flip' => 1,
     ]);
     $input['IAmAHorribleNastyBeyondExcusableHackInTheCRMEventFORMTaskClassThatNeedsToBERemoved'] = $params['IAmAHorribleNastyBeyondExcusableHackInTheCRMEventFORMTaskClassThatNeedsToBERemoved'] ?? NULL;
-    if ($statusId == $contributionStatuses['Cancelled']) {
-      $transaction = new CRM_Core_Transaction();
-      $baseIPN->cancelled($objects, $transaction, $input);
-      $transaction->commit();
-      return;
-    }
-    if ($statusId == $contributionStatuses['Failed']) {
-      $transaction = new CRM_Core_Transaction();
-      $baseIPN->failed($objects, $transaction, $input);
-      $transaction->commit();
-      return;
-    }
 
     // status is not pending
     if ($contribution->contribution_status_id != $contributionStatuses['Pending']) {
@@ -449,11 +418,11 @@ class CRM_Event_Form_Task_Batch extends CRM_Event_Form_Task {
   public function submit($params) {
     $statusClasses = CRM_Event_PseudoConstant::participantStatusClass();
     if (isset($params['field'])) {
-      foreach ($params['field'] as $key => $value) {
+      foreach ($params['field'] as $participantID => $value) {
 
         //check for custom data
         $value['custom'] = CRM_Core_BAO_CustomField::postProcess($value,
-          $key,
+          $participantID,
           'Participant'
         );
         foreach (array_keys($value) as $fieldName) {
@@ -467,7 +436,7 @@ class CRM_Event_Form_Task_Batch extends CRM_Event_Form_Task {
           }
         }
 
-        $value['id'] = $key;
+        $value['id'] = $participantID;
 
         if (!empty($value['participant_role'])) {
           if (is_array($value['participant_role'])) {
@@ -483,9 +452,9 @@ class CRM_Event_Form_Task_Batch extends CRM_Event_Form_Task {
         $relatedStatusChange = FALSE;
         if (!empty($value['participant_status'])) {
           $value['status_id'] = $value['participant_status'];
-          $fromStatusId = $this->_fromStatusIds[$key] ?? NULL;
+          $fromStatusId = $this->_fromStatusIds[$participantID] ?? NULL;
           if (!$fromStatusId) {
-            $fromStatusId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $key, 'status_id');
+            $fromStatusId = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Participant', $participantID, 'status_id');
           }
 
           if ($fromStatusId != $value['status_id']) {
@@ -502,11 +471,11 @@ class CRM_Event_Form_Task_Batch extends CRM_Event_Form_Task {
 
         //need to trigger mails when we change status
         if ($statusChange) {
-          CRM_Event_BAO_Participant::transitionParticipants([$key], $value['status_id'], $fromStatusId);
+          CRM_Event_BAO_Participant::transitionParticipants([$participantID], $value['status_id'], $fromStatusId);
         }
-        if ($relatedStatusChange && $key && $value['status_id']) {
+        if ($relatedStatusChange && $participantID && $value['status_id']) {
           //update related contribution status, CRM-4395
-          self::updatePendingOnlineContribution($key, $value['status_id']);
+          self::updatePendingOnlineContribution((int) $participantID, $value['status_id']);
         }
       }
       CRM_Core_Session::setStatus(ts('The updates have been saved.'), ts('Saved'), 'success');