Merge pull request #23213 from eileenmcnaughton/post
[civicrm-core.git] / CRM / Event / Form / Task.php
index e3bbae330dac960309f39ffb304d15d2411b945c..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.
    *
@@ -83,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;
   }
 
   /**
@@ -119,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'];
+  }
+
 }