diff --git a/CRM/Activity/Tokens.php b/CRM/Activity/Tokens.php index fff8e6224a50..4dcb6f448f06 100644 --- a/CRM/Activity/Tokens.php +++ b/CRM/Activity/Tokens.php @@ -130,7 +130,7 @@ public function evaluateToken(\Civi\Token\TokenRow $row, $entity, $field, $prefe ]; // 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 = (object) $prefetch['activity'][$activityId]; diff --git a/CRM/Admin/Form/ScheduleReminders.php b/CRM/Admin/Form/ScheduleReminders.php index a7b94e70f411..c76ba223cc03 100644 --- a/CRM/Admin/Form/ScheduleReminders.php +++ b/CRM/Admin/Form/ScheduleReminders.php @@ -681,8 +681,14 @@ public function parseActionSchedule($values) { * @return array */ public function listTokens() { - $tokens = CRM_Core_SelectValues::contactTokens(); - $tokens = array_merge(CRM_Core_SelectValues::activityTokens(), $tokens); + $tokenProcessor = new \Civi\Token\TokenProcessor(\Civi::dispatcher(), [ + 'controller' => get_class(), + 'smarty' => FALSE, + 'schema' => ['activityId'], + ]); + $tokens = $tokenProcessor->listTokens(); + + $tokens = array_merge(CRM_Core_SelectValues::contactTokens(), $tokens); $tokens = array_merge(CRM_Core_SelectValues::eventTokens(), $tokens); $tokens = array_merge(CRM_Core_SelectValues::membershipTokens(), $tokens); $tokens = array_merge(CRM_Core_SelectValues::contributionTokens(), $tokens); diff --git a/CRM/Core/BAO/ActionSchedule.php b/CRM/Core/BAO/ActionSchedule.php index 11d9b209c80a..af9bb6e92ffc 100644 --- a/CRM/Core/BAO/ActionSchedule.php +++ b/CRM/Core/BAO/ActionSchedule.php @@ -283,10 +283,13 @@ public static function sendMailings($mappingID, $now) { $errors = []; try { - $tokenProcessor = self::createTokenProcessor($actionSchedule, $mapping); + $entityContextId = \Civi\Token\TokenProcessor::getEntityTableToSchemaMapping($mapping->getEntity()); + $schema = ['contactId', $entityContextId]; + $tokenProcessor = self::createTokenProcessor($actionSchedule, $mapping, $schema); $tokenProcessor->addRow() ->context('contactId', $dao->contactID) - ->context('actionSearchResult', (object) $dao->toArray()); + ->context('actionSearchResult', (object) $dao->toArray()) + ->context($entityContextId, $dao->id); foreach ($tokenProcessor->evaluate()->getRows() as $tokenRow) { if ($actionSchedule->mode == 'SMS' or $actionSchedule->mode == 'User_Preference') { CRM_Utils_Array::extend($errors, self::sendReminderSms($tokenRow, $actionSchedule, $dao->contactID)); @@ -641,14 +644,17 @@ protected static function sendReminderEmail($tokenRow, $schedule, $toContactID) /** * @param CRM_Core_DAO_ActionSchedule $schedule * @param \Civi\ActionSchedule\Mapping $mapping + * @param array $schema (eg. ['contactId', 'activityId']) + * * @return \Civi\Token\TokenProcessor */ - protected static function createTokenProcessor($schedule, $mapping) { - $tp = new \Civi\Token\TokenProcessor(\Civi\Core\Container::singleton()->get('dispatcher'), [ + protected static function createTokenProcessor($schedule, $mapping, $schema) { + $tp = new \Civi\Token\TokenProcessor(\Civi::dispatcher(), [ 'controller' => __CLASS__, 'actionSchedule' => $schedule, 'actionMapping' => $mapping, 'smarty' => TRUE, + 'schema' => $schema, ]); $tp->addMessage('body_text', $schedule->body_text, 'text/plain'); $tp->addMessage('body_html', $schedule->body_html, 'text/html'); diff --git a/Civi/Token/TokenProcessor.php b/Civi/Token/TokenProcessor.php index 1364ed714faa..f0e871a4632f 100644 --- a/Civi/Token/TokenProcessor.php +++ b/Civi/Token/TokenProcessor.php @@ -80,14 +80,30 @@ class TokenProcessor { protected $next = 0; + /** + * Get a mapping from entity_table to context (currently used by actionSchedule to map parameters) + * + * @param string $entityTable eg. civicrm_activity + * + * @return string|null + */ + public static function getEntityTableToSchemaMapping($entityTable) { + $mapping = [ + 'civicrm_activity' => 'activityId', + 'civicrm_contact' => 'contactId', + 'civicrm_contribution' => 'contributionId', + ]; + if (array_key_exists($entityTable, $mapping)) { + return $mapping[$entityTable]; + } + return NULL; + } + /** * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher * @param array $context */ public function __construct($dispatcher, $context) { - $context['schema'] = isset($context['schema']) - ? array_unique(array_merge($context['schema'], array_keys($context))) - : array_keys($context); $this->dispatcher = $dispatcher; $this->context = $context; }