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