/** * Replace all the org-level tokens in $str * * @param string $str * The string with tokens to be replaced. * @param object $org * Associative array of org properties. * @param bool $html * Replace tokens with HTML or plain text. * * @param bool $escapeSmarty * * @return string * The processed string */ public static function &replaceOrgTokens($str, &$org, $html = FALSE, $escapeSmarty = FALSE) { self::$_tokens['org'] = array_merge(array_keys(CRM_Contact_BAO_Contact::importableFields('Organization')), array('address', 'display_name', 'checksum', 'contact_id')); $cv = NULL; foreach (self::$_tokens['org'] as $token) { // print "Getting token value for $token<br/><br/>"; if ($token == '') { continue; } // If the string doesn't contain this token, skip it. if (!self::token_match('org', $token, $str)) { continue; } // Construct value from $token and $contact $value = NULL; if ($cfID = CRM_Core_BAO_CustomField::getKeyID($token)) { // only generate cv if we need it if ($cv === NULL) { $cv = CRM_Core_BAO_CustomValue::getContactValues($org['contact_id']); } foreach ($cv as $cvFieldID => $value) { if ($cvFieldID == $cfID) { $value = CRM_Core_BAO_CustomOption::getOptionLabel($cfID, $value); break; } } } elseif ($token == 'checksum') { $cs = CRM_Contact_BAO_Contact_Utils::generateChecksum($org['contact_id']); $value = "cs={$cs}"; } elseif ($token == 'address') { // Build the location values array $loc = array(); $loc['display_name'] = CRM_Utils_Array::retrieveValueRecursive($org, 'display_name'); $loc['street_address'] = CRM_Utils_Array::retrieveValueRecursive($org, 'street_address'); $loc['city'] = CRM_Utils_Array::retrieveValueRecursive($org, 'city'); $loc['state_province'] = CRM_Utils_Array::retrieveValueRecursive($org, 'state_province'); $loc['postal_code'] = CRM_Utils_Array::retrieveValueRecursive($org, 'postal_code'); // Construct the address token $value = CRM_Utils_Address::format($loc); if ($html) { $value = str_replace("\n", '<br />', $value); } } else { $value = CRM_Utils_Array::retrieveValueRecursive($org, $token); } self::token_replace('org', $token, $value, $str, $escapeSmarty); } return $str; }
/** * Get an existing contact. * * Returns a single existing Contact object which matches ALL property * values passed in $params. An error object is returned if there is * no match, or more than one match. This API can be used to retrieve * the CRM internal identifier (contact_id) based on a unique property * (e.g. email address). It can also be used to retrieve any desired * contact properties based on a known contact_id. Available * properties for each type of Contact are listed in the {@link * http://objectledge.org/confluence/display/CRM/Data+Model#DataModel-ContactRef * CRM Data Model.} Modules may also invoke crm_get_class_properties() * to retrieve all available property names, including extended * (i.e. custom) properties.contact of the specific type that matches * the input params * * <b>Primary Location and Communication Info</b> * * <ul> * <li>Primary location properties (email address, phone, postal address, * etc.) are available in the Contact data objects. Primary email and * phone number are returned by default. Postal address fields and * primary instant messenger identifier are returned when specified in * $return_properties. For contacts with multiple locations, use * crm_get_locations() to retrieve additional location data.</li> * </ul> * * @see crm_get_class_properties() * @see crm_get_locations() * * @example api/Contact.php * * @param array $params Associative array of property name/value * pairs to attempt to match on. * @param array $returnProperties Which properties should be included in the * returned Contact object. If NULL, the default * set of properties will be included. * * @return CRM_Contact|CRM_Core_Error Return the Contact Object if found, else * Error Object * * @access public * */ function &crm_get_contact($params, $returnProperties = null) { _crm_initialize(); // empty parameters ? if (empty($params)) { return _crm_error('$params is empty'); } // correct parameter format ? if (!is_array($params)) { return _crm_error('$params is not an array'); } if (!CRM_Utils_Array::value('contact_id', $params)) { $returnProperties = array('display_name'); list($contacts, $options) = crm_contact_search($params, $returnProperties); if (count($contacts) != 1) { return _crm_error(count($contacts) . " contacts matching input params."); } $contactIds = array_keys($contacts); $params['contact_id'] = $contactIds[0]; } $params['id'] = $params['contact_id']; $ids = array(); $contact =& CRM_Contact_BAO_Contact::getValues($params, $defaults, $ids); if ($contact == null || is_a($contact, 'CRM_Core_Error') || !$contact->id) { return _crm_error('Did not find contact object for ' . $params['contact_id']); } unset($params['id']); require_once str_replace('_', DIRECTORY_SEPARATOR, "CRM_Contact_BAO_" . $contact->contact_type) . ".php"; $contact->contact_type_object = eval('return CRM_Contact_BAO_' . $contact->contact_type . '::getValues( $params, $defaults, $ids );'); $contact->location =& CRM_Core_BAO_Location::getValues($params, $defaults, $ids, 2); //changed the location no $contact->custom_values =& CRM_Core_BAO_CustomValue::getContactValues($contact->id); return $contact; }
/** * Replace all the org-level tokens in $str * * @param string $str The string with tokens to be replaced * @param object $org Associative array of org properties * @param boolean $html Replace tokens with HTML or plain text * @return string The processed string * @access public * @static */ public static function &replaceOrgTokens($str, &$org, $html = false) { self::$_tokens['org'] = array_merge(array_keys(CRM_Contact_BAO_Contact::importableFields('Organization')), array('address', 'display_name', 'checksum', 'contact_id')); /* print "org tokens: <pre>"; print_r( $_tokens['org'] ); print "</pre>"; */ $cv = null; foreach (self::$_tokens['org'] as $token) { // print "Getting token value for $token<br/><br/>"; if ($token == '') { continue; } /* If the string doesn't contain this token, skip it. */ if (!self::token_match('org', $token, $str)) { continue; } /* Construct value from $token and $contact */ $value = null; if ($cfID = CRM_Core_BAO_CustomField::getKeyID($token)) { // only generate cv if we need it if ($cv === null) { $cv =& CRM_Core_BAO_CustomValue::getContactValues($org['contact_id']); } foreach ($cv as $cvFieldID => $value) { if ($cvFieldID == $cfID) { $value = CRM_Core_BAO_CustomOption::getOptionLabel($cfID, $value); break; } } } else { if ($token == 'checksum') { require_once 'CRM/Contact/BAO/Contact/Utils.php'; $cs = CRM_Contact_BAO_Contact_Utils::generateChecksum($org['contact_id']); $value = "cs={$cs}"; } else { if ($token == 'address') { /* Build the location values array */ $loc = array(); $loc['display_name'] = CRM_Utils_Array::retrieveValueRecursive($org, 'display_name'); $loc['street_address'] = CRM_Utils_Array::retrieveValueRecursive($org, 'street_address'); $loc['city'] = CRM_Utils_Array::retrieveValueRecursive($org, 'city'); $loc['state_province'] = CRM_Utils_Array::retrieveValueRecursive($org, 'state_province'); $loc['postal_code'] = CRM_Utils_Array::retrieveValueRecursive($org, 'postal_code'); /* Construct the address token */ $value = CRM_Utils_Address::format($loc); if ($html) { $value = str_replace("\n", '<br />', $value); } } else { /* print "\$org: <pre>"; print_r( $org ); print "</pre>"; */ $value = CRM_Utils_Array::retrieveValueRecursive($org, $token); /* print "\$value: <pre>"; print_r( $value ); print "</pre>"; */ } } } self::token_replace('org', $token, $value, $str); } return $str; }
/** * 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; }