DomainTokens - {domain.now} should abide by mocked times
[civicrm-core.git] / CRM / Core / DomainTokens.php
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'),
50 'now' => ts('Current time/date'),
51 ];
52 }
53
54 /**
55 * @inheritDoc
56 * @throws \CRM_Core_Exception
57 */
58 public function evaluateToken(TokenRow $row, $entity, $field, $prefetch = NULL): void {
59 if ($field === 'now') {
60 $nowObj = (new \DateTime())->setTimestamp(\CRM_Utils_Time::time());
61 $row->format('text/html')->tokens($entity, $field, $nowObj);
62 return;
63 }
64 $row->format('text/html')->tokens($entity, $field, self::getDomainTokenValues()[$field]);
65 $row->format('text/plain')->tokens($entity, $field, self::getDomainTokenValues(NULL, FALSE)[$field]);
66 }
67
68 /**
69 * Get the tokens available for the domain.
70 *
71 * This function will be made protected soon...
72 *
73 * @param int|null $domainID
74 * @param bool $html
75 *
76 * @return array
77 * @throws \CRM_Core_Exception
78 * @internal
79 *
80 */
81 public static function getDomainTokenValues(?int $domainID = NULL, bool $html = TRUE): array {
82 if (!$domainID) {
83 $domainID = CRM_Core_Config::domainID();
84 }
85 $cacheKey = __CLASS__ . 'domain_tokens' . $html . '_' . $domainID . '_' . CRM_Core_I18n::getLocale();
86 if (!Civi::cache('metadata')->has($cacheKey)) {
87 if (CRM_Core_Config::domainID() === $domainID) {
88 $domain = CRM_Core_BAO_Domain::getDomain();
89 }
90 else {
91 $domain = new CRM_Core_BAO_Domain();
92 $domain->find(TRUE);
93 }
94 $tokens = [
95 'name' => $domain->name,
96 'id' => $domain->id,
97 'description' => $domain->description,
98 ];
99 $loc = $domain->getLocationValues();
100 if ($html) {
101 $tokens['address'] = str_replace("\n", '<br />', ($loc['address'][1]['display'] ?? ''));
102 }
103 else {
104 $tokens['address'] = $loc['address'][1]['display_text'] ?? '';
105 }
106 $phone = reset($loc['phone']);
107 $email = reset($loc['email']);
108 $tokens['phone'] = $phone['phone'] ?? '';
109 $tokens['email'] = $email['email'] ?? '';
110 Civi::cache('metadata')->set($cacheKey, $tokens);
111 }
112 return Civi::cache('metadata')->get($cacheKey);
113 }
114
115 }