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'),
50 'now' => ts('Current time/date'),
51 'tax_term' => ts('Sales tax term (e.g VAT)'),
57 * @throws \CRM_Core_Exception
59 public function evaluateToken(TokenRow
$row, $entity, $field, $prefetch = NULL): void
{
60 if ($field === 'now') {
61 $nowObj = (new \
DateTime())->setTimestamp(\CRM_Utils_Time
::time());
62 $row->format('text/html')->tokens($entity, $field, $nowObj);
65 $row->format('text/html')->tokens($entity, $field, self
::getDomainTokenValues()[$field]);
66 $row->format('text/plain')->tokens($entity, $field, self
::getDomainTokenValues(NULL, FALSE)[$field]);
70 * Get the tokens available for the domain.
72 * This function will be made protected soon...
74 * @param int|null $domainID
78 * @throws \CRM_Core_Exception
82 public static function getDomainTokenValues(?
int $domainID = NULL, bool $html = TRUE): array {
84 $domainID = CRM_Core_Config
::domainID();
86 $cacheKey = __CLASS__
. 'domain_tokens' . $html . '_' . $domainID . '_' . CRM_Core_I18n
::getLocale();
87 if (!Civi
::cache('metadata')->has($cacheKey)) {
88 if (CRM_Core_Config
::domainID() === $domainID) {
89 $domain = CRM_Core_BAO_Domain
::getDomain();
92 $domain = new CRM_Core_BAO_Domain();
96 'name' => $domain->name ??
'',
98 'description' => $domain->description ??
'',
100 $loc = $domain->getLocationValues();
102 $tokens['address'] = str_replace("\n", '<br />', ($loc['address'][1]['display'] ??
''));
105 $tokens['address'] = $loc['address'][1]['display_text'] ??
'';
107 $phone = reset($loc['phone']);
108 $email = reset($loc['email']);
109 $tokens['phone'] = $phone['phone'] ??
'';
110 $tokens['email'] = $email['email'] ??
'';
111 $tokens['tax_term'] = (string) Civi
::settings()->get('tax_term');
112 Civi
::cache('metadata')->set($cacheKey, $tokens);
114 return Civi
::cache('metadata')->get($cacheKey);