CRM_Activity_Tokens - Simplify prefetch. Remove special-cases for `actionSearchResult`.
[civicrm-core.git] / CRM / Activity / Tokens.php
index 93dad6fc11de66be784cf2bd707009b2eb66d037..bfcb4e76e5c03edf283fac22b71be66848517593 100644 (file)
  * @copyright CiviCRM LLC https://civicrm.org/licensing
  */
 
+use Civi\Token\AbstractTokenSubscriber;
+use Civi\Token\Event\TokenValueEvent;
+use Civi\Token\TokenRow;
+
 /**
  * Class CRM_Member_Tokens
  *
@@ -29,7 +33,7 @@
  *
  * This has been enhanced to work with PDF/letter merge
  */
-class CRM_Activity_Tokens extends \Civi\Token\AbstractTokenSubscriber {
+class CRM_Activity_Tokens extends AbstractTokenSubscriber {
 
   use CRM_Core_TokenTrait;
 
@@ -80,41 +84,39 @@ class CRM_Activity_Tokens extends \Civi\Token\AbstractTokenSubscriber {
     // Multiple revisions of the activity.
     // Q: Could we simplify & move the extra AND clauses into `where(...)`?
     $e->query->param('casEntityJoinExpr', 'e.id = reminder.entity_id AND e.is_current_revision = 1 AND e.is_deleted = 0');
+    $e->query->select('e.id AS tokenContext_' . $this->getEntityContextSchema());
   }
 
   /**
    * @inheritDoc
    */
-  public function prefetch(\Civi\Token\Event\TokenValueEvent $e) {
+  public function prefetch(TokenValueEvent $e) {
     // Find all the entity IDs
-    $entityIds
-      = $e->getTokenProcessor()->getContextValues('actionSearchResult', 'entityID')
-      + $e->getTokenProcessor()->getContextValues($this->getEntityContextSchema());
+    $entityIds = $e->getTokenProcessor()->getContextValues($this->getEntityContextSchema());
 
     if (!$entityIds) {
       return NULL;
     }
 
     // Get data on all activities for basic and customfield tokens
-    $activities = civicrm_api3('Activity', 'get', [
+    $prefetch['activity'] = civicrm_api3('Activity', 'get', [
       'id' => ['IN' => $entityIds],
       'options' => ['limit' => 0],
       'return' => self::getReturnFields($this->activeTokens),
-    ]);
-    $prefetch['activity'] = $activities['values'];
+    ])['values'];
 
     // Store the activity types if needed
-    if (in_array('activity_type', $this->activeTokens)) {
+    if (in_array('activity_type', $this->activeTokens, TRUE)) {
       $this->activityTypes = \CRM_Core_OptionGroup::values('activity_type');
     }
 
     // Store the activity statuses if needed
-    if (in_array('status', $this->activeTokens)) {
+    if (in_array('status', $this->activeTokens, TRUE)) {
       $this->activityStatuses = \CRM_Core_OptionGroup::values('activity_status');
     }
 
     // Store the campaigns if needed
-    if (in_array('campaign', $this->activeTokens)) {
+    if (in_array('campaign', $this->activeTokens, TRUE)) {
       $this->campaigns = \CRM_Campaign_BAO_Campaign::getCampaigns();
     }
 
@@ -122,16 +124,26 @@ class CRM_Activity_Tokens extends \Civi\Token\AbstractTokenSubscriber {
   }
 
   /**
-   * @inheritDoc
+   * Evaluate the content of a single token.
+   *
+   * @param \Civi\Token\TokenRow $row
+   *   The record for which we want token values.
+   * @param string $entity
+   *   The name of the token entity.
+   * @param string $field
+   *   The name of the token field.
+   * @param mixed $prefetch
+   *   Any data that was returned by the prefetch().
+   *
+   * @throws \CRM_Core_Exception
    */
-  public function evaluateToken(\Civi\Token\TokenRow $row, $entity, $field, $prefetch = NULL) {
+  public function evaluateToken(TokenRow $row, $entity, $field, $prefetch = NULL) {
     // maps token name to api field
     $mapping = [
       'activity_id' => 'id',
     ];
 
-    // Get ActivityID either from actionSearchResult (for scheduled reminders) if exists
-    $activityId = $row->context['actionSearchResult']->entityID ?? $row->context[$this->getEntityContextSchema()];
+    $activityId = $row->context[$this->getEntityContextSchema()];
 
     $activity = $prefetch['activity'][$activityId];
 
@@ -172,7 +184,7 @@ class CRM_Activity_Tokens extends \Civi\Token\AbstractTokenSubscriber {
    *
    * @return array token name => token label
    */
-  protected function getBasicTokens() {
+  protected function getBasicTokens(): array {
     if (!isset($this->basicTokens)) {
       $this->basicTokens = [
         'activity_id' => ts('Activity ID'),