You are here

function sf_user_salesforce_form in Salesforce Suite 6.2

Same name and namespace in other branches
  1. 5.2 sf_user/sf_user.module \sf_user_salesforce_form()
1 string reference to 'sf_user_salesforce_form'
sf_user_menu in sf_user/sf_user.module
Implementation of hook_menu().

File

sf_user/sf_user.module, line 211
Integrates the core user object and profile module with the Salesforce API.

Code

function sf_user_salesforce_form(&$form_state, $account) {

  // Fail out if the user didn't exist!
  if (!$account->uid) {
    drupal_not_found();
  }
  if (isset($form_state['storage']['confirm'])) {
    $form['account'] = array(
      '#type' => 'value',
      '#value' => $account,
    );
    return confirm_form($form, 'Are you sure you want to unlink this user from Salesforce?', 'user/' . $account->uid . '/salesforce', '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 user page title.
  drupal_set_title(check_plain($account->name));
  $form = array();
  $form['uid'] = array(
    '#type' => 'value',
    '#value' => $account->uid,
  );
  if ($account->salesforce->sfid) {

    // Retrieve the object from Salesforce.
    $sf_data = salesforce_api_retrieve(array(
      $account->salesforce->sfid,
    ), $account->salesforce->name);

    // Check to see if salesforce_api_retrieve returned an array of objects
    if (is_array($sf_data) && count($sf_data) > 0) {
      $sf_data = $sf_data[0];
    }
    if (!$sf_data && SALESFORCE_DELETED_POLICY_UPSERT == variable_get('salesforce_api_entity_deleted_policy', SALESFORCE_DELETED_POLICY_UPSERT)) {

      // Unlink the object
      salesforce_api_id_unlink(array(
        'oid' => $account->uid,
        'name' => $account->salesforce->name,
      ));
      $account = user_load($account->uid);
    }
    elseif (!$sf_data) {
      drupal_set_message(t('Unable to retrieve Salesforce data for record !sfid.', array(
        '!sfid' => $account->salesforce->sfid,
      )), 'warning');
    }
  }

  // Display an export button if the user hasn't been exported before.
  if (!$account->salesforce->sfid) {
    $form['export'] = array(
      '#type' => 'fieldset',
      '#title' => t('Export user to Salesforce'),
      '#description' => t('This user may be exported to Salesforce using any fieldmap listed below.'),
    );

    // Get an array of fieldmaps that export users of this type to Salesforce.
    $options = salesforce_api_fieldmap_options('user');

    // Add the export form
    $form['export']['name'] = 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']['export_user'] = array(
      '#type' => 'submit',
      '#value' => t('Export user'),
    );
  }
  else {

    // Otherwise add synchronization information.
    $form['sfid'] = array(
      '#type' => 'value',
      '#value' => $account->salesforce->sfid,
    );
    $form['name'] = array(
      '#type' => 'value',
      '#value' => $account->salesforce->name,
    );

    // Load the fieldmap data.
    $map = salesforce_api_fieldmap_load($account->salesforce->name);
    $sf_object_definition = salesforce_api_fieldmap_objects_load('salesforce', $map->salesforce);
    $export_data = salesforce_api_fieldmap_export_create($account->salesforce->name, $account);
    $header = array(
      t('Field name'),
      t('Drupal user value'),
      t('Salesforce @type value', array(
        '@type' => salesforce_api_fieldmap_object_label('salesforce', $map->salesforce),
      )),
    );
    $rows = array();
    foreach ($map->fields as $sf_fieldname => $drupal_fieldname) {
      $rows[] = array(
        $sf_object_definition['fields'][$sf_fieldname]['label'],
        $export_data->{$sf_fieldname},
        $sf_data->{$sf_fieldname},
      );
    }
    $form['mapped'] = array(
      '#type' => 'fieldset',
      '#title' => t('Mapped field values'),
      '#description' => t('<a href="!url">Edit this fieldmap</a>.', array(
        '!url' => url(SALESFORCE_PATH_FIELDMAPS . '/' . $account->salesforce->name . '/edit'),
      )),
    );
    $form['mapped']['fieldmap_values'] = array(
      '#value' => theme('table', $header, $rows),
    );
    $form['mapped']['export_values'] = array(
      '#type' => 'submit',
      '#value' => t('Export changes to Salesforce'),
      '#attributes' => array(
        'class' => 'sf-confirm',
      ),
    );
    $form['mapped']['import_values'] = array(
      '#type' => 'submit',
      '#value' => t('Import changes from Salesforce'),
      '#attributes' => array(
        'class' => 'sf-confirm',
      ),
    );
    $form['mapped']['unlink'] = array(
      '#type' => 'submit',
      '#value' => t('Unlink from Salesforce object...'),
      '#attributes' => array(
        'class' => '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', $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 user. Some of these values may only be available when importing from Salesforce.'),
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
      );
      $form['unmapped']['unmmaped_fields'] = array(
        '#value' => theme('table', $header, $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(
      '#value' => theme('table', $header, $rows),
    );
    $form['raw'] = array(
      '#type' => 'fieldset',
      '#title' => t('Raw data'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    );
    $form['raw']['data'] = array(
      '#value' => '<pre>' . print_r($sf_data, TRUE) . '</pre>',
    );
  }
  return $form;
}