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) Full Address'),
46 'street_address' => ts('Domain (Organization) Street Address'),
47 'supplemental_address_1' => ts('Domain (Organization) Supplemental Address'),
48 'supplemental_address_2' => ts('Domain (Organization) Supplemental Address 2'),
49 'supplemental_address_3' => ts('Domain (Organization) Supplemental Address 3'),
50 'city' => ts('Domain (Organization) City'),
51 'postal_code' => ts('Domain (Organization) Postal Code'),
52 'state_province_id:label' => ts('Domain (Organization) State'),
53 'country_id:label' => ts('Domain (Organization) Country'),
54 'phone' => ts('Domain (Organization) Phone'),
55 'email' => ts('Domain (Organization) Email'),
56 'id' => ts('Domain ID'),
57 'description' => ts('Domain Description'),
58 'now' => ts('Current time/date'),
59 'tax_term' => ts('Sales tax term (e.g VAT)'),
65 * @throws \CRM_Core_Exception
67 public function evaluateToken(TokenRow
$row, $entity, $field, $prefetch = NULL): void
{
68 if ($field === 'now') {
69 $nowObj = (new \
DateTime())->setTimestamp(\CRM_Utils_Time
::time());
70 $row->format('text/html')->tokens($entity, $field, $nowObj);
73 $row->format('text/html')->tokens($entity, $field, self
::getDomainTokenValues()[$field]);
74 $row->format('text/plain')->tokens($entity, $field, self
::getDomainTokenValues(NULL, FALSE)[$field]);
78 * Get the tokens available for the domain.
80 * This function will be made protected soon...
82 * @param int|null $domainID
86 * @throws \CRM_Core_Exception
90 public static function getDomainTokenValues(?
int $domainID = NULL, bool $html = TRUE): array {
92 $domainID = CRM_Core_Config
::domainID();
94 $cacheKey = __CLASS__
. 'domain_tokens' . $html . '_' . $domainID . '_' . CRM_Core_I18n
::getLocale();
95 if (!Civi
::cache('metadata')->has($cacheKey)) {
96 if (CRM_Core_Config
::domainID() === $domainID) {
97 $domain = CRM_Core_BAO_Domain
::getDomain();
100 $domain = new CRM_Core_BAO_Domain();
104 'name' => $domain->name ??
'',
106 'description' => $domain->description ??
'',
108 $loc = $domain->getLocationValues();
111 $tokens['address'] = str_replace("\n", '<br />', ($loc['address'][1]['display'] ??
''));
114 $tokens['address'] = $loc['address'][1]['display_text'] ??
'';
116 $tokens['street_address'] = $loc['address'][1]['street_address'] ??
'';
117 $tokens['supplemental_address_1'] = $loc['address'][1]['supplemental_address_1'] ??
'';
118 $tokens['supplemental_address_2'] = $loc['address'][1]['supplemental_address_2'] ??
'';
119 $tokens['supplemental_address_3'] = $loc['address'][1]['supplemental_address_3'] ??
'';
120 $tokens['city'] = $loc['address'][1]['city'] ??
'';
121 $tokens['postal_code'] = $loc['address'][1]['postal_code'] ??
'';
122 $tokens['state_province_id:label'] = $loc['address'][1]['state_province'] ??
'';
123 $tokens['country_id:label'] = $loc['address'][1]['country'] ??
'';
124 $phone = reset($loc['phone']);
125 $email = reset($loc['email']);
126 $tokens['phone'] = $phone['phone'] ??
'';
127 $tokens['email'] = $email['email'] ??
'';
128 $tokens['tax_term'] = (string) Civi
::settings()->get('tax_term');
129 Civi
::cache('metadata')->set($cacheKey, $tokens);
131 return Civi
::cache('metadata')->get($cacheKey);