/** * Replace all the contact-level tokens in $str with information from * $contact. * * @param string $str The string with tokens to be replaced * @param array $contact Associative array of contact properties * @param boolean $html Replace tokens with HTML or plain text * @return string The processed string * @access public * @static */ function &replaceContactTokens($str, &$contact, $html = false) { if ($GLOBALS['_CRM_UTILS_TOKEN']['_tokens']['contact'] == null) { /* This should come from UF */ ($GLOBALS['_CRM_UTILS_TOKEN']['_tokens']['contact'] =& array_keys(CRM_Contact_BAO_Contact::importableFields())) + array('display_name'); } $cv =& CRM_Core_BAO_CustomValue::getContactValues($contact['id']); foreach ($GLOBALS['_CRM_UTILS_TOKEN']['_tokens']['contact'] as $token) { if ($token == '') { continue; } /* If the string doesn't contain this token, skip it. */ if (!CRM_Utils_Token::token_match('contact', $token, $str)) { continue; } /* Construct value from $token and $contact */ $value = null; if ($cfID = CRM_Core_BAO_CustomField::getKeyID($token)) { foreach ($cv as $customValue) { if ($customValue->custom_field_id == $cfID) { $value = $customValue->getValue(); break; } } } else { $value = CRM_Contact_BAO_Contact::retrieveValue($contact, $token); } CRM_Utils_Token::token_replace('contact', $token, $value, $str); } return $str; }
/** * Given a parameter array from CRM_Contact_BAO_Contact::retrieve() and a * key to search for, search recursively for that key's value. * * @param array $values The parameter array * @param string $key The key to search for * @return mixed The value of the key, or null. * @access public * @static */ function retrieveValue(&$params, $key) { if (!is_array($params)) { return null; } else { if ($value = CRM_Utils_Array::value($key, $params)) { return $value; } else { foreach ($params as $subParam) { if ($value = CRM_Contact_BAO_Contact::retrieveValue($subParam, $key)) { return $value; } } } } return null; }