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