function sf_node_import in Salesforce Suite 6.2
Same name and namespace in other branches
- 5.2 sf_node/sf_node.module \sf_node_import()
Imports data from Salesforce into a node.
Parameters
$sf_data: The Salesforce Object OR The Salesforce ID of the object to be imported.
$name: The name of the fieldmap to use to create the export object.
$nid: The nid of the node to update. If left NULL, a new node will be created.
Return value
The nid of the imported node or FALSE on failure.
1 call to sf_node_import()
- sf_node_salesforce_form_submit in sf_node/
sf_node.module
File
- sf_node/
sf_node.module, line 785 - Integrates the core node object and various node related modules with the Salesforce API.
Code
function sf_node_import($sf_data, $name, $nid = NULL, $options = array()) {
// Retrieve the object from Salesforce.
$sf = salesforce_api_connect();
if (!$sf) {
if (user_access('administer salesforce')) {
drupal_set_message(t('Unable to connect to Salesforce using <a href="!url">current credentials</a>.', array(
'!url' => url(SALESFORCE_PATH_ADMIN),
)));
}
return FALSE;
}
if (is_sfid($sf_data)) {
$sf_data = salesforce_api_retrieve(array(
$sf_data,
), $name);
// Check to see if sf_data is an array of objects
if (is_array($sf_data) && count($sf_data) > 0) {
$sf_data = $sf_data[0];
}
}
elseif (is_array($sf_data)) {
$sf_data = (object) $sf_data;
}
if (empty($sf_data)) {
return FALSE;
}
// Load the fieldmap data.
$map = salesforce_api_fieldmap_load($name);
// Load the object definitions.
$drupal_object_definition = salesforce_api_fieldmap_objects_load('drupal', $map->drupal);
$salesforce_object_definition = salesforce_api_fieldmap_objects_load('salesforce', $map->salesforce);
// If the node exists, simply update the existing node.
$node = node_load($nid);
$create = FALSE;
if (empty($nid) || empty($node)) {
// Look for any matching records which we might want to update instead of creating duplicates.
$matches = salesforce_api_search_for_duplicates('import', 'node', $sf_data, $name);
if (!empty($matches)) {
$nid = reset($matches);
if (!empty($nid)) {
$node = node_load($nid);
}
}
if (empty($node)) {
$create = TRUE;
global $language;
$node = (object) array(
'type' => substr($map->drupal, 5),
'language' => $language->language,
'uid' => 1,
);
}
}
$changed_fields = 0;
// Loop through the fields on the fieldmap.
foreach ($map->fields as $sf_fieldname => $drupal_fieldname) {
$previous_value = $node->{$drupal_fieldname};
// If a handler is specified for importing a value from Salesforce....
if (is_array($drupal_fieldname)) {
// There is no logical way to import Salesforce values into Drupal fixed
// or php values.
continue;
}
elseif (isset($drupal_object_definition['fields'][$drupal_fieldname]['import'])) {
$drupal_field_import_handler = $drupal_object_definition['fields'][$drupal_fieldname]['import'];
$drupal_field_definition = $drupal_object_definition['fields'][$drupal_fieldname];
$sf_field_definition = $salesforce_object_definition['fields'][$sf_fieldname];
// Let the handler function set the value for the field on the node.
$drupal_field_import_handler($node, $drupal_fieldname, $drupal_field_definition, $sf_data, $sf_fieldname, $sf_field_definition);
}
elseif (isset($sf_data->{$sf_fieldname})) {
// Otherwise set the field on the export object to the value of the source
// field if it's present on the source object.
$node->{$drupal_fieldname} = $sf_data->{$sf_fieldname};
}
if (isset($previous_value[0]['date_type'])) {
$new_value = $node->{$drupal_fieldname};
$new_value = $new_value[0]['value'];
// This looks wrong but I need to convert an object element which is an array to a string
if (strncmp($previous_value[0]['value'], $new_value, strlen($new_value)) != 0) {
$changed_fields++;
}
}
else {
if ($node->{$drupal_fieldname} != $previous_value) {
$changed_fields++;
}
}
}
// end of loop through fields
$comparison_node = drupal_clone($node);
foreach (module_implements('salesforce_api_pre_import') as $module) {
$function = $module . '_salesforce_api_pre_import';
$continue = $function($node, $name, $sf_data);
if ($continue === FALSE) {
return;
}
}
if ($changed_fields == 0 && $comparison_node == $node) {
// No fields changed, so don't save anything.
// Return the node ID to signal a "successful" save
return $node->nid;
}
$node->sf_node_skip_export = TRUE;
node_save($node);
$sfid = $sf_data->Id;
if ($map->automatic & SALESFORCE_AUTO_SYNC_CREATE && $create || $map->automatic & SALESFORCE_AUTO_SYNC_UPDATE && !$create || !empty($options['extra-linked']) && $options['extra-linked'] == TRUE) {
// Store the Salesforce ID for the node and return TRUE.
salesforce_api_id_save('node', $node->nid, $sfid, $name, 'import');
}
module_invoke_all('salesforce_api_post_import', $node, $name, $sf_data, $create);
unset($node->sf_node_skip_export);
return $node->nid;
}