From defba8ff2dfb5ad153c5e6d7e4bd834c533d3bc5 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Sun, 19 Sep 2021 11:24:18 +1200 Subject: [PATCH] Add now token --- CRM/Core/DomainTokens.php | 5 ++++ Civi/Token/TokenProcessor.php | 14 ++++++++++- Civi/Token/TokenRow.php | 7 ++++-- .../CRM/Utils/TokenConsistencyTest.php | 25 +++++++++++++++++++ 4 files changed, 48 insertions(+), 3 deletions(-) diff --git a/CRM/Core/DomainTokens.php b/CRM/Core/DomainTokens.php index 8aaafaff4c..a79368e301 100644 --- a/CRM/Core/DomainTokens.php +++ b/CRM/Core/DomainTokens.php @@ -47,6 +47,7 @@ class CRM_Core_DomainTokens extends AbstractTokenSubscriber { 'email' => ts('Domain (organization) email'), 'id' => ts('Domain ID'), 'description' => ts('Domain Description'), + 'now' => ts('Current time/date'), ]; } @@ -55,6 +56,10 @@ class CRM_Core_DomainTokens extends AbstractTokenSubscriber { * @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]); } diff --git a/Civi/Token/TokenProcessor.php b/Civi/Token/TokenProcessor.php index 3bad2d8918..2023a0dc17 100644 --- a/Civi/Token/TokenProcessor.php +++ b/Civi/Token/TokenProcessor.php @@ -365,6 +365,11 @@ class TokenProcessor { [$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); } @@ -384,7 +389,7 @@ class TokenProcessor { // 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; @@ -413,6 +418,13 @@ class TokenProcessor { 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"); } diff --git a/Civi/Token/TokenRow.php b/Civi/Token/TokenRow.php index fb1fda5c21..46aa8a889d 100644 --- a/Civi/Token/TokenRow.php +++ b/Civi/Token/TokenRow.php @@ -283,15 +283,18 @@ class TokenRow { // 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; diff --git a/tests/phpunit/CRM/Utils/TokenConsistencyTest.php b/tests/phpunit/CRM/Utils/TokenConsistencyTest.php index 0a0ab45571..34d5fea5fe 100644 --- a/tests/phpunit/CRM/Utils/TokenConsistencyTest.php +++ b/tests/phpunit/CRM/Utils/TokenConsistencyTest.php @@ -509,9 +509,33 @@ December 21st, 2007 ]); $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. * @@ -525,6 +549,7 @@ December 21st, 2007 '{domain.email}' => 'Domain (organization) email', '{domain.id}' => ts('Domain ID'), '{domain.description}' => ts('Domain Description'), + '{domain.now}' => 'Current time/date', ]; } -- 2.25.1