Skip to content

Commit

Permalink
Standardise what we pass to tokenProcessor so we don't have to add sp…
Browse files Browse the repository at this point in the history
…ecific handling in each class for actionSchedule
  • Loading branch information
mattwire committed Apr 6, 2020
1 parent cb94806 commit 1bcfb46
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 10 deletions.
2 changes: 1 addition & 1 deletion CRM/Activity/Tokens.php
Expand Up @@ -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];

Expand Down
10 changes: 8 additions & 2 deletions CRM/Admin/Form/ScheduleReminders.php
Expand Up @@ -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);
Expand Down
14 changes: 10 additions & 4 deletions CRM/Core/BAO/ActionSchedule.php
Expand Up @@ -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));
Expand Down Expand Up @@ -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');
Expand Down
22 changes: 19 additions & 3 deletions Civi/Token/TokenProcessor.php
Expand Up @@ -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;
}
Expand Down

0 comments on commit 1bcfb46

Please sign in to comment.