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;
68 $this->format('text/plain'); // Set a default.
69 $this->context
= new TokenRowContext($tokenProcessor, $key);
73 * @param string $format
76 public function format($format) {
77 $this->format
= $format;
78 $this->tokens
= &$this->tokenProcessor
->rowValues
[$this->tokenRow
][$format];
83 * Update the value of a context element.
85 * @param string|array $a
89 public function context($a = NULL, $b = NULL) {
91 \CRM_Utils_Array
::extend($this->tokenProcessor
->rowContexts
[$this->tokenRow
], $a);
93 elseif (is_array($b)) {
94 \CRM_Utils_Array
::extend($this->tokenProcessor
->rowContexts
[$this->tokenRow
][$a], $b);
97 $this->tokenProcessor
->rowContexts
[$this->tokenRow
][$a] = $b;
103 * Update the value of a token.
105 * @param string|array $a
106 * @param string|array $b
110 public function tokens($a = NULL, $b = NULL, $c = NULL) {
112 \CRM_Utils_Array
::extend($this->tokens
, $a);
114 elseif (is_array($b)) {
115 \CRM_Utils_Array
::extend($this->tokens
[$a], $b);
117 elseif (is_array($c)) {
118 \CRM_Utils_Array
::extend($this->tokens
[$a][$b], $c);
120 elseif ($c === NULL) {
121 $this->tokens
[$a] = $b;
124 $this->tokens
[$a][$b] = $c;
130 * Update the value of a token. Apply formatting based on DB schema.
132 * @param string $tokenEntity
133 * @param string $tokenField
134 * @param string $baoName
135 * @param array $baoField
136 * @param mixed $fieldValue
138 public function dbToken($tokenEntity, $tokenField, $baoName, $baoField, $fieldValue) {
139 if ($fieldValue === NULL ||
$fieldValue === '') {
140 return $this->tokens($tokenEntity, $tokenField, '');
143 $fields = $baoName::fields();
144 if (!empty($fields[$baoField]['pseudoconstant'])) {
145 $options = $baoName::buildOptions($baoField, 'get');
146 return $this->format('text/plain')->tokens($tokenEntity, $tokenField, $options[$fieldValue]);
149 switch ($fields[$baoField]['type']) {
150 case \CRM_Utils_Type
::T_DATE + \CRM_Utils_Type
::T_TIME
:
151 return $this->format('text/plain')->tokens($tokenEntity, $tokenField, \CRM_Utils_Date
::customFormat($fieldValue));
153 case \CRM_Utils_Type
::T_MONEY
:
154 // Is this something you should ever use? Seems like you need more context
155 // to know which currency to use.
156 return $this->format('text/plain')->tokens($tokenEntity, $tokenField, \CRM_Utils_Money
::format($fieldValue));
158 case \CRM_Utils_Type
::T_STRING
:
159 case \CRM_Utils_Type
::T_BOOLEAN
:
160 case \CRM_Utils_Type
::T_INT
:
161 case \CRM_Utils_Type
::T_TEXT
:
162 return $this->format('text/plain')->tokens($tokenEntity, $tokenField, $fieldValue);
166 throw new \
CRM_Core_Exception("Cannot format token for field '$baoField' in '$baoName'");
170 * Auto-convert between different formats
172 * @param string $format
176 public function fill($format = NULL) {
177 if ($format === NULL) {
178 $format = $this->format
;
181 if (!isset($this->tokenProcessor
->rowValues
[$this->tokenRow
]['text/html'])) {
182 $this->tokenProcessor
->rowValues
[$this->tokenRow
]['text/html'] = array();
184 if (!isset($this->tokenProcessor
->rowValues
[$this->tokenRow
]['text/plain'])) {
185 $this->tokenProcessor
->rowValues
[$this->tokenRow
]['text/plain'] = array();
188 $htmlTokens = &$this->tokenProcessor
->rowValues
[$this->tokenRow
]['text/html'];
189 $textTokens = &$this->tokenProcessor
->rowValues
[$this->tokenRow
]['text/plain'];
194 foreach ($textTokens as $entity => $values) {
195 foreach ($values as $field => $value) {
196 if (!isset($htmlTokens[$entity][$field])) {
197 $htmlTokens[$entity][$field] = htmlentities($value);
205 foreach ($htmlTokens as $entity => $values) {
206 foreach ($values as $field => $value) {
207 if (!isset($textTokens[$entity][$field])) {
208 $textTokens[$entity][$field] = html_entity_decode(strip_tags($value));
215 throw new \
RuntimeException("Invalid format");
224 * @param string $name
225 * The name previously registered with TokenProcessor::addMessage.
227 * Fully rendered message, with tokens merged.
229 public function render($name) {
230 return $this->tokenProcessor
->render($name, $this);
236 * Class TokenRowContext
237 * @package Civi\Token
239 * Combine the row-context and general-context into a single array-like facade.
241 class TokenRowContext
implements \ArrayAccess
, \IteratorAggregate
, \Countable
{
244 * @var TokenProcessor
246 protected $tokenProcessor;
253 * @param array $tokenProcessor
254 * @param array $tokenRow
256 public function __construct($tokenProcessor, $tokenRow) {
257 $this->tokenProcessor
= $tokenProcessor;
258 $this->tokenRow
= $tokenRow;
264 * @param mixed $offset
268 public function offsetExists($offset) {
270 isset($this->tokenProcessor
->rowContexts
[$this->tokenRow
][$offset])
271 ||
isset($this->tokenProcessor
->context
[$offset]);
277 * @param string $offset
281 public function &offsetGet($offset) {
282 if (isset($this->tokenProcessor
->rowContexts
[$this->tokenRow
][$offset])) {
283 return $this->tokenProcessor
->rowContexts
[$this->tokenRow
][$offset];
285 if (isset($this->tokenProcessor
->context
[$offset])) {
286 return $this->tokenProcessor
->context
[$offset];
295 * @param string $offset
296 * @param mixed $value
298 public function offsetSet($offset, $value) {
299 $this->tokenProcessor
->rowContexts
[$this->tokenRow
][$offset] = $value;
305 * @param mixed $offset
307 public function offsetUnset($offset) {
308 unset($this->tokenProcessor
->rowContexts
[$this->tokenRow
][$offset]);
314 * @return \ArrayIterator
316 public function getIterator() {
317 return new \
ArrayIterator($this->createMergedArray());
325 public function count() {
326 return count($this->createMergedArray());
330 * Create merged array.
334 protected function createMergedArray() {
336 $this->tokenProcessor
->rowContexts
[$this->tokenRow
],
337 $this->tokenProcessor
->context