You are here

function sf_entity_salesforce_form_submit in Salesforce Suite 7.2

Same name and namespace in other branches
  1. 7 sf_entity/sf_entity.module \sf_entity_salesforce_form_submit()

Form submit handler for the [entity-name]/%/salesforce form.

File

sf_entity/sf_entity.module, line 707
Integrates fieldable entities with the Salesforce API.

Code

function sf_entity_salesforce_form_submit($form, &$form_state) {
  if ($form_state['values']['op'] == t('Export') || $form_state['values']['op'] == t('Import') || $form_state['values']['op'] == t('Create Link')) {
    $info = $form_state['values']['entity info'];
    list($oid, $vid, $bundle) = $form_state['values']['entity keys'];

    // entity_load actually accepts an array of entity ids
    $entities = entity_load($form_state['values']['entity type'], array(
      $oid,
    ));
    $entity = current($entities);
  }
  switch ($form_state['values']['op']) {

    // Export the node to Salesforce.
    case t('Export'):
      $sfid = empty($form_state['values']['sfid']) ? NULL : $form_state['values']['sfid'];
      if (sf_entity_export($entity, $form_state['values']['fieldmap'], $sfid)) {
        drupal_set_message(t('%entity successfully exported to Salesforce.', array(
          '%entity' => $form_state['values']['entity type'],
        )));
      }
      else {
        drupal_set_message(t('An error occurred while exporting the %entity to Salesforce.  Check the watchdog for more information.', array(
          '%entity' => $form_state['values']['entity type'],
        )), 'error');
      }
      break;

    // Import changes from Salesforce.
    case t('Import'):

      // Call the sf_entity_import() function with the extra-linked parameter, so that the import time will get updated.
      if (sf_entity_import($form_state['values']['sfid'], $form_state['values']['fieldmap'], $oid, array(
        'extra-linked' => TRUE,
      ))) {
        drupal_set_message(t('The %entity has been updated with values from Salesforce.', array(
          '%entity' => $form_state['values']['entity type'],
        )));
      }
      else {
        drupal_set_message(t('An error occurred while importing the changes from Salesforce. Check watchdog for more information.'), 'error');
      }
      break;
    case t('Create Link'):
      if (!is_sfid($form_state['values']['sfid'])) {
        drupal_set_message(t('Invalid SFID provided.'), 'error');
        break;
      }
      list($oid, $vid, $bundle) = $form_state['values']['entity keys'];
      salesforce_api_id_save($oid, $form_state['values']['sfid'], $form_state['values']['fieldmap'], $form_state['values']['entity type'], $bundle, 'link');
      break;

    // Unlink from Salesforce.
    case isset($form_state['values']['unlink']):
    case t('Unlink'):
      if (!isset($form_state['storage']['confirm'])) {
        $form_state['storage']['confirm'] = TRUE;
        $form_state['rebuild'] = TRUE;
      }
      else {
        unset($form_state['storage']['confirm']);
        $entity = $form_state['values']['entity'];
        $entity_type = $form_state['values']['entity_type'];
        list($oid, $vid, $bundle) = entity_extract_ids($entity_type, $entity);
        salesforce_api_id_unlink(array(
          'oid' => $oid,
          'name' => $entity->salesforce->name,
        ));
        drupal_set_message(t('The %type !oid has been unlinked from Salesforce record !sfid.', array(
          '%type' => $entity_type,
          '!oid' => $oid,
          '!sfid' => $entity->salesforce->sfid,
        )));
      }
      break;
  }
}