Merge pull request #24022 from colemanw/afformFrontend
[civicrm-core.git] / CRM / Core / DomainTokens.php
CommitLineData
3c78698e
EM
1<?php
2
3/*
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
6 | |
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 +--------------------------------------------------------------------+
11 */
12
13use Civi\Token\AbstractTokenSubscriber;
14use Civi\Token\TokenRow;
15
16/**
17 * Class CRM_Case_Tokens
18 *
19 * Generate "case.*" tokens.
20 */
21class CRM_Core_DomainTokens extends AbstractTokenSubscriber {
22 /**
23 * @var string
24 * Token prefix
25 */
26 public $entity = 'domain';
27
28 /**
29 * @var array
30 * List of tokens provided by this class
31 * Array(string $fieldName => string $label).
32 */
33 public $tokenNames;
34
35 /**
36 * Class constructor.
37 */
38 public function __construct() {
39 parent::__construct($this->entity, $this->getDomainTokens());
40 }
41
42 public function getDomainTokens(): array {
43 return [
08592720
TO
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'),
3c78698e
EM
56 'id' => ts('Domain ID'),
57 'description' => ts('Domain Description'),
defba8ff 58 'now' => ts('Current time/date'),
5a82bb68 59 'base_url' => ts('Domain absolute base url'),
b8fe55cd 60 'tax_term' => ts('Sales tax term (e.g VAT)'),
3c78698e
EM
61 ];
62 }
63
64 /**
65 * @inheritDoc
66 * @throws \CRM_Core_Exception
67 */
68 public function evaluateToken(TokenRow $row, $entity, $field, $prefetch = NULL): void {
defba8ff 69 if ($field === 'now') {
d0017b16
TO
70 $nowObj = (new \DateTime())->setTimestamp(\CRM_Utils_Time::time());
71 $row->format('text/html')->tokens($entity, $field, $nowObj);
defba8ff
EM
72 return;
73 }
3c78698e
EM
74 $row->format('text/html')->tokens($entity, $field, self::getDomainTokenValues()[$field]);
75 $row->format('text/plain')->tokens($entity, $field, self::getDomainTokenValues(NULL, FALSE)[$field]);
76 }
77
78 /**
79 * Get the tokens available for the domain.
80 *
81 * This function will be made protected soon...
82 *
83 * @param int|null $domainID
84 * @param bool $html
85 *
86 * @return array
87 * @throws \CRM_Core_Exception
88 * @internal
89 *
90 */
91 public static function getDomainTokenValues(?int $domainID = NULL, bool $html = TRUE): array {
92 if (!$domainID) {
93 $domainID = CRM_Core_Config::domainID();
94 }
95 $cacheKey = __CLASS__ . 'domain_tokens' . $html . '_' . $domainID . '_' . CRM_Core_I18n::getLocale();
96 if (!Civi::cache('metadata')->has($cacheKey)) {
97 if (CRM_Core_Config::domainID() === $domainID) {
98 $domain = CRM_Core_BAO_Domain::getDomain();
99 }
100 else {
101 $domain = new CRM_Core_BAO_Domain();
102 $domain->find(TRUE);
103 }
104 $tokens = [
b8fe55cd 105 'name' => $domain->name ?? '',
3c78698e 106 'id' => $domain->id,
b8fe55cd 107 'description' => $domain->description ?? '',
3c78698e
EM
108 ];
109 $loc = $domain->getLocationValues();
2a46f455 110
3c78698e
EM
111 if ($html) {
112 $tokens['address'] = str_replace("\n", '<br />', ($loc['address'][1]['display'] ?? ''));
113 }
114 else {
115 $tokens['address'] = $loc['address'][1]['display_text'] ?? '';
116 }
2a46f455
EM
117 $tokens['street_address'] = $loc['address'][1]['street_address'] ?? '';
118 $tokens['supplemental_address_1'] = $loc['address'][1]['supplemental_address_1'] ?? '';
119 $tokens['supplemental_address_2'] = $loc['address'][1]['supplemental_address_2'] ?? '';
120 $tokens['supplemental_address_3'] = $loc['address'][1]['supplemental_address_3'] ?? '';
121 $tokens['city'] = $loc['address'][1]['city'] ?? '';
122 $tokens['postal_code'] = $loc['address'][1]['postal_code'] ?? '';
123 $tokens['state_province_id:label'] = $loc['address'][1]['state_province'] ?? '';
124 $tokens['country_id:label'] = $loc['address'][1]['country'] ?? '';
3c78698e
EM
125 $phone = reset($loc['phone']);
126 $email = reset($loc['email']);
127 $tokens['phone'] = $phone['phone'] ?? '';
128 $tokens['email'] = $email['email'] ?? '';
5a82bb68 129 $tokens['base_url'] = Civi::paths()->getVariable('cms.root', 'url');
b8fe55cd 130 $tokens['tax_term'] = (string) Civi::settings()->get('tax_term');
3c78698e
EM
131 Civi::cache('metadata')->set($cacheKey, $tokens);
132 }
133 return Civi::cache('metadata')->get($cacheKey);
134 }
135
136}