Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.3 | | |
5 | +--------------------------------------------------------------------+ | |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
7 | +--------------------------------------------------------------------+ | |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
31 | * @copyright CiviCRM LLC (c) 2004-2013 | |
32 | * $Id: $ | |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * Class to abstract token replacement | |
38 | */ | |
39 | class CRM_Utils_Token { | |
40 | static $_requiredTokens = NULL; | |
41 | ||
42 | static $_tokens = array( | |
43 | 'action' => array( | |
44 | 'forward', | |
45 | 'optOut', | |
46 | 'optOutUrl', | |
47 | 'reply', | |
48 | 'unsubscribe', | |
49 | 'unsubscribeUrl', | |
50 | 'resubscribe', | |
51 | 'resubscribeUrl', | |
52 | 'subscribeUrl', | |
53 | ), | |
54 | 'mailing' => array( | |
55 | 'id', | |
56 | 'name', | |
57 | 'group', | |
58 | 'subject', | |
59 | 'viewUrl', | |
60 | 'editUrl', | |
61 | 'scheduleUrl', | |
62 | 'approvalStatus', | |
63 | 'approvalNote', | |
64 | 'approveUrl', | |
65 | 'creator', | |
66 | 'creatorEmail', | |
67 | ), | |
68 | 'user' => array( | |
69 | // we extract the stuff after the role / permission and return the | |
70 | // civicrm email addresses of all users with that role / permission | |
71 | // useful with rules integration | |
72 | 'permission:', | |
73 | 'role:', | |
74 | ), | |
75 | // populate this dynamically | |
76 | 'contact' => NULL, | |
77 | // populate this dynamically | |
78 | 'contribution' => NULL, | |
79 | 'domain' => array( | |
80 | 'name', | |
81 | 'phone', | |
82 | 'address', | |
83 | 'email', | |
e3470b79 | 84 | 'id', |
85 | 'description', | |
6a488035 TO |
86 | ), |
87 | 'subscribe' => array( 'group' ), | |
88 | 'unsubscribe' => array( 'group' ), | |
89 | 'resubscribe' => array( 'group' ), | |
90 | 'welcome' => array( 'group' ), | |
91 | ); | |
92 | ||
93 | /** | |
94 | * Check a string (mailing body) for required tokens. | |
95 | * | |
96 | * @param string $str The message | |
97 | * | |
98 | * @return true|array true if all required tokens are found, | |
99 | * else an array of the missing tokens | |
100 | * @access public | |
101 | * @static | |
102 | */ | |
103 | public static function requiredTokens(&$str) { | |
104 | if (self::$_requiredTokens == NULL) { | |
105 | self::$_requiredTokens = array( | |
106 | 'domain.address' => ts("Domain address - displays your organization's postal address."), | |
107 | 'action.optOutUrl or action.subscribeUrl' => | |
108 | array( | |
109 | 'action.optOut' => ts("'Opt out via email' - displays an email address for recipients to opt out of receiving emails from your organization."), | |
110 | 'action.optOutUrl' => ts("'Opt out via web page' - creates a link for recipients to click if they want to opt out of receiving emails from your organization. Alternatively, you can include the 'Opt out via email' token."), | |
111 | 'action.unsubscribe' => ts("'Unsubscribe via email' - displays an email address for recipients to unsubscribe from the specific mailing list used to send this message."), | |
112 | 'action.unsubscribe' => ts("'Unsubscribe via web page' - creates a link for recipients to unsubscribe from the specific mailing list used to send this message. Alternatively, you can include the 'Unsubscribe via email' token or one of the Opt-out tokens."), | |
113 | ), | |
114 | ); | |
115 | } | |
116 | ||
117 | $missing = array(); | |
118 | foreach (self::$_requiredTokens as $token => $value) { | |
119 | if (!is_array($value)) { | |
120 | if (!preg_match('/(^|[^\{])' . preg_quote('{' . $token . '}') . '/', $str)) { | |
121 | $missing[$token] = $value; | |
122 | } | |
123 | } | |
124 | else { | |
125 | $present = FALSE; | |
126 | $desc = NULL; | |
127 | foreach ($value as $t => $d) { | |
128 | $desc = $d; | |
129 | if (preg_match('/(^|[^\{])' . preg_quote('{' . $t . '}') . '/', $str)) { | |
130 | $present = TRUE; | |
131 | } | |
132 | } | |
133 | if (!$present) { | |
134 | $missing[$token] = $desc; | |
135 | } | |
136 | } | |
137 | } | |
138 | ||
139 | if (empty($missing)) { | |
140 | return TRUE; | |
141 | } | |
142 | return $missing; | |
143 | } | |
144 | ||
145 | /** | |
146 | * Wrapper for token matching | |
147 | * | |
148 | * @param string $type The token type (domain,mailing,contact,action) | |
149 | * @param string $var The token variable | |
150 | * @param string $str The string to search | |
151 | * | |
152 | * @return boolean Was there a match | |
153 | * @access public | |
154 | * @static | |
155 | */ | |
156 | public static function token_match($type, $var, &$str) { | |
157 | $token = preg_quote('{' . "$type.$var") . '(\|.+?)?' . preg_quote('}'); | |
93f087c7 | 158 | return preg_match("/(^|[^\{])$token/", $str); |
6a488035 TO |
159 | } |
160 | ||
161 | /** | |
162 | * Wrapper for token replacing | |
163 | * | |
164 | * @param string $type The token type | |
165 | * @param string $var The token variable | |
166 | * @param string $value The value to substitute for the token | |
167 | * @param string (reference) $str The string to replace in | |
168 | * | |
169 | * @return string The processed string | |
170 | * @access public | |
171 | * @static | |
172 | */ | |
173 | public static function &token_replace($type, $var, $value, &$str, $escapeSmarty = FALSE) { | |
174 | $token = preg_quote('{' . "$type.$var") . '(\|([^\}]+?))?' . preg_quote('}'); | |
175 | if (!$value) { | |
176 | $value = '$3'; | |
177 | } | |
178 | if ($escapeSmarty) { | |
179 | $value = self::tokenEscapeSmarty($value); | |
180 | } | |
181 | $str = preg_replace("/([^\{])?$token/", "\${1}$value", $str); | |
182 | return $str; | |
183 | } | |
184 | ||
185 | /** | |
186 | * get< the regex for token replacement | |
187 | * | |
188 | * @param string $key a string indicating the the type of token to be used in the expression | |
189 | * | |
190 | * @return string regular expression sutiable for using in preg_replace | |
191 | * @access private | |
192 | * @static | |
193 | */ | |
194 | private static function tokenRegex($token_type) { | |
8bab0eb0 | 195 | return '/(?<!\{|\\\\)\{' . $token_type . '\.([\w]+(\-[\w\s]+)?)\}(?!\})/'; |
6a488035 TO |
196 | } |
197 | ||
198 | /** | |
199 | * escape the string so a malicious user cannot inject smarty code into the template | |
200 | * | |
201 | * @param string $string a string that needs to be escaped from smarty parsing | |
202 | * | |
203 | * @return string the escaped string | |
204 | * @access private | |
205 | * @static | |
206 | */ | |
207 | private static function tokenEscapeSmarty($string) { | |
208 | // need to use negative look-behind, as both str_replace() and preg_replace() are sequential | |
209 | return preg_replace(array('/{/', '/(?<!{ldelim)}/'), array('{ldelim}', '{rdelim}'), $string); | |
210 | } | |
211 | ||
212 | /** | |
213 | /** | |
214 | * Replace all the domain-level tokens in $str | |
215 | * | |
216 | * @param string $str The string with tokens to be replaced | |
217 | * @param object $domain The domain BAO | |
218 | * @param boolean $html Replace tokens with HTML or plain text | |
219 | * | |
220 | * @return string The processed string | |
221 | * @access public | |
222 | * @static | |
223 | */ | |
21bb6c7b DL |
224 | public static function &replaceDomainTokens( |
225 | $str, | |
226 | &$domain, | |
227 | $html = FALSE, | |
228 | $knownTokens = NULL, | |
229 | $escapeSmarty = FALSE | |
230 | ) { | |
6a488035 | 231 | $key = 'domain'; |
21bb6c7b DL |
232 | if ( |
233 | !$knownTokens || | |
6a488035 TO |
234 | !CRM_Utils_Array::value($key, $knownTokens) |
235 | ) { | |
236 | return $str; | |
237 | } | |
238 | ||
8bab0eb0 | 239 | $str = preg_replace_callback( |
21bb6c7b | 240 | self::tokenRegex($key), |
8bab0eb0 DL |
241 | function ($matches) use(&$domain, $html, $escapeSmarty) { |
242 | return CRM_Utils_Token::getDomainTokenReplacement($matches[1], $domain, $html, $escapeSmarty); | |
243 | }, | |
21bb6c7b DL |
244 | $str |
245 | ); | |
6a488035 TO |
246 | return $str; |
247 | } | |
248 | ||
249 | public static function getDomainTokenReplacement($token, &$domain, $html = FALSE, $escapeSmarty = FALSE) { | |
250 | // check if the token we were passed is valid | |
251 | // we have to do this because this function is | |
252 | // called only when we find a token in the string | |
253 | ||
254 | $loc = &$domain->getLocationValues(); | |
255 | ||
256 | if (!in_array($token, self::$_tokens['domain'])) { | |
257 | $value = "{domain.$token}"; | |
258 | } | |
259 | elseif ($token == 'address') { | |
260 | static $addressCache = array(); | |
261 | ||
262 | $cache_key = $html ? 'address-html' : 'address-text'; | |
263 | if (array_key_exists($cache_key, $addressCache)) { | |
264 | return $addressCache[$cache_key]; | |
265 | } | |
266 | ||
267 | $value = NULL; | |
268 | /* Construct the address token */ | |
269 | ||
270 | if (CRM_Utils_Array::value($token, $loc)) { | |
271 | if ($html) { | |
272 | $value = $loc[$token][1]['display']; | |
273 | $value = str_replace("\n", '<br />', $value); | |
274 | } | |
275 | else { | |
276 | $value = $loc[$token][1]['display_text']; | |
277 | } | |
278 | $addressCache[$cache_key] = $value; | |
279 | } | |
280 | } | |
e3470b79 | 281 | elseif ($token == 'name' || $token == 'id' || $token == 'description') { |
6a488035 TO |
282 | $value = $domain->$token; |
283 | } | |
284 | elseif ($token == 'phone' || $token == 'email') { | |
285 | /* Construct the phone and email tokens */ | |
286 | ||
287 | $value = NULL; | |
288 | if (CRM_Utils_Array::value($token, $loc)) { | |
289 | foreach ($loc[$token] as $index => $entity) { | |
290 | $value = $entity[$token]; | |
291 | break; | |
292 | } | |
293 | } | |
294 | } | |
295 | ||
296 | if ($escapeSmarty) { | |
297 | $value = self::tokenEscapeSmarty($value); | |
298 | } | |
299 | ||
300 | return $value; | |
301 | } | |
302 | ||
303 | /** | |
304 | * Replace all the org-level tokens in $str | |
305 | * | |
306 | * @param string $str The string with tokens to be replaced | |
307 | * @param object $org Associative array of org properties | |
308 | * @param boolean $html Replace tokens with HTML or plain text | |
309 | * | |
310 | * @return string The processed string | |
311 | * @access public | |
312 | * @static | |
313 | */ | |
314 | public static function &replaceOrgTokens($str, &$org, $html = FALSE, $escapeSmarty = FALSE) { | |
21bb6c7b DL |
315 | self::$_tokens['org'] = |
316 | array_merge( | |
317 | array_keys(CRM_Contact_BAO_Contact::importableFields('Organization')), | |
318 | array('address', 'display_name', 'checksum', 'contact_id') | |
319 | ); | |
6a488035 TO |
320 | |
321 | $cv = NULL; | |
322 | foreach (self::$_tokens['org'] as $token) { | |
323 | // print "Getting token value for $token<br/><br/>"; | |
324 | if ($token == '') { | |
325 | continue; | |
326 | } | |
327 | ||
328 | /* If the string doesn't contain this token, skip it. */ | |
329 | ||
330 | if (!self::token_match('org', $token, $str)) { | |
331 | continue; | |
332 | } | |
333 | ||
334 | /* Construct value from $token and $contact */ | |
335 | ||
336 | $value = NULL; | |
337 | ||
338 | if ($cfID = CRM_Core_BAO_CustomField::getKeyID($token)) { | |
339 | // only generate cv if we need it | |
340 | if ($cv === NULL) { | |
341 | $cv = CRM_Core_BAO_CustomValue::getContactValues($org['contact_id']); | |
342 | } | |
343 | foreach ($cv as $cvFieldID => $value) { | |
344 | if ($cvFieldID == $cfID) { | |
345 | $value = CRM_Core_BAO_CustomOption::getOptionLabel($cfID, $value); | |
346 | break; | |
347 | } | |
348 | } | |
349 | } | |
350 | elseif ($token == 'checksum') { | |
351 | $cs = CRM_Contact_BAO_Contact_Utils::generateChecksum($org['contact_id']); | |
352 | $value = "cs={$cs}"; | |
353 | } | |
354 | elseif ($token == 'address') { | |
355 | /* Build the location values array */ | |
356 | ||
357 | $loc = array(); | |
358 | $loc['display_name'] = CRM_Utils_Array::retrieveValueRecursive($org, 'display_name'); | |
359 | $loc['street_address'] = CRM_Utils_Array::retrieveValueRecursive($org, 'street_address'); | |
360 | $loc['city'] = CRM_Utils_Array::retrieveValueRecursive($org, 'city'); | |
361 | $loc['state_province'] = CRM_Utils_Array::retrieveValueRecursive($org, 'state_province'); | |
362 | $loc['postal_code'] = CRM_Utils_Array::retrieveValueRecursive($org, 'postal_code'); | |
363 | ||
364 | /* Construct the address token */ | |
365 | ||
366 | $value = CRM_Utils_Address::format($loc); | |
367 | if ($html) { | |
368 | $value = str_replace("\n", '<br />', $value); | |
369 | } | |
370 | } | |
371 | else { | |
372 | $value = CRM_Utils_Array::retrieveValueRecursive($org, $token); | |
373 | } | |
374 | ||
375 | self::token_replace('org', $token, $value, $str, $escapeSmarty); | |
376 | } | |
377 | ||
378 | return $str; | |
379 | } | |
380 | ||
381 | /** | |
382 | * Replace all mailing tokens in $str | |
383 | * | |
384 | * @param string $str The string with tokens to be replaced | |
385 | * @param object $mailing The mailing BAO, or null for validation | |
386 | * @param boolean $html Replace tokens with HTML or plain text | |
387 | * | |
388 | * @return string The processed sstring | |
389 | * @access public | |
390 | * @static | |
391 | */ | |
21bb6c7b DL |
392 | public static function &replaceMailingTokens( |
393 | $str, | |
394 | &$mailing, | |
395 | $html = FALSE, | |
396 | $knownTokens = NULL, | |
397 | $escapeSmarty = FALSE | |
398 | ) { | |
6a488035 TO |
399 | $key = 'mailing'; |
400 | if (!$knownTokens || !isset($knownTokens[$key])) { | |
401 | return $str; | |
402 | } | |
403 | ||
8bab0eb0 | 404 | $str = preg_replace_callback( |
6a488035 | 405 | self::tokenRegex($key), |
8bab0eb0 DL |
406 | function ($matches) use(&$mailing, $escapeSmarty) { |
407 | return CRM_Utils_Token::getMailingTokenReplacement($matches[1], $mailing, $escapeSmarty); | |
408 | }, | |
409 | $str | |
6a488035 TO |
410 | ); |
411 | return $str; | |
412 | } | |
413 | ||
414 | public static function getMailingTokenReplacement($token, &$mailing, $escapeSmarty = FALSE) { | |
415 | $value = ''; | |
416 | switch ($token) { | |
417 | // CRM-7663 | |
418 | ||
419 | case 'id': | |
420 | $value = $mailing ? $mailing->id : 'undefined'; | |
421 | break; | |
422 | ||
423 | case 'name': | |
424 | $value = $mailing ? $mailing->name : 'Mailing Name'; | |
425 | break; | |
426 | ||
427 | case 'group': | |
428 | $groups = $mailing ? $mailing->getGroupNames() : array('Mailing Groups'); | |
429 | $value = implode(', ', $groups); | |
430 | break; | |
431 | ||
432 | case 'subject': | |
433 | $value = $mailing->subject; | |
434 | break; | |
435 | ||
436 | case 'viewUrl': | |
437 | $value = CRM_Utils_System::url('civicrm/mailing/view', | |
438 | "reset=1&id={$mailing->id}", | |
439 | TRUE, NULL, FALSE, TRUE | |
440 | ); | |
441 | break; | |
442 | ||
443 | case 'editUrl': | |
444 | $value = CRM_Utils_System::url('civicrm/mailing/send', | |
445 | "reset=1&mid={$mailing->id}&continue=true", | |
446 | TRUE, NULL, FALSE, TRUE | |
447 | ); | |
448 | break; | |
449 | ||
450 | case 'scheduleUrl': | |
451 | $value = CRM_Utils_System::url('civicrm/mailing/schedule', | |
452 | "reset=1&mid={$mailing->id}", | |
453 | TRUE, NULL, FALSE, TRUE | |
454 | ); | |
455 | break; | |
456 | ||
457 | case 'html': | |
458 | $page = new CRM_Mailing_Page_View(); | |
459 | $value = $page->run($mailing->id, NULL, FALSE); | |
460 | break; | |
461 | ||
462 | case 'approvalStatus': | |
463 | $mailApprovalStatus = CRM_Mailing_PseudoConstant::approvalStatus(); | |
464 | $value = $mailApprovalStatus[$mailing->approval_status_id]; | |
465 | break; | |
466 | ||
467 | case 'approvalNote': | |
468 | $value = $mailing->approval_note; | |
469 | break; | |
470 | ||
471 | case 'approveUrl': | |
472 | $value = CRM_Utils_System::url('civicrm/mailing/approve', | |
473 | "reset=1&mid={$mailing->id}", | |
474 | TRUE, NULL, FALSE, TRUE | |
475 | ); | |
476 | break; | |
477 | ||
478 | case 'creator': | |
479 | $value = CRM_Contact_BAO_Contact::displayName($mailing->created_id); | |
480 | break; | |
481 | ||
482 | case 'creatorEmail': | |
483 | $value = CRM_Contact_BAO_Contact::getPrimaryEmail($mailing->created_id); | |
484 | break; | |
485 | ||
486 | default: | |
487 | $value = "{mailing.$token}"; | |
488 | break; | |
489 | } | |
490 | ||
491 | if ($escapeSmarty) { | |
492 | $value = self::tokenEscapeSmarty($value); | |
493 | } | |
494 | return $value; | |
495 | } | |
496 | ||
497 | /** | |
498 | * Replace all action tokens in $str | |
499 | * | |
500 | * @param string $str The string with tokens to be replaced | |
501 | * @param array $addresses Assoc. array of VERP event addresses | |
502 | * @param array $urls Assoc. array of action URLs | |
503 | * @param boolean $html Replace tokens with HTML or plain text | |
504 | * @param array $knownTokens A list of tokens that are known to exist in the email body | |
505 | * | |
506 | * @return string The processed string | |
507 | * @access public | |
508 | * @static | |
509 | */ | |
510 | public static function &replaceActionTokens( | |
511 | $str, | |
512 | &$addresses, | |
513 | &$urls, | |
514 | $html = FALSE, | |
515 | $knownTokens = NULL, | |
516 | $escapeSmarty = FALSE | |
517 | ) { | |
518 | $key = 'action'; | |
519 | // here we intersect with the list of pre-configured valid tokens | |
520 | // so that we remove anything we do not recognize | |
521 | // I hope to move this step out of here soon and | |
522 | // then we will just iterate on a list of tokens that are passed to us | |
523 | if (!$knownTokens || !CRM_Utils_Array::value($key, $knownTokens)) { | |
524 | return $str; | |
525 | } | |
526 | ||
8bab0eb0 DL |
527 | $str = preg_replace_callback( |
528 | self::tokenRegex($key), | |
529 | function ($matches) use(&$addresses, &$urls, $html, $escapeSmarty) { | |
530 | return CRM_Utils_Token::getActionTokenReplacement($matches[1], $addresses, $urls, $html, $escapeSmarty); | |
531 | }, | |
6a488035 TO |
532 | $str |
533 | ); | |
534 | return $str; | |
535 | } | |
536 | ||
21bb6c7b DL |
537 | public static function getActionTokenReplacement( |
538 | $token, | |
539 | &$addresses, | |
540 | &$urls, | |
541 | $html = FALSE, | |
542 | $escapeSmarty = FALSE | |
543 | ) { | |
6a488035 TO |
544 | /* If the token is an email action, use it. Otherwise, find the |
545 | * appropriate URL */ | |
546 | ||
547 | if (!in_array($token, self::$_tokens['action'])) { | |
548 | $value = "{action.$token}"; | |
549 | } | |
550 | else { | |
551 | $value = CRM_Utils_Array::value($token, $addresses); | |
552 | ||
553 | if ($value == NULL) { | |
554 | $value = CRM_Utils_Array::value($token, $urls); | |
555 | } | |
556 | ||
557 | if ($value && $html) { | |
558 | //fix for CRM-2318 | |
559 | if ((substr($token, -3) != 'Url') && ($token != 'forward')) { | |
560 | $value = "mailto:$value"; | |
561 | } | |
562 | } | |
563 | elseif ($value && !$html) { | |
564 | $value = str_replace('&', '&', $value); | |
565 | } | |
566 | } | |
567 | ||
568 | if ($escapeSmarty) { | |
569 | $value = self::tokenEscapeSmarty($value); | |
570 | } | |
571 | return $value; | |
572 | } | |
573 | ||
574 | /** | |
575 | * Replace all the contact-level tokens in $str with information from | |
576 | * $contact. | |
577 | * | |
578 | * @param string $str The string with tokens to be replaced | |
579 | * @param array $contact Associative array of contact properties | |
580 | * @param boolean $html Replace tokens with HTML or plain text | |
581 | * @param array $knownTokens A list of tokens that are known to exist in the email body | |
582 | * @param boolean $returnBlankToken return unevaluated token if value is null | |
583 | * | |
584 | * @return string The processed string | |
585 | * @access public | |
586 | * @static | |
587 | */ | |
21bb6c7b DL |
588 | public static function &replaceContactTokens( |
589 | $str, | |
590 | &$contact, | |
591 | $html = FALSE, | |
592 | $knownTokens = NULL, | |
593 | $returnBlankToken = FALSE, | |
594 | $escapeSmarty = FALSE | |
6a488035 TO |
595 | ) { |
596 | $key = 'contact'; | |
597 | if (self::$_tokens[$key] == NULL) { | |
598 | /* This should come from UF */ | |
599 | ||
21bb6c7b DL |
600 | self::$_tokens[$key] = |
601 | array_merge( | |
602 | array_keys(CRM_Contact_BAO_Contact::exportableFields('All')), | |
603 | array('checksum', 'contact_id') | |
604 | ); | |
6a488035 TO |
605 | } |
606 | ||
607 | // here we intersect with the list of pre-configured valid tokens | |
608 | // so that we remove anything we do not recognize | |
609 | // I hope to move this step out of here soon and | |
610 | // then we will just iterate on a list of tokens that are passed to us | |
611 | if (!$knownTokens || !CRM_Utils_Array::value($key, $knownTokens)) { | |
612 | return $str; | |
613 | } | |
614 | ||
8bab0eb0 | 615 | $str = preg_replace_callback( |
21bb6c7b | 616 | self::tokenRegex($key), |
8bab0eb0 DL |
617 | function ($matches) use(&$contact, $key, $html, $escapeSmarty) { |
618 | return CRM_Utils_Token::getHookTokenReplacement($matches[1], $contact, $key, $html, $escapeSmarty); | |
619 | }, | |
6a488035 TO |
620 | $str |
621 | ); | |
622 | ||
623 | $str = preg_replace('/\\\\|\{(\s*)?\}/', ' ', $str); | |
624 | return $str; | |
625 | } | |
626 | ||
21bb6c7b DL |
627 | public static function getContactTokenReplacement( |
628 | $token, | |
629 | &$contact, | |
630 | $html = FALSE, | |
631 | $returnBlankToken = FALSE, | |
632 | $escapeSmarty = FALSE | |
6a488035 TO |
633 | ) { |
634 | if (self::$_tokens['contact'] == NULL) { | |
635 | /* This should come from UF */ | |
636 | ||
21bb6c7b DL |
637 | self::$_tokens['contact'] = |
638 | array_merge( | |
639 | array_keys(CRM_Contact_BAO_Contact::exportableFields('All')), | |
640 | array('checksum', 'contact_id') | |
641 | ); | |
6a488035 TO |
642 | } |
643 | ||
644 | /* Construct value from $token and $contact */ | |
645 | ||
646 | $value = NULL; | |
647 | ||
648 | // check if the token we were passed is valid | |
649 | // we have to do this because this function is | |
650 | // called only when we find a token in the string | |
651 | ||
652 | if (!in_array($token, self::$_tokens['contact'])) { | |
653 | $value = "{contact.$token}"; | |
654 | } | |
655 | elseif ($token == 'checksum') { | |
656 | $hash = CRM_Utils_Array::value('hash', $contact); | |
657 | $contactID = CRM_Utils_Array::retrieveValueRecursive($contact, 'contact_id'); | |
658 | $cs = CRM_Contact_BAO_Contact_Utils::generateChecksum($contactID, | |
659 | NULL, | |
660 | NULL, | |
661 | $hash | |
662 | ); | |
663 | $value = "cs={$cs}"; | |
664 | } | |
665 | else { | |
666 | $value = CRM_Utils_Array::retrieveValueRecursive($contact, $token); | |
667 | } | |
668 | ||
669 | if (!$html) { | |
670 | $value = str_replace('&', '&', $value); | |
671 | } | |
672 | ||
673 | // if null then return actual token | |
674 | if ($returnBlankToken && !$value) { | |
675 | $value = "{contact.$token}"; | |
676 | } | |
677 | ||
678 | if ($escapeSmarty) { | |
679 | $value = self::tokenEscapeSmarty($value); | |
680 | } | |
681 | ||
682 | return $value; | |
683 | } | |
684 | ||
685 | /** | |
686 | * Replace all the hook tokens in $str with information from | |
687 | * $contact. | |
688 | * | |
689 | * @param string $str The string with tokens to be replaced | |
690 | * @param array $contact Associative array of contact properties (including hook token values) | |
691 | * @param boolean $html Replace tokens with HTML or plain text | |
692 | * | |
693 | * @return string The processed string | |
694 | * @access public | |
695 | * @static | |
696 | */ | |
21bb6c7b DL |
697 | public static function &replaceHookTokens( |
698 | $str, | |
699 | &$contact, | |
700 | &$categories, | |
701 | $html = FALSE, | |
702 | $escapeSmarty = FALSE | |
703 | ) { | |
6a488035 | 704 | foreach ($categories as $key) { |
8bab0eb0 | 705 | $str = preg_replace_callback( |
21bb6c7b | 706 | self::tokenRegex($key), |
8bab0eb0 DL |
707 | function ($matches) use(&$contact, $key, $html, $escapeSmarty) { |
708 | return CRM_Utils_Token::getHookTokenReplacement($matches[1], $contact, $key, $html, $escapeSmarty); | |
709 | }, | |
6a488035 TO |
710 | $str |
711 | ); | |
712 | } | |
713 | return $str; | |
714 | } | |
715 | ||
21bb6c7b DL |
716 | public static function getHookTokenReplacement( |
717 | $token, | |
718 | &$contact, | |
719 | $category, | |
720 | $html = FALSE, | |
721 | $escapeSmarty = FALSE | |
722 | ) { | |
6a488035 TO |
723 | $value = CRM_Utils_Array::value("{$category}.{$token}", $contact); |
724 | ||
21bb6c7b | 725 | if ($value && !$html) { |
6a488035 TO |
726 | $value = str_replace('&', '&', $value); |
727 | } | |
728 | ||
729 | if ($escapeSmarty) { | |
730 | $value = self::tokenEscapeSmarty($value); | |
731 | } | |
732 | ||
733 | return $value; | |
734 | } | |
735 | ||
736 | /** | |
737 | * unescapeTokens removes any characters that caused the replacement routines to skip token replacement | |
738 | * for example {{token}} or \{token} will result in {token} in the final email | |
739 | * | |
740 | * this routine will remove the extra backslashes and braces | |
741 | * | |
742 | * @param $str ref to the string that will be scanned and modified | |
743 | * @return void this function works directly on the string that is passed | |
744 | * @access public | |
745 | * @static | |
746 | */ | |
747 | public static function unescapeTokens(&$str) { | |
748 | $str = preg_replace('/\\\\|\{(\{\w+\.\w+\})\}/', '\\1', $str); | |
749 | } | |
750 | ||
751 | /** | |
752 | * Replace unsubscribe tokens | |
753 | * | |
754 | * @param string $str the string with tokens to be replaced | |
755 | * @param object $domain The domain BAO | |
756 | * @param array $groups The groups (if any) being unsubscribed | |
757 | * @param boolean $html Replace tokens with html or plain text | |
758 | * @param int $contact_id The contact ID | |
759 | * @param string hash The security hash of the unsub event | |
760 | * | |
761 | * @return string The processed string | |
762 | * @access public | |
763 | * @static | |
764 | */ | |
765 | public static function &replaceUnsubscribeTokens( | |
766 | $str, | |
767 | &$domain, | |
768 | &$groups, | |
769 | $html, | |
770 | $contact_id, | |
771 | $hash | |
772 | ) { | |
773 | if (self::token_match('unsubscribe', 'group', $str)) { | |
774 | if (!empty($groups)) { | |
775 | $config = CRM_Core_Config::singleton(); | |
776 | $base = CRM_Utils_System::baseURL(); | |
777 | ||
778 | // FIXME: an ugly hack for CRM-2035, to be dropped once CRM-1799 is implemented | |
779 | $dao = new CRM_Contact_DAO_Group(); | |
780 | $dao->find(); | |
781 | while ($dao->fetch()) { | |
782 | if (substr($dao->visibility, 0, 6) == 'Public') { | |
783 | $visibleGroups[] = $dao->id; | |
784 | } | |
785 | } | |
786 | $value = implode(', ', $groups); | |
787 | self::token_replace('unsubscribe', 'group', $value, $str); | |
788 | } | |
789 | } | |
790 | return $str; | |
791 | } | |
792 | ||
793 | /** | |
794 | * Replace resubscribe tokens | |
795 | * | |
796 | * @param string $str the string with tokens to be replaced | |
797 | * @param object $domain The domain BAO | |
798 | * @param array $groups The groups (if any) being resubscribed | |
799 | * @param boolean $html Replace tokens with html or plain text | |
800 | * @param int $contact_id The contact ID | |
801 | * @param string hash The security hash of the resub event | |
802 | * | |
803 | * @return string The processed string | |
804 | * @access public | |
805 | * @static | |
806 | */ | |
807 | public static function &replaceResubscribeTokens($str, &$domain, &$groups, $html, | |
808 | $contact_id, $hash | |
809 | ) { | |
810 | if (self::token_match('resubscribe', 'group', $str)) { | |
811 | if (!empty($groups)) { | |
812 | $value = implode(', ', $groups); | |
813 | self::token_replace('resubscribe', 'group', $value, $str); | |
814 | } | |
815 | } | |
816 | return $str; | |
817 | } | |
818 | ||
819 | /** | |
820 | * Replace subscription-confirmation-request tokens | |
821 | * | |
822 | * @param string $str The string with tokens to be replaced | |
823 | * @param string $group The name of the group being subscribed | |
824 | * @param boolean $html Replace tokens with html or plain text | |
825 | * | |
826 | * @return string The processed string | |
827 | * @access public | |
828 | * @static | |
829 | */ | |
830 | public static function &replaceSubscribeTokens($str, $group, $url, $html) { | |
831 | if (self::token_match('subscribe', 'group', $str)) { | |
832 | self::token_replace('subscribe', 'group', $group, $str); | |
833 | } | |
834 | if (self::token_match('subscribe', 'url', $str)) { | |
835 | self::token_replace('subscribe', 'url', $url, $str); | |
836 | } | |
837 | return $str; | |
838 | } | |
839 | ||
840 | /** | |
841 | * Replace subscription-invitation tokens | |
842 | * | |
843 | * @param string $str The string with tokens to be replaced | |
844 | * | |
845 | * @return string The processed string | |
846 | * @access public | |
847 | * @static | |
848 | */ | |
849 | public static function &replaceSubscribeInviteTokens($str) { | |
850 | if (preg_match('/\{action\.subscribeUrl\}/', $str)) { | |
851 | $url = CRM_Utils_System::url('civicrm/mailing/subscribe', | |
852 | 'reset=1', | |
853 | TRUE, NULL, TRUE, TRUE | |
854 | ); | |
855 | $str = preg_replace('/\{action\.subscribeUrl\}/', $url, $str); | |
856 | } | |
857 | ||
858 | if (preg_match('/\{action\.subscribeUrl.\d+\}/', $str, $matches)) { | |
859 | foreach ($matches as $key => $value) { | |
860 | $gid = substr($value, 21, -1); | |
861 | $url = CRM_Utils_System::url('civicrm/mailing/subscribe', | |
862 | "reset=1&gid={$gid}", | |
863 | TRUE, NULL, TRUE, TRUE | |
864 | ); | |
865 | $url = str_replace('&', '&', $url); | |
866 | $str = preg_replace('/' . preg_quote($value) . '/', $url, $str); | |
867 | } | |
868 | } | |
869 | ||
870 | if (preg_match('/\{action\.subscribe.\d+\}/', $str, $matches)) { | |
871 | foreach ($matches as $key => $value) { | |
872 | $gid = substr($value, 18, -1); | |
873 | $config = CRM_Core_Config::singleton(); | |
874 | $domain = CRM_Core_BAO_MailSettings::defaultDomain(); | |
875 | $localpart = CRM_Core_BAO_MailSettings::defaultLocalpart(); | |
876 | // we add the 0.0000000000000000 part to make this match the other email patterns (with action, two ids and a hash) | |
877 | $str = preg_replace('/' . preg_quote($value) . '/', "mailto:{$localpart}s.{$gid}.0.0000000000000000@$domain", $str); | |
878 | } | |
879 | } | |
880 | return $str; | |
881 | } | |
882 | ||
883 | /** | |
884 | * Replace welcome/confirmation tokens | |
885 | * | |
886 | * @param string $str The string with tokens to be replaced | |
887 | * @param string $group The name of the group being subscribed | |
888 | * @param boolean $html Replace tokens with html or plain text | |
889 | * | |
890 | * @return string The processed string | |
891 | * @access public | |
892 | * @static | |
893 | */ | |
894 | public static function &replaceWelcomeTokens($str, $group, $html) { | |
895 | if (self::token_match('welcome', 'group', $str)) { | |
896 | self::token_replace('welcome', 'group', $group, $str); | |
897 | } | |
898 | return $str; | |
899 | } | |
900 | ||
901 | /** | |
902 | * Find unprocessed tokens (call this last) | |
903 | * | |
904 | * @param string $str The string to search | |
905 | * | |
906 | * @return array Array of tokens that weren't replaced | |
907 | * @access public | |
908 | * @static | |
909 | */ | |
910 | public static function &unmatchedTokens(&$str) { | |
911 | //preg_match_all('/[^\{\\\\]\{(\w+\.\w+)\}[^\}]/', $str, $match); | |
912 | preg_match_all('/\{(\w+\.\w+)\}/', $str, $match); | |
913 | return $match[1]; | |
914 | } | |
915 | ||
916 | /** | |
917 | * Find and replace tokens for each component | |
918 | * | |
919 | * @param string $str The string to search | |
920 | * @param array $contact Associative array of contact properties | |
921 | * @param array $components A list of tokens that are known to exist in the email body | |
922 | * | |
923 | * @return string The processed string | |
924 | * @access public | |
925 | * @static | |
926 | */ | |
927 | public static function &replaceComponentTokens(&$str, $contact, $components, $escapeSmarty = FALSE, $returnEmptyToken = TRUE) { | |
928 | if (!is_array($components) || empty($contact)) { | |
929 | return $str; | |
930 | } | |
931 | ||
932 | foreach ($components as $name => $tokens) { | |
933 | if (!is_array($tokens) || empty($tokens)) { | |
934 | continue; | |
935 | } | |
936 | ||
937 | foreach ($tokens as $token) { | |
938 | if (self::token_match($name, $token, $str) && isset($contact[$name . '.' . $token])) { | |
939 | self::token_replace($name, $token, $contact[$name . '.' . $token], $str, $escapeSmarty); | |
940 | } | |
941 | elseif (!$returnEmptyToken) { | |
942 | //replacing empty token | |
943 | self::token_replace($name, $token, "", $str, $escapeSmarty); | |
944 | } | |
945 | } | |
946 | } | |
947 | return $str; | |
948 | } | |
949 | ||
950 | /** | |
951 | * Get array of string tokens | |
952 | * | |
953 | * @param $string the input string to parse for tokens | |
954 | * | |
955 | * @return $tokens array of tokens mentioned in field | |
956 | * @access public | |
957 | * @static | |
958 | */ | |
959 | static function getTokens($string) { | |
960 | $matches = array(); | |
961 | $tokens = array(); | |
962 | preg_match_all('/(?<!\{|\\\\)\{(\w+\.\w+)\}(?!\})/', | |
963 | $string, | |
964 | $matches, | |
965 | PREG_PATTERN_ORDER | |
966 | ); | |
967 | ||
968 | if ($matches[1]) { | |
969 | foreach ($matches[1] as $token) { | |
970 | list($type, $name) = preg_split('/\./', $token, 2); | |
971 | if ($name && $type) { | |
972 | if (!isset($tokens[$type])) { | |
973 | $tokens[$type] = array(); | |
974 | } | |
975 | $tokens[$type][] = $name; | |
976 | } | |
977 | } | |
978 | } | |
979 | return $tokens; | |
980 | } | |
981 | ||
982 | /** | |
983 | * gives required details of contacts in an indexed array format so we | |
984 | * can iterate in a nice loop and do token evaluation | |
985 | * | |
986 | * @param array $contactIds of contacts | |
987 | * @param array $returnProperties of required properties | |
988 | * @param boolean $skipOnHold don't return on_hold contact info also. | |
989 | * @param boolean $skipDeceased don't return deceased contact info. | |
990 | * @param array $extraParams extra params | |
991 | * @param array $tokens the list of tokens we've extracted from the content | |
992 | * @param int $jobID the mailing list jobID - this is a legacy param | |
993 | * | |
994 | * @return array | |
995 | * @access public | |
996 | * @static | |
997 | */ | |
998 | static function getTokenDetails($contactIDs, | |
999 | $returnProperties = NULL, | |
1000 | $skipOnHold = TRUE, | |
1001 | $skipDeceased = TRUE, | |
1002 | $extraParams = NULL, | |
1003 | $tokens = array(), | |
1004 | $className = NULL, | |
1005 | $jobID = NULL | |
1006 | ) { | |
1007 | if (empty($contactIDs)) { | |
1008 | // putting a fatal here so we can track if/when this happens | |
1009 | CRM_Core_Error::fatal(); | |
1010 | } | |
1011 | ||
1012 | $params = array(); | |
1013 | foreach ($contactIDs as $key => $contactID) { | |
1014 | $params[] = array( | |
1015 | CRM_Core_Form::CB_PREFIX . $contactID, | |
1016 | '=', 1, 0, 0, | |
1017 | ); | |
1018 | } | |
1019 | ||
1020 | // fix for CRM-2613 | |
1021 | if ($skipDeceased) { | |
1022 | $params[] = array('is_deceased', '=', 0, 0, 0); | |
1023 | } | |
1024 | ||
1025 | //fix for CRM-3798 | |
1026 | if ($skipOnHold) { | |
1027 | $params[] = array('on_hold', '=', 0, 0, 0); | |
1028 | } | |
1029 | ||
1030 | if ($extraParams) { | |
1031 | $params = array_merge($params, $extraParams); | |
1032 | } | |
1033 | ||
1034 | // if return properties are not passed then get all return properties | |
1035 | if (empty($returnProperties)) { | |
1036 | $fields = array_merge(array_keys(CRM_Contact_BAO_Contact::exportableFields()), | |
1037 | array('display_name', 'checksum', 'contact_id') | |
1038 | ); | |
1039 | foreach ($fields as $key => $val) { | |
1040 | $returnProperties[$val] = 1; | |
1041 | } | |
1042 | } | |
1043 | ||
1044 | $custom = array(); | |
1045 | foreach ($returnProperties as $name => $dontCare) { | |
1046 | $cfID = CRM_Core_BAO_CustomField::getKeyID($name); | |
1047 | if ($cfID) { | |
1048 | $custom[] = $cfID; | |
1049 | } | |
1050 | } | |
1051 | ||
1052 | //get the total number of contacts to fetch from database. | |
1053 | $numberofContacts = count($contactIDs); | |
1054 | $query = new CRM_Contact_BAO_Query($params, $returnProperties); | |
1055 | ||
1056 | $details = $query->apiQuery($params, $returnProperties, NULL, NULL, 0, $numberofContacts); | |
1057 | ||
1058 | $contactDetails = &$details[0]; | |
1059 | ||
1060 | foreach ($contactIDs as $key => $contactID) { | |
1061 | if (array_key_exists($contactID, $contactDetails)) { | |
1062 | if (CRM_Utils_Array::value('preferred_communication_method', $returnProperties) == 1 | |
1063 | && array_key_exists('preferred_communication_method', $contactDetails[$contactID]) | |
1064 | ) { | |
1065 | $pcm = CRM_Core_PseudoConstant::pcm(); | |
1066 | ||
1067 | // communication Prefferance | |
1068 | $contactPcm = explode(CRM_Core_DAO::VALUE_SEPARATOR, | |
1069 | $contactDetails[$contactID]['preferred_communication_method'] | |
1070 | ); | |
1071 | $result = array(); | |
1072 | foreach ($contactPcm as $key => $val) { | |
1073 | if ($val) { | |
1074 | $result[$val] = $pcm[$val]; | |
1075 | } | |
1076 | } | |
1077 | $contactDetails[$contactID]['preferred_communication_method'] = implode(', ', $result); | |
1078 | } | |
1079 | ||
1080 | foreach ($custom as $cfID) { | |
1081 | if (isset($contactDetails[$contactID]["custom_{$cfID}"])) { | |
1082 | $contactDetails[$contactID]["custom_{$cfID}"] = CRM_Core_BAO_CustomField::getDisplayValue($contactDetails[$contactID]["custom_{$cfID}"], | |
1083 | $cfID, $details[1] | |
1084 | ); | |
1085 | } | |
1086 | } | |
1087 | ||
1088 | //special case for greeting replacement | |
1089 | foreach (array( | |
1090 | 'email_greeting', 'postal_greeting', 'addressee') as $val) { | |
1091 | if (CRM_Utils_Array::value($val, $contactDetails[$contactID])) { | |
1092 | $contactDetails[$contactID][$val] = $contactDetails[$contactID]["{$val}_display"]; | |
1093 | } | |
1094 | } | |
1095 | } | |
1096 | } | |
1097 | ||
1098 | // also call a hook and get token details | |
1099 | CRM_Utils_Hook::tokenValues($details[0], | |
1100 | $contactIDs, | |
1101 | $jobID, | |
1102 | $tokens, | |
1103 | $className | |
1104 | ); | |
1105 | return $details; | |
1106 | } | |
1107 | ||
1108 | /** | |
1109 | * gives required details of contribuion in an indexed array format so we | |
1110 | * can iterate in a nice loop and do token evaluation | |
1111 | * | |
1112 | * @param array $contributionId one contribution id | |
1113 | * @param array $returnProperties of required properties | |
1114 | * @param boolean $skipOnHold don't return on_hold contact info. | |
1115 | * @param boolean $skipDeceased don't return deceased contact info. | |
1116 | * @param array $extraParams extra params | |
1117 | * @param array $tokens the list of tokens we've extracted from the content | |
1118 | * | |
1119 | * @return array | |
1120 | * @access public | |
1121 | * @static | |
1122 | */ | |
1123 | static function getContributionTokenDetails($contributionIDs, | |
1124 | $returnProperties = NULL, | |
1125 | $extraParams = NULL, | |
1126 | $tokens = array(), | |
1127 | $className = NULL | |
1128 | ) { | |
1129 | if (empty($contributionIDs)) { | |
1130 | // putting a fatal here so we can track if/when this happens | |
1131 | CRM_Core_Error::fatal(); | |
1132 | } | |
1133 | ||
1134 | $details = array(); | |
1135 | ||
1136 | // no apiQuery helper yet, so do a loop and find contribution by id | |
1137 | foreach ($contributionIDs as $contributionID) { | |
1138 | ||
1139 | $dao = new CRM_Contribute_DAO_Contribution(); | |
1140 | $dao->id = $contributionID; | |
1141 | ||
1142 | if ($dao->find(TRUE)) { | |
1143 | ||
1144 | $details[$dao->id] = array(); | |
1145 | CRM_Core_DAO::storeValues($dao, $details[$dao->id]); | |
1146 | ||
1147 | // do the necessary transformation | |
1148 | if (CRM_Utils_Array::value('payment_instrument_id', $details[$dao->id])) { | |
1149 | $piId = $details[$dao->id]['payment_instrument_id']; | |
1150 | $pis = CRM_Contribute_PseudoConstant::paymentInstrument(); | |
1151 | $details[$dao->id]['payment_instrument'] = $pis[$piId]; | |
1152 | } | |
1153 | if (CRM_Utils_Array::value('campaign_id', $details[$dao->id])) { | |
1154 | $campaignId = $details[$dao->id]['campaign_id']; | |
1155 | $campaigns = CRM_Campaign_BAO_Campaign::getCampaigns($campaignId); | |
1156 | $details[$dao->id]['campaign'] = $campaigns[$campaignId]; | |
1157 | } | |
1158 | ||
1159 | // TODO: call a hook to get token contribution details | |
1160 | } | |
1161 | } | |
1162 | ||
1163 | return $details; | |
1164 | } | |
1165 | ||
1166 | /** | |
1167 | * replace greeting tokens exists in message/subject | |
1168 | * | |
1169 | * @access public | |
1170 | */ | |
1171 | static function replaceGreetingTokens(&$tokenString, $contactDetails = NULL, $contactId = NULL, $className = NULL) { | |
1172 | ||
1173 | if (!$contactDetails && !$contactId) { | |
1174 | return; | |
1175 | } | |
1176 | ||
1177 | // check if there are any tokens | |
1178 | $greetingTokens = self::getTokens($tokenString); | |
1179 | ||
1180 | if (!empty($greetingTokens)) { | |
1181 | // first use the existing contact object for token replacement | |
1182 | if (!empty($contactDetails)) { | |
1183 | $tokenString = CRM_Utils_Token::replaceContactTokens($tokenString, $contactDetails, TRUE, $greetingTokens, TRUE); | |
1184 | } | |
1185 | ||
1186 | // check if there are any unevaluated tokens | |
1187 | $greetingTokens = self::getTokens($tokenString); | |
1188 | ||
1189 | // $greetingTokens not empty, means there are few tokens which are not evaluated, like custom data etc | |
1190 | // so retrieve it from database | |
1191 | if (!empty($greetingTokens) && array_key_exists('contact', $greetingTokens)) { | |
1192 | $greetingsReturnProperties = array_flip(CRM_Utils_Array::value('contact', $greetingTokens)); | |
1193 | $greetingsReturnProperties = array_fill_keys(array_keys($greetingsReturnProperties), 1); | |
1194 | $contactParams = array('contact_id' => $contactId); | |
1195 | ||
1196 | $greetingDetails = self::getTokenDetails($contactParams, | |
1197 | $greetingsReturnProperties, | |
1198 | FALSE, FALSE, NULL, | |
1199 | $greetingTokens, | |
1200 | $className | |
1201 | ); | |
1202 | ||
1203 | // again replace tokens | |
1204 | $tokenString = CRM_Utils_Token::replaceContactTokens($tokenString, | |
1205 | $greetingDetails, | |
1206 | TRUE, | |
1207 | $greetingTokens | |
1208 | ); | |
1209 | } | |
1210 | } | |
1211 | } | |
1212 | ||
1213 | static function flattenTokens(&$tokens) { | |
1214 | $flattenTokens = array(); | |
1215 | ||
1216 | foreach (array( | |
1217 | 'html', 'text', 'subject') as $prop) { | |
1218 | if (!isset($tokens[$prop])) { | |
1219 | continue; | |
1220 | } | |
1221 | foreach ($tokens[$prop] as $type => $names) { | |
1222 | if (!isset($flattenTokens[$type])) { | |
1223 | $flattenTokens[$type] = array(); | |
1224 | } | |
1225 | foreach ($names as $name) { | |
1226 | $flattenTokens[$type][$name] = 1; | |
1227 | } | |
1228 | } | |
1229 | } | |
1230 | ||
1231 | return $flattenTokens; | |
1232 | } | |
1233 | ||
1234 | /** | |
1235 | * Replace all user tokens in $str | |
1236 | * | |
1237 | * @param string $str The string with tokens to be replaced | |
1238 | * | |
1239 | * @return string The processed string | |
1240 | * @access public | |
1241 | * @static | |
1242 | */ | |
1243 | public static function &replaceUserTokens($str, $knownTokens = NULL, $escapeSmarty = FALSE) { | |
1244 | $key = 'user'; | |
1245 | if (!$knownTokens || | |
1246 | !isset($knownTokens[$key]) | |
1247 | ) { | |
1248 | return $str; | |
1249 | } | |
1250 | ||
8bab0eb0 DL |
1251 | $str = preg_replace_callback( |
1252 | self::tokenRegex($key), | |
1253 | function ($matches) use($escapeSmarty) { | |
1254 | return CRM_Utils_Token::getUserTokenReplacement($matches[1], $escapeSmarty); | |
1255 | }, | |
1256 | $str | |
6a488035 TO |
1257 | ); |
1258 | return $str; | |
1259 | } | |
1260 | ||
1261 | public static function getUserTokenReplacement($token, $escapeSmarty = FALSE) { | |
1262 | $value = ''; | |
1263 | ||
1264 | list($objectName, $objectValue) = explode('-', $token, 2); | |
1265 | ||
1266 | switch ($objectName) { | |
1267 | case 'permission': | |
1268 | $value = CRM_Core_Permission::permissionEmails($objectValue); | |
1269 | break; | |
1270 | ||
1271 | case 'role': | |
1272 | $value = CRM_Core_Permission::roleEmails($objectValue); | |
1273 | break; | |
1274 | } | |
1275 | ||
1276 | if ($escapeSmarty) { | |
1277 | $value = self::tokenEscapeSmarty($value); | |
1278 | } | |
1279 | ||
1280 | return $value; | |
1281 | } | |
1282 | ||
1283 | ||
1284 | protected static function _buildContributionTokens() { | |
1285 | $key = 'contribution'; | |
1286 | if (self::$_tokens[$key] == NULL) { | |
1287 | self::$_tokens[$key] = array_keys(array_merge(CRM_Contribute_BAO_Contribution::exportableFields('All'), | |
1288 | array('campaign', 'financial_type') | |
1289 | )); | |
1290 | } | |
1291 | } | |
1292 | ||
1293 | public static function &replaceContributionTokens($str, &$contribution, $html = FALSE, $knownTokens = NULL, $escapeSmarty = FALSE) { | |
1294 | self::_buildContributionTokens(); | |
1295 | ||
1296 | // here we intersect with the list of pre-configured valid tokens | |
1297 | // so that we remove anything we do not recognize | |
1298 | // I hope to move this step out of here soon and | |
1299 | // then we will just iterate on a list of tokens that are passed to us | |
1300 | $key = 'contribution'; | |
1301 | if (!$knownTokens || !CRM_Utils_Array::value($key, $knownTokens)) { | |
1302 | return $str; | |
1303 | } | |
1304 | ||
8bab0eb0 DL |
1305 | $str = preg_replace_callback( |
1306 | self::tokenRegex($key), | |
1307 | function ($matches) use(&$contribution, $html, $escapeSmarty) { | |
1308 | return CRM_Utils_Token::getContributionTokenReplacement($matches[1], $contribution, $html, $escapeSmarty); | |
1309 | }, | |
6a488035 TO |
1310 | $str |
1311 | ); | |
1312 | ||
1313 | $str = preg_replace('/\\\\|\{(\s*)?\}/', ' ', $str); | |
1314 | return $str; | |
1315 | } | |
1316 | ||
1317 | public static function getContributionTokenReplacement($token, &$contribution, $html = FALSE, $escapeSmarty = FALSE) { | |
1318 | self::_buildContributionTokens(); | |
1319 | ||
1320 | switch ($token) { | |
1321 | case 'total_amount': | |
1322 | case 'net_amount': | |
1323 | case 'fee_amount': | |
1324 | case 'non_deductible_amount': | |
1325 | $value = CRM_Utils_Money::format(CRM_Utils_Array::retrieveValueRecursive($contribution, $token)); | |
1326 | break; | |
1327 | ||
1328 | case 'receive_date': | |
1329 | $value = CRM_Utils_Array::retrieveValueRecursive($contribution, $token); | |
1330 | $value = CRM_Utils_Date::customFormat($value, NULL, array('j', 'm', 'Y')); | |
1331 | break; | |
1332 | ||
1333 | default: | |
1334 | if (!in_array($token, self::$_tokens['contribution'])) { | |
1335 | $value = "{contribution.$token}"; | |
1336 | } | |
1337 | else { | |
1338 | $value = CRM_Utils_Array::retrieveValueRecursive($contribution, $token); | |
1339 | } | |
1340 | break; | |
1341 | } | |
1342 | ||
1343 | ||
1344 | if ($escapeSmarty) { | |
1345 | $value = self::tokenEscapeSmarty($value); | |
1346 | } | |
1347 | return $value; | |
1348 | } | |
1349 | ||
1350 | function getPermissionEmails($permissionName) {} | |
1351 | ||
1352 | function getRoleEmails($roleName) {} | |
1353 | } |