4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
10 +--------------------------------------------------------------------+
13 use Civi\Token\AbstractTokenSubscriber
;
14 use Civi\Token\TokenRow
;
17 * Class CRM_Case_Tokens
19 * Generate "case.*" tokens.
21 class CRM_Core_DomainTokens
extends AbstractTokenSubscriber
{
26 public $entity = 'domain';
30 * List of tokens provided by this class
31 * Array(string $fieldName => string $label).
38 public function __construct() {
39 parent
::__construct($this->entity
, $this->getDomainTokens());
42 public function getDomainTokens(): array {
44 'name' => ts('Domain name'),
45 'address' => ts('Domain (organization) address'),
46 'phone' => ts('Domain (organization) phone'),
47 'email' => ts('Domain (organization) email'),
48 'id' => ts('Domain ID'),
49 'description' => ts('Domain Description'),
55 * @throws \CRM_Core_Exception
57 public function evaluateToken(TokenRow
$row, $entity, $field, $prefetch = NULL): void
{
58 $row->format('text/html')->tokens($entity, $field, self
::getDomainTokenValues()[$field]);
59 $row->format('text/plain')->tokens($entity, $field, self
::getDomainTokenValues(NULL, FALSE)[$field]);
63 * Get the tokens available for the domain.
65 * This function will be made protected soon...
67 * @param int|null $domainID
71 * @throws \CRM_Core_Exception
75 public static function getDomainTokenValues(?
int $domainID = NULL, bool $html = TRUE): array {
77 $domainID = CRM_Core_Config
::domainID();
79 $cacheKey = __CLASS__
. 'domain_tokens' . $html . '_' . $domainID . '_' . CRM_Core_I18n
::getLocale();
80 if (!Civi
::cache('metadata')->has($cacheKey)) {
81 if (CRM_Core_Config
::domainID() === $domainID) {
82 $domain = CRM_Core_BAO_Domain
::getDomain();
85 $domain = new CRM_Core_BAO_Domain();
89 'name' => $domain->name
,
91 'description' => $domain->description
,
93 $loc = $domain->getLocationValues();
95 $tokens['address'] = str_replace("\n", '<br />', ($loc['address'][1]['display'] ??
''));
98 $tokens['address'] = $loc['address'][1]['display_text'] ??
'';
100 $phone = reset($loc['phone']);
101 $email = reset($loc['email']);
102 $tokens['phone'] = $phone['phone'] ??
'';
103 $tokens['email'] = $email['email'] ??
'';
104 Civi
::cache('metadata')->set($cacheKey, $tokens);
106 return Civi
::cache('metadata')->get($cacheKey);