Commit | Line | Data |
---|---|---|
88d88c53 MW |
1 | <?php |
2 | ||
c2a33d9c | 3 | use Civi\Token\Event\TokenValueEvent; |
4 | use Civi\Token\TokenProcessor; | |
5 | ||
88d88c53 MW |
6 | trait CRM_Core_TokenTrait { |
7 | ||
8 | private $basicTokens; | |
9 | private $customFieldTokens; | |
10 | ||
11 | /** | |
12 | * CRM_Entity_Tokens constructor. | |
13 | */ | |
14 | public function __construct() { | |
15 | parent::__construct($this->getEntityName(), array_merge( | |
16 | $this->getBasicTokens(), | |
17 | $this->getCustomFieldTokens() | |
18 | )); | |
19 | } | |
20 | ||
21 | /** | |
350d582f MW |
22 | * Check if the token processor is active. |
23 | * | |
24 | * @param \Civi\Token\TokenProcessor $processor | |
25 | * | |
26 | * @return bool | |
88d88c53 | 27 | */ |
c2a33d9c | 28 | public function checkActive(TokenProcessor $processor) { |
51e25a23 | 29 | return in_array($this->getEntityContextSchema(), $processor->context['schema']) || |
88d88c53 MW |
30 | (!empty($processor->context['actionMapping']) |
31 | && $processor->context['actionMapping']->getEntity() === $this->getEntityTableName()); | |
32 | } | |
33 | ||
34 | /** | |
35 | * @inheritDoc | |
36 | */ | |
c2a33d9c | 37 | public function getActiveTokens(TokenValueEvent $e) { |
88d88c53 MW |
38 | $messageTokens = $e->getTokenProcessor()->getMessageTokens(); |
39 | if (!isset($messageTokens[$this->entity])) { | |
40 | return NULL; | |
41 | } | |
42 | ||
43 | $activeTokens = []; | |
44 | // if message token contains '_\d+_', then treat as '_N_' | |
45 | foreach ($messageTokens[$this->entity] as $msgToken) { | |
46 | if (array_key_exists($msgToken, $this->tokenNames)) { | |
47 | $activeTokens[] = $msgToken; | |
48 | } | |
49 | else { | |
50 | $altToken = preg_replace('/_\d+_/', '_N_', $msgToken); | |
51 | if (array_key_exists($altToken, $this->tokenNames)) { | |
52 | $activeTokens[] = $msgToken; | |
53 | } | |
54 | } | |
55 | } | |
56 | return array_unique($activeTokens); | |
57 | } | |
58 | ||
59 | /** | |
60 | * Find the fields that we need to get to construct the tokens requested. | |
b441a504 | 61 | * |
88d88c53 MW |
62 | * @return array list of fields needed to generate those tokens |
63 | */ | |
b441a504 | 64 | public function getReturnFields(): array { |
88d88c53 MW |
65 | // Make sure we always return something |
66 | $fields = ['id']; | |
67 | ||
b441a504 EM |
68 | $tokensInUse = |
69 | array_merge(array_keys(self::getBasicTokens()), array_keys(self::getCustomFieldTokens())); | |
f10c962e | 70 | foreach ($tokensInUse as $token) { |
88d88c53 MW |
71 | if (isset(self::$fieldMapping[$token])) { |
72 | $fields = array_merge($fields, self::$fieldMapping[$token]); | |
73 | } | |
74 | else { | |
75 | $fields[] = $token; | |
76 | } | |
77 | } | |
78 | return array_unique($fields); | |
79 | } | |
80 | ||
81 | /** | |
82 | * Get the tokens for custom fields | |
83 | * @return array token name => token label | |
84 | */ | |
c2a33d9c | 85 | protected function getCustomFieldTokens(): array { |
88d88c53 MW |
86 | if (!isset($this->customFieldTokens)) { |
87 | $this->customFieldTokens = \CRM_Utils_Token::getCustomFieldTokens(ucfirst($this->getEntityName())); | |
88 | } | |
89 | return $this->customFieldTokens; | |
90 | } | |
91 | ||
92 | } |