Commit | Line | Data |
---|---|---|
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 | ||
13 | use Civi\Token\AbstractTokenSubscriber; | |
14 | use Civi\Token\TokenRow; | |
15 | ||
16 | /** | |
17 | * Class CRM_Case_Tokens | |
18 | * | |
19 | * Generate "case.*" tokens. | |
20 | */ | |
21 | class 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 [ | |
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'), | |
defba8ff | 50 | 'now' => ts('Current time/date'), |
b8fe55cd | 51 | 'tax_term' => ts('Sales tax term (e.g VAT)'), |
3c78698e EM |
52 | ]; |
53 | } | |
54 | ||
55 | /** | |
56 | * @inheritDoc | |
57 | * @throws \CRM_Core_Exception | |
58 | */ | |
59 | public function evaluateToken(TokenRow $row, $entity, $field, $prefetch = NULL): void { | |
defba8ff | 60 | if ($field === 'now') { |
d0017b16 TO |
61 | $nowObj = (new \DateTime())->setTimestamp(\CRM_Utils_Time::time()); |
62 | $row->format('text/html')->tokens($entity, $field, $nowObj); | |
defba8ff EM |
63 | return; |
64 | } | |
3c78698e EM |
65 | $row->format('text/html')->tokens($entity, $field, self::getDomainTokenValues()[$field]); |
66 | $row->format('text/plain')->tokens($entity, $field, self::getDomainTokenValues(NULL, FALSE)[$field]); | |
67 | } | |
68 | ||
69 | /** | |
70 | * Get the tokens available for the domain. | |
71 | * | |
72 | * This function will be made protected soon... | |
73 | * | |
74 | * @param int|null $domainID | |
75 | * @param bool $html | |
76 | * | |
77 | * @return array | |
78 | * @throws \CRM_Core_Exception | |
79 | * @internal | |
80 | * | |
81 | */ | |
82 | public static function getDomainTokenValues(?int $domainID = NULL, bool $html = TRUE): array { | |
83 | if (!$domainID) { | |
84 | $domainID = CRM_Core_Config::domainID(); | |
85 | } | |
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(); | |
90 | } | |
91 | else { | |
92 | $domain = new CRM_Core_BAO_Domain(); | |
93 | $domain->find(TRUE); | |
94 | } | |
95 | $tokens = [ | |
b8fe55cd | 96 | 'name' => $domain->name ?? '', |
3c78698e | 97 | 'id' => $domain->id, |
b8fe55cd | 98 | 'description' => $domain->description ?? '', |
3c78698e EM |
99 | ]; |
100 | $loc = $domain->getLocationValues(); | |
101 | if ($html) { | |
102 | $tokens['address'] = str_replace("\n", '<br />', ($loc['address'][1]['display'] ?? '')); | |
103 | } | |
104 | else { | |
105 | $tokens['address'] = $loc['address'][1]['display_text'] ?? ''; | |
106 | } | |
107 | $phone = reset($loc['phone']); | |
108 | $email = reset($loc['email']); | |
109 | $tokens['phone'] = $phone['phone'] ?? ''; | |
110 | $tokens['email'] = $email['email'] ?? ''; | |
b8fe55cd | 111 | $tokens['tax_term'] = (string) Civi::settings()->get('tax_term'); |
3c78698e EM |
112 | Civi::cache('metadata')->set($cacheKey, $tokens); |
113 | } | |
114 | return Civi::cache('metadata')->get($cacheKey); | |
115 | } | |
116 | ||
117 | } |