From 3c78698ea9c7eca124091bb0f1727532be4a36db Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Fri, 17 Sep 2021 18:14:20 +1200 Subject: [PATCH] [REF] Extract domain tokens to their own class Note that the moved function was only extracted a few days ago. --- CRM/Admin/Form/MessageTemplates.php | 6 +- CRM/Core/DomainTokens.php | 109 ++++++++++++++++++ CRM/Core/SelectValues.php | 30 +++-- CRM/Utils/Token.php | 42 +------ Civi/Core/Container.php | 4 + Civi/Token/TokenCompatSubscriber.php | 3 - .../Form/Task/PDFLetterCommonTest.php | 2 +- .../CRM/Core/BAO/MessageTemplateTest.php | 2 +- .../CRM/Utils/TokenConsistencyTest.php | 39 ++++++- 9 files changed, 176 insertions(+), 61 deletions(-) create mode 100644 CRM/Core/DomainTokens.php diff --git a/CRM/Admin/Form/MessageTemplates.php b/CRM/Admin/Form/MessageTemplates.php index 1c2fcd199b..dcf97bba3c 100644 --- a/CRM/Admin/Form/MessageTemplates.php +++ b/CRM/Admin/Form/MessageTemplates.php @@ -10,6 +10,7 @@ */ use Civi\Api4\MessageTemplate; +use Civi\Token\TokenProcessor; /** * @@ -170,9 +171,8 @@ class CRM_Admin_Form_MessageTemplates extends CRM_Core_Form { CRM_Core_DAO::getAttribute('CRM_Core_DAO_MessageTemplate', 'msg_subject') ); - //get the tokens. - $tokens = CRM_Core_SelectValues::contactTokens(); - $tokens = array_merge($tokens, CRM_Core_SelectValues::domainTokens()); + $tokenProcessor = new TokenProcessor(Civi::dispatcher(), ['schema' => ['contactId']]); + $tokens = $tokenProcessor->listTokens(); $this->assign('tokens', CRM_Utils_Token::formatTokensForDisplay($tokens)); diff --git a/CRM/Core/DomainTokens.php b/CRM/Core/DomainTokens.php new file mode 100644 index 0000000000..8aaafaff4c --- /dev/null +++ b/CRM/Core/DomainTokens.php @@ -0,0 +1,109 @@ + string $label). + */ + public $tokenNames; + + /** + * Class constructor. + */ + public function __construct() { + parent::__construct($this->entity, $this->getDomainTokens()); + } + + public function getDomainTokens(): array { + return [ + 'name' => ts('Domain name'), + 'address' => ts('Domain (organization) address'), + 'phone' => ts('Domain (organization) phone'), + 'email' => ts('Domain (organization) email'), + 'id' => ts('Domain ID'), + 'description' => ts('Domain Description'), + ]; + } + + /** + * @inheritDoc + * @throws \CRM_Core_Exception + */ + public function evaluateToken(TokenRow $row, $entity, $field, $prefetch = NULL): void { + $row->format('text/html')->tokens($entity, $field, self::getDomainTokenValues()[$field]); + $row->format('text/plain')->tokens($entity, $field, self::getDomainTokenValues(NULL, FALSE)[$field]); + } + + /** + * Get the tokens available for the domain. + * + * This function will be made protected soon... + * + * @param int|null $domainID + * @param bool $html + * + * @return array + * @throws \CRM_Core_Exception + * @internal + * + */ + public static function getDomainTokenValues(?int $domainID = NULL, bool $html = TRUE): array { + if (!$domainID) { + $domainID = CRM_Core_Config::domainID(); + } + $cacheKey = __CLASS__ . 'domain_tokens' . $html . '_' . $domainID . '_' . CRM_Core_I18n::getLocale(); + if (!Civi::cache('metadata')->has($cacheKey)) { + if (CRM_Core_Config::domainID() === $domainID) { + $domain = CRM_Core_BAO_Domain::getDomain(); + } + else { + $domain = new CRM_Core_BAO_Domain(); + $domain->find(TRUE); + } + $tokens = [ + 'name' => $domain->name, + 'id' => $domain->id, + 'description' => $domain->description, + ]; + $loc = $domain->getLocationValues(); + if ($html) { + $tokens['address'] = str_replace("\n", '
', ($loc['address'][1]['display'] ?? '')); + } + else { + $tokens['address'] = $loc['address'][1]['display_text'] ?? ''; + } + $phone = reset($loc['phone']); + $email = reset($loc['email']); + $tokens['phone'] = $phone['phone'] ?? ''; + $tokens['email'] = $email['email'] ?? ''; + Civi::cache('metadata')->set($cacheKey, $tokens); + } + return Civi::cache('metadata')->get($cacheKey); + } + +} diff --git a/CRM/Core/SelectValues.php b/CRM/Core/SelectValues.php index c2c2afba9a..f8aba56101 100644 --- a/CRM/Core/SelectValues.php +++ b/CRM/Core/SelectValues.php @@ -494,14 +494,12 @@ class CRM_Core_SelectValues { * Domain tokens * * @return array + * + * @deprecated */ public static function domainTokens() { - return [ - '{domain.name}' => ts('Domain name'), - '{domain.address}' => ts('Domain (organization) address'), - '{domain.phone}' => ts('Domain (organization) phone'), - '{domain.email}' => ts('Domain (organization) email'), - ]; + $tokenProcessor = new TokenProcessor(Civi::dispatcher(), []); + return $tokenProcessor->listTokens(); } /** @@ -562,21 +560,37 @@ class CRM_Core_SelectValues { /** * Different type of Contribution Tokens. * + * @deprecated + * * @return array */ public static function contributionTokens(): array { $tokenProcessor = new TokenProcessor(Civi::dispatcher(), ['schema' => ['contributionId']]); - return $tokenProcessor->listTokens(); + $allTokens = $tokenProcessor->listTokens(); + foreach (array_keys($allTokens) as $token) { + if (strpos($token, '{domain.') === 0) { + unset($allTokens[$token]); + } + } + return $allTokens; } /** * Different type of Contact Tokens. * + * @deprecated + * * @return array */ public static function contactTokens(): array { $tokenProcessor = new TokenProcessor(Civi::dispatcher(), ['schema' => ['contactId']]); - return $tokenProcessor->listTokens(); + $allTokens = $tokenProcessor->listTokens(); + foreach (array_keys($allTokens) as $token) { + if (strpos($token, '{domain.') === 0) { + unset($allTokens[$token]); + } + } + return $allTokens; } /** diff --git a/CRM/Utils/Token.php b/CRM/Utils/Token.php index c2ec7615ab..30d5af6f99 100644 --- a/CRM/Utils/Token.php +++ b/CRM/Utils/Token.php @@ -257,7 +257,7 @@ class CRM_Utils_Token { * @return null|string */ public static function getDomainTokenReplacement($token, $domain, $html = FALSE, $escapeSmarty = FALSE): ?string { - $tokens = self::getDomainTokens($domain->id, $html); + $tokens = CRM_Core_DomainTokens::getDomainTokenValues($domain->id, $html); $value = $tokens[$token] ?? "{domain.$token}"; if ($escapeSmarty) { $value = self::tokenEscapeSmarty($value); @@ -1924,44 +1924,4 @@ class CRM_Utils_Token { ]; } - /** - * Get the tokens available for the domain. - * - * @param int $domainID - * @param bool $html - * - * @return array - * @throws \CRM_Core_Exception - */ - protected static function getDomainTokens(int $domainID, bool $html): array { - $cacheKey = __CLASS__ . 'domain_tokens' . $html . '_' . $domainID . '_' . CRM_Core_I18n::getLocale(); - if (!Civi::cache('metadata')->has($cacheKey)) { - if (CRM_Core_Config::domainID() === $domainID) { - $domain = CRM_Core_BAO_Domain::getDomain(); - } - else { - $domain = new CRM_Core_BAO_Domain(); - $domain->find(TRUE); - } - $tokens = [ - 'name' => $domain->name, - 'id' => $domain->id, - 'description' => $domain->description, - ]; - $loc = $domain->getLocationValues(); - if ($html) { - $tokens['address'] = str_replace("\n", '
', ($loc['address'][1]['display'] ?? '')); - } - else { - $tokens['address'] = $loc['address'][1]['display_text'] ?? ''; - } - $phone = reset($loc['phone']); - $email = reset($loc['email']); - $tokens['phone'] = $phone['phone'] ?? ''; - $tokens['email'] = $email['email'] ?? ''; - Civi::cache('metadata')->set($cacheKey, $tokens); - } - return Civi::cache('metadata')->get($cacheKey); - } - } diff --git a/Civi/Core/Container.php b/Civi/Core/Container.php index 1673f35c27..5c74d402bc 100644 --- a/Civi/Core/Container.php +++ b/Civi/Core/Container.php @@ -337,6 +337,10 @@ class Container { 'CRM_Contribute_RecurTokens', [] ))->addTag('kernel.event_subscriber')->setPublic(TRUE); + $container->setDefinition('crm_domain_tokens', new Definition( + 'CRM_Core_DomainTokens', + [] + ))->addTag('kernel.event_subscriber')->setPublic(TRUE); $dispatcherDefn = $container->getDefinition('dispatcher'); foreach (\CRM_Core_DAO_AllCoreTables::getBaoClasses() as $baoEntity => $baoClass) { diff --git a/Civi/Token/TokenCompatSubscriber.php b/Civi/Token/TokenCompatSubscriber.php index f6bd98c7d2..3133df6eb3 100644 --- a/Civi/Token/TokenCompatSubscriber.php +++ b/Civi/Token/TokenCompatSubscriber.php @@ -338,9 +338,6 @@ class TokenCompatSubscriber implements EventSubscriberInterface { $isHtml = ($e->message['format'] === 'text/html'); $useSmarty = !empty($e->context['smarty']); - $domain = \CRM_Core_BAO_Domain::getDomain(); - $e->string = \CRM_Utils_Token::replaceDomainTokens($e->string, $domain, $isHtml, $e->message['tokens'], $useSmarty); - if (!empty($e->context['contact'])) { \CRM_Utils_Token::replaceGreetingTokens($e->string, $e->context['contact'], $e->context['contact']['contact_id'] ?? $e->context['contactId'], NULL, $useSmarty); } diff --git a/tests/phpunit/CRM/Activity/Form/Task/PDFLetterCommonTest.php b/tests/phpunit/CRM/Activity/Form/Task/PDFLetterCommonTest.php index 6f45f35912..4e920f45f5 100644 --- a/tests/phpunit/CRM/Activity/Form/Task/PDFLetterCommonTest.php +++ b/tests/phpunit/CRM/Activity/Form/Task/PDFLetterCommonTest.php @@ -45,7 +45,7 @@ class CRM_Activity_Form_Task_PDFLetterCommonTest extends CiviUnitTestCase { ]; $tokenProcessor = new TokenProcessor(Civi::dispatcher(), ['schema' => ['activityId']]); - $this->assertEquals($this->getActivityTokens(), $tokenProcessor->listTokens()); + $this->assertEquals(array_merge($this->getActivityTokens(), CRM_Core_SelectValues::domainTokens()), $tokenProcessor->listTokens()); $html_message = "\n" . implode("\n", CRM_Utils_Array::collect('0', $data)) . "\n"; $form = $this->getFormObject('CRM_Activity_Form_Task_PDF'); $output = $form->createDocument([$activity['id']], $html_message, ['is_unit_test' => TRUE]); diff --git a/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php b/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php index f68bb1aa05..049653d0f4 100644 --- a/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php +++ b/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php @@ -308,7 +308,7 @@ London, 90210 } /** - * Test rendering of domain tokens. + * Test rendering of contact tokens. * * @throws \API_Exception * @throws \CRM_Core_Exception diff --git a/tests/phpunit/CRM/Utils/TokenConsistencyTest.php b/tests/phpunit/CRM/Utils/TokenConsistencyTest.php index 6e7e5cfc0e..edc3bc43f1 100644 --- a/tests/phpunit/CRM/Utils/TokenConsistencyTest.php +++ b/tests/phpunit/CRM/Utils/TokenConsistencyTest.php @@ -85,7 +85,7 @@ class CRM_Utils_TokenConsistencyTest extends CiviUnitTestCase { 'smarty' => FALSE, 'schema' => ['caseId'], ]); - $this->assertEquals($expectedTokens, $tokenProcessor->listTokens()); + $this->assertEquals(array_merge($expectedTokens, $this->getDomainTokens()), $tokenProcessor->listTokens()); $tokenProcessor->addRow([ 'caseId' => $this->getCaseID(), ]); @@ -203,7 +203,7 @@ No 'smarty' => FALSE, 'schema' => ['contribution_recurId'], ]); - $this->assertEquals($this->getContributionRecurTokens(), $tokenProcessor->listTokens()); + $this->assertEquals(array_merge($this->getContributionRecurTokens(), $this->getDomainTokens()), $tokenProcessor->listTokens()); $tokenString = implode("\n", array_keys($this->getContributionRecurTokens())); $tokenProcessor->addMessage('html', $tokenString, 'text/plain'); @@ -406,7 +406,7 @@ Check'; $tokens = $tokenProcessor->listTokens(); // Add in custom tokens as token processor supports these. $expectedTokens['{membership.custom_1}'] = 'Enter text here :: Group with field text'; - $this->assertEquals($expectedTokens, $tokens); + $this->assertEquals(array_merge($expectedTokens, $this->getDomainTokens()), $tokens); $tokenProcessor->addMessage('html', $tokenString, 'text/plain'); $tokenProcessor->addRow(['membershipId' => $this->getMembershipID()]); $tokenProcessor->evaluate(); @@ -477,7 +477,7 @@ December 21st, 2007 } /** - * Get declared membership tokens. + * Get declared participant tokens. * * @return string[] */ @@ -504,4 +504,35 @@ December 21st, 2007 ]; } + /** + * Test that domain tokens are consistently rendered. + */ + public function testDomainTokenConsistency(): void { + $tokens = CRM_Core_SelectValues::domainTokens(); + $this->assertEquals($this->getDomainTokens(), $tokens); + $tokenProcessor = new TokenProcessor(\Civi::dispatcher(), [ + 'controller' => __CLASS__, + 'smarty' => FALSE, + ]); + $tokens['{domain.id}'] = 'Domain ID'; + $tokens['{domain.description}'] = 'Domain Description'; + $this->assertEquals($tokens, $tokenProcessor->listTokens()); + } + + /** + * Get declared participant tokens. + * + * @return string[] + */ + public function getDomainTokens(): array { + return [ + '{domain.name}' => ts('Domain name'), + '{domain.address}' => ts('Domain (organization) address'), + '{domain.phone}' => ts('Domain (organization) phone'), + '{domain.email}' => 'Domain (organization) email', + '{domain.id}' => ts('Domain ID'), + '{domain.description}' => ts('Domain Description'), + ]; + } + } -- 2.25.1