Invoice Workflow template - add domain tokens
[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 [
44 'name' => ts('Domain name'),
45 'address' => ts('Domain (organization) address'),
2a46f455
EM
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'),
3c78698e
EM
54 'phone' => ts('Domain (organization) phone'),
55 'email' => ts('Domain (organization) email'),
56 'id' => ts('Domain ID'),
57 'description' => ts('Domain Description'),
defba8ff 58 'now' => ts('Current time/date'),
b8fe55cd 59 'tax_term' => ts('Sales tax term (e.g VAT)'),
3c78698e
EM
60 ];
61 }
62
63 /**
64 * @inheritDoc
65 * @throws \CRM_Core_Exception
66 */
67 public function evaluateToken(TokenRow $row, $entity, $field, $prefetch = NULL): void {
defba8ff 68 if ($field === 'now') {
d0017b16
TO
69 $nowObj = (new \DateTime())->setTimestamp(\CRM_Utils_Time::time());
70 $row->format('text/html')->tokens($entity, $field, $nowObj);
defba8ff
EM
71 return;
72 }
3c78698e
EM
73 $row->format('text/html')->tokens($entity, $field, self::getDomainTokenValues()[$field]);
74 $row->format('text/plain')->tokens($entity, $field, self::getDomainTokenValues(NULL, FALSE)[$field]);
75 }
76
77 /**
78 * Get the tokens available for the domain.
79 *
80 * This function will be made protected soon...
81 *
82 * @param int|null $domainID
83 * @param bool $html
84 *
85 * @return array
86 * @throws \CRM_Core_Exception
87 * @internal
88 *
89 */
90 public static function getDomainTokenValues(?int $domainID = NULL, bool $html = TRUE): array {
91 if (!$domainID) {
92 $domainID = CRM_Core_Config::domainID();
93 }
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();
98 }
99 else {
100 $domain = new CRM_Core_BAO_Domain();
101 $domain->find(TRUE);
102 }
103 $tokens = [
b8fe55cd 104 'name' => $domain->name ?? '',
3c78698e 105 'id' => $domain->id,
b8fe55cd 106 'description' => $domain->description ?? '',
3c78698e
EM
107 ];
108 $loc = $domain->getLocationValues();
2a46f455 109
3c78698e
EM
110 if ($html) {
111 $tokens['address'] = str_replace("\n", '<br />', ($loc['address'][1]['display'] ?? ''));
112 }
113 else {
114 $tokens['address'] = $loc['address'][1]['display_text'] ?? '';
115 }
2a46f455
EM
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'] ?? '';
3c78698e
EM
124 $phone = reset($loc['phone']);
125 $email = reset($loc['email']);
126 $tokens['phone'] = $phone['phone'] ?? '';
127 $tokens['email'] = $email['email'] ?? '';
b8fe55cd 128 $tokens['tax_term'] = (string) Civi::settings()->get('tax_term');
3c78698e
EM
129 Civi::cache('metadata')->set($cacheKey, $tokens);
130 }
131 return Civi::cache('metadata')->get($cacheKey);
132 }
133
134}