'email' => ts('Domain (organization) email'),
'id' => ts('Domain ID'),
'description' => ts('Domain Description'),
+ 'now' => ts('Current time/date'),
];
}
* @throws \CRM_Core_Exception
*/
public function evaluateToken(TokenRow $row, $entity, $field, $prefetch = NULL): void {
+ if ($field === 'now') {
+ $row->format('text/html')->tokens($entity, $field, new DateTime());
+ return;
+ }
$row->format('text/html')->tokens($entity, $field, self::getDomainTokenValues()[$field]);
$row->format('text/plain')->tokens($entity, $field, self::getDomainTokenValues(NULL, FALSE)[$field]);
}
[$full, $entity, $field] = $m;
if (isset($tokens[$entity][$field])) {
$v = $tokens[$entity][$field];
+ if ($v instanceof \DateTime) {
+ if (!isset($m[3])) {
+ $m[3] = 'crmDate';
+ }
+ }
if (isset($m[3])) {
$v = $this->filterTokenValue($v, $m[3], $row);
}
// Regex counter-examples: '{foobar}', '{foo bar}', '{$foo.bar}', '{$foo.bar|whiz}', '{foo.bar|whiz{bang}}'
// Key observations: Civi tokens MUST have a `.` and MUST NOT have a `$`. Civi filters MUST NOT have `{}`s or `$`s.
$tokRegex = '([\w]+)\.([\w:\.]+)';
- $filterRegex = '(\w+)';
+ $filterRegex = '(\w+:?\w+)';
$event->string = preg_replace_callback(";\{$tokRegex(?:\|$filterRegex)?\};", $getToken, $message['string']);
$this->dispatcher->dispatch('civi.token.render', $event);
return $event->string;
case 'lower':
return mb_strtolower($value);
+ case 'crmDate':
+ if ($value instanceof \DateTime) {
+ // @todo cludgey.
+ require_once 'CRM/Core/Smarty/plugins/modifier.crmDate.php';
+ return \smarty_modifier_crmDate($value->format('Y-m-d H:i:s'));
+ }
+
default:
throw new \CRM_Core_Exception("Invalid token filter: $filter");
}
// HTML => Plain.
foreach ($htmlTokens as $entity => $values) {
foreach ($values as $field => $value) {
+ if (!$value instanceof \DateTime) {
+ $value = html_entity_decode(strip_tags($value));
+ }
if (!isset($textTokens[$entity][$field])) {
- $textTokens[$entity][$field] = html_entity_decode(strip_tags($value));
+ $textTokens[$entity][$field] = $value;
}
}
}
break;
default:
- throw new \RuntimeException("Invalid format");
+ throw new \RuntimeException('Invalid format');
}
return $this;
]);
$tokens['{domain.id}'] = 'Domain ID';
$tokens['{domain.description}'] = 'Domain Description';
+ $tokens['domain.now'] = 'Current time/date';
$this->assertEquals($tokens, $tokenProcessor->listTokens());
}
+ /**
+ * @throws \API_Exception
+ * @throws \CRM_Core_Exception
+ */
+ public function testDomainNow(): void {
+ putenv('TIME_FUNC=frozen');
+ CRM_Utils_Time::setTime('2021-21-18 11:58:00');
+ $resolved = CRM_Core_BAO_MessageTemplate::renderTemplate([
+ 'messageTemplate' => [
+ 'msg_text' => '{domain.now|crmDate:short}',
+ ],
+ ])['text'];
+ $this->assertEquals('September 18th, 2021 11:58 PM', $resolved);
+ $resolved = CRM_Core_BAO_MessageTemplate::renderTemplate([
+ 'messageTemplate' => [
+ 'msg_text' => '{domain.now}',
+ ],
+ ])['text'];
+ $this->assertEquals('September 18th, 2021 11:58 PM', $resolved);
+
+ $b1 = 1;
+ }
+
/**
* Get declared participant tokens.
*
'{domain.email}' => 'Domain (organization) email',
'{domain.id}' => ts('Domain ID'),
'{domain.description}' => ts('Domain Description'),
+ '{domain.now}' => 'Current time/date',
];
}