Merge pull request #23213 from eileenmcnaughton/post
[civicrm-core.git] / CRM / Event / Form / Task.php
index 745fe3c06fa74cb3bf09a21b8f159f89bec6c86f..a23da4c6b5312a6c5d9f49dd66792b17464764bd 100644 (file)
@@ -15,6 +15,8 @@
  * @copyright CiviCRM LLC https://civicrm.org/licensing
  */
 
+use Civi\Api4\Participant;
+
 /**
  * Class for event form task actions.
  * FIXME: This needs refactoring to properly inherit from CRM_Core_Form_Task and share more functions.
@@ -28,6 +30,16 @@ class CRM_Event_Form_Task extends CRM_Core_Form_Task {
    */
   protected $_participantIds;
 
+  /**
+   * Rows to act on.
+   *
+   * Each row will have a participant ID & a contact ID using
+   * the keys the token processor expects.
+   *
+   * @var array
+   */
+  protected $rows = [];
+
   /**
    * Build all the data structures needed to build the form.
    *
@@ -53,15 +65,9 @@ class CRM_Event_Form_Task extends CRM_Core_Form_Task {
       CRM_Core_Error::statusBounce(ts('You do not have permission to access this page.'));
     }
 
-    $ids = [];
-    if ($values['radio_ts'] == 'ts_sel') {
-      foreach ($values as $name => $value) {
-        if (substr($name, 0, CRM_Core_Form::CB_PREFIX_LEN) == CRM_Core_Form::CB_PREFIX) {
-          $ids[] = substr($name, CRM_Core_Form::CB_PREFIX_LEN);
-        }
-      }
-    }
-    else {
+    $ids = $form->getSelectedIDs($values);
+
+    if (!$ids) {
       $queryParams = $form->get('queryParams');
       $sortOrder = NULL;
       if ($form->get(CRM_Utils_Sort::SORT_ORDER)) {
@@ -89,14 +95,36 @@ class CRM_Event_Form_Task extends CRM_Core_Form_Task {
     $form->setNextUrl('event');
   }
 
+  /**
+   * Get the participant IDs.
+   *
+   * @return array
+   */
+  public function getIDs(): array {
+    return $this->_participantIds;
+  }
+
   /**
    * Given the participant id, compute the contact id
    * since its used for things like send email
    */
-  public function setContactIDs() {
-    $this->_contactIds = CRM_Core_DAO::getContactIDsFromComponent($this->_participantIds,
-      'civicrm_participant'
-    );
+  public function setContactIDs(): void {
+    $this->_contactIds = $this->getContactIDs();
+  }
+
+  /**
+   * Get the relevant contact IDs.
+   *
+   * @return array
+   */
+  protected function getContactIDs(): array {
+    if (isset($this->_contactIds)) {
+      return $this->_contactIds;
+    }
+    foreach ($this->getRows() as $row) {
+      $this->_contactIds[] = $row['contact_id'];
+    }
+    return $this->_contactIds;
   }
 
   /**
@@ -125,4 +153,38 @@ class CRM_Event_Form_Task extends CRM_Core_Form_Task {
     ]);
   }
 
+  /**
+   * Get the rows form the search, keyed to make the token processor happy.
+   *
+   * @throws \API_Exception
+   */
+  protected function getRows(): array {
+    if (empty($this->rows)) {
+      // checkPermissions set to false - in case form is bypassing in some way.
+      $participants = Participant::get(FALSE)
+        ->addWhere('id', 'IN', $this->getIDs())
+        ->setSelect(['id', 'contact_id'])->execute();
+      foreach ($participants as $participant) {
+        $this->rows[] = [
+          'contact_id' => $participant['contact_id'],
+          'participant_id' => $participant['id'],
+          'schema' => [
+            'contactId' => $participant['contact_id'],
+            'participantId' => $participant['id'],
+          ],
+        ];
+      }
+    }
+    return $this->rows;
+  }
+
+  /**
+   * Get the token processor schema required to list any tokens for this task.
+   *
+   * @return array
+   */
+  public function getTokenSchema(): array {
+    return ['participantId', 'contactId', 'eventId'];
+  }
+
 }