sf_contrib.module in Salesforce Suite 6.2
Provides integration with Location and CCK.
File
sf_contrib/sf_contrib.moduleView source
<?php
/**
* @file
* Provides integration with Location and CCK.
*/
/**
* Implementation of hook_fieldmap_objects().
**/
function sf_contrib_fieldmap_objects_alter(&$objects) {
_sf_contrib_add_location($objects);
_sf_contrib_add_fields($objects);
}
function _sf_contrib_add_location(&$objects) {
// Define the data fields available for Drupal objects.
$extra_fields = array();
// Use first, last & organization fields if module installed.
if (module_exists('location_first_last_org')) {
$extra_fields = array_merge($extra_fields, array(
'first_name' => array(
'label' => t('First Name'),
'group' => t('Location fields'),
'export' => '_sf_contrib_export_location',
'import' => '_sf_contrib_import_location',
),
'last_name' => array(
'label' => t('Last Name'),
'group' => t('Location fields'),
'export' => '_sf_contrib_export_location',
'import' => '_sf_contrib_import_location',
),
'organization' => array(
'label' => t('Organization'),
'group' => t('Location fields'),
'export' => '_sf_contrib_export_location',
'import' => '_sf_contrib_import_location',
),
));
}
$extra_fields = array_merge($extra_fields, array(
'street' => array(
'label' => t('Street'),
'group' => t('Location fields'),
'export' => '_sf_contrib_export_location',
'import' => '_sf_contrib_import_location',
),
'additional' => array(
'label' => t('Additional'),
'group' => t('Location fields'),
'export' => '_sf_contrib_export_location',
'import' => '_sf_contrib_import_location',
),
'city' => array(
'label' => t('City'),
'group' => t('Location fields'),
'export' => '_sf_contrib_export_location',
'import' => '_sf_contrib_import_location',
),
'province_name' => array(
'label' => t('Province'),
'group' => t('Location fields'),
'export' => '_sf_contrib_export_location',
'import' => '_sf_contrib_import_location',
),
'postal_code' => array(
'label' => t('Postal code'),
'group' => t('Location fields'),
'export' => '_sf_contrib_export_location',
'import' => '_sf_contrib_import_location',
),
'country_name' => array(
'label' => t('Country'),
'group' => t('Location fields'),
'export' => '_sf_contrib_export_location',
'import' => '_sf_contrib_import_location',
),
'phone' => array(
'label' => t('Phone'),
'group' => t('Location fields'),
'export' => '_sf_contrib_export_location',
'import' => '_sf_contrib_import_location',
),
));
$node_types = node_get_types();
foreach ($extra_fields as $obj_key => $obj_def) {
$objects['drupal']['user']['fields'][$obj_key] = $obj_def;
foreach ($node_types as $type) {
$objects['drupal']['node_' . $type->type]['fields'][$obj_key] = $obj_def;
}
}
}
function _sf_contrib_add_fields(&$objects) {
if (!module_exists('content')) {
return;
}
// Loop through each of the content types.
foreach (content_types() as $type) {
// Add each of the fields to the node object definition.
$group = t('@type CCK fields', array(
'@type' => $type['name'],
));
$index = 'node_' . $type['type'];
foreach ((array) $type['fields'] as $field) {
switch ($field['type']) {
case 'location':
unset($objects['drupal'][$index]['fields'][$field['field_name']]);
$proceed = FALSE;
if (!module_exists('location')) {
break;
}
static $location_field_definitions;
if (empty($location_field_definitions)) {
$dummy = NULL;
$location_field_definitions = location_locationapi($dummy, 'fields');
}
// Location is special: add each of its fields individually
// and use a special import/export handler
if (empty($field['location_settings']['form']['fields']) || !is_array($field['location_settings']['form']['fields'])) {
break;
}
foreach ($field['location_settings']['form']['fields'] as $key => $loc_setting) {
if ($key == 'additional') {
// Salesforce street address is a textarea.
// Append "additional" to "address1" instead of using a separate field
continue;
}
$objects['drupal'][$index]['fields'][$field['field_name'] . ':' . $key] = array(
'group' => t('@group: @label', array(
'@group' => $group,
'@label' => $field['widget']['label'],
)),
'label' => isset($location_field_definitions[$key]) ? check_plain($location_field_definitions[$key]) : $key,
'export' => '_sf_node_export_cck_location',
'import' => '_sf_node_import_cck_location',
);
}
break;
case 'datetime':
case 'datestamp':
case 'date':
$objects['drupal'][$index]['fields'][$field['field_name'] . ':value2']['label'] = $objects['drupal'][$index]['fields'][$field['field_name']]['label'] . ' ' . t('(to date)');
$objects['drupal'][$index]['fields'][$field['field_name']]['export'] = $objects['drupal'][$index]['fields'][$field['field_name'] . ':value2']['export'] = '_sf_node_export_cck_date';
$objects['drupal'][$index]['fields'][$field['field_name']]['import'] = $objects['drupal'][$index]['fields'][$field['field_name'] . ':value2']['import'] = '_sf_node_import_cck_date';
break;
}
}
}
}
// Export a timestamp in a format SalesForce comprehends.
function _sf_node_export_cck_date($node, $drupal_fieldname, $drupal_field_definition, $sf_field_definition) {
list($fieldname, $column) = explode(':', $drupal_fieldname, 2);
if (empty($column)) {
$column = 'value';
}
switch ($node->{$fieldname}[0]['date_type']) {
case 'datestamp':
$from_type = DATE_UNIX;
break;
case 'datetime':
$from_type = DATE_DATETIME;
break;
case 'date':
$from_type = DATE_ISO;
break;
}
$date = date_convert($node->{$fieldname}[0][$column], $from_type, DATE_UNIX);
return gmdate(DATE_ATOM, $date);
}
// Given a SalesForce time, import a node timestamp.
function _sf_node_import_cck_date(&$node, $drupal_fieldname, $drupal_field_definition, $sf_data, $sf_fieldname, $sf_field_definition) {
if (empty($sf_data->{$sf_fieldname})) {
return;
}
list($fieldname, $column) = explode(':', $drupal_fieldname, 2);
if (empty($column)) {
$column = 'value';
}
switch ($node->{$fieldname}[0]['date_type']) {
case 'datestamp':
$to_type = DATE_UNIX;
break;
case 'datetime':
$to_type = DATE_DATETIME;
break;
case 'date':
$to_type = DATE_ISO;
break;
}
$date = date_convert($sf_data->{$sf_fieldname}, DATE_ISO, $to_type);
$node->{$fieldname}[0][$column] = $date;
}
// Export sfid of referenced node
function _sf_node_export_cck_nodereference($node, $fieldname, $drupal_field_definition, $sf_field_definition) {
list($fieldname, $dummy) = explode(':', $fieldname, 2);
$sfid = '';
if ($nid = $node->{$fieldname}[0]['nid']) {
$sf_data = salesforce_api_id_load('node', $nid);
$sfid = isset($sf_data->sfid) ? $sf_data->sfid : '';
}
return $sfid;
}
// Import node id of referenced node
function _sf_node_import_cck_nodereference(&$node, $drupal_fieldname, $drupal_field_definition, $sf_data, $sf_fieldname, $sf_field_definition) {
if (empty($sf_data->{$sf_fieldname})) {
return;
}
list($drupal_fieldname, $dummy) = explode(':', $drupal_fieldname, 2);
$node->{$drupal_fieldname}[0]['nid'] = salesforce_api_get_id_with_sfid($sf_data->{$sf_fieldname});
}
// Export sfid of referenced user
function _sf_node_export_cck_userreference($node, $fieldname, $drupal_field_definition, $sf_field_definition) {
list($fieldname, $dummy) = explode(':', $fieldname, 2);
$sfid = '';
if ($uid = $node->{$fieldname}[0]['uid']) {
$sf_data = salesforce_api_id_load('user', $uid);
$sfid = isset($sf_data->sfid) ? $sf_data->sfid : '';
}
return $sfid;
}
// Import node id of referenced user
function _sf_node_import_cck_userreference(&$node, $drupal_fieldname, $drupal_field_definition, $sf_data, $sf_fieldname, $sf_field_definition) {
if (empty($sf_data->{$sf_fieldname})) {
return;
}
list($drupal_fieldname, $dummy) = explode(':', $drupal_fieldname, 2);
$node->{$drupal_fieldname}[0]['uid'] = salesforce_api_get_id_with_sfid($sf_data->{$sf_fieldname}, $type = 'user');
}
// Export an element of a CCK location field
function _sf_node_export_cck_location($source, $field, $drupal_field_definition, $sf_field_definition) {
static $locations;
list($field, $type) = explode(':', $field, 2);
$data = $source->{$field};
$lid = $data[0]['lid'];
if (empty($locations[$lid])) {
$locations[$lid] = location_load_location($lid);
}
if ($type == 'street' && !empty($locations[$lid]['street']) && !empty($locations[$lid]['additional'])) {
$locations[$lid]['street'] .= "\n" . $locations[$lid]['additional'];
}
return $locations[$lid][$type];
}
// Import an element of a CCK location field
function _sf_node_import_cck_location(&$node, $drupal_fieldname, $drupal_field_definition, $sf_data, $sf_fieldname, $sf_field_definition) {
if (empty($sf_data->{$sf_fieldname})) {
return;
}
static $locations;
list($local_field, $type) = explode(':', $drupal_fieldname, 2);
$data = $sf_data->{$sf_fieldname};
if (!isset($node->{$local_field})) {
$node->{$local_field} = array();
}
if (!isset($node->{$local_field}[0])) {
$node->{$local_field}[0] = array();
}
$node->{$local_field}[0][$type] = $data;
}
// Wrapper for user objects to use _sf_contrib_export_location
function _sf_user_export_location($source, $fieldname, $drupal_field_definition, $sf_field_definition) {
return _sf_contrib_export_location($source, $fieldname, $drupal_field_definition, $sf_field_definition, 'uid');
}
// Wrapper for user objects to use _sf_contrib_import_location
function _sf_user_import_location(&$user, $drupal_fieldname, $drupal_field_definition, $sf_data, $sf_fieldname, $sf_field_definition) {
if (empty($sf_data->{$sf_fieldname})) {
return;
}
_sf_contrib_import_location($user, $drupal_fieldname, $drupal_field_definition, $sf_data, $sf_fieldname, $sf_field_definition);
}
// Wrapper for node objects to use _sf_contrib_export_location
function _sf_node_export_location($source, $fieldname, $drupal_field_definition, $sf_field_definition) {
return _sf_contrib_export_location($source, $fieldname, $drupal_field_definition, $sf_field_definition, 'nid');
}
// Wrapper for node objects to use _sf_contrib_import_location
function _sf_node_import_location(&$node, $drupal_fieldname, $drupal_field_definition, $sf_data, $sf_fieldname, $sf_field_definition) {
_sf_contrib_import_location($node, $drupal_fieldname, $drupal_field_definition, $sf_data, $sf_fieldname, $sf_field_definition);
}
// Export a location element
function _sf_contrib_export_location($source, $fieldname, $drupal_field_definition, $sf_field_definition, $key = 'nid') {
// If locations doesn't exist, fail fast.
if (empty($source->locations)) {
return FALSE;
}
foreach ($source->locations as $location) {
if ($location['is_primary'] == 1) {
return $location[$fieldname];
}
}
// Primary not set, so make first location primary.
$source->locations[0]['is_primary'] = 1;
$criteria = array(
$key => $source->{$key},
);
location_save_locations($source->locations, $criteria);
return $source->locations[0][$fieldname];
}
// Import a location element
function _sf_contrib_import_location(&$object, $drupal_fieldname, $drupal_field_definition, $sf_data, $sf_fieldname, $sf_field_definition) {
// Find primary location, set field and return.
if (empty($sf_data->{$sf_fieldname})) {
return;
}
if (empty($object->locations)) {
$object->locations = array();
}
foreach ($object->locations as $location) {
if ($location['is_primary'] == 1) {
$location[$drupal_fieldname] = $sf_data->{$sf_fieldname};
return;
}
}
// No primary location so use the first one.
$object->locations[0][$drupal_fieldname] = $sf_data->{$sf_fieldname};
}
function _sf_node_import_cck_content_taxonomy(&$object, $drupal_fieldname, $drupal_field_definition, $sf_data, $sf_fieldname, $sf_field_definition) {
if (empty($sf_data->{$sf_fieldname})) {
return;
}
$info = _content_type_info();
$info = $info['content types'][$object->type]['fields'][$drupal_fieldname];
if (strpos($sf_data->{$sf_fieldname}, ';') === FALSE) {
$tid = db_result(db_query('SELECT tid FROM {term_data} WHERE name = "%s" AND vid = %d', $sf_data->{$sf_fieldname}, $info['vid']));
$object->{$drupal_fieldname} = array(
array(
'value' => $tid,
),
);
}
else {
$terms = str_replace(array(
'"',
), array(
'\\"',
), $sf_data->{$sf_fieldname});
$terms = explode(';', $terms);
$ph = db_placeholders($terms, 'text');
$sql = 'SELECT tid FROM {term_data}
WHERE vid = ' . $info['vid'] . ' AND name in ("' . implode('", "', $terms) . '")';
$result = db_query($sql);
$object->{$drupal_fieldname} = array();
while ($row = db_fetch_array($result)) {
$object->{$drupal_fieldname}[] = array(
'value' => $row['tid'],
);
}
}
}
function _sf_node_export_cck_content_taxonomy($source, $fieldname, $drupal_field_definition, $sf_field_definition) {
if (empty($source->{$fieldname})) {
return;
}
$data = $source->{$fieldname};
$terms = NULL;
if (!empty($data) && is_array($data)) {
foreach ($data as $value) {
$tid = $value['value'];
$term = taxonomy_get_term($tid);
if (!empty($term->name)) {
if (!empty($terms)) {
$terms .= ';';
}
$terms .= $term->name;
}
}
}
return $terms;
}