Commit | Line | Data |
---|---|---|
8adcd073 TO |
1 | <?php |
2 | namespace Civi\Token; | |
3 | use Civi\Token\Event\TokenRenderEvent; | |
4 | ||
5 | /** | |
6 | * Class TokenRow | |
7 | * @package Civi\Token | |
8 | * | |
9 | * A TokenRow is a helper providing simplified access to the | |
10 | * TokenProcessor. | |
11 | * | |
12 | * A TokenRow combines two elements: | |
13 | * - context: This is backend data provided by the controller. | |
14 | * - tokens: This is frontend data that can be mail-merged. | |
15 | * | |
16 | * The context and tokens can be accessed using either methods | |
17 | * or attributes. The methods are appropriate for updates | |
18 | * (and generally accept a mix of arrays), and the attributes | |
19 | * are appropriate for reads. | |
20 | * | |
21 | * To update the context or the tokens, use the methods. | |
22 | * Note that the methods are fairly flexible about accepting | |
23 | * single values or arrays. If given an array, the values | |
24 | * will be merged recursively. | |
25 | * | |
26 | * @code | |
27 | * $row | |
28 | * ->context('contact_id', 123) | |
29 | * ->context(array('contact_id' => 123)) | |
30 | * ->tokens('profile', array('viewUrl' => 'http://example.com')) | |
31 | * ->tokens('profile', 'viewUrl, 'http://example.com'); | |
32 | * | |
33 | * echo $row->context['contact_id']; | |
34 | * echo $row->tokens['profile']['viewUrl']; | |
35 | * | |
36 | * $row->tokens('profile', array( | |
37 | * 'viewUrl' => 'http://example.com/view/' . urlencode($row->context['contact_id']; | |
38 | * )); | |
39 | * @endcode | |
40 | */ | |
41 | class TokenRow { | |
42 | ||
43 | /** | |
44 | * @var TokenProcessor | |
45 | */ | |
46 | public $tokenProcessor; | |
47 | ||
48 | public $tokenRow; | |
49 | ||
50 | public $format; | |
51 | ||
52 | /** | |
09c2328a | 53 | * @var array|\ArrayAccess |
8adcd073 TO |
54 | * List of token values. |
55 | * Ex: array('contact' => array('display_name' => 'Alice')). | |
56 | */ | |
57 | public $tokens; | |
58 | ||
59 | /** | |
09c2328a | 60 | * @var array|\ArrayAccess |
8adcd073 TO |
61 | * List of context values. |
62 | * Ex: array('controller' => 'CRM_Foo_Bar'). | |
63 | */ | |
64 | public $context; | |
65 | ||
66 | public function __construct(TokenProcessor $tokenProcessor, $key) { | |
67 | $this->tokenProcessor = $tokenProcessor; | |
68 | $this->tokenRow = $key; | |
69 | $this->format('text/plain'); // Set a default. | |
70 | $this->context = new TokenRowContext($tokenProcessor, $key); | |
71 | } | |
72 | ||
73 | /** | |
74 | * @param string $format | |
75 | * @return $this | |
76 | */ | |
77 | public function format($format) { | |
78 | $this->format = $format; | |
79 | $this->tokens = &$this->tokenProcessor->rowValues[$this->tokenRow][$format]; | |
80 | return $this; | |
81 | } | |
82 | ||
83 | /** | |
84 | * Update the value of a context element. | |
85 | * | |
86 | * @param string|array $a | |
87 | * @param mixed $b | |
88 | * @return $this | |
89 | */ | |
90 | public function context($a = NULL, $b = NULL) { | |
91 | if (is_array($a)) { | |
92 | \CRM_Utils_Array::extend($this->tokenProcessor->rowContexts[$this->tokenRow], $a); | |
93 | } | |
94 | elseif (is_array($b)) { | |
95 | \CRM_Utils_Array::extend($this->tokenProcessor->rowContexts[$this->tokenRow][$a], $b); | |
96 | } | |
97 | else { | |
98 | $this->tokenProcessor->rowContexts[$this->tokenRow][$a] = $b; | |
99 | } | |
100 | return $this; | |
101 | } | |
102 | ||
103 | /** | |
104 | * Update the value of a token. | |
105 | * | |
106 | * @param string|array $a | |
107 | * @param string|array $b | |
108 | * @param mixed $c | |
109 | * @return $this | |
110 | */ | |
111 | public function tokens($a = NULL, $b = NULL, $c = NULL) { | |
112 | if (is_array($a)) { | |
113 | \CRM_Utils_Array::extend($this->tokens, $a); | |
114 | } | |
115 | elseif (is_array($b)) { | |
116 | \CRM_Utils_Array::extend($this->tokens[$a], $b); | |
117 | } | |
118 | elseif (is_array($c)) { | |
119 | \CRM_Utils_Array::extend($this->tokens[$a][$b], $c); | |
120 | } | |
121 | elseif ($c === NULL) { | |
122 | $this->tokens[$a] = $b; | |
123 | } | |
124 | else { | |
125 | $this->tokens[$a][$b] = $c; | |
126 | } | |
127 | return $this; | |
128 | } | |
129 | ||
2045389a TO |
130 | /** |
131 | * Update the value of a token. Apply formatting based on DB schema. | |
132 | * | |
133 | * @param string $tokenEntity | |
134 | * @param string $tokenField | |
135 | * @param string $baoName | |
136 | * @param array $baoField | |
137 | * @param mixed $fieldValue | |
138 | */ | |
139 | public function dbToken($tokenEntity, $tokenField, $baoName, $baoField, $fieldValue) { | |
140 | if ($fieldValue === NULL || $fieldValue === '') { | |
141 | return $this->tokens($tokenEntity, $tokenField, ''); | |
142 | } | |
143 | ||
144 | $fields = $baoName::fields(); | |
145 | if (!empty($fields[$baoField]['pseudoconstant'])) { | |
146 | $options = $baoName::buildOptions($baoField, 'get'); | |
147 | return $this->format('text/plain')->tokens($tokenEntity, $tokenField, $options[$fieldValue]); | |
148 | } | |
149 | ||
150 | switch ($fields[$baoField]['type']) { | |
151 | case \CRM_Utils_Type::T_DATE + \CRM_Utils_Type::T_TIME: | |
152 | return $this->format('text/plain')->tokens($tokenEntity, $tokenField, \CRM_Utils_Date::customFormat($fieldValue)); | |
153 | ||
154 | case \CRM_Utils_Type::T_MONEY: | |
155 | // Is this something you should ever use? Seems like you need more context | |
156 | // to know which currency to use. | |
157 | return $this->format('text/plain')->tokens($tokenEntity, $tokenField, \CRM_Utils_Money::format($fieldValue)); | |
158 | ||
159 | case \CRM_Utils_Type::T_STRING: | |
160 | case \CRM_Utils_Type::T_BOOLEAN: | |
161 | case \CRM_Utils_Type::T_INT: | |
162 | case \CRM_Utils_Type::T_TEXT: | |
163 | return $this->format('text/plain')->tokens($tokenEntity, $tokenField, $fieldValue); | |
164 | ||
165 | } | |
166 | ||
167 | throw new \CRM_Core_Exception("Cannot format token for field '$baoField' in '$baoName'"); | |
168 | } | |
169 | ||
8adcd073 TO |
170 | /** |
171 | * Auto-convert between different formats | |
172 | */ | |
173 | public function fill($format = NULL) { | |
174 | if ($format === NULL) { | |
175 | $format = $this->format; | |
176 | } | |
177 | ||
178 | if (!isset($this->tokenProcessor->rowValues[$this->tokenRow]['text/html'])) { | |
179 | $this->tokenProcessor->rowValues[$this->tokenRow]['text/html'] = array(); | |
180 | } | |
181 | if (!isset($this->tokenProcessor->rowValues[$this->tokenRow]['text/plain'])) { | |
182 | $this->tokenProcessor->rowValues[$this->tokenRow]['text/plain'] = array(); | |
183 | } | |
184 | ||
185 | $htmlTokens = &$this->tokenProcessor->rowValues[$this->tokenRow]['text/html']; | |
186 | $textTokens = &$this->tokenProcessor->rowValues[$this->tokenRow]['text/plain']; | |
187 | ||
188 | switch ($format) { | |
189 | case 'text/html': | |
190 | // Plain => HTML. | |
191 | foreach ($textTokens as $entity => $values) { | |
192 | foreach ($values as $field => $value) { | |
193 | if (!isset($htmlTokens[$entity][$field])) { | |
194 | $htmlTokens[$entity][$field] = htmlentities($value); | |
195 | } | |
196 | } | |
197 | } | |
198 | break; | |
199 | ||
200 | case 'text/plain': | |
201 | // HTML => Plain. | |
202 | foreach ($htmlTokens as $entity => $values) { | |
203 | foreach ($values as $field => $value) { | |
204 | if (!isset($textTokens[$entity][$field])) { | |
205 | $textTokens[$entity][$field] = html_entity_decode(strip_tags($value)); | |
206 | } | |
207 | } | |
208 | } | |
209 | break; | |
210 | ||
211 | default: | |
212 | throw new \RuntimeException("Invalid format"); | |
213 | } | |
214 | ||
215 | return $this; | |
216 | } | |
217 | ||
218 | /** | |
219 | * Render a message. | |
220 | * | |
221 | * @param string $name | |
222 | * The name previously registered with TokenProcessor::addMessage. | |
223 | * @return string | |
224 | * Fully rendered message, with tokens merged. | |
225 | */ | |
226 | public function render($name) { | |
227 | return $this->tokenProcessor->render($name, $this); | |
228 | } | |
229 | ||
230 | } | |
231 | ||
232 | /** | |
233 | * Class TokenRowContext | |
234 | * @package Civi\Token | |
235 | * | |
236 | * Combine the row-context and general-context into a single array-like facade. | |
237 | */ | |
238 | class TokenRowContext implements \ArrayAccess, \IteratorAggregate, \Countable { | |
239 | ||
240 | /** | |
241 | * @var TokenProcessor | |
242 | */ | |
243 | protected $tokenProcessor; | |
244 | ||
245 | protected $tokenRow; | |
246 | ||
247 | /** | |
248 | * @param $tokenProcessor | |
249 | * @param $tokenRow | |
250 | */ | |
251 | public function __construct($tokenProcessor, $tokenRow) { | |
252 | $this->tokenProcessor = $tokenProcessor; | |
253 | $this->tokenRow = $tokenRow; | |
254 | } | |
255 | ||
256 | public function offsetExists($offset) { | |
257 | return | |
258 | isset($this->tokenProcessor->rowContexts[$this->tokenRow][$offset]) | |
259 | || isset($this->tokenProcessor->context[$offset]); | |
260 | } | |
261 | ||
262 | public function &offsetGet($offset) { | |
263 | if (isset($this->tokenProcessor->rowContexts[$this->tokenRow][$offset])) { | |
264 | return $this->tokenProcessor->rowContexts[$this->tokenRow][$offset]; | |
265 | } | |
266 | if (isset($this->tokenProcessor->context[$offset])) { | |
267 | return $this->tokenProcessor->context[$offset]; | |
268 | } | |
269 | $val = NULL; | |
270 | return $val; | |
271 | } | |
272 | ||
273 | public function offsetSet($offset, $value) { | |
274 | $this->tokenProcessor->rowContexts[$this->tokenRow][$offset] = $value; | |
275 | } | |
276 | ||
277 | public function offsetUnset($offset) { | |
278 | unset($this->tokenProcessor->rowContexts[$this->tokenRow][$offset]); | |
279 | } | |
280 | ||
281 | public function getIterator() { | |
282 | return new \ArrayIterator($this->createMergedArray()); | |
283 | } | |
284 | ||
285 | public function count() { | |
286 | return count($this->createMergedArray()); | |
287 | } | |
288 | ||
289 | protected function createMergedArray() { | |
290 | return array_merge( | |
291 | $this->tokenProcessor->rowContexts[$this->tokenRow], | |
292 | $this->tokenProcessor->context | |
293 | ); | |
294 | } | |
295 | ||
296 | } |