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