8 * A TokenRow is a helper providing simplified access to the
11 * A TokenRow combines two elements:
12 * - context: This is backend data provided by the controller.
13 * - tokens: This is frontend data that can be mail-merged.
15 * The context and tokens can be accessed using either methods
16 * or attributes. The methods are appropriate for updates
17 * (and generally accept a mix of arrays), and the attributes
18 * are appropriate for reads.
20 * To update the context or the tokens, use the methods.
21 * Note that the methods are fairly flexible about accepting
22 * single values or arrays. If given an array, the values
23 * will be merged recursively.
27 * ->context('contact_id', 123)
28 * ->context(array('contact_id' => 123))
29 * ->tokens('profile', array('viewUrl' => 'http://example.com'))
30 * ->tokens('profile', 'viewUrl, 'http://example.com');
32 * echo $row->context['contact_id'];
33 * echo $row->tokens['profile']['viewUrl'];
35 * $row->tokens('profile', array(
36 * 'viewUrl' => 'http://example.com/view/' . urlencode($row->context['contact_id'];
45 public $tokenProcessor;
52 * @var array|\ArrayAccess
53 * List of token values.
54 * Ex: array('contact' => array('display_name' => 'Alice')).
59 * @var array|\ArrayAccess
60 * List of context values.
61 * Ex: array('controller' => 'CRM_Foo_Bar').
65 public function __construct(TokenProcessor
$tokenProcessor, $key) {
66 $this->tokenProcessor
= $tokenProcessor;
67 $this->tokenRow
= $key;
69 $this->format('text/plain');
70 $this->context
= new TokenRowContext($tokenProcessor, $key);
74 * @param string $format
77 public function format($format) {
78 $this->format
= $format;
79 $this->tokens
= &$this->tokenProcessor
->rowValues
[$this->tokenRow
][$format];
84 * Update the value of a context element.
86 * @param string|array $a
90 public function context($a = NULL, $b = NULL) {
92 \CRM_Utils_Array
::extend($this->tokenProcessor
->rowContexts
[$this->tokenRow
], $a);
94 elseif (is_array($b)) {
95 \CRM_Utils_Array
::extend($this->tokenProcessor
->rowContexts
[$this->tokenRow
][$a], $b);
98 $this->tokenProcessor
->rowContexts
[$this->tokenRow
][$a] = $b;
104 * Update the value of a token.
106 * @param string|array $a
107 * @param string|array $b
111 public function tokens($a = NULL, $b = NULL, $c = NULL) {
113 \CRM_Utils_Array
::extend($this->tokens
, $a);
115 elseif (is_array($b)) {
116 \CRM_Utils_Array
::extend($this->tokens
[$a], $b);
118 elseif (is_array($c)) {
119 \CRM_Utils_Array
::extend($this->tokens
[$a][$b], $c);
121 elseif ($c === NULL) {
122 $this->tokens
[$a] = $b;
125 $this->tokens
[$a][$b] = $c;
131 * Update the value of a custom field token.
133 * @param string $entity
134 * @param int $customFieldID
135 * @param int $entityID
138 public function customToken($entity, $customFieldID, $entityID) {
139 $customFieldName = "custom_" . $customFieldID;
140 $record = civicrm_api3($entity, "getSingle", [
141 'return' => $customFieldName,
144 $fieldValue = \CRM_Utils_Array
::value($customFieldName, $record, '');
146 // format the raw custom field value into proper display value
147 if (isset($fieldValue)) {
148 $fieldValue = \CRM_Core_BAO_CustomField
::displayValue($fieldValue, $customFieldID);
151 return $this->tokens($entity, $customFieldName, $fieldValue);
155 * Update the value of a token. Apply formatting based on DB schema.
157 * @param string $tokenEntity
158 * @param string $tokenField
159 * @param string $baoName
160 * @param string $baoField
161 * @param mixed $fieldValue
163 * @throws \CRM_Core_Exception
165 public function dbToken($tokenEntity, $tokenField, $baoName, $baoField, $fieldValue) {
166 if ($fieldValue === NULL ||
$fieldValue === '') {
167 return $this->tokens($tokenEntity, $tokenField, '');
170 $fields = $baoName::fields();
171 if (!empty($fields[$baoField]['pseudoconstant'])) {
172 $options = $baoName::buildOptions($baoField, 'get');
173 return $this->format('text/plain')->tokens($tokenEntity, $tokenField, $options[$fieldValue]);
176 switch ($fields[$baoField]['type']) {
177 case \CRM_Utils_Type
::T_DATE + \CRM_Utils_Type
::T_TIME
:
178 return $this->format('text/plain')->tokens($tokenEntity, $tokenField, \CRM_Utils_Date
::customFormat($fieldValue));
180 case \CRM_Utils_Type
::T_MONEY
:
181 // Is this something you should ever use? Seems like you need more context
182 // to know which currency to use.
183 return $this->format('text/plain')->tokens($tokenEntity, $tokenField, \CRM_Utils_Money
::format($fieldValue));
185 case \CRM_Utils_Type
::T_STRING
:
186 case \CRM_Utils_Type
::T_BOOLEAN
:
187 case \CRM_Utils_Type
::T_INT
:
188 case \CRM_Utils_Type
::T_TEXT
:
189 return $this->format('text/plain')->tokens($tokenEntity, $tokenField, $fieldValue);
193 throw new \
CRM_Core_Exception("Cannot format token for field '$baoField' in '$baoName'");
197 * Auto-convert between different formats
199 * @param string $format
203 public function fill($format = NULL) {
204 if ($format === NULL) {
205 $format = $this->format
;
208 if (!isset($this->tokenProcessor
->rowValues
[$this->tokenRow
]['text/html'])) {
209 $this->tokenProcessor
->rowValues
[$this->tokenRow
]['text/html'] = [];
211 if (!isset($this->tokenProcessor
->rowValues
[$this->tokenRow
]['text/plain'])) {
212 $this->tokenProcessor
->rowValues
[$this->tokenRow
]['text/plain'] = [];
215 $htmlTokens = &$this->tokenProcessor
->rowValues
[$this->tokenRow
]['text/html'];
216 $textTokens = &$this->tokenProcessor
->rowValues
[$this->tokenRow
]['text/plain'];
221 foreach ($textTokens as $entity => $values) {
222 $entityFields = civicrm_api3($entity, "getFields", ['api_action' => 'get']);
223 foreach ($values as $field => $value) {
224 if (!isset($htmlTokens[$entity][$field])) {
225 // CRM-18420 - Activity Details Field are enclosed within <p>,
226 // hence if $body_text is empty, htmlentities will lead to
227 // conversion of these tags resulting in raw HTML.
228 if ($entity == 'activity' && $field == 'details') {
229 $htmlTokens[$entity][$field] = $value;
231 elseif (\CRM_Utils_Array
::value('data_type', \CRM_Utils_Array
::value($field, $entityFields['values'])) == 'Memo') {
232 // Memo fields aka custom fields of type Note are html.
233 $htmlTokens[$entity][$field] = CRM_Utils_String
::purifyHTML($value);
236 $htmlTokens[$entity][$field] = htmlentities($value);
245 foreach ($htmlTokens as $entity => $values) {
246 foreach ($values as $field => $value) {
247 if (!isset($textTokens[$entity][$field])) {
248 $textTokens[$entity][$field] = html_entity_decode(strip_tags($value));
255 throw new \
RuntimeException("Invalid format");
264 * @param string $name
265 * The name previously registered with TokenProcessor::addMessage.
267 * Fully rendered message, with tokens merged.
269 public function render($name) {
270 return $this->tokenProcessor
->render($name, $this);
276 * Class TokenRowContext
277 * @package Civi\Token
279 * Combine the row-context and general-context into a single array-like facade.
281 class TokenRowContext
implements \ArrayAccess
, \IteratorAggregate
, \Countable
{
284 * @var TokenProcessor
286 protected $tokenProcessor;
293 * @param array $tokenProcessor
294 * @param array $tokenRow
296 public function __construct($tokenProcessor, $tokenRow) {
297 $this->tokenProcessor
= $tokenProcessor;
298 $this->tokenRow
= $tokenRow;
304 * @param mixed $offset
308 public function offsetExists($offset) {
309 return isset($this->tokenProcessor
->rowContexts
[$this->tokenRow
][$offset])
310 ||
isset($this->tokenProcessor
->context
[$offset]);
316 * @param string $offset
320 public function &offsetGet($offset) {
321 if (isset($this->tokenProcessor
->rowContexts
[$this->tokenRow
][$offset])) {
322 return $this->tokenProcessor
->rowContexts
[$this->tokenRow
][$offset];
324 if (isset($this->tokenProcessor
->context
[$offset])) {
325 return $this->tokenProcessor
->context
[$offset];
334 * @param string $offset
335 * @param mixed $value
337 public function offsetSet($offset, $value) {
338 $this->tokenProcessor
->rowContexts
[$this->tokenRow
][$offset] = $value;
344 * @param mixed $offset
346 public function offsetUnset($offset) {
347 unset($this->tokenProcessor
->rowContexts
[$this->tokenRow
][$offset]);
353 * @return \ArrayIterator
355 public function getIterator() {
356 return new \
ArrayIterator($this->createMergedArray());
364 public function count() {
365 return count($this->createMergedArray());
369 * Create merged array.
373 protected function createMergedArray() {
375 $this->tokenProcessor
->rowContexts
[$this->tokenRow
],
376 $this->tokenProcessor
->context