function sf_entity_salesforce_form in Salesforce Suite 7.2
Same name and namespace in other branches
- 7 sf_entity/sf_entity.module \sf_entity_salesforce_form()
Displays the Salesforce synchronization form.
1 string reference to 'sf_entity_salesforce_form'
- sf_entity_menu in sf_entity/
sf_entity.module - Implements hook_menu(). Creates a %/salesforce menu callback for all fieldable entities.
File
- sf_entity/
sf_entity.module, line 464 - Integrates fieldable entities with the Salesforce API.
Code
function sf_entity_salesforce_form($form, &$form_state, $entity_type, $entity) {
if (!$entity || !$entity_type) {
drupal_not_found();
exit;
}
list($oid, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
// Fail out if the entity doesn't exist!
if (!$oid || !$bundle) {
drupal_not_found();
exit;
}
if (isset($form_state['storage']['confirm'])) {
$form['entity'] = array(
'#type' => 'value',
'#value' => $entity,
);
$form['entity_type'] = array(
'#type' => 'value',
'#value' => $entity_type,
);
$uri = entity_uri($entity_type, $entity);
return confirm_form($form, 'Are you sure you want to unlink this entity from Salesforce?', $uri['path'], 'Unlinking this object will remove the connection between the Drupal object and the Salesforce record. This action will <strong>not</strong> delete the Drupal object or the Salesforce record. This cannot be undone.', 'Unlink', 'Cancel');
}
// Set the entity page title.
drupal_set_title(t('@entity_name: Salesforce Export/Import', array(
'@entity_name' => entity_label($entity_type, $entity),
)));
// Build the $form array
$info = entity_get_info($entity_type);
$form = array();
$form['entity info'] = array(
'#type' => 'value',
'#value' => $info,
);
$form['entity type'] = array(
'#type' => 'value',
'#value' => $entity_type,
);
$form['entity keys'] = array(
'#type' => 'value',
'#value' => array(
$oid,
$vid,
$bundle,
),
);
if ($entity->salesforce->sfid) {
// Retrieve the object from Salesforce.
$sf_data = salesforce_api_retrieve(array(
$entity->salesforce->sfid,
), $entity->salesforce->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];
}
// If $sf_data is empty, we assume the record is deleted. retrieve() does
// not return the ENTITY_IS_DELETED error that upsert() does.
if (!$sf_data && SALESFORCE_DELETED_POLICY_UPSERT == variable_get('salesforce_api_entity_deleted_policy', SALESFORCE_DELETED_POLICY_UPSERT)) {
drupal_set_message(t('Unable to retrieve Salesforce data for record !sfid. Drupal and Salesforce records have been unlinked.', array(
'!sfid' => $entity->salesforce->sfid,
)), 'warning');
// Unlink the object and reload the entity, resetting the cache
salesforce_api_id_unlink(array(
'oid' => $oid,
'name' => $entity->salesforce->name,
));
$entity = entity_load($entity_type, array(
$oid,
), array(), TRUE);
}
elseif (!$sf_data) {
drupal_set_message(t('Unable to retrieve Salesforce data for record !sfid.', array(
'!sfid' => $entity->salesforce->sfid,
)), 'warning');
}
}
$options = salesforce_api_fieldmap_options($entity_type, $bundle);
// Display an export button if the entity hasn't been exported before,
// or doesn't currently have a linked object in Salesforce.
if (isset($entity->salesforce->sfid) && empty($entity->salesforce->sfid) || !isset($entity->salesforce->sfid)) {
$form['export'] = array(
'#type' => 'fieldset',
'#title' => t('Export to Salesforce'),
'#description' => t('This @entity may be exported to Salesforce using any fieldmap listed below.', array(
'@entity' => strtolower($info['label']),
)),
);
if (!empty($options)) {
// Add the export form.
$form['export']['fieldmap'] = array(
'#type' => 'select',
'#title' => t('Export fieldmap'),
'#options' => $options,
);
$form['export']['manual_linking'] = array(
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#title' => t('Manual linking'),
);
$form['export']['manual_linking']['sfid'] = array(
'#type' => 'textfield',
'#title' => t('Salesforce ID'),
'#description' => t('If this node already has a corresponding object
in Salesforce, enter the Salesforce ID to manually link the entities.
Salesforce record will be linked using the fieldmap selected above.
<strong>Please ensure that the Salesforce object type matches that of
fieldmap selected above.</strong>.<br /><br /><em>Create Link</em> will
link two Drupal and Salesforce records, leaving field values unchanged.
<br /><em>Export Node</em> will upsert the Drupal record, inserting or
updating any Salesforce record as necessary.'),
'#size' => 18,
'#maxlength' => 18,
);
$form['export']['manual_linking']['link'] = array(
'#type' => 'submit',
'#value' => t('Create Link'),
);
$form['export']['submit'] = array(
'#type' => 'submit',
'#value' => t('Export'),
);
}
else {
$form['export']['notice'] = array(
'#type' => 'item',
'#title' => t('No @entity fieldmaps', array(
'@entity' => $entity_type . ':' . $bundle,
)),
'#markup' => t('You have not created any @entity fieldmaps. Please <a href="/!url">go create a fieldmap</a> and come back.', array(
'@entity' => strtolower($info['label']),
'!url' => SALESFORCE_PATH_FIELDMAPS,
)),
);
}
}
elseif (isset($entity->salesforce->sfid) && !empty($entity->salesforce->sfid)) {
// Otherwise add synchronization information.
$form['sfid'] = array(
'#type' => 'value',
'#value' => $entity->salesforce->sfid,
);
$form['fieldmap'] = array(
'#type' => 'value',
'#value' => $entity->salesforce->name,
);
// Load the fieldmap data.
$map = salesforce_api_salesforce_fieldmap_load($entity->salesforce->name);
$sf_object_definition = salesforce_api_fieldmap_objects_load('salesforce', 'salesforce', $map->salesforce);
$export_data = salesforce_api_fieldmap_export_create($entity->salesforce->name, $entity);
$header = array(
t('Field name'),
t('Drupal @type value', array(
'@type' => salesforce_api_fieldmap_object_label('drupal', $map->drupal_entity, $map->drupal_bundle),
)),
t('Salesforce @type value', array(
'@type' => salesforce_api_fieldmap_object_label('salesforce', 'salesforce', $map->salesforce),
)),
);
$rows = array();
foreach ($map->fields as $sf_fieldname => $drupal_fieldname) {
$row = array();
$row[] = $sf_object_definition['fields'][$sf_fieldname]['label'];
$row[] = isset($export_data->{$sf_fieldname}) ? $export_data->{$sf_fieldname} : ' ';
$row[] = isset($sf_data->{$sf_fieldname}) ? $sf_data->{$sf_fieldname} : ' ';
$rows[] = $row;
}
$form['mapped'] = array(
'#type' => 'fieldset',
'#title' => t('Mapped field values'),
'#description' => t('These fields have been mapped through fieldmap <a href="!url">@index</a>.', array(
'!url' => url(SALESFORCE_PATH_FIELDMAPS . '/' . $entity->salesforce->name . '/edit'),
'@index' => $entity->salesforce->name,
)),
);
$form['mapped']['fieldmap_values'] = array(
'#markup' => theme('table', array(
'header' => $header,
'rows' => $rows,
)),
);
$form['mapped']['export_values'] = array(
'#type' => 'submit',
'#value' => t('Export'),
'#attributes' => array(
'class' => array(
'sf-confirm',
),
),
);
$form['mapped']['import_values'] = array(
'#type' => 'submit',
'#value' => t('Import'),
'#attributes' => array(
'class' => array(
'sf-confirm',
),
),
);
$form['mapped']['unlink'] = array(
'#type' => 'submit',
'#value' => t('Unlink from Salesforce object...'),
'#attributes' => array(
'class' => array(
'sf-confirm',
),
),
);
// Create a table for the unmapped fields.
$header = array(
t('Field name'),
t('Salesforce @type value', array(
'@type' => salesforce_api_fieldmap_object_label('salesforce', 'salesforce', $map->salesforce),
)),
);
$rows = array();
foreach ((array) $sf_data as $key => $value) {
if (!isset($map->fields[$key]) && isset($sf_object_definition['fields'][$key])) {
$rows[] = array(
$sf_object_definition['fields'][$key]['label'],
$value,
);
}
}
if (count($rows) > 0) {
$form['unmapped'] = array(
'#type' => 'fieldset',
'#title' => t('Unmapped fields'),
'#description' => t('These fields are available on Salesforce but are not currently mapped through the fieldmap used for this @entity.', array(
'@entity' => strtolower($info['label']),
)),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['unmapped']['unmapped_fields'] = array(
'#markup' => theme('table', array(
'header' => $header,
'rows' => $rows,
)),
);
}
$rows = array();
foreach (salesforce_api_fieldmap_system_fields() as $key => $value) {
$rows[] = array(
$value['label'],
$sf_data->{$key},
);
}
$form['system'] = array(
'#type' => 'fieldset',
'#title' => t('System fields'),
'#description' => t('These fields provide additional system information about the Salesforce object but cannot be exported to Salesforce.'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['system']['system_fields'] = array(
'#markup' => theme('table', array(
'header' => $header,
'rows' => $rows,
)),
);
$form['raw'] = array(
'#type' => 'fieldset',
'#title' => t('Raw data'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['raw']['data'] = array(
'#markup' => '<pre>' . print_r($sf_data, TRUE) . '</pre>',
);
}
return $form;
}