Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
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 | 86 | ), |
481a74f4 TO |
87 | 'subscribe' => array('group'), |
88 | 'unsubscribe' => array('group'), | |
89 | 'resubscribe' => array('group'), | |
90 | 'welcome' => array('group'), | |
6a488035 TO |
91 | ); |
92 | ||
93 | /** | |
94 | * Check a string (mailing body) for required tokens. | |
95 | * | |
77855840 TO |
96 | * @param string $str |
97 | * The message. | |
6a488035 | 98 | * |
72b3a70c CW |
99 | * @return bool|array |
100 | * true if all required tokens are found, | |
101 | * else an array of the missing tokens | |
6a488035 TO |
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."), | |
9d72cede | 107 | 'action.optOutUrl or action.unsubscribeUrl' => array( |
6a488035 TO |
108 | 'action.optOut' => ts("'Opt out via email' - displays an email address for recipients to opt out of receiving emails from your organization."), |
109 | '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."), | |
110 | 'action.unsubscribe' => ts("'Unsubscribe via email' - displays an email address for recipients to unsubscribe from the specific mailing list used to send this message."), | |
d72434db | 111 | '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."), |
6a488035 TO |
112 | ), |
113 | ); | |
114 | } | |
115 | ||
116 | $missing = array(); | |
117 | foreach (self::$_requiredTokens as $token => $value) { | |
118 | if (!is_array($value)) { | |
119 | if (!preg_match('/(^|[^\{])' . preg_quote('{' . $token . '}') . '/', $str)) { | |
120 | $missing[$token] = $value; | |
121 | } | |
122 | } | |
123 | else { | |
124 | $present = FALSE; | |
125 | $desc = NULL; | |
126 | foreach ($value as $t => $d) { | |
127 | $desc = $d; | |
128 | if (preg_match('/(^|[^\{])' . preg_quote('{' . $t . '}') . '/', $str)) { | |
129 | $present = TRUE; | |
130 | } | |
131 | } | |
132 | if (!$present) { | |
133 | $missing[$token] = $desc; | |
134 | } | |
135 | } | |
136 | } | |
137 | ||
138 | if (empty($missing)) { | |
139 | return TRUE; | |
140 | } | |
141 | return $missing; | |
142 | } | |
143 | ||
144 | /** | |
fe482240 | 145 | * Wrapper for token matching. |
6a488035 | 146 | * |
77855840 TO |
147 | * @param string $type |
148 | * The token type (domain,mailing,contact,action). | |
149 | * @param string $var | |
150 | * The token variable. | |
151 | * @param string $str | |
152 | * The string to search. | |
6a488035 | 153 | * |
e7483cbe | 154 | * @return bool |
a6c01b45 | 155 | * Was there a match |
6a488035 TO |
156 | */ |
157 | public static function token_match($type, $var, &$str) { | |
158 | $token = preg_quote('{' . "$type.$var") . '(\|.+?)?' . preg_quote('}'); | |
93f087c7 | 159 | return preg_match("/(^|[^\{])$token/", $str); |
6a488035 TO |
160 | } |
161 | ||
162 | /** | |
fe482240 | 163 | * Wrapper for token replacing. |
6a488035 | 164 | * |
77855840 TO |
165 | * @param string $type |
166 | * The token type. | |
167 | * @param string $var | |
168 | * The token variable. | |
169 | * @param string $value | |
170 | * The value to substitute for the token. | |
e39893f5 EM |
171 | * @param string (reference) $str The string to replace in |
172 | * | |
173 | * @param bool $escapeSmarty | |
6a488035 | 174 | * |
a6c01b45 CW |
175 | * @return string |
176 | * The processed string | |
6a488035 TO |
177 | */ |
178 | public static function &token_replace($type, $var, $value, &$str, $escapeSmarty = FALSE) { | |
179 | $token = preg_quote('{' . "$type.$var") . '(\|([^\}]+?))?' . preg_quote('}'); | |
180 | if (!$value) { | |
181 | $value = '$3'; | |
182 | } | |
183 | if ($escapeSmarty) { | |
184 | $value = self::tokenEscapeSmarty($value); | |
185 | } | |
186 | $str = preg_replace("/([^\{])?$token/", "\${1}$value", $str); | |
187 | return $str; | |
188 | } | |
189 | ||
190 | /** | |
100fef9d | 191 | * Get< the regex for token replacement |
6a488035 | 192 | * |
77855840 TO |
193 | * @param string $token_type |
194 | * A string indicating the the type of token to be used in the expression. | |
6a488035 | 195 | * |
a6c01b45 CW |
196 | * @return string |
197 | * regular expression sutiable for using in preg_replace | |
6a488035 TO |
198 | */ |
199 | private static function tokenRegex($token_type) { | |
8bab0eb0 | 200 | return '/(?<!\{|\\\\)\{' . $token_type . '\.([\w]+(\-[\w\s]+)?)\}(?!\})/'; |
6a488035 TO |
201 | } |
202 | ||
203 | /** | |
fe482240 | 204 | * Escape the string so a malicious user cannot inject smarty code into the template. |
6a488035 | 205 | * |
77855840 TO |
206 | * @param string $string |
207 | * A string that needs to be escaped from smarty parsing. | |
6a488035 | 208 | * |
a6c01b45 CW |
209 | * @return string |
210 | * the escaped string | |
6a488035 TO |
211 | */ |
212 | private static function tokenEscapeSmarty($string) { | |
213 | // need to use negative look-behind, as both str_replace() and preg_replace() are sequential | |
214 | return preg_replace(array('/{/', '/(?<!{ldelim)}/'), array('{ldelim}', '{rdelim}'), $string); | |
215 | } | |
216 | ||
e7292422 | 217 | /** |
6a488035 TO |
218 | * Replace all the domain-level tokens in $str |
219 | * | |
77855840 TO |
220 | * @param string $str |
221 | * The string with tokens to be replaced. | |
222 | * @param object $domain | |
223 | * The domain BAO. | |
224 | * @param bool $html | |
225 | * Replace tokens with HTML or plain text. | |
e39893f5 EM |
226 | * |
227 | * @param null $knownTokens | |
228 | * @param bool $escapeSmarty | |
6a488035 | 229 | * |
a6c01b45 CW |
230 | * @return string |
231 | * The processed string | |
6a488035 | 232 | */ |
21bb6c7b DL |
233 | public static function &replaceDomainTokens( |
234 | $str, | |
235 | &$domain, | |
236 | $html = FALSE, | |
237 | $knownTokens = NULL, | |
238 | $escapeSmarty = FALSE | |
239 | ) { | |
6a488035 | 240 | $key = 'domain'; |
21bb6c7b | 241 | if ( |
353ffa53 TO |
242 | !$knownTokens || empty($knownTokens[$key]) |
243 | ) { | |
6a488035 TO |
244 | return $str; |
245 | } | |
246 | ||
8bab0eb0 | 247 | $str = preg_replace_callback( |
21bb6c7b | 248 | self::tokenRegex($key), |
353ffa53 TO |
249 | function ($matches) use (&$domain, $html, $escapeSmarty) { |
250 | return CRM_Utils_Token::getDomainTokenReplacement($matches[1], $domain, $html, $escapeSmarty); | |
8bab0eb0 | 251 | }, |
21bb6c7b DL |
252 | $str |
253 | ); | |
6a488035 TO |
254 | return $str; |
255 | } | |
256 | ||
e39893f5 EM |
257 | /** |
258 | * @param $token | |
259 | * @param $domain | |
260 | * @param bool $html | |
261 | * @param bool $escapeSmarty | |
262 | * | |
263 | * @return mixed|null|string | |
264 | */ | |
6a488035 TO |
265 | public static function getDomainTokenReplacement($token, &$domain, $html = FALSE, $escapeSmarty = FALSE) { |
266 | // check if the token we were passed is valid | |
267 | // we have to do this because this function is | |
268 | // called only when we find a token in the string | |
269 | ||
270 | $loc = &$domain->getLocationValues(); | |
271 | ||
272 | if (!in_array($token, self::$_tokens['domain'])) { | |
273 | $value = "{domain.$token}"; | |
274 | } | |
275 | elseif ($token == 'address') { | |
276 | static $addressCache = array(); | |
277 | ||
278 | $cache_key = $html ? 'address-html' : 'address-text'; | |
279 | if (array_key_exists($cache_key, $addressCache)) { | |
280 | return $addressCache[$cache_key]; | |
281 | } | |
282 | ||
283 | $value = NULL; | |
284 | /* Construct the address token */ | |
285 | ||
a7488080 | 286 | if (!empty($loc[$token])) { |
6a488035 TO |
287 | if ($html) { |
288 | $value = $loc[$token][1]['display']; | |
289 | $value = str_replace("\n", '<br />', $value); | |
290 | } | |
291 | else { | |
292 | $value = $loc[$token][1]['display_text']; | |
293 | } | |
294 | $addressCache[$cache_key] = $value; | |
295 | } | |
296 | } | |
e3470b79 | 297 | elseif ($token == 'name' || $token == 'id' || $token == 'description') { |
6a488035 TO |
298 | $value = $domain->$token; |
299 | } | |
300 | elseif ($token == 'phone' || $token == 'email') { | |
301 | /* Construct the phone and email tokens */ | |
302 | ||
303 | $value = NULL; | |
a7488080 | 304 | if (!empty($loc[$token])) { |
6a488035 TO |
305 | foreach ($loc[$token] as $index => $entity) { |
306 | $value = $entity[$token]; | |
307 | break; | |
308 | } | |
309 | } | |
310 | } | |
311 | ||
312 | if ($escapeSmarty) { | |
313 | $value = self::tokenEscapeSmarty($value); | |
314 | } | |
315 | ||
316 | return $value; | |
317 | } | |
318 | ||
319 | /** | |
320 | * Replace all the org-level tokens in $str | |
321 | * | |
77855840 TO |
322 | * @param string $str |
323 | * The string with tokens to be replaced. | |
324 | * @param object $org | |
325 | * Associative array of org properties. | |
326 | * @param bool $html | |
327 | * Replace tokens with HTML or plain text. | |
e39893f5 EM |
328 | * |
329 | * @param bool $escapeSmarty | |
6a488035 | 330 | * |
a6c01b45 CW |
331 | * @return string |
332 | * The processed string | |
6a488035 TO |
333 | */ |
334 | public static function &replaceOrgTokens($str, &$org, $html = FALSE, $escapeSmarty = FALSE) { | |
e7483cbe J |
335 | self::$_tokens['org'] |
336 | = array_merge( | |
21bb6c7b DL |
337 | array_keys(CRM_Contact_BAO_Contact::importableFields('Organization')), |
338 | array('address', 'display_name', 'checksum', 'contact_id') | |
339 | ); | |
6a488035 TO |
340 | |
341 | $cv = NULL; | |
342 | foreach (self::$_tokens['org'] as $token) { | |
343 | // print "Getting token value for $token<br/><br/>"; | |
344 | if ($token == '') { | |
345 | continue; | |
346 | } | |
347 | ||
348 | /* If the string doesn't contain this token, skip it. */ | |
349 | ||
350 | if (!self::token_match('org', $token, $str)) { | |
351 | continue; | |
352 | } | |
353 | ||
354 | /* Construct value from $token and $contact */ | |
355 | ||
356 | $value = NULL; | |
357 | ||
358 | if ($cfID = CRM_Core_BAO_CustomField::getKeyID($token)) { | |
359 | // only generate cv if we need it | |
360 | if ($cv === NULL) { | |
361 | $cv = CRM_Core_BAO_CustomValue::getContactValues($org['contact_id']); | |
362 | } | |
363 | foreach ($cv as $cvFieldID => $value) { | |
364 | if ($cvFieldID == $cfID) { | |
365 | $value = CRM_Core_BAO_CustomOption::getOptionLabel($cfID, $value); | |
366 | break; | |
367 | } | |
368 | } | |
369 | } | |
370 | elseif ($token == 'checksum') { | |
371 | $cs = CRM_Contact_BAO_Contact_Utils::generateChecksum($org['contact_id']); | |
372 | $value = "cs={$cs}"; | |
373 | } | |
374 | elseif ($token == 'address') { | |
375 | /* Build the location values array */ | |
376 | ||
377 | $loc = array(); | |
378 | $loc['display_name'] = CRM_Utils_Array::retrieveValueRecursive($org, 'display_name'); | |
379 | $loc['street_address'] = CRM_Utils_Array::retrieveValueRecursive($org, 'street_address'); | |
380 | $loc['city'] = CRM_Utils_Array::retrieveValueRecursive($org, 'city'); | |
381 | $loc['state_province'] = CRM_Utils_Array::retrieveValueRecursive($org, 'state_province'); | |
382 | $loc['postal_code'] = CRM_Utils_Array::retrieveValueRecursive($org, 'postal_code'); | |
383 | ||
384 | /* Construct the address token */ | |
385 | ||
386 | $value = CRM_Utils_Address::format($loc); | |
387 | if ($html) { | |
388 | $value = str_replace("\n", '<br />', $value); | |
389 | } | |
390 | } | |
391 | else { | |
392 | $value = CRM_Utils_Array::retrieveValueRecursive($org, $token); | |
393 | } | |
394 | ||
395 | self::token_replace('org', $token, $value, $str, $escapeSmarty); | |
396 | } | |
397 | ||
398 | return $str; | |
399 | } | |
400 | ||
401 | /** | |
402 | * Replace all mailing tokens in $str | |
403 | * | |
77855840 TO |
404 | * @param string $str |
405 | * The string with tokens to be replaced. | |
406 | * @param object $mailing | |
407 | * The mailing BAO, or null for validation. | |
408 | * @param bool $html | |
409 | * Replace tokens with HTML or plain text. | |
e39893f5 EM |
410 | * |
411 | * @param null $knownTokens | |
412 | * @param bool $escapeSmarty | |
6a488035 | 413 | * |
a6c01b45 CW |
414 | * @return string |
415 | * The processed sstring | |
6a488035 | 416 | */ |
21bb6c7b DL |
417 | public static function &replaceMailingTokens( |
418 | $str, | |
419 | &$mailing, | |
420 | $html = FALSE, | |
421 | $knownTokens = NULL, | |
422 | $escapeSmarty = FALSE | |
423 | ) { | |
6a488035 TO |
424 | $key = 'mailing'; |
425 | if (!$knownTokens || !isset($knownTokens[$key])) { | |
426 | return $str; | |
427 | } | |
428 | ||
8bab0eb0 | 429 | $str = preg_replace_callback( |
6a488035 | 430 | self::tokenRegex($key), |
353ffa53 TO |
431 | function ($matches) use (&$mailing, $escapeSmarty) { |
432 | return CRM_Utils_Token::getMailingTokenReplacement($matches[1], $mailing, $escapeSmarty); | |
8bab0eb0 DL |
433 | }, |
434 | $str | |
6a488035 TO |
435 | ); |
436 | return $str; | |
437 | } | |
438 | ||
e39893f5 EM |
439 | /** |
440 | * @param $token | |
441 | * @param $mailing | |
442 | * @param bool $escapeSmarty | |
443 | * | |
444 | * @return string | |
445 | */ | |
6a488035 TO |
446 | public static function getMailingTokenReplacement($token, &$mailing, $escapeSmarty = FALSE) { |
447 | $value = ''; | |
448 | switch ($token) { | |
449 | // CRM-7663 | |
450 | ||
451 | case 'id': | |
452 | $value = $mailing ? $mailing->id : 'undefined'; | |
453 | break; | |
454 | ||
455 | case 'name': | |
456 | $value = $mailing ? $mailing->name : 'Mailing Name'; | |
457 | break; | |
458 | ||
459 | case 'group': | |
460 | $groups = $mailing ? $mailing->getGroupNames() : array('Mailing Groups'); | |
461 | $value = implode(', ', $groups); | |
462 | break; | |
463 | ||
464 | case 'subject': | |
465 | $value = $mailing->subject; | |
466 | break; | |
467 | ||
468 | case 'viewUrl': | |
c57f36a1 PJ |
469 | $mailingKey = $mailing->id; |
470 | if ($hash = CRM_Mailing_BAO_Mailing::getMailingHash($mailingKey)) { | |
471 | $mailingKey = $hash; | |
472 | } | |
6a488035 | 473 | $value = CRM_Utils_System::url('civicrm/mailing/view', |
c57f36a1 | 474 | "reset=1&id={$mailingKey}", |
6a488035 TO |
475 | TRUE, NULL, FALSE, TRUE |
476 | ); | |
477 | break; | |
478 | ||
479 | case 'editUrl': | |
480 | $value = CRM_Utils_System::url('civicrm/mailing/send', | |
481 | "reset=1&mid={$mailing->id}&continue=true", | |
482 | TRUE, NULL, FALSE, TRUE | |
483 | ); | |
484 | break; | |
485 | ||
486 | case 'scheduleUrl': | |
487 | $value = CRM_Utils_System::url('civicrm/mailing/schedule', | |
488 | "reset=1&mid={$mailing->id}", | |
489 | TRUE, NULL, FALSE, TRUE | |
490 | ); | |
491 | break; | |
492 | ||
493 | case 'html': | |
494 | $page = new CRM_Mailing_Page_View(); | |
c57f36a1 | 495 | $value = $page->run($mailing->id, NULL, FALSE, TRUE); |
6a488035 TO |
496 | break; |
497 | ||
498 | case 'approvalStatus': | |
a8c23526 | 499 | $value = CRM_Core_PseudoConstant::getLabel('CRM_Mailing_DAO_Mailing', 'approval_status_id', $mailing->approval_status_id); |
6a488035 TO |
500 | break; |
501 | ||
502 | case 'approvalNote': | |
503 | $value = $mailing->approval_note; | |
504 | break; | |
505 | ||
506 | case 'approveUrl': | |
507 | $value = CRM_Utils_System::url('civicrm/mailing/approve', | |
508 | "reset=1&mid={$mailing->id}", | |
509 | TRUE, NULL, FALSE, TRUE | |
510 | ); | |
511 | break; | |
512 | ||
513 | case 'creator': | |
514 | $value = CRM_Contact_BAO_Contact::displayName($mailing->created_id); | |
515 | break; | |
516 | ||
517 | case 'creatorEmail': | |
518 | $value = CRM_Contact_BAO_Contact::getPrimaryEmail($mailing->created_id); | |
519 | break; | |
520 | ||
521 | default: | |
522 | $value = "{mailing.$token}"; | |
523 | break; | |
524 | } | |
525 | ||
526 | if ($escapeSmarty) { | |
527 | $value = self::tokenEscapeSmarty($value); | |
528 | } | |
529 | return $value; | |
530 | } | |
531 | ||
532 | /** | |
533 | * Replace all action tokens in $str | |
534 | * | |
77855840 TO |
535 | * @param string $str |
536 | * The string with tokens to be replaced. | |
537 | * @param array $addresses | |
538 | * Assoc. array of VERP event addresses. | |
539 | * @param array $urls | |
540 | * Assoc. array of action URLs. | |
541 | * @param bool $html | |
542 | * Replace tokens with HTML or plain text. | |
543 | * @param array $knownTokens | |
544 | * A list of tokens that are known to exist in the email body. | |
e39893f5 EM |
545 | * |
546 | * @param bool $escapeSmarty | |
6a488035 | 547 | * |
a6c01b45 CW |
548 | * @return string |
549 | * The processed string | |
6a488035 TO |
550 | */ |
551 | public static function &replaceActionTokens( | |
552 | $str, | |
553 | &$addresses, | |
554 | &$urls, | |
555 | $html = FALSE, | |
556 | $knownTokens = NULL, | |
557 | $escapeSmarty = FALSE | |
558 | ) { | |
559 | $key = 'action'; | |
560 | // here we intersect with the list of pre-configured valid tokens | |
561 | // so that we remove anything we do not recognize | |
562 | // I hope to move this step out of here soon and | |
563 | // then we will just iterate on a list of tokens that are passed to us | |
8cc574cf | 564 | if (!$knownTokens || empty($knownTokens[$key])) { |
6a488035 TO |
565 | return $str; |
566 | } | |
567 | ||
8bab0eb0 DL |
568 | $str = preg_replace_callback( |
569 | self::tokenRegex($key), | |
353ffa53 TO |
570 | function ($matches) use (&$addresses, &$urls, $html, $escapeSmarty) { |
571 | return CRM_Utils_Token::getActionTokenReplacement($matches[1], $addresses, $urls, $html, $escapeSmarty); | |
8bab0eb0 | 572 | }, |
6a488035 TO |
573 | $str |
574 | ); | |
575 | return $str; | |
576 | } | |
577 | ||
e39893f5 EM |
578 | /** |
579 | * @param $token | |
580 | * @param $addresses | |
581 | * @param $urls | |
582 | * @param bool $html | |
583 | * @param bool $escapeSmarty | |
584 | * | |
585 | * @return mixed|string | |
586 | */ | |
21bb6c7b DL |
587 | public static function getActionTokenReplacement( |
588 | $token, | |
589 | &$addresses, | |
590 | &$urls, | |
591 | $html = FALSE, | |
592 | $escapeSmarty = FALSE | |
593 | ) { | |
6a488035 | 594 | /* If the token is an email action, use it. Otherwise, find the |
e70a7fc0 | 595 | * appropriate URL */ |
6a488035 TO |
596 | |
597 | if (!in_array($token, self::$_tokens['action'])) { | |
598 | $value = "{action.$token}"; | |
599 | } | |
600 | else { | |
601 | $value = CRM_Utils_Array::value($token, $addresses); | |
602 | ||
603 | if ($value == NULL) { | |
604 | $value = CRM_Utils_Array::value($token, $urls); | |
605 | } | |
606 | ||
607 | if ($value && $html) { | |
608 | //fix for CRM-2318 | |
609 | if ((substr($token, -3) != 'Url') && ($token != 'forward')) { | |
610 | $value = "mailto:$value"; | |
611 | } | |
612 | } | |
613 | elseif ($value && !$html) { | |
614 | $value = str_replace('&', '&', $value); | |
615 | } | |
616 | } | |
617 | ||
618 | if ($escapeSmarty) { | |
619 | $value = self::tokenEscapeSmarty($value); | |
620 | } | |
621 | return $value; | |
622 | } | |
623 | ||
624 | /** | |
625 | * Replace all the contact-level tokens in $str with information from | |
626 | * $contact. | |
627 | * | |
77855840 TO |
628 | * @param string $str |
629 | * The string with tokens to be replaced. | |
630 | * @param array $contact | |
631 | * Associative array of contact properties. | |
632 | * @param bool $html | |
633 | * Replace tokens with HTML or plain text. | |
634 | * @param array $knownTokens | |
635 | * A list of tokens that are known to exist in the email body. | |
636 | * @param bool $returnBlankToken | |
637 | * Return unevaluated token if value is null. | |
e39893f5 EM |
638 | * |
639 | * @param bool $escapeSmarty | |
6a488035 | 640 | * |
a6c01b45 CW |
641 | * @return string |
642 | * The processed string | |
6a488035 | 643 | */ |
21bb6c7b DL |
644 | public static function &replaceContactTokens( |
645 | $str, | |
646 | &$contact, | |
647 | $html = FALSE, | |
648 | $knownTokens = NULL, | |
649 | $returnBlankToken = FALSE, | |
650 | $escapeSmarty = FALSE | |
6a488035 TO |
651 | ) { |
652 | $key = 'contact'; | |
653 | if (self::$_tokens[$key] == NULL) { | |
654 | /* This should come from UF */ | |
655 | ||
e7483cbe J |
656 | self::$_tokens[$key] |
657 | = array_merge( | |
21bb6c7b DL |
658 | array_keys(CRM_Contact_BAO_Contact::exportableFields('All')), |
659 | array('checksum', 'contact_id') | |
660 | ); | |
6a488035 TO |
661 | } |
662 | ||
663 | // here we intersect with the list of pre-configured valid tokens | |
664 | // so that we remove anything we do not recognize | |
665 | // I hope to move this step out of here soon and | |
666 | // then we will just iterate on a list of tokens that are passed to us | |
8cc574cf | 667 | if (!$knownTokens || empty($knownTokens[$key])) { |
6a488035 TO |
668 | return $str; |
669 | } | |
670 | ||
8bab0eb0 | 671 | $str = preg_replace_callback( |
21bb6c7b | 672 | self::tokenRegex($key), |
353ffa53 TO |
673 | function ($matches) use (&$contact, $html, $returnBlankToken, $escapeSmarty) { |
674 | return CRM_Utils_Token::getContactTokenReplacement($matches[1], $contact, $html, $returnBlankToken, $escapeSmarty); | |
8bab0eb0 | 675 | }, |
6a488035 TO |
676 | $str |
677 | ); | |
678 | ||
679 | $str = preg_replace('/\\\\|\{(\s*)?\}/', ' ', $str); | |
680 | return $str; | |
681 | } | |
682 | ||
e39893f5 EM |
683 | /** |
684 | * @param $token | |
685 | * @param $contact | |
686 | * @param bool $html | |
687 | * @param bool $returnBlankToken | |
688 | * @param bool $escapeSmarty | |
689 | * | |
690 | * @return bool|mixed|null|string | |
691 | */ | |
21bb6c7b DL |
692 | public static function getContactTokenReplacement( |
693 | $token, | |
694 | &$contact, | |
695 | $html = FALSE, | |
696 | $returnBlankToken = FALSE, | |
697 | $escapeSmarty = FALSE | |
6a488035 TO |
698 | ) { |
699 | if (self::$_tokens['contact'] == NULL) { | |
700 | /* This should come from UF */ | |
701 | ||
e7483cbe J |
702 | self::$_tokens['contact'] |
703 | = array_merge( | |
21bb6c7b DL |
704 | array_keys(CRM_Contact_BAO_Contact::exportableFields('All')), |
705 | array('checksum', 'contact_id') | |
706 | ); | |
6a488035 TO |
707 | } |
708 | ||
709 | /* Construct value from $token and $contact */ | |
710 | ||
711 | $value = NULL; | |
73d64eb6 | 712 | $noReplace = FALSE; |
6a488035 | 713 | |
091133bb CW |
714 | // Support legacy tokens |
715 | $token = CRM_Utils_Array::value($token, self::legacyContactTokens(), $token); | |
716 | ||
6a488035 TO |
717 | // check if the token we were passed is valid |
718 | // we have to do this because this function is | |
719 | // called only when we find a token in the string | |
720 | ||
721 | if (!in_array($token, self::$_tokens['contact'])) { | |
73d64eb6 | 722 | $noReplace = TRUE; |
6a488035 TO |
723 | } |
724 | elseif ($token == 'checksum') { | |
725 | $hash = CRM_Utils_Array::value('hash', $contact); | |
726 | $contactID = CRM_Utils_Array::retrieveValueRecursive($contact, 'contact_id'); | |
727 | $cs = CRM_Contact_BAO_Contact_Utils::generateChecksum($contactID, | |
728 | NULL, | |
729 | NULL, | |
730 | $hash | |
731 | ); | |
732 | $value = "cs={$cs}"; | |
733 | } | |
734 | else { | |
735 | $value = CRM_Utils_Array::retrieveValueRecursive($contact, $token); | |
ee70f332 | 736 | |
091133bb | 737 | // FIXME: for some pseudoconstants we get array ( 0 => id, 1 => label ) |
ee70f332 | 738 | if (is_array($value)) { |
739 | $value = $value[1]; | |
740 | } | |
091133bb CW |
741 | // Convert pseudoconstants using metadata |
742 | elseif ($value && is_numeric($value)) { | |
743 | $allFields = CRM_Contact_BAO_Contact::exportableFields('All'); | |
744 | if (!empty($allFields[$token]['pseudoconstant'])) { | |
745 | $value = CRM_Core_PseudoConstant::getLabel('CRM_Contact_BAO_Contact', $token, $value); | |
746 | } | |
747 | } | |
6a488035 TO |
748 | } |
749 | ||
750 | if (!$html) { | |
751 | $value = str_replace('&', '&', $value); | |
752 | } | |
753 | ||
754 | // if null then return actual token | |
755 | if ($returnBlankToken && !$value) { | |
73d64eb6 OB |
756 | $noReplace = TRUE; |
757 | } | |
758 | ||
759 | if ($noReplace) { | |
6a488035 TO |
760 | $value = "{contact.$token}"; |
761 | } | |
762 | ||
73d64eb6 | 763 | if ($escapeSmarty |
353ffa53 | 764 | && !($returnBlankToken && $noReplace) |
e7483cbe J |
765 | ) { |
766 | // $returnBlankToken means the caller wants to do further attempts at | |
767 | // processing unreplaced tokens -- so don't escape them yet in this case. | |
6a488035 TO |
768 | $value = self::tokenEscapeSmarty($value); |
769 | } | |
770 | ||
771 | return $value; | |
772 | } | |
773 | ||
774 | /** | |
775 | * Replace all the hook tokens in $str with information from | |
776 | * $contact. | |
777 | * | |
77855840 TO |
778 | * @param string $str |
779 | * The string with tokens to be replaced. | |
780 | * @param array $contact | |
781 | * Associative array of contact properties (including hook token values). | |
e39893f5 | 782 | * @param $categories |
77855840 TO |
783 | * @param bool $html |
784 | * Replace tokens with HTML or plain text. | |
e39893f5 EM |
785 | * |
786 | * @param bool $escapeSmarty | |
6a488035 | 787 | * |
a6c01b45 CW |
788 | * @return string |
789 | * The processed string | |
6a488035 | 790 | */ |
21bb6c7b DL |
791 | public static function &replaceHookTokens( |
792 | $str, | |
793 | &$contact, | |
794 | &$categories, | |
795 | $html = FALSE, | |
796 | $escapeSmarty = FALSE | |
797 | ) { | |
6a488035 | 798 | foreach ($categories as $key) { |
8bab0eb0 | 799 | $str = preg_replace_callback( |
21bb6c7b | 800 | self::tokenRegex($key), |
353ffa53 TO |
801 | function ($matches) use (&$contact, $key, $html, $escapeSmarty) { |
802 | return CRM_Utils_Token::getHookTokenReplacement($matches[1], $contact, $key, $html, $escapeSmarty); | |
8bab0eb0 | 803 | }, |
6a488035 TO |
804 | $str |
805 | ); | |
806 | } | |
807 | return $str; | |
808 | } | |
809 | ||
2d3e3c7b | 810 | /** |
fe482240 | 811 | * Parse html through Smarty resolving any smarty functions. |
2d3e3c7b | 812 | * @param string $tokenHtml |
813 | * @param array $entity | |
814 | * @param string $entityType | |
a6c01b45 CW |
815 | * @return string |
816 | * html parsed through smarty | |
2d3e3c7b | 817 | */ |
818 | public static function parseThroughSmarty($tokenHtml, $entity, $entityType = 'contact') { | |
819 | if (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY) { | |
820 | $smarty = CRM_Core_Smarty::singleton(); | |
821 | // also add the tokens to the template | |
822 | $smarty->assign_by_ref($entityType, $entity); | |
823 | $tokenHtml = $smarty->fetch("string:$tokenHtml"); | |
824 | } | |
825 | return $tokenHtml; | |
826 | } | |
5bc392e6 EM |
827 | |
828 | /** | |
829 | * @param $token | |
830 | * @param $contact | |
831 | * @param $category | |
832 | * @param bool $html | |
833 | * @param bool $escapeSmarty | |
834 | * | |
835 | * @return mixed|string | |
95ea96be EM |
836 | */ |
837 | public static function getHookTokenReplacement( | |
21bb6c7b DL |
838 | $token, |
839 | &$contact, | |
840 | $category, | |
841 | $html = FALSE, | |
842 | $escapeSmarty = FALSE | |
843 | ) { | |
6a488035 TO |
844 | $value = CRM_Utils_Array::value("{$category}.{$token}", $contact); |
845 | ||
21bb6c7b | 846 | if ($value && !$html) { |
6a488035 TO |
847 | $value = str_replace('&', '&', $value); |
848 | } | |
849 | ||
850 | if ($escapeSmarty) { | |
851 | $value = self::tokenEscapeSmarty($value); | |
852 | } | |
853 | ||
854 | return $value; | |
855 | } | |
856 | ||
857 | /** | |
858 | * unescapeTokens removes any characters that caused the replacement routines to skip token replacement | |
859 | * for example {{token}} or \{token} will result in {token} in the final email | |
860 | * | |
861 | * this routine will remove the extra backslashes and braces | |
862 | * | |
e7292422 | 863 | * @param $str ref to the string that will be scanned and modified |
a6c01b45 CW |
864 | * @return void |
865 | * this function works directly on the string that is passed | |
6a488035 TO |
866 | */ |
867 | public static function unescapeTokens(&$str) { | |
868 | $str = preg_replace('/\\\\|\{(\{\w+\.\w+\})\}/', '\\1', $str); | |
869 | } | |
870 | ||
871 | /** | |
fe482240 | 872 | * Replace unsubscribe tokens. |
6a488035 | 873 | * |
77855840 TO |
874 | * @param string $str |
875 | * The string with tokens to be replaced. | |
876 | * @param object $domain | |
877 | * The domain BAO. | |
878 | * @param array $groups | |
879 | * The groups (if any) being unsubscribed. | |
880 | * @param bool $html | |
881 | * Replace tokens with html or plain text. | |
882 | * @param int $contact_id | |
883 | * The contact ID. | |
e7483cbe | 884 | * @param string $hash The security hash of the unsub event |
6a488035 | 885 | * |
a6c01b45 CW |
886 | * @return string |
887 | * The processed string | |
6a488035 TO |
888 | */ |
889 | public static function &replaceUnsubscribeTokens( | |
890 | $str, | |
891 | &$domain, | |
892 | &$groups, | |
893 | $html, | |
894 | $contact_id, | |
895 | $hash | |
896 | ) { | |
897 | if (self::token_match('unsubscribe', 'group', $str)) { | |
898 | if (!empty($groups)) { | |
899 | $config = CRM_Core_Config::singleton(); | |
900 | $base = CRM_Utils_System::baseURL(); | |
901 | ||
902 | // FIXME: an ugly hack for CRM-2035, to be dropped once CRM-1799 is implemented | |
903 | $dao = new CRM_Contact_DAO_Group(); | |
904 | $dao->find(); | |
905 | while ($dao->fetch()) { | |
906 | if (substr($dao->visibility, 0, 6) == 'Public') { | |
907 | $visibleGroups[] = $dao->id; | |
908 | } | |
909 | } | |
910 | $value = implode(', ', $groups); | |
911 | self::token_replace('unsubscribe', 'group', $value, $str); | |
912 | } | |
913 | } | |
914 | return $str; | |
915 | } | |
916 | ||
917 | /** | |
fe482240 | 918 | * Replace resubscribe tokens. |
6a488035 | 919 | * |
77855840 TO |
920 | * @param string $str |
921 | * The string with tokens to be replaced. | |
922 | * @param object $domain | |
923 | * The domain BAO. | |
924 | * @param array $groups | |
925 | * The groups (if any) being resubscribed. | |
926 | * @param bool $html | |
927 | * Replace tokens with html or plain text. | |
928 | * @param int $contact_id | |
929 | * The contact ID. | |
e7483cbe | 930 | * @param string $hash The security hash of the resub event |
6a488035 | 931 | * |
a6c01b45 CW |
932 | * @return string |
933 | * The processed string | |
6a488035 | 934 | */ |
a3e55d9c TO |
935 | public static function &replaceResubscribeTokens( |
936 | $str, &$domain, &$groups, $html, | |
6a488035 TO |
937 | $contact_id, $hash |
938 | ) { | |
939 | if (self::token_match('resubscribe', 'group', $str)) { | |
940 | if (!empty($groups)) { | |
941 | $value = implode(', ', $groups); | |
942 | self::token_replace('resubscribe', 'group', $value, $str); | |
943 | } | |
944 | } | |
945 | return $str; | |
946 | } | |
947 | ||
948 | /** | |
949 | * Replace subscription-confirmation-request tokens | |
950 | * | |
77855840 TO |
951 | * @param string $str |
952 | * The string with tokens to be replaced. | |
953 | * @param string $group | |
954 | * The name of the group being subscribed. | |
e39893f5 | 955 | * @param $url |
77855840 TO |
956 | * @param bool $html |
957 | * Replace tokens with html or plain text. | |
6a488035 | 958 | * |
a6c01b45 CW |
959 | * @return string |
960 | * The processed string | |
6a488035 TO |
961 | */ |
962 | public static function &replaceSubscribeTokens($str, $group, $url, $html) { | |
963 | if (self::token_match('subscribe', 'group', $str)) { | |
964 | self::token_replace('subscribe', 'group', $group, $str); | |
965 | } | |
966 | if (self::token_match('subscribe', 'url', $str)) { | |
967 | self::token_replace('subscribe', 'url', $url, $str); | |
968 | } | |
969 | return $str; | |
970 | } | |
971 | ||
972 | /** | |
973 | * Replace subscription-invitation tokens | |
974 | * | |
77855840 TO |
975 | * @param string $str |
976 | * The string with tokens to be replaced. | |
6a488035 | 977 | * |
a6c01b45 CW |
978 | * @return string |
979 | * The processed string | |
6a488035 TO |
980 | */ |
981 | public static function &replaceSubscribeInviteTokens($str) { | |
982 | if (preg_match('/\{action\.subscribeUrl\}/', $str)) { | |
983 | $url = CRM_Utils_System::url('civicrm/mailing/subscribe', | |
984 | 'reset=1', | |
985 | TRUE, NULL, TRUE, TRUE | |
986 | ); | |
987 | $str = preg_replace('/\{action\.subscribeUrl\}/', $url, $str); | |
988 | } | |
989 | ||
990 | if (preg_match('/\{action\.subscribeUrl.\d+\}/', $str, $matches)) { | |
991 | foreach ($matches as $key => $value) { | |
992 | $gid = substr($value, 21, -1); | |
993 | $url = CRM_Utils_System::url('civicrm/mailing/subscribe', | |
994 | "reset=1&gid={$gid}", | |
995 | TRUE, NULL, TRUE, TRUE | |
996 | ); | |
997 | $url = str_replace('&', '&', $url); | |
998 | $str = preg_replace('/' . preg_quote($value) . '/', $url, $str); | |
999 | } | |
1000 | } | |
1001 | ||
1002 | if (preg_match('/\{action\.subscribe.\d+\}/', $str, $matches)) { | |
1003 | foreach ($matches as $key => $value) { | |
353ffa53 TO |
1004 | $gid = substr($value, 18, -1); |
1005 | $config = CRM_Core_Config::singleton(); | |
1006 | $domain = CRM_Core_BAO_MailSettings::defaultDomain(); | |
6a488035 TO |
1007 | $localpart = CRM_Core_BAO_MailSettings::defaultLocalpart(); |
1008 | // we add the 0.0000000000000000 part to make this match the other email patterns (with action, two ids and a hash) | |
1009 | $str = preg_replace('/' . preg_quote($value) . '/', "mailto:{$localpart}s.{$gid}.0.0000000000000000@$domain", $str); | |
1010 | } | |
1011 | } | |
1012 | return $str; | |
1013 | } | |
1014 | ||
1015 | /** | |
1016 | * Replace welcome/confirmation tokens | |
1017 | * | |
77855840 TO |
1018 | * @param string $str |
1019 | * The string with tokens to be replaced. | |
1020 | * @param string $group | |
1021 | * The name of the group being subscribed. | |
1022 | * @param bool $html | |
1023 | * Replace tokens with html or plain text. | |
6a488035 | 1024 | * |
a6c01b45 CW |
1025 | * @return string |
1026 | * The processed string | |
6a488035 TO |
1027 | */ |
1028 | public static function &replaceWelcomeTokens($str, $group, $html) { | |
1029 | if (self::token_match('welcome', 'group', $str)) { | |
1030 | self::token_replace('welcome', 'group', $group, $str); | |
1031 | } | |
1032 | return $str; | |
1033 | } | |
1034 | ||
1035 | /** | |
1036 | * Find unprocessed tokens (call this last) | |
1037 | * | |
77855840 TO |
1038 | * @param string $str |
1039 | * The string to search. | |
6a488035 | 1040 | * |
a6c01b45 CW |
1041 | * @return array |
1042 | * Array of tokens that weren't replaced | |
6a488035 TO |
1043 | */ |
1044 | public static function &unmatchedTokens(&$str) { | |
1045 | //preg_match_all('/[^\{\\\\]\{(\w+\.\w+)\}[^\}]/', $str, $match); | |
1046 | preg_match_all('/\{(\w+\.\w+)\}/', $str, $match); | |
1047 | return $match[1]; | |
1048 | } | |
1049 | ||
1050 | /** | |
fe482240 | 1051 | * Find and replace tokens for each component. |
6a488035 | 1052 | * |
77855840 TO |
1053 | * @param string $str |
1054 | * The string to search. | |
1055 | * @param array $contact | |
1056 | * Associative array of contact properties. | |
1057 | * @param array $components | |
1058 | * A list of tokens that are known to exist in the email body. | |
6a488035 | 1059 | * |
e39893f5 EM |
1060 | * @param bool $escapeSmarty |
1061 | * @param bool $returnEmptyToken | |
1062 | * | |
a6c01b45 CW |
1063 | * @return string |
1064 | * The processed string | |
6a488035 TO |
1065 | */ |
1066 | public static function &replaceComponentTokens(&$str, $contact, $components, $escapeSmarty = FALSE, $returnEmptyToken = TRUE) { | |
1067 | if (!is_array($components) || empty($contact)) { | |
1068 | return $str; | |
1069 | } | |
1070 | ||
1071 | foreach ($components as $name => $tokens) { | |
1072 | if (!is_array($tokens) || empty($tokens)) { | |
1073 | continue; | |
1074 | } | |
1075 | ||
1076 | foreach ($tokens as $token) { | |
1077 | if (self::token_match($name, $token, $str) && isset($contact[$name . '.' . $token])) { | |
1078 | self::token_replace($name, $token, $contact[$name . '.' . $token], $str, $escapeSmarty); | |
1079 | } | |
1080 | elseif (!$returnEmptyToken) { | |
1081 | //replacing empty token | |
1082 | self::token_replace($name, $token, "", $str, $escapeSmarty); | |
1083 | } | |
1084 | } | |
1085 | } | |
1086 | return $str; | |
1087 | } | |
1088 | ||
1089 | /** | |
fe482240 | 1090 | * Get array of string tokens. |
6a488035 | 1091 | * |
5a4f6742 | 1092 | * @param string $string |
77855840 | 1093 | * The input string to parse for tokens. |
6a488035 | 1094 | * |
a6c01b45 CW |
1095 | * @return array |
1096 | * array of tokens mentioned in field | |
6a488035 | 1097 | */ |
00be9182 | 1098 | public static function getTokens($string) { |
6a488035 TO |
1099 | $matches = array(); |
1100 | $tokens = array(); | |
1101 | preg_match_all('/(?<!\{|\\\\)\{(\w+\.\w+)\}(?!\})/', | |
1102 | $string, | |
1103 | $matches, | |
1104 | PREG_PATTERN_ORDER | |
1105 | ); | |
1106 | ||
1107 | if ($matches[1]) { | |
1108 | foreach ($matches[1] as $token) { | |
1109 | list($type, $name) = preg_split('/\./', $token, 2); | |
1110 | if ($name && $type) { | |
1111 | if (!isset($tokens[$type])) { | |
1112 | $tokens[$type] = array(); | |
1113 | } | |
1114 | $tokens[$type][] = $name; | |
1115 | } | |
1116 | } | |
1117 | } | |
1118 | return $tokens; | |
1119 | } | |
1120 | ||
bdd49e38 EM |
1121 | /** |
1122 | * Function to determine which values to retrieve to insert into tokens. The heavy resemblance between this function | |
1123 | * and getTokens appears to be historical rather than intentional and should be reviewed | |
1124 | * @param $string | |
a6c01b45 CW |
1125 | * @return array |
1126 | * fields to pass in as return properties when populating token | |
bdd49e38 EM |
1127 | */ |
1128 | public static function getReturnProperties(&$string) { | |
e7292422 TO |
1129 | $returnProperties = array(); |
1130 | $matches = array(); | |
1131 | preg_match_all('/(?<!\{|\\\\)\{(\w+\.\w+)\}(?!\})/', | |
353ffa53 TO |
1132 | $string, |
1133 | $matches, | |
1134 | PREG_PATTERN_ORDER | |
1135 | ); | |
e7292422 TO |
1136 | if ($matches[1]) { |
1137 | foreach ($matches[1] as $token) { | |
1138 | list($type, $name) = preg_split('/\./', $token, 2); | |
1139 | if ($name) { | |
1140 | $returnProperties["{$name}"] = 1; | |
bdd49e38 EM |
1141 | } |
1142 | } | |
e7292422 | 1143 | } |
bdd49e38 | 1144 | |
e7292422 | 1145 | return $returnProperties; |
bdd49e38 EM |
1146 | } |
1147 | ||
6a488035 | 1148 | /** |
100fef9d | 1149 | * Gives required details of contacts in an indexed array format so we |
6a488035 TO |
1150 | * can iterate in a nice loop and do token evaluation |
1151 | * | |
e39893f5 | 1152 | * @param $contactIDs |
77855840 TO |
1153 | * @param array $returnProperties |
1154 | * Of required properties. | |
e7483cbe | 1155 | * @param bool $skipOnHold Don't return on_hold contact info also. |
77855840 | 1156 | * Don't return on_hold contact info also. |
e7483cbe | 1157 | * @param bool $skipDeceased Don't return deceased contact info. |
77855840 TO |
1158 | * Don't return deceased contact info. |
1159 | * @param array $extraParams | |
1160 | * Extra params. | |
1161 | * @param array $tokens | |
1162 | * The list of tokens we've extracted from the content. | |
e39893f5 | 1163 | * @param null $className |
77855840 TO |
1164 | * @param int $jobID |
1165 | * The mailing list jobID - this is a legacy param. | |
6a488035 TO |
1166 | * |
1167 | * @return array | |
6a488035 | 1168 | */ |
e7483cbe | 1169 | public static function getTokenDetails( |
a3e55d9c | 1170 | $contactIDs, |
6a488035 | 1171 | $returnProperties = NULL, |
e7292422 TO |
1172 | $skipOnHold = TRUE, |
1173 | $skipDeceased = TRUE, | |
1174 | $extraParams = NULL, | |
1175 | $tokens = array(), | |
1176 | $className = NULL, | |
1177 | $jobID = NULL | |
6a488035 TO |
1178 | ) { |
1179 | if (empty($contactIDs)) { | |
1180 | // putting a fatal here so we can track if/when this happens | |
1181 | CRM_Core_Error::fatal(); | |
1182 | } | |
1183 | ||
1184 | $params = array(); | |
1185 | foreach ($contactIDs as $key => $contactID) { | |
1186 | $params[] = array( | |
1187 | CRM_Core_Form::CB_PREFIX . $contactID, | |
353ffa53 TO |
1188 | '=', |
1189 | 1, | |
1190 | 0, | |
1191 | 0, | |
6a488035 TO |
1192 | ); |
1193 | } | |
1194 | ||
1195 | // fix for CRM-2613 | |
1196 | if ($skipDeceased) { | |
1197 | $params[] = array('is_deceased', '=', 0, 0, 0); | |
1198 | } | |
1199 | ||
1200 | //fix for CRM-3798 | |
1201 | if ($skipOnHold) { | |
1202 | $params[] = array('on_hold', '=', 0, 0, 0); | |
1203 | } | |
1204 | ||
1205 | if ($extraParams) { | |
1206 | $params = array_merge($params, $extraParams); | |
1207 | } | |
1208 | ||
1209 | // if return properties are not passed then get all return properties | |
1210 | if (empty($returnProperties)) { | |
1211 | $fields = array_merge(array_keys(CRM_Contact_BAO_Contact::exportableFields()), | |
1212 | array('display_name', 'checksum', 'contact_id') | |
1213 | ); | |
1214 | foreach ($fields as $key => $val) { | |
1215 | $returnProperties[$val] = 1; | |
1216 | } | |
1217 | } | |
1218 | ||
1219 | $custom = array(); | |
1220 | foreach ($returnProperties as $name => $dontCare) { | |
1221 | $cfID = CRM_Core_BAO_CustomField::getKeyID($name); | |
1222 | if ($cfID) { | |
1223 | $custom[] = $cfID; | |
1224 | } | |
1225 | } | |
1226 | ||
1227 | //get the total number of contacts to fetch from database. | |
1228 | $numberofContacts = count($contactIDs); | |
1229 | $query = new CRM_Contact_BAO_Query($params, $returnProperties); | |
1230 | ||
1231 | $details = $query->apiQuery($params, $returnProperties, NULL, NULL, 0, $numberofContacts); | |
1232 | ||
1233 | $contactDetails = &$details[0]; | |
1234 | ||
1235 | foreach ($contactIDs as $key => $contactID) { | |
1236 | if (array_key_exists($contactID, $contactDetails)) { | |
1237 | if (CRM_Utils_Array::value('preferred_communication_method', $returnProperties) == 1 | |
1238 | && array_key_exists('preferred_communication_method', $contactDetails[$contactID]) | |
1239 | ) { | |
e7e657f0 | 1240 | $pcm = CRM_Core_PseudoConstant::get('CRM_Contact_DAO_Contact', 'preferred_communication_method'); |
6a488035 TO |
1241 | |
1242 | // communication Prefferance | |
1243 | $contactPcm = explode(CRM_Core_DAO::VALUE_SEPARATOR, | |
1244 | $contactDetails[$contactID]['preferred_communication_method'] | |
1245 | ); | |
1246 | $result = array(); | |
1247 | foreach ($contactPcm as $key => $val) { | |
1248 | if ($val) { | |
1249 | $result[$val] = $pcm[$val]; | |
1250 | } | |
1251 | } | |
1252 | $contactDetails[$contactID]['preferred_communication_method'] = implode(', ', $result); | |
1253 | } | |
1254 | ||
1255 | foreach ($custom as $cfID) { | |
1256 | if (isset($contactDetails[$contactID]["custom_{$cfID}"])) { | |
1257 | $contactDetails[$contactID]["custom_{$cfID}"] = CRM_Core_BAO_CustomField::getDisplayValue($contactDetails[$contactID]["custom_{$cfID}"], | |
1258 | $cfID, $details[1] | |
1259 | ); | |
1260 | } | |
1261 | } | |
1262 | ||
1263 | //special case for greeting replacement | |
1264 | foreach (array( | |
353ffa53 TO |
1265 | 'email_greeting', |
1266 | 'postal_greeting', | |
e7483cbe | 1267 | 'addressee', |
353ffa53 | 1268 | ) as $val) { |
a7488080 | 1269 | if (!empty($contactDetails[$contactID][$val])) { |
6a488035 TO |
1270 | $contactDetails[$contactID][$val] = $contactDetails[$contactID]["{$val}_display"]; |
1271 | } | |
1272 | } | |
1273 | } | |
1274 | } | |
1275 | ||
1276 | // also call a hook and get token details | |
1277 | CRM_Utils_Hook::tokenValues($details[0], | |
1278 | $contactIDs, | |
1279 | $jobID, | |
1280 | $tokens, | |
1281 | $className | |
1282 | ); | |
1283 | return $details; | |
1284 | } | |
1285 | ||
d20c4dad EM |
1286 | /** |
1287 | * Call hooks on tokens for anonymous users - contact id is set to 0 - this allows non-contact | |
1288 | * specific tokens to be rendered | |
1289 | * | |
77855840 TO |
1290 | * @param array $contactIDs |
1291 | * This should always be array(0) or its not anonymous - left to keep signature same. | |
16b10e64 | 1292 | * as main fn |
d20c4dad | 1293 | * @param string $returnProperties |
77855840 TO |
1294 | * @param bool $skipOnHold |
1295 | * @param bool $skipDeceased | |
d20c4dad EM |
1296 | * @param string $extraParams |
1297 | * @param array $tokens | |
77855840 TO |
1298 | * @param string $className |
1299 | * Sent as context to the hook. | |
d20c4dad | 1300 | * @param string $jobID |
a6c01b45 CW |
1301 | * @return array |
1302 | * contactDetails with hooks swapped out | |
d20c4dad | 1303 | */ |
a3e55d9c | 1304 | public function getAnonymousTokenDetails($contactIDs = array( |
e7483cbe | 1305 | 0, |
353ffa53 TO |
1306 | ), |
1307 | $returnProperties = NULL, | |
1308 | $skipOnHold = TRUE, | |
1309 | $skipDeceased = TRUE, | |
1310 | $extraParams = NULL, | |
1311 | $tokens = array(), | |
1312 | $className = NULL, | |
1313 | $jobID = NULL) { | |
d20c4dad | 1314 | $details = array(0 => array()); |
e7292422 TO |
1315 | // also call a hook and get token details |
1316 | CRM_Utils_Hook::tokenValues($details[0], | |
d20c4dad EM |
1317 | $contactIDs, |
1318 | $jobID, | |
1319 | $tokens, | |
1320 | $className | |
1321 | ); | |
1322 | return $details; | |
1323 | } | |
e39893f5 | 1324 | |
6a488035 | 1325 | /** |
100fef9d | 1326 | * Gives required details of contribuion in an indexed array format so we |
6a488035 TO |
1327 | * can iterate in a nice loop and do token evaluation |
1328 | * | |
c490a46a | 1329 | * @param array $contributionIDs |
77855840 TO |
1330 | * @param array $returnProperties |
1331 | * Of required properties. | |
1332 | * @param array $extraParams | |
1333 | * Extra params. | |
1334 | * @param array $tokens | |
1335 | * The list of tokens we've extracted from the content. | |
c490a46a | 1336 | * @param string $className |
6a488035 TO |
1337 | * |
1338 | * @return array | |
6a488035 | 1339 | */ |
e7483cbe | 1340 | public static function getContributionTokenDetails( |
a3e55d9c | 1341 | $contributionIDs, |
6a488035 | 1342 | $returnProperties = NULL, |
e7292422 TO |
1343 | $extraParams = NULL, |
1344 | $tokens = array(), | |
1345 | $className = NULL | |
6a488035 | 1346 | ) { |
e7483cbe J |
1347 | //@todo - this function basically replicates calling |
1348 | //civicrm_api3('contribution', 'get', array('id' => array('IN' => array()) | |
6a488035 TO |
1349 | if (empty($contributionIDs)) { |
1350 | // putting a fatal here so we can track if/when this happens | |
1351 | CRM_Core_Error::fatal(); | |
1352 | } | |
1353 | ||
1354 | $details = array(); | |
1355 | ||
1356 | // no apiQuery helper yet, so do a loop and find contribution by id | |
1357 | foreach ($contributionIDs as $contributionID) { | |
1358 | ||
1359 | $dao = new CRM_Contribute_DAO_Contribution(); | |
1360 | $dao->id = $contributionID; | |
1361 | ||
1362 | if ($dao->find(TRUE)) { | |
1363 | ||
1364 | $details[$dao->id] = array(); | |
1365 | CRM_Core_DAO::storeValues($dao, $details[$dao->id]); | |
1366 | ||
1367 | // do the necessary transformation | |
a7488080 | 1368 | if (!empty($details[$dao->id]['payment_instrument_id'])) { |
6a488035 TO |
1369 | $piId = $details[$dao->id]['payment_instrument_id']; |
1370 | $pis = CRM_Contribute_PseudoConstant::paymentInstrument(); | |
1371 | $details[$dao->id]['payment_instrument'] = $pis[$piId]; | |
1372 | } | |
a7488080 | 1373 | if (!empty($details[$dao->id]['campaign_id'])) { |
6a488035 TO |
1374 | $campaignId = $details[$dao->id]['campaign_id']; |
1375 | $campaigns = CRM_Campaign_BAO_Campaign::getCampaigns($campaignId); | |
1376 | $details[$dao->id]['campaign'] = $campaigns[$campaignId]; | |
1377 | } | |
1378 | ||
a7488080 | 1379 | if (!empty($details[$dao->id]['financial_type_id'])) { |
8b712d49 DG |
1380 | $financialtypeId = $details[$dao->id]['financial_type_id']; |
1381 | $ftis = CRM_Contribute_PseudoConstant::financialType(); | |
1382 | $details[$dao->id]['financial_type'] = $ftis[$financialtypeId]; | |
1383 | } | |
1384 | ||
6a488035 TO |
1385 | // TODO: call a hook to get token contribution details |
1386 | } | |
1387 | } | |
1388 | ||
1389 | return $details; | |
1390 | } | |
1391 | ||
2d3e3c7b | 1392 | /** |
fe482240 | 1393 | * Get Membership Token Details. |
77855840 TO |
1394 | * @param array $membershipIDs |
1395 | * Array of membership IDS. | |
2d3e3c7b | 1396 | */ |
00be9182 | 1397 | public static function getMembershipTokenDetails($membershipIDs) { |
353ffa53 TO |
1398 | $memberships = civicrm_api3('membership', 'get', array( |
1399 | 'options' => array('limit' => 200000), | |
e7483cbe | 1400 | 'membership_id' => array('IN' => (array) $membershipIDs), |
353ffa53 | 1401 | )); |
2d3e3c7b | 1402 | return $memberships['values']; |
1403 | } | |
353ffa53 | 1404 | |
6a488035 | 1405 | /** |
100fef9d | 1406 | * Replace greeting tokens exists in message/subject |
6a488035 | 1407 | */ |
00be9182 | 1408 | public static function replaceGreetingTokens(&$tokenString, $contactDetails = NULL, $contactId = NULL, $className = NULL, $escapeSmarty = FALSE) { |
6a488035 TO |
1409 | |
1410 | if (!$contactDetails && !$contactId) { | |
1411 | return; | |
1412 | } | |
1413 | ||
1414 | // check if there are any tokens | |
1415 | $greetingTokens = self::getTokens($tokenString); | |
1416 | ||
1417 | if (!empty($greetingTokens)) { | |
1418 | // first use the existing contact object for token replacement | |
1419 | if (!empty($contactDetails)) { | |
73d64eb6 | 1420 | $tokenString = CRM_Utils_Token::replaceContactTokens($tokenString, $contactDetails, TRUE, $greetingTokens, TRUE, $escapeSmarty); |
6a488035 TO |
1421 | } |
1422 | ||
1423 | // check if there are any unevaluated tokens | |
1424 | $greetingTokens = self::getTokens($tokenString); | |
1425 | ||
e7483cbe J |
1426 | // $greetingTokens not empty, means there are few tokens which are not |
1427 | // evaluated, like custom data etc | |
6a488035 TO |
1428 | // so retrieve it from database |
1429 | if (!empty($greetingTokens) && array_key_exists('contact', $greetingTokens)) { | |
1430 | $greetingsReturnProperties = array_flip(CRM_Utils_Array::value('contact', $greetingTokens)); | |
1431 | $greetingsReturnProperties = array_fill_keys(array_keys($greetingsReturnProperties), 1); | |
1432 | $contactParams = array('contact_id' => $contactId); | |
1433 | ||
1434 | $greetingDetails = self::getTokenDetails($contactParams, | |
1435 | $greetingsReturnProperties, | |
1436 | FALSE, FALSE, NULL, | |
1437 | $greetingTokens, | |
1438 | $className | |
1439 | ); | |
1440 | ||
1441 | // again replace tokens | |
1442 | $tokenString = CRM_Utils_Token::replaceContactTokens($tokenString, | |
1443 | $greetingDetails, | |
1444 | TRUE, | |
73d64eb6 OB |
1445 | $greetingTokens, |
1446 | FALSE, | |
1447 | $escapeSmarty | |
6a488035 TO |
1448 | ); |
1449 | } | |
d75f2f47 | 1450 | |
ac21a108 | 1451 | // check if there are still any unevaluated tokens |
562cf4d7 | 1452 | $remainingTokens = self::getTokens($tokenString); |
ac21a108 | 1453 | |
d75f2f47 | 1454 | // contact related $greetingTokens not empty, there are customized or hook tokens to replace |
481a74f4 | 1455 | if (!empty($remainingTokens['contact'])) { |
ac21a108 | 1456 | // Fill the return properties array |
562cf4d7 | 1457 | $greetingTokens = $remainingTokens['contact']; |
ac21a108 JM |
1458 | reset($greetingTokens); |
1459 | $greetingsReturnProperties = array(); | |
22e263ad | 1460 | while (list($key) = each($greetingTokens)) { |
ac21a108 JM |
1461 | $props = array_flip(CRM_Utils_Array::value($key, $greetingTokens)); |
1462 | $props = array_fill_keys(array_keys($props), 1); | |
1463 | $greetingsReturnProperties = $greetingsReturnProperties + $props; | |
1464 | } | |
1465 | $contactParams = array('contact_id' => $contactId); | |
1466 | $greetingDetails = self::getTokenDetails($contactParams, | |
1467 | $greetingsReturnProperties, | |
1468 | FALSE, FALSE, NULL, | |
1469 | $greetingTokens, | |
1470 | $className | |
1471 | ); | |
1472 | // Prepare variables for calling replaceHookTokens | |
1473 | $categories = array_keys($greetingTokens); | |
1474 | list($contact) = $greetingDetails; | |
1475 | // Replace tokens defined in Hooks. | |
1476 | $tokenString = CRM_Utils_Token::replaceHookTokens($tokenString, $contact[$contactId], $categories); | |
1477 | } | |
6a488035 TO |
1478 | } |
1479 | } | |
1480 | ||
5bc392e6 EM |
1481 | /** |
1482 | * @param $tokens | |
1483 | * | |
1484 | * @return array | |
1485 | */ | |
00be9182 | 1486 | public static function flattenTokens(&$tokens) { |
6a488035 TO |
1487 | $flattenTokens = array(); |
1488 | ||
1489 | foreach (array( | |
353ffa53 TO |
1490 | 'html', |
1491 | 'text', | |
e7483cbe | 1492 | 'subject', |
353ffa53 | 1493 | ) as $prop) { |
6a488035 TO |
1494 | if (!isset($tokens[$prop])) { |
1495 | continue; | |
1496 | } | |
1497 | foreach ($tokens[$prop] as $type => $names) { | |
1498 | if (!isset($flattenTokens[$type])) { | |
1499 | $flattenTokens[$type] = array(); | |
1500 | } | |
1501 | foreach ($names as $name) { | |
1502 | $flattenTokens[$type][$name] = 1; | |
1503 | } | |
1504 | } | |
1505 | } | |
1506 | ||
1507 | return $flattenTokens; | |
1508 | } | |
1509 | ||
1510 | /** | |
1511 | * Replace all user tokens in $str | |
1512 | * | |
77855840 TO |
1513 | * @param string $str |
1514 | * The string with tokens to be replaced. | |
e39893f5 EM |
1515 | * |
1516 | * @param null $knownTokens | |
1517 | * @param bool $escapeSmarty | |
6a488035 | 1518 | * |
a6c01b45 CW |
1519 | * @return string |
1520 | * The processed string | |
6a488035 TO |
1521 | */ |
1522 | public static function &replaceUserTokens($str, $knownTokens = NULL, $escapeSmarty = FALSE) { | |
1523 | $key = 'user'; | |
1524 | if (!$knownTokens || | |
1525 | !isset($knownTokens[$key]) | |
1526 | ) { | |
1527 | return $str; | |
1528 | } | |
1529 | ||
8bab0eb0 DL |
1530 | $str = preg_replace_callback( |
1531 | self::tokenRegex($key), | |
353ffa53 TO |
1532 | function ($matches) use ($escapeSmarty) { |
1533 | return CRM_Utils_Token::getUserTokenReplacement($matches[1], $escapeSmarty); | |
8bab0eb0 DL |
1534 | }, |
1535 | $str | |
6a488035 TO |
1536 | ); |
1537 | return $str; | |
1538 | } | |
1539 | ||
e39893f5 EM |
1540 | /** |
1541 | * @param $token | |
1542 | * @param bool $escapeSmarty | |
1543 | * | |
1544 | * @return string | |
1545 | */ | |
6a488035 TO |
1546 | public static function getUserTokenReplacement($token, $escapeSmarty = FALSE) { |
1547 | $value = ''; | |
1548 | ||
1549 | list($objectName, $objectValue) = explode('-', $token, 2); | |
1550 | ||
1551 | switch ($objectName) { | |
1552 | case 'permission': | |
1553 | $value = CRM_Core_Permission::permissionEmails($objectValue); | |
1554 | break; | |
1555 | ||
1556 | case 'role': | |
1557 | $value = CRM_Core_Permission::roleEmails($objectValue); | |
1558 | break; | |
1559 | } | |
1560 | ||
1561 | if ($escapeSmarty) { | |
1562 | $value = self::tokenEscapeSmarty($value); | |
1563 | } | |
1564 | ||
1565 | return $value; | |
1566 | } | |
1567 | ||
e39893f5 | 1568 | /** |
e39893f5 | 1569 | */ |
6a488035 TO |
1570 | protected static function _buildContributionTokens() { |
1571 | $key = 'contribution'; | |
1572 | if (self::$_tokens[$key] == NULL) { | |
1573 | self::$_tokens[$key] = array_keys(array_merge(CRM_Contribute_BAO_Contribution::exportableFields('All'), | |
353ffa53 TO |
1574 | array('campaign', 'financial_type') |
1575 | )); | |
6a488035 TO |
1576 | } |
1577 | } | |
1578 | ||
2d3e3c7b | 1579 | /** |
fe482240 | 1580 | * Store membership tokens on the static _tokens array. |
2d3e3c7b | 1581 | */ |
1582 | protected static function _buildMembershipTokens() { | |
1583 | $key = 'membership'; | |
21d6154c | 1584 | if (!isset(self::$_tokens[$key]) || self::$_tokens[$key] == NULL) { |
2d3e3c7b | 1585 | $membershipTokens = array(); |
1586 | $tokens = CRM_Core_SelectValues::membershipTokens(); | |
1587 | foreach ($tokens as $token => $dontCare) { | |
1588 | $membershipTokens[] = substr($token, (strpos($token, '.') + 1), -1); | |
1589 | } | |
1590 | self::$_tokens[$key] = $membershipTokens; | |
1591 | } | |
1592 | } | |
1593 | ||
1594 | /** | |
fe482240 | 1595 | * Replace tokens for an entity. |
2d3e3c7b | 1596 | * @param string $entity |
77855840 TO |
1597 | * @param array $entityArray |
1598 | * (e.g. in format from api). | |
1599 | * @param string $str | |
1600 | * String to replace in. | |
1601 | * @param array $knownTokens | |
1602 | * Array of tokens present. | |
1603 | * @param bool $escapeSmarty | |
a6c01b45 CW |
1604 | * @return string |
1605 | * string with replacements made | |
2d3e3c7b | 1606 | */ |
1607 | public static function replaceEntityTokens($entity, $entityArray, $str, $knownTokens = array(), $escapeSmarty = FALSE) { | |
8cc574cf | 1608 | if (!$knownTokens || empty($knownTokens[$entity])) { |
2d3e3c7b | 1609 | return $str; |
1610 | } | |
1611 | ||
e7292422 | 1612 | $fn = 'get' . ucfirst($entity) . 'tokenReplacement'; |
2d3e3c7b | 1613 | //since we already know the tokens lets just use them & do str_replace which is faster & simpler than preg_replace |
1614 | foreach ($knownTokens[$entity] as $token) { | |
1615 | $replaceMent = CRM_Utils_Token::$fn($token, $entityArray, $escapeSmarty); | |
1616 | $str = str_replace('{' . $entity . '.' . $token . '}', $replaceMent, $str); | |
1617 | } | |
1618 | $str = preg_replace('/\\\\|\{(\s*)?\}/', ' ', $str); | |
1619 | return $str; | |
1620 | } | |
1621 | ||
383c047b | 1622 | /** |
fe482240 | 1623 | * Replace Contribution tokens in html. |
e39893f5 | 1624 | * |
115dba92 EM |
1625 | * @param string $str |
1626 | * @param array $contribution | |
e39893f5 | 1627 | * @param bool|string $html |
383c047b | 1628 | * @param string $knownTokens |
e39893f5 EM |
1629 | * @param bool|string $escapeSmarty |
1630 | * | |
72b3a70c | 1631 | * @return mixed |
383c047b DG |
1632 | */ |
1633 | public static function replaceContributionTokens($str, &$contribution, $html = FALSE, $knownTokens = NULL, $escapeSmarty = FALSE) { | |
1634 | $key = 'contribution'; | |
1635 | if (!$knownTokens || !CRM_Utils_Array::value($key, $knownTokens)) { | |
1636 | return $str; //early return | |
1637 | } | |
6a488035 TO |
1638 | self::_buildContributionTokens(); |
1639 | ||
1640 | // here we intersect with the list of pre-configured valid tokens | |
1641 | // so that we remove anything we do not recognize | |
1642 | // I hope to move this step out of here soon and | |
1643 | // then we will just iterate on a list of tokens that are passed to us | |
6a488035 | 1644 | |
8bab0eb0 DL |
1645 | $str = preg_replace_callback( |
1646 | self::tokenRegex($key), | |
353ffa53 TO |
1647 | function ($matches) use (&$contribution, $html, $escapeSmarty) { |
1648 | return CRM_Utils_Token::getContributionTokenReplacement($matches[1], $contribution, $html, $escapeSmarty); | |
8bab0eb0 | 1649 | }, |
6a488035 TO |
1650 | $str |
1651 | ); | |
1652 | ||
1653 | $str = preg_replace('/\\\\|\{(\s*)?\}/', ' ', $str); | |
1654 | return $str; | |
1655 | } | |
1656 | ||
383c047b DG |
1657 | /** |
1658 | * We have a situation where we are rendering more than one token in each field because we are combining | |
1659 | * tokens from more than one contribution when pdf thank you letters are grouped (CRM-14367) | |
1660 | * | |
1661 | * The replaceContributionToken doesn't handle receive_date correctly in this scenario because of the formatting | |
1662 | * it applies (other tokens are OK including date fields) | |
1663 | * | |
1664 | * So we sort this out & then call the main function. Note that we are not escaping smarty on this fields like the main function | |
1665 | * does - but the fields is already being formatted through a date function | |
1666 | * | |
1667 | * @param string $separator | |
1668 | * @param string $str | |
1669 | * @param array $contribution | |
e39893f5 | 1670 | * @param bool|string $html |
383c047b | 1671 | * @param string $knownTokens |
e39893f5 EM |
1672 | * @param bool|string $escapeSmarty |
1673 | * | |
1674 | * @return \Ambigous|mixed|string|\unknown | |
383c047b DG |
1675 | */ |
1676 | public static function replaceMultipleContributionTokens($separator, $str, &$contribution, $html = FALSE, $knownTokens = NULL, $escapeSmarty = FALSE) { | |
22e263ad | 1677 | if (empty($knownTokens['contribution'])) { |
383c047b DG |
1678 | return $str; |
1679 | } | |
1680 | ||
22e263ad | 1681 | if (in_array('receive_date', $knownTokens['contribution'])) { |
383c047b DG |
1682 | $formattedDates = array(); |
1683 | $dates = explode($separator, $contribution['receive_date']); | |
1684 | foreach ($dates as $date) { | |
1685 | $formattedDates[] = CRM_Utils_Date::customFormat($date, NULL, array('j', 'm', 'Y')); | |
1686 | } | |
1687 | $str = str_replace("{contribution.receive_date}", implode($separator, $formattedDates), $str); | |
1688 | unset($knownTokens['contribution']['receive_date']); | |
1689 | } | |
1690 | return self::replaceContributionTokens($str, $contribution, $html, $knownTokens, $escapeSmarty); | |
1691 | } | |
1692 | ||
2d3e3c7b | 1693 | /** |
1694 | * Get replacement strings for any membership tokens (only a small number of tokens are implemnted in the first instance | |
1695 | * - this is used by the pdfLetter task from membership search | |
1696 | * @param string $token | |
77855840 TO |
1697 | * @param array $membership |
1698 | * An api result array for a single membership. | |
1699 | * @param bool $escapeSmarty | |
a6c01b45 CW |
1700 | * @return string |
1701 | * token replacement | |
2d3e3c7b | 1702 | */ |
1703 | public static function getMembershipTokenReplacement($token, $membership, $escapeSmarty = FALSE) { | |
1704 | $entity = 'membership'; | |
1705 | self::_buildMembershipTokens(); | |
e7292422 TO |
1706 | switch ($token) { |
1707 | case 'type': | |
1708 | $value = $membership['membership_name']; | |
1709 | break; | |
1710 | ||
1711 | case 'status': | |
1712 | $statuses = CRM_Member_BAO_Membership::buildOptions('status_id'); | |
1713 | $value = $statuses[$membership['status_id']]; | |
1714 | break; | |
1715 | ||
1716 | case 'fee': | |
92e4c2a5 | 1717 | try { |
353ffa53 TO |
1718 | $value = civicrm_api3('membership_type', 'getvalue', array( |
1719 | 'id' => $membership['membership_type_id'], | |
e7483cbe | 1720 | 'return' => 'minimum_fee', |
353ffa53 | 1721 | )); |
e7292422 TO |
1722 | } |
1723 | catch (CiviCRM_API3_Exception $e) { | |
1724 | // we can anticipate we will get an error if the minimum fee is set to 'NULL' because of the way the | |
1725 | // api handles NULL (4.4) | |
1726 | $value = 0; | |
1727 | } | |
1728 | break; | |
1729 | ||
1730 | default: | |
1731 | if (in_array($token, self::$_tokens[$entity])) { | |
1732 | $value = $membership[$token]; | |
1733 | } | |
1734 | else { | |
1735 | //ie unchanged | |
1736 | $value = "{$entity}.{$token}"; | |
1737 | } | |
1738 | break; | |
2d3e3c7b | 1739 | } |
1740 | ||
1741 | if ($escapeSmarty) { | |
1742 | $value = self::tokenEscapeSmarty($value); | |
1743 | } | |
1744 | return $value; | |
1745 | } | |
1746 | ||
e39893f5 EM |
1747 | /** |
1748 | * @param $token | |
1749 | * @param $contribution | |
1750 | * @param bool $html | |
1751 | * @param bool $escapeSmarty | |
1752 | * | |
1753 | * @return mixed|string | |
1754 | */ | |
6a488035 TO |
1755 | public static function getContributionTokenReplacement($token, &$contribution, $html = FALSE, $escapeSmarty = FALSE) { |
1756 | self::_buildContributionTokens(); | |
1757 | ||
1758 | switch ($token) { | |
1759 | case 'total_amount': | |
1760 | case 'net_amount': | |
1761 | case 'fee_amount': | |
1762 | case 'non_deductible_amount': | |
1763 | $value = CRM_Utils_Money::format(CRM_Utils_Array::retrieveValueRecursive($contribution, $token)); | |
1764 | break; | |
1765 | ||
1766 | case 'receive_date': | |
1767 | $value = CRM_Utils_Array::retrieveValueRecursive($contribution, $token); | |
1768 | $value = CRM_Utils_Date::customFormat($value, NULL, array('j', 'm', 'Y')); | |
1769 | break; | |
1770 | ||
1771 | default: | |
1772 | if (!in_array($token, self::$_tokens['contribution'])) { | |
1773 | $value = "{contribution.$token}"; | |
1774 | } | |
1775 | else { | |
1776 | $value = CRM_Utils_Array::retrieveValueRecursive($contribution, $token); | |
1777 | } | |
1778 | break; | |
1779 | } | |
1780 | ||
6a488035 TO |
1781 | if ($escapeSmarty) { |
1782 | $value = self::tokenEscapeSmarty($value); | |
1783 | } | |
1784 | return $value; | |
1785 | } | |
1786 | ||
091133bb | 1787 | /** |
a6c01b45 CW |
1788 | * @return array |
1789 | * legacy_token => new_token | |
091133bb | 1790 | */ |
00be9182 | 1791 | public static function legacyContactTokens() { |
091133bb CW |
1792 | return array( |
1793 | 'individual_prefix' => 'prefix_id', | |
1794 | 'individual_suffix' => 'suffix_id', | |
1795 | 'gender' => 'gender_id', | |
aa62b355 | 1796 | 'communication_style' => 'communication_style_id', |
091133bb CW |
1797 | ); |
1798 | } | |
1799 | ||
ac0a3db5 CW |
1800 | /** |
1801 | * Formats a token list for the select2 widget | |
1802 | * @param $tokens | |
1803 | * @return array | |
1804 | */ | |
00be9182 | 1805 | public static function formatTokensForDisplay($tokens) { |
ac0a3db5 CW |
1806 | $sorted = $output = array(); |
1807 | ||
1808 | // Sort in ascending order by ignoring word case | |
1809 | natcasesort($tokens); | |
1810 | ||
1811 | // Attempt to place tokens into optgroups | |
1812 | // TODO: These groupings could be better and less hackish. Getting them pre-grouped from upstream would be nice. | |
1813 | foreach ($tokens as $k => $v) { | |
1814 | // Check to see if this token is already in a group e.g. for custom fields | |
1815 | $split = explode(' :: ', $v); | |
1816 | if (!empty($split[1])) { | |
1817 | $sorted[$split[1]][] = array('id' => $k, 'text' => $split[0]); | |
1818 | } | |
1819 | // Group by entity | |
1820 | else { | |
1821 | $split = explode('.', trim($k, '{}')); | |
cc666210 CW |
1822 | if (isset($split[1])) { |
1823 | $entity = array_key_exists($split[1], CRM_Core_DAO_Address::export()) ? 'Address' : ucfirst($split[0]); | |
1824 | } | |
1825 | else { | |
1826 | $entity = 'Contact'; | |
1827 | } | |
ac0a3db5 CW |
1828 | $sorted[ts($entity)][] = array('id' => $k, 'text' => $v); |
1829 | } | |
1830 | } | |
1831 | ||
1832 | ksort($sorted); | |
1833 | foreach ($sorted as $k => $v) { | |
1834 | $output[] = array('text' => $k, 'children' => $v); | |
1835 | } | |
1836 | ||
1837 | return $output; | |
1838 | } | |
96025800 | 1839 | |
6a488035 | 1840 | } |