Merge pull request #2575 from lcdservices/CRM-14287
[civicrm-core.git] / CRM / Utils / Token.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
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',
84 'id',
85 'description',
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.unsubscribeUrl' =>
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.unsubscribeUrl' => 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('}');
158 return preg_match("/(^|[^\{])$token/", $str);
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) {
195 return '/(?<!\{|\\\\)\{' . $token_type . '\.([\w]+(\-[\w\s]+)?)\}(?!\})/';
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 */
224 public static function &replaceDomainTokens(
225 $str,
226 &$domain,
227 $html = FALSE,
228 $knownTokens = NULL,
229 $escapeSmarty = FALSE
230 ) {
231 $key = 'domain';
232 if (
233 !$knownTokens || empty($knownTokens[$key])) {
234 return $str;
235 }
236
237 $str = preg_replace_callback(
238 self::tokenRegex($key),
239 function ($matches) use(&$domain, $html, $escapeSmarty) {
240 return CRM_Utils_Token::getDomainTokenReplacement($matches[1], $domain, $html, $escapeSmarty);
241 },
242 $str
243 );
244 return $str;
245 }
246
247 public static function getDomainTokenReplacement($token, &$domain, $html = FALSE, $escapeSmarty = FALSE) {
248 // check if the token we were passed is valid
249 // we have to do this because this function is
250 // called only when we find a token in the string
251
252 $loc = &$domain->getLocationValues();
253
254 if (!in_array($token, self::$_tokens['domain'])) {
255 $value = "{domain.$token}";
256 }
257 elseif ($token == 'address') {
258 static $addressCache = array();
259
260 $cache_key = $html ? 'address-html' : 'address-text';
261 if (array_key_exists($cache_key, $addressCache)) {
262 return $addressCache[$cache_key];
263 }
264
265 $value = NULL;
266 /* Construct the address token */
267
268 if (!empty($loc[$token])) {
269 if ($html) {
270 $value = $loc[$token][1]['display'];
271 $value = str_replace("\n", '<br />', $value);
272 }
273 else {
274 $value = $loc[$token][1]['display_text'];
275 }
276 $addressCache[$cache_key] = $value;
277 }
278 }
279 elseif ($token == 'name' || $token == 'id' || $token == 'description') {
280 $value = $domain->$token;
281 }
282 elseif ($token == 'phone' || $token == 'email') {
283 /* Construct the phone and email tokens */
284
285 $value = NULL;
286 if (!empty($loc[$token])) {
287 foreach ($loc[$token] as $index => $entity) {
288 $value = $entity[$token];
289 break;
290 }
291 }
292 }
293
294 if ($escapeSmarty) {
295 $value = self::tokenEscapeSmarty($value);
296 }
297
298 return $value;
299 }
300
301 /**
302 * Replace all the org-level tokens in $str
303 *
304 * @param string $str The string with tokens to be replaced
305 * @param object $org Associative array of org properties
306 * @param boolean $html Replace tokens with HTML or plain text
307 *
308 * @return string The processed string
309 * @access public
310 * @static
311 */
312 public static function &replaceOrgTokens($str, &$org, $html = FALSE, $escapeSmarty = FALSE) {
313 self::$_tokens['org'] =
314 array_merge(
315 array_keys(CRM_Contact_BAO_Contact::importableFields('Organization')),
316 array('address', 'display_name', 'checksum', 'contact_id')
317 );
318
319 $cv = NULL;
320 foreach (self::$_tokens['org'] as $token) {
321 // print "Getting token value for $token<br/><br/>";
322 if ($token == '') {
323 continue;
324 }
325
326 /* If the string doesn't contain this token, skip it. */
327
328 if (!self::token_match('org', $token, $str)) {
329 continue;
330 }
331
332 /* Construct value from $token and $contact */
333
334 $value = NULL;
335
336 if ($cfID = CRM_Core_BAO_CustomField::getKeyID($token)) {
337 // only generate cv if we need it
338 if ($cv === NULL) {
339 $cv = CRM_Core_BAO_CustomValue::getContactValues($org['contact_id']);
340 }
341 foreach ($cv as $cvFieldID => $value) {
342 if ($cvFieldID == $cfID) {
343 $value = CRM_Core_BAO_CustomOption::getOptionLabel($cfID, $value);
344 break;
345 }
346 }
347 }
348 elseif ($token == 'checksum') {
349 $cs = CRM_Contact_BAO_Contact_Utils::generateChecksum($org['contact_id']);
350 $value = "cs={$cs}";
351 }
352 elseif ($token == 'address') {
353 /* Build the location values array */
354
355 $loc = array();
356 $loc['display_name'] = CRM_Utils_Array::retrieveValueRecursive($org, 'display_name');
357 $loc['street_address'] = CRM_Utils_Array::retrieveValueRecursive($org, 'street_address');
358 $loc['city'] = CRM_Utils_Array::retrieveValueRecursive($org, 'city');
359 $loc['state_province'] = CRM_Utils_Array::retrieveValueRecursive($org, 'state_province');
360 $loc['postal_code'] = CRM_Utils_Array::retrieveValueRecursive($org, 'postal_code');
361
362 /* Construct the address token */
363
364 $value = CRM_Utils_Address::format($loc);
365 if ($html) {
366 $value = str_replace("\n", '<br />', $value);
367 }
368 }
369 else {
370 $value = CRM_Utils_Array::retrieveValueRecursive($org, $token);
371 }
372
373 self::token_replace('org', $token, $value, $str, $escapeSmarty);
374 }
375
376 return $str;
377 }
378
379 /**
380 * Replace all mailing tokens in $str
381 *
382 * @param string $str The string with tokens to be replaced
383 * @param object $mailing The mailing BAO, or null for validation
384 * @param boolean $html Replace tokens with HTML or plain text
385 *
386 * @return string The processed sstring
387 * @access public
388 * @static
389 */
390 public static function &replaceMailingTokens(
391 $str,
392 &$mailing,
393 $html = FALSE,
394 $knownTokens = NULL,
395 $escapeSmarty = FALSE
396 ) {
397 $key = 'mailing';
398 if (!$knownTokens || !isset($knownTokens[$key])) {
399 return $str;
400 }
401
402 $str = preg_replace_callback(
403 self::tokenRegex($key),
404 function ($matches) use(&$mailing, $escapeSmarty) {
405 return CRM_Utils_Token::getMailingTokenReplacement($matches[1], $mailing, $escapeSmarty);
406 },
407 $str
408 );
409 return $str;
410 }
411
412 public static function getMailingTokenReplacement($token, &$mailing, $escapeSmarty = FALSE) {
413 $value = '';
414 switch ($token) {
415 // CRM-7663
416
417 case 'id':
418 $value = $mailing ? $mailing->id : 'undefined';
419 break;
420
421 case 'name':
422 $value = $mailing ? $mailing->name : 'Mailing Name';
423 break;
424
425 case 'group':
426 $groups = $mailing ? $mailing->getGroupNames() : array('Mailing Groups');
427 $value = implode(', ', $groups);
428 break;
429
430 case 'subject':
431 $value = $mailing->subject;
432 break;
433
434 case 'viewUrl':
435 $value = CRM_Utils_System::url('civicrm/mailing/view',
436 "reset=1&id={$mailing->id}",
437 TRUE, NULL, FALSE, TRUE
438 );
439 break;
440
441 case 'editUrl':
442 $value = CRM_Utils_System::url('civicrm/mailing/send',
443 "reset=1&mid={$mailing->id}&continue=true",
444 TRUE, NULL, FALSE, TRUE
445 );
446 break;
447
448 case 'scheduleUrl':
449 $value = CRM_Utils_System::url('civicrm/mailing/schedule',
450 "reset=1&mid={$mailing->id}",
451 TRUE, NULL, FALSE, TRUE
452 );
453 break;
454
455 case 'html':
456 $page = new CRM_Mailing_Page_View();
457 $value = $page->run($mailing->id, NULL, FALSE);
458 break;
459
460 case 'approvalStatus':
461 $value = CRM_Core_PseudoConstant::getLabel('CRM_Mailing_DAO_Mailing', 'approval_status_id', $mailing->approval_status_id);
462 break;
463
464 case 'approvalNote':
465 $value = $mailing->approval_note;
466 break;
467
468 case 'approveUrl':
469 $value = CRM_Utils_System::url('civicrm/mailing/approve',
470 "reset=1&mid={$mailing->id}",
471 TRUE, NULL, FALSE, TRUE
472 );
473 break;
474
475 case 'creator':
476 $value = CRM_Contact_BAO_Contact::displayName($mailing->created_id);
477 break;
478
479 case 'creatorEmail':
480 $value = CRM_Contact_BAO_Contact::getPrimaryEmail($mailing->created_id);
481 break;
482
483 default:
484 $value = "{mailing.$token}";
485 break;
486 }
487
488 if ($escapeSmarty) {
489 $value = self::tokenEscapeSmarty($value);
490 }
491 return $value;
492 }
493
494 /**
495 * Replace all action tokens in $str
496 *
497 * @param string $str The string with tokens to be replaced
498 * @param array $addresses Assoc. array of VERP event addresses
499 * @param array $urls Assoc. array of action URLs
500 * @param boolean $html Replace tokens with HTML or plain text
501 * @param array $knownTokens A list of tokens that are known to exist in the email body
502 *
503 * @return string The processed string
504 * @access public
505 * @static
506 */
507 public static function &replaceActionTokens(
508 $str,
509 &$addresses,
510 &$urls,
511 $html = FALSE,
512 $knownTokens = NULL,
513 $escapeSmarty = FALSE
514 ) {
515 $key = 'action';
516 // here we intersect with the list of pre-configured valid tokens
517 // so that we remove anything we do not recognize
518 // I hope to move this step out of here soon and
519 // then we will just iterate on a list of tokens that are passed to us
520 if (!$knownTokens || empty($knownTokens[$key])) {
521 return $str;
522 }
523
524 $str = preg_replace_callback(
525 self::tokenRegex($key),
526 function ($matches) use(&$addresses, &$urls, $html, $escapeSmarty) {
527 return CRM_Utils_Token::getActionTokenReplacement($matches[1], $addresses, $urls, $html, $escapeSmarty);
528 },
529 $str
530 );
531 return $str;
532 }
533
534 public static function getActionTokenReplacement(
535 $token,
536 &$addresses,
537 &$urls,
538 $html = FALSE,
539 $escapeSmarty = FALSE
540 ) {
541 /* If the token is an email action, use it. Otherwise, find the
542 * appropriate URL */
543
544 if (!in_array($token, self::$_tokens['action'])) {
545 $value = "{action.$token}";
546 }
547 else {
548 $value = CRM_Utils_Array::value($token, $addresses);
549
550 if ($value == NULL) {
551 $value = CRM_Utils_Array::value($token, $urls);
552 }
553
554 if ($value && $html) {
555 //fix for CRM-2318
556 if ((substr($token, -3) != 'Url') && ($token != 'forward')) {
557 $value = "mailto:$value";
558 }
559 }
560 elseif ($value && !$html) {
561 $value = str_replace('&amp;', '&', $value);
562 }
563 }
564
565 if ($escapeSmarty) {
566 $value = self::tokenEscapeSmarty($value);
567 }
568 return $value;
569 }
570
571 /**
572 * Replace all the contact-level tokens in $str with information from
573 * $contact.
574 *
575 * @param string $str The string with tokens to be replaced
576 * @param array $contact Associative array of contact properties
577 * @param boolean $html Replace tokens with HTML or plain text
578 * @param array $knownTokens A list of tokens that are known to exist in the email body
579 * @param boolean $returnBlankToken return unevaluated token if value is null
580 *
581 * @return string The processed string
582 * @access public
583 * @static
584 */
585 public static function &replaceContactTokens(
586 $str,
587 &$contact,
588 $html = FALSE,
589 $knownTokens = NULL,
590 $returnBlankToken = FALSE,
591 $escapeSmarty = FALSE
592 ) {
593 $key = 'contact';
594 if (self::$_tokens[$key] == NULL) {
595 /* This should come from UF */
596
597 self::$_tokens[$key] =
598 array_merge(
599 array_keys(CRM_Contact_BAO_Contact::exportableFields('All')),
600 array('checksum', 'contact_id')
601 );
602 }
603
604 // here we intersect with the list of pre-configured valid tokens
605 // so that we remove anything we do not recognize
606 // I hope to move this step out of here soon and
607 // then we will just iterate on a list of tokens that are passed to us
608 if (!$knownTokens || empty($knownTokens[$key])) {
609 return $str;
610 }
611
612 $str = preg_replace_callback(
613 self::tokenRegex($key),
614 function ($matches) use(&$contact, $html, $returnBlankToken, $escapeSmarty) {
615 return CRM_Utils_Token::getContactTokenReplacement($matches[1], $contact, $html, $returnBlankToken, $escapeSmarty);
616 },
617 $str
618 );
619
620 $str = preg_replace('/\\\\|\{(\s*)?\}/', ' ', $str);
621 return $str;
622 }
623
624 public static function getContactTokenReplacement(
625 $token,
626 &$contact,
627 $html = FALSE,
628 $returnBlankToken = FALSE,
629 $escapeSmarty = FALSE
630 ) {
631 if (self::$_tokens['contact'] == NULL) {
632 /* This should come from UF */
633
634 self::$_tokens['contact'] =
635 array_merge(
636 array_keys(CRM_Contact_BAO_Contact::exportableFields('All')),
637 array('checksum', 'contact_id')
638 );
639 }
640
641 /* Construct value from $token and $contact */
642
643 $value = NULL;
644 $noReplace = FALSE;
645
646 // Support legacy tokens
647 $token = CRM_Utils_Array::value($token, self::legacyContactTokens(), $token);
648
649 // check if the token we were passed is valid
650 // we have to do this because this function is
651 // called only when we find a token in the string
652
653 if (!in_array($token, self::$_tokens['contact'])) {
654 $noReplace = TRUE;
655 }
656 elseif ($token == 'checksum') {
657 $hash = CRM_Utils_Array::value('hash', $contact);
658 $contactID = CRM_Utils_Array::retrieveValueRecursive($contact, 'contact_id');
659 $cs = CRM_Contact_BAO_Contact_Utils::generateChecksum($contactID,
660 NULL,
661 NULL,
662 $hash
663 );
664 $value = "cs={$cs}";
665 }
666 else {
667 $value = CRM_Utils_Array::retrieveValueRecursive($contact, $token);
668
669 // FIXME: for some pseudoconstants we get array ( 0 => id, 1 => label )
670 if (is_array($value)) {
671 $value = $value[1];
672 }
673 // Convert pseudoconstants using metadata
674 elseif ($value && is_numeric($value)) {
675 $allFields = CRM_Contact_BAO_Contact::exportableFields('All');
676 if (!empty($allFields[$token]['pseudoconstant'])) {
677 $value = CRM_Core_PseudoConstant::getLabel('CRM_Contact_BAO_Contact', $token, $value);
678 }
679 }
680 }
681
682 if (!$html) {
683 $value = str_replace('&amp;', '&', $value);
684 }
685
686 // if null then return actual token
687 if ($returnBlankToken && !$value) {
688 $noReplace = TRUE;
689 }
690
691 if ($noReplace) {
692 $value = "{contact.$token}";
693 }
694
695 if ($escapeSmarty
696 && !($returnBlankToken && $noReplace)) { // $returnBlankToken means the caller wants to do further attempts at processing unreplaced tokens -- so don't escape them yet in this case.
697 $value = self::tokenEscapeSmarty($value);
698 }
699
700 return $value;
701 }
702
703 /**
704 * Replace all the hook tokens in $str with information from
705 * $contact.
706 *
707 * @param string $str The string with tokens to be replaced
708 * @param array $contact Associative array of contact properties (including hook token values)
709 * @param boolean $html Replace tokens with HTML or plain text
710 *
711 * @return string The processed string
712 * @access public
713 * @static
714 */
715 public static function &replaceHookTokens(
716 $str,
717 &$contact,
718 &$categories,
719 $html = FALSE,
720 $escapeSmarty = FALSE
721 ) {
722 foreach ($categories as $key) {
723 $str = preg_replace_callback(
724 self::tokenRegex($key),
725 function ($matches) use(&$contact, $key, $html, $escapeSmarty) {
726 return CRM_Utils_Token::getHookTokenReplacement($matches[1], $contact, $key, $html, $escapeSmarty);
727 },
728 $str
729 );
730 }
731 return $str;
732 }
733
734 /**
735 * Parse html through Smarty resolving any smarty functions
736 * @param string $tokenHtml
737 * @param array $entity
738 * @param string $entityType
739 * @return string html parsed through smarty
740 */
741 public static function parseThroughSmarty($tokenHtml, $entity, $entityType = 'contact') {
742 if (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY) {
743 $smarty = CRM_Core_Smarty::singleton();
744 // also add the tokens to the template
745 $smarty->assign_by_ref($entityType, $entity);
746 $tokenHtml = $smarty->fetch("string:$tokenHtml");
747 }
748 return $tokenHtml;
749 }
750 public static function getHookTokenReplacement(
751 $token,
752 &$contact,
753 $category,
754 $html = FALSE,
755 $escapeSmarty = FALSE
756 ) {
757 $value = CRM_Utils_Array::value("{$category}.{$token}", $contact);
758
759 if ($value && !$html) {
760 $value = str_replace('&amp;', '&', $value);
761 }
762
763 if ($escapeSmarty) {
764 $value = self::tokenEscapeSmarty($value);
765 }
766
767 return $value;
768 }
769
770 /**
771 * unescapeTokens removes any characters that caused the replacement routines to skip token replacement
772 * for example {{token}} or \{token} will result in {token} in the final email
773 *
774 * this routine will remove the extra backslashes and braces
775 *
776 * @param $str ref to the string that will be scanned and modified
777 * @return void this function works directly on the string that is passed
778 * @access public
779 * @static
780 */
781 public static function unescapeTokens(&$str) {
782 $str = preg_replace('/\\\\|\{(\{\w+\.\w+\})\}/', '\\1', $str);
783 }
784
785 /**
786 * Replace unsubscribe tokens
787 *
788 * @param string $str the string with tokens to be replaced
789 * @param object $domain The domain BAO
790 * @param array $groups The groups (if any) being unsubscribed
791 * @param boolean $html Replace tokens with html or plain text
792 * @param int $contact_id The contact ID
793 * @param string hash The security hash of the unsub event
794 *
795 * @return string The processed string
796 * @access public
797 * @static
798 */
799 public static function &replaceUnsubscribeTokens(
800 $str,
801 &$domain,
802 &$groups,
803 $html,
804 $contact_id,
805 $hash
806 ) {
807 if (self::token_match('unsubscribe', 'group', $str)) {
808 if (!empty($groups)) {
809 $config = CRM_Core_Config::singleton();
810 $base = CRM_Utils_System::baseURL();
811
812 // FIXME: an ugly hack for CRM-2035, to be dropped once CRM-1799 is implemented
813 $dao = new CRM_Contact_DAO_Group();
814 $dao->find();
815 while ($dao->fetch()) {
816 if (substr($dao->visibility, 0, 6) == 'Public') {
817 $visibleGroups[] = $dao->id;
818 }
819 }
820 $value = implode(', ', $groups);
821 self::token_replace('unsubscribe', 'group', $value, $str);
822 }
823 }
824 return $str;
825 }
826
827 /**
828 * Replace resubscribe tokens
829 *
830 * @param string $str the string with tokens to be replaced
831 * @param object $domain The domain BAO
832 * @param array $groups The groups (if any) being resubscribed
833 * @param boolean $html Replace tokens with html or plain text
834 * @param int $contact_id The contact ID
835 * @param string hash The security hash of the resub event
836 *
837 * @return string The processed string
838 * @access public
839 * @static
840 */
841 public static function &replaceResubscribeTokens($str, &$domain, &$groups, $html,
842 $contact_id, $hash
843 ) {
844 if (self::token_match('resubscribe', 'group', $str)) {
845 if (!empty($groups)) {
846 $value = implode(', ', $groups);
847 self::token_replace('resubscribe', 'group', $value, $str);
848 }
849 }
850 return $str;
851 }
852
853 /**
854 * Replace subscription-confirmation-request tokens
855 *
856 * @param string $str The string with tokens to be replaced
857 * @param string $group The name of the group being subscribed
858 * @param boolean $html Replace tokens with html or plain text
859 *
860 * @return string The processed string
861 * @access public
862 * @static
863 */
864 public static function &replaceSubscribeTokens($str, $group, $url, $html) {
865 if (self::token_match('subscribe', 'group', $str)) {
866 self::token_replace('subscribe', 'group', $group, $str);
867 }
868 if (self::token_match('subscribe', 'url', $str)) {
869 self::token_replace('subscribe', 'url', $url, $str);
870 }
871 return $str;
872 }
873
874 /**
875 * Replace subscription-invitation tokens
876 *
877 * @param string $str The string with tokens to be replaced
878 *
879 * @return string The processed string
880 * @access public
881 * @static
882 */
883 public static function &replaceSubscribeInviteTokens($str) {
884 if (preg_match('/\{action\.subscribeUrl\}/', $str)) {
885 $url = CRM_Utils_System::url('civicrm/mailing/subscribe',
886 'reset=1',
887 TRUE, NULL, TRUE, TRUE
888 );
889 $str = preg_replace('/\{action\.subscribeUrl\}/', $url, $str);
890 }
891
892 if (preg_match('/\{action\.subscribeUrl.\d+\}/', $str, $matches)) {
893 foreach ($matches as $key => $value) {
894 $gid = substr($value, 21, -1);
895 $url = CRM_Utils_System::url('civicrm/mailing/subscribe',
896 "reset=1&gid={$gid}",
897 TRUE, NULL, TRUE, TRUE
898 );
899 $url = str_replace('&amp;', '&', $url);
900 $str = preg_replace('/' . preg_quote($value) . '/', $url, $str);
901 }
902 }
903
904 if (preg_match('/\{action\.subscribe.\d+\}/', $str, $matches)) {
905 foreach ($matches as $key => $value) {
906 $gid = substr($value, 18, -1);
907 $config = CRM_Core_Config::singleton();
908 $domain = CRM_Core_BAO_MailSettings::defaultDomain();
909 $localpart = CRM_Core_BAO_MailSettings::defaultLocalpart();
910 // we add the 0.0000000000000000 part to make this match the other email patterns (with action, two ids and a hash)
911 $str = preg_replace('/' . preg_quote($value) . '/', "mailto:{$localpart}s.{$gid}.0.0000000000000000@$domain", $str);
912 }
913 }
914 return $str;
915 }
916
917 /**
918 * Replace welcome/confirmation tokens
919 *
920 * @param string $str The string with tokens to be replaced
921 * @param string $group The name of the group being subscribed
922 * @param boolean $html Replace tokens with html or plain text
923 *
924 * @return string The processed string
925 * @access public
926 * @static
927 */
928 public static function &replaceWelcomeTokens($str, $group, $html) {
929 if (self::token_match('welcome', 'group', $str)) {
930 self::token_replace('welcome', 'group', $group, $str);
931 }
932 return $str;
933 }
934
935 /**
936 * Find unprocessed tokens (call this last)
937 *
938 * @param string $str The string to search
939 *
940 * @return array Array of tokens that weren't replaced
941 * @access public
942 * @static
943 */
944 public static function &unmatchedTokens(&$str) {
945 //preg_match_all('/[^\{\\\\]\{(\w+\.\w+)\}[^\}]/', $str, $match);
946 preg_match_all('/\{(\w+\.\w+)\}/', $str, $match);
947 return $match[1];
948 }
949
950 /**
951 * Find and replace tokens for each component
952 *
953 * @param string $str The string to search
954 * @param array $contact Associative array of contact properties
955 * @param array $components A list of tokens that are known to exist in the email body
956 *
957 * @return string The processed string
958 * @access public
959 * @static
960 */
961 public static function &replaceComponentTokens(&$str, $contact, $components, $escapeSmarty = FALSE, $returnEmptyToken = TRUE) {
962 if (!is_array($components) || empty($contact)) {
963 return $str;
964 }
965
966 foreach ($components as $name => $tokens) {
967 if (!is_array($tokens) || empty($tokens)) {
968 continue;
969 }
970
971 foreach ($tokens as $token) {
972 if (self::token_match($name, $token, $str) && isset($contact[$name . '.' . $token])) {
973 self::token_replace($name, $token, $contact[$name . '.' . $token], $str, $escapeSmarty);
974 }
975 elseif (!$returnEmptyToken) {
976 //replacing empty token
977 self::token_replace($name, $token, "", $str, $escapeSmarty);
978 }
979 }
980 }
981 return $str;
982 }
983
984 /**
985 * Get array of string tokens
986 *
987 * @param $string the input string to parse for tokens
988 *
989 * @return $tokens array of tokens mentioned in field
990 * @access public
991 * @static
992 */
993 static function getTokens($string) {
994 $matches = array();
995 $tokens = array();
996 preg_match_all('/(?<!\{|\\\\)\{(\w+\.\w+)\}(?!\})/',
997 $string,
998 $matches,
999 PREG_PATTERN_ORDER
1000 );
1001
1002 if ($matches[1]) {
1003 foreach ($matches[1] as $token) {
1004 list($type, $name) = preg_split('/\./', $token, 2);
1005 if ($name && $type) {
1006 if (!isset($tokens[$type])) {
1007 $tokens[$type] = array();
1008 }
1009 $tokens[$type][] = $name;
1010 }
1011 }
1012 }
1013 return $tokens;
1014 }
1015
1016 /**
1017 * gives required details of contacts in an indexed array format so we
1018 * can iterate in a nice loop and do token evaluation
1019 *
1020 * @param array $contactIds of contacts
1021 * @param array $returnProperties of required properties
1022 * @param boolean $skipOnHold don't return on_hold contact info also.
1023 * @param boolean $skipDeceased don't return deceased contact info.
1024 * @param array $extraParams extra params
1025 * @param array $tokens the list of tokens we've extracted from the content
1026 * @param int $jobID the mailing list jobID - this is a legacy param
1027 *
1028 * @return array
1029 * @access public
1030 * @static
1031 */
1032 static function getTokenDetails($contactIDs,
1033 $returnProperties = NULL,
1034 $skipOnHold = TRUE,
1035 $skipDeceased = TRUE,
1036 $extraParams = NULL,
1037 $tokens = array(),
1038 $className = NULL,
1039 $jobID = NULL
1040 ) {
1041 if (empty($contactIDs)) {
1042 // putting a fatal here so we can track if/when this happens
1043 CRM_Core_Error::fatal();
1044 }
1045
1046 $params = array();
1047 foreach ($contactIDs as $key => $contactID) {
1048 $params[] = array(
1049 CRM_Core_Form::CB_PREFIX . $contactID,
1050 '=', 1, 0, 0,
1051 );
1052 }
1053
1054 // fix for CRM-2613
1055 if ($skipDeceased) {
1056 $params[] = array('is_deceased', '=', 0, 0, 0);
1057 }
1058
1059 //fix for CRM-3798
1060 if ($skipOnHold) {
1061 $params[] = array('on_hold', '=', 0, 0, 0);
1062 }
1063
1064 if ($extraParams) {
1065 $params = array_merge($params, $extraParams);
1066 }
1067
1068 // if return properties are not passed then get all return properties
1069 if (empty($returnProperties)) {
1070 $fields = array_merge(array_keys(CRM_Contact_BAO_Contact::exportableFields()),
1071 array('display_name', 'checksum', 'contact_id')
1072 );
1073 foreach ($fields as $key => $val) {
1074 $returnProperties[$val] = 1;
1075 }
1076 }
1077
1078 $custom = array();
1079 foreach ($returnProperties as $name => $dontCare) {
1080 $cfID = CRM_Core_BAO_CustomField::getKeyID($name);
1081 if ($cfID) {
1082 $custom[] = $cfID;
1083 }
1084 }
1085
1086 //get the total number of contacts to fetch from database.
1087 $numberofContacts = count($contactIDs);
1088 $query = new CRM_Contact_BAO_Query($params, $returnProperties);
1089
1090 $details = $query->apiQuery($params, $returnProperties, NULL, NULL, 0, $numberofContacts);
1091
1092 $contactDetails = &$details[0];
1093
1094 foreach ($contactIDs as $key => $contactID) {
1095 if (array_key_exists($contactID, $contactDetails)) {
1096 if (CRM_Utils_Array::value('preferred_communication_method', $returnProperties) == 1
1097 && array_key_exists('preferred_communication_method', $contactDetails[$contactID])
1098 ) {
1099 $pcm = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'preferred_communication_method');
1100
1101 // communication Prefferance
1102 $contactPcm = explode(CRM_Core_DAO::VALUE_SEPARATOR,
1103 $contactDetails[$contactID]['preferred_communication_method']
1104 );
1105 $result = array();
1106 foreach ($contactPcm as $key => $val) {
1107 if ($val) {
1108 $result[$val] = $pcm[$val];
1109 }
1110 }
1111 $contactDetails[$contactID]['preferred_communication_method'] = implode(', ', $result);
1112 }
1113
1114 foreach ($custom as $cfID) {
1115 if (isset($contactDetails[$contactID]["custom_{$cfID}"])) {
1116 $contactDetails[$contactID]["custom_{$cfID}"] = CRM_Core_BAO_CustomField::getDisplayValue($contactDetails[$contactID]["custom_{$cfID}"],
1117 $cfID, $details[1]
1118 );
1119 }
1120 }
1121
1122 //special case for greeting replacement
1123 foreach (array(
1124 'email_greeting', 'postal_greeting', 'addressee') as $val) {
1125 if (!empty($contactDetails[$contactID][$val])) {
1126 $contactDetails[$contactID][$val] = $contactDetails[$contactID]["{$val}_display"];
1127 }
1128 }
1129 }
1130 }
1131
1132 // also call a hook and get token details
1133 CRM_Utils_Hook::tokenValues($details[0],
1134 $contactIDs,
1135 $jobID,
1136 $tokens,
1137 $className
1138 );
1139 return $details;
1140 }
1141
1142 /**
1143 * Call hooks on tokens for anonymous users - contact id is set to 0 - this allows non-contact
1144 * specific tokens to be rendered
1145 *
1146 * @param array $contactIDs - this should always be array(0) or its not anonymous - left to keep signature same
1147 * as main fn
1148 * @param string $returnProperties
1149 * @param boolean $skipOnHold
1150 * @param boolean $skipDeceased
1151 * @param string $extraParams
1152 * @param array $tokens
1153 * @param string $className sent as context to the hook
1154 * @param string $jobID
1155 * @return array contactDetails with hooks swapped out
1156 */
1157 function getAnonymousTokenDetails($contactIDs = array(0),
1158 $returnProperties = NULL,
1159 $skipOnHold = TRUE,
1160 $skipDeceased = TRUE,
1161 $extraParams = NULL,
1162 $tokens = array(),
1163 $className = NULL,
1164 $jobID = NULL) {
1165 $details = array(0 => array());
1166 // also call a hook and get token details
1167 CRM_Utils_Hook::tokenValues($details[0],
1168 $contactIDs,
1169 $jobID,
1170 $tokens,
1171 $className
1172 );
1173 return $details;
1174 }
1175 /**
1176 * gives required details of contribuion in an indexed array format so we
1177 * can iterate in a nice loop and do token evaluation
1178 *
1179 * @param array $contributionId one contribution id
1180 * @param array $returnProperties of required properties
1181 * @param boolean $skipOnHold don't return on_hold contact info.
1182 * @param boolean $skipDeceased don't return deceased contact info.
1183 * @param array $extraParams extra params
1184 * @param array $tokens the list of tokens we've extracted from the content
1185 *
1186 * @return array
1187 * @access public
1188 * @static
1189 */
1190 static function getContributionTokenDetails($contributionIDs,
1191 $returnProperties = NULL,
1192 $extraParams = NULL,
1193 $tokens = array(),
1194 $className = NULL
1195 ) {
1196 if (empty($contributionIDs)) {
1197 // putting a fatal here so we can track if/when this happens
1198 CRM_Core_Error::fatal();
1199 }
1200
1201 $details = array();
1202
1203 // no apiQuery helper yet, so do a loop and find contribution by id
1204 foreach ($contributionIDs as $contributionID) {
1205
1206 $dao = new CRM_Contribute_DAO_Contribution();
1207 $dao->id = $contributionID;
1208
1209 if ($dao->find(TRUE)) {
1210
1211 $details[$dao->id] = array();
1212 CRM_Core_DAO::storeValues($dao, $details[$dao->id]);
1213
1214 // do the necessary transformation
1215 if (!empty($details[$dao->id]['payment_instrument_id'])) {
1216 $piId = $details[$dao->id]['payment_instrument_id'];
1217 $pis = CRM_Contribute_PseudoConstant::paymentInstrument();
1218 $details[$dao->id]['payment_instrument'] = $pis[$piId];
1219 }
1220 if (!empty($details[$dao->id]['campaign_id'])) {
1221 $campaignId = $details[$dao->id]['campaign_id'];
1222 $campaigns = CRM_Campaign_BAO_Campaign::getCampaigns($campaignId);
1223 $details[$dao->id]['campaign'] = $campaigns[$campaignId];
1224 }
1225
1226 if (!empty($details[$dao->id]['financial_type_id'])) {
1227 $financialtypeId = $details[$dao->id]['financial_type_id'];
1228 $ftis = CRM_Contribute_PseudoConstant::financialType();
1229 $details[$dao->id]['financial_type'] = $ftis[$financialtypeId];
1230 }
1231
1232 // TODO: call a hook to get token contribution details
1233 }
1234 }
1235
1236 return $details;
1237 }
1238
1239 /**
1240 * Get Membership Token Details
1241 * @param array $membershipIDs array of membership IDS
1242 */
1243 static function getMembershipTokenDetails($membershipIDs) {
1244 $memberships = civicrm_api3('membership', 'get', array('options' => array('limit' => 200000), 'membership_id' => array('IN' => (array) $membershipIDs)));
1245 return $memberships['values'];
1246 }
1247 /**
1248 * replace greeting tokens exists in message/subject
1249 *
1250 * @access public
1251 */
1252 static function replaceGreetingTokens(&$tokenString, $contactDetails = NULL, $contactId = NULL, $className = NULL, $escapeSmarty = FALSE) {
1253
1254 if (!$contactDetails && !$contactId) {
1255 return;
1256 }
1257
1258 // check if there are any tokens
1259 $greetingTokens = self::getTokens($tokenString);
1260
1261 if (!empty($greetingTokens)) {
1262 // first use the existing contact object for token replacement
1263 if (!empty($contactDetails)) {
1264 $tokenString = CRM_Utils_Token::replaceContactTokens($tokenString, $contactDetails, TRUE, $greetingTokens, TRUE, $escapeSmarty);
1265 }
1266
1267 // check if there are any unevaluated tokens
1268 $greetingTokens = self::getTokens($tokenString);
1269
1270 // $greetingTokens not empty, means there are few tokens which are not evaluated, like custom data etc
1271 // so retrieve it from database
1272 if (!empty($greetingTokens) && array_key_exists('contact', $greetingTokens)) {
1273 $greetingsReturnProperties = array_flip(CRM_Utils_Array::value('contact', $greetingTokens));
1274 $greetingsReturnProperties = array_fill_keys(array_keys($greetingsReturnProperties), 1);
1275 $contactParams = array('contact_id' => $contactId);
1276
1277 $greetingDetails = self::getTokenDetails($contactParams,
1278 $greetingsReturnProperties,
1279 FALSE, FALSE, NULL,
1280 $greetingTokens,
1281 $className
1282 );
1283
1284 // again replace tokens
1285 $tokenString = CRM_Utils_Token::replaceContactTokens($tokenString,
1286 $greetingDetails,
1287 TRUE,
1288 $greetingTokens,
1289 FALSE,
1290 $escapeSmarty
1291 );
1292 }
1293 }
1294 }
1295
1296 static function flattenTokens(&$tokens) {
1297 $flattenTokens = array();
1298
1299 foreach (array(
1300 'html', 'text', 'subject') as $prop) {
1301 if (!isset($tokens[$prop])) {
1302 continue;
1303 }
1304 foreach ($tokens[$prop] as $type => $names) {
1305 if (!isset($flattenTokens[$type])) {
1306 $flattenTokens[$type] = array();
1307 }
1308 foreach ($names as $name) {
1309 $flattenTokens[$type][$name] = 1;
1310 }
1311 }
1312 }
1313
1314 return $flattenTokens;
1315 }
1316
1317 /**
1318 * Replace all user tokens in $str
1319 *
1320 * @param string $str The string with tokens to be replaced
1321 *
1322 * @return string The processed string
1323 * @access public
1324 * @static
1325 */
1326 public static function &replaceUserTokens($str, $knownTokens = NULL, $escapeSmarty = FALSE) {
1327 $key = 'user';
1328 if (!$knownTokens ||
1329 !isset($knownTokens[$key])
1330 ) {
1331 return $str;
1332 }
1333
1334 $str = preg_replace_callback(
1335 self::tokenRegex($key),
1336 function ($matches) use($escapeSmarty) {
1337 return CRM_Utils_Token::getUserTokenReplacement($matches[1], $escapeSmarty);
1338 },
1339 $str
1340 );
1341 return $str;
1342 }
1343
1344 public static function getUserTokenReplacement($token, $escapeSmarty = FALSE) {
1345 $value = '';
1346
1347 list($objectName, $objectValue) = explode('-', $token, 2);
1348
1349 switch ($objectName) {
1350 case 'permission':
1351 $value = CRM_Core_Permission::permissionEmails($objectValue);
1352 break;
1353
1354 case 'role':
1355 $value = CRM_Core_Permission::roleEmails($objectValue);
1356 break;
1357 }
1358
1359 if ($escapeSmarty) {
1360 $value = self::tokenEscapeSmarty($value);
1361 }
1362
1363 return $value;
1364 }
1365
1366
1367 protected static function _buildContributionTokens() {
1368 $key = 'contribution';
1369 if (self::$_tokens[$key] == NULL) {
1370 self::$_tokens[$key] = array_keys(array_merge(CRM_Contribute_BAO_Contribution::exportableFields('All'),
1371 array('campaign', 'financial_type')
1372 ));
1373 }
1374 }
1375
1376 /**
1377 * store membership tokens on the static _tokens array
1378 */
1379 protected static function _buildMembershipTokens() {
1380 $key = 'membership';
1381 if (!isset(self::$_tokens[$key]) || self::$_tokens[$key] == NULL) {
1382 $membershipTokens = array();
1383 $tokens = CRM_Core_SelectValues::membershipTokens();
1384 foreach ($tokens as $token => $dontCare) {
1385 $membershipTokens[] = substr($token, (strpos($token, '.') + 1), -1);
1386 }
1387 self::$_tokens[$key] = $membershipTokens;
1388 }
1389 }
1390
1391 /**
1392 * Replace tokens for an entity
1393 * @param string $entity
1394 * @param array $entityArray (e.g. in format from api)
1395 * @param string $str string to replace in
1396 * @param array $knownTokens array of tokens present
1397 * @param boolean $escapeSmarty
1398 * @return string string with replacements made
1399 */
1400 public static function replaceEntityTokens($entity, $entityArray, $str, $knownTokens = array(), $escapeSmarty = FALSE) {
1401 if (!$knownTokens || empty($knownTokens[$entity])) {
1402 return $str;
1403 }
1404
1405 $fn = 'get' . ucFirst($entity) . 'tokenReplacement';
1406 //since we already know the tokens lets just use them & do str_replace which is faster & simpler than preg_replace
1407 foreach ($knownTokens[$entity] as $token) {
1408 $replaceMent = CRM_Utils_Token::$fn($token, $entityArray, $escapeSmarty);
1409 $str = str_replace('{' . $entity . '.' . $token . '}', $replaceMent, $str);
1410 }
1411 $str = preg_replace('/\\\\|\{(\s*)?\}/', ' ', $str);
1412 return $str;
1413 }
1414
1415 public static function &replaceContributionTokens($str, &$contribution, $html = FALSE, $knownTokens = NULL, $escapeSmarty = FALSE) {
1416 self::_buildContributionTokens();
1417
1418 // here we intersect with the list of pre-configured valid tokens
1419 // so that we remove anything we do not recognize
1420 // I hope to move this step out of here soon and
1421 // then we will just iterate on a list of tokens that are passed to us
1422 $key = 'contribution';
1423 if (!$knownTokens || empty($knownTokens[$key])) {
1424 return $str;
1425 }
1426
1427 $str = preg_replace_callback(
1428 self::tokenRegex($key),
1429 function ($matches) use(&$contribution, $html, $escapeSmarty) {
1430 return CRM_Utils_Token::getContributionTokenReplacement($matches[1], $contribution, $html, $escapeSmarty);
1431 },
1432 $str
1433 );
1434
1435 $str = preg_replace('/\\\\|\{(\s*)?\}/', ' ', $str);
1436 return $str;
1437 }
1438
1439 /**
1440 * Get replacement strings for any membership tokens (only a small number of tokens are implemnted in the first instance
1441 * - this is used by the pdfLetter task from membership search
1442 * @param string $token
1443 * @param array $membership an api result array for a single membership
1444 * @param boolean $escapeSmarty
1445 * @return string token replacement
1446 */
1447 public static function getMembershipTokenReplacement($token, $membership, $escapeSmarty = FALSE) {
1448 $entity = 'membership';
1449 self::_buildMembershipTokens();
1450 switch ($token) {
1451 case 'type':
1452 $value = $membership['membership_name'];
1453 break;
1454 case 'status':
1455 $statuses = CRM_Member_BAO_Membership::buildOptions('status_id');
1456 $value = $statuses[$membership['status_id']];
1457 break;
1458 case 'fee':
1459 try{
1460 $value = civicrm_api3('membership_type', 'getvalue', array('id' => $membership['membership_type_id'], 'return' => 'minimum_fee'));
1461 }
1462 catch (CiviCRM_API3_Exception $e) {
1463 // we can anticipate we will get an error if the minimum fee is set to 'NULL' because of the way the
1464 // api handles NULL (4.4)
1465 $value = 0;
1466 }
1467 break;
1468 default:
1469 if (in_array($token, self::$_tokens[$entity])) {
1470 $value = $membership[$token];
1471 }
1472 else {
1473 //ie unchanged
1474 $value = "{$entity}.{$token}";
1475 }
1476 break;
1477 }
1478
1479 if ($escapeSmarty) {
1480 $value = self::tokenEscapeSmarty($value);
1481 }
1482 return $value;
1483 }
1484
1485 public static function getContributionTokenReplacement($token, &$contribution, $html = FALSE, $escapeSmarty = FALSE) {
1486 self::_buildContributionTokens();
1487
1488 switch ($token) {
1489 case 'total_amount':
1490 case 'net_amount':
1491 case 'fee_amount':
1492 case 'non_deductible_amount':
1493 $value = CRM_Utils_Money::format(CRM_Utils_Array::retrieveValueRecursive($contribution, $token));
1494 break;
1495
1496 case 'receive_date':
1497 $value = CRM_Utils_Array::retrieveValueRecursive($contribution, $token);
1498 $value = CRM_Utils_Date::customFormat($value, NULL, array('j', 'm', 'Y'));
1499 break;
1500
1501 default:
1502 if (!in_array($token, self::$_tokens['contribution'])) {
1503 $value = "{contribution.$token}";
1504 }
1505 else {
1506 $value = CRM_Utils_Array::retrieveValueRecursive($contribution, $token);
1507 }
1508 break;
1509 }
1510
1511
1512 if ($escapeSmarty) {
1513 $value = self::tokenEscapeSmarty($value);
1514 }
1515 return $value;
1516 }
1517
1518 function getPermissionEmails($permissionName) {}
1519
1520 function getRoleEmails($roleName) {}
1521
1522 /**
1523 * @return array: legacy_token => new_token
1524 */
1525 static function legacyContactTokens() {
1526 return array(
1527 'individual_prefix' => 'prefix_id',
1528 'individual_suffix' => 'suffix_id',
1529 'gender' => 'gender_id',
1530 'communication_style' => 'communication_style_id',
1531 );
1532 }
1533
1534 /**
1535 * Formats a token list for the select2 widget
1536 * @param $tokens
1537 * @return array
1538 */
1539 static function formatTokensForDisplay($tokens) {
1540 $sorted = $output = array();
1541
1542 // Sort in ascending order by ignoring word case
1543 natcasesort($tokens);
1544
1545 // Attempt to place tokens into optgroups
1546 // TODO: These groupings could be better and less hackish. Getting them pre-grouped from upstream would be nice.
1547 foreach ($tokens as $k => $v) {
1548 // Check to see if this token is already in a group e.g. for custom fields
1549 $split = explode(' :: ', $v);
1550 if (!empty($split[1])) {
1551 $sorted[$split[1]][] = array('id' => $k, 'text' => $split[0]);
1552 }
1553 // Group by entity
1554 else {
1555 $split = explode('.', trim($k, '{}'));
1556 $entity = isset($split[1]) ? ucfirst($split[0]) : 'Contact';
1557 $sorted[ts($entity)][] = array('id' => $k, 'text' => $v);
1558 }
1559 }
1560
1561 ksort($sorted);
1562 foreach ($sorted as $k => $v) {
1563 $output[] = array('text' => $k, 'children' => $v);
1564 }
1565
1566 return $output;
1567 }
1568
1569 }