You are here

function sf_user_salesforce_form in Salesforce Suite 5.2

Same name and namespace in other branches
  1. 6.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 102
Integrates the core user object and profile module with the Salesforce API.

Code

function sf_user_salesforce_form($uid) {
  $account = user_load(array(
    'uid' => $uid,
  ));

  // Fail out if the user didn't exist!
  if (!$account->uid) {
    drupal_not_found();
  }

  // Set the node page title.
  drupal_set_title(check_plain($account->name));
  $form = array();
  $form['uid'] = array(
    '#type' => 'value',
    '#value' => $uid,
  );

  // Display an export button if the node 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 nodes of this type to Salesforce.
    $options = salesforce_api_fieldmap_options('export', 'user');

    // If no corresponding fieldmaps were found...
    if (count($options) == 0) {

      // Display a message appropriate to the user's permissions.
      if (user_access('administer salesforce')) {
        $form['export']['#description'] = t('To export this user you must first <a href="!url">add a fieldmap</a> that exports users.');
      }
      else {
        $form['export']['#description'] = t('Please contact a site administrator to add a fieldmap that exports users.');
      }
    }
    else {

      // Otherwise add the export form!
      $form['export']['fieldmap'] = array(
        '#type' => 'select',
        '#title' => t('Export fieldmap'),
        '#options' => $options,
      );
      $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['fieldmap'] = array(
      '#type' => 'value',
      '#value' => $account->salesforce['fieldmap'],
    );

    // Retrieve the object from Salesforce.
    $sf = salesforce_api_connect();
    $data = $sf
      ->retrieve(array(
      $account->salesforce['sfid'],
    ), $account->salesforce['fieldmap']);

    // Load the fieldmap data.
    $map = salesforce_api_fieldmap_load($account->salesforce['fieldmap']);

    // Load the object definitions.
    $drupal_object = salesforce_api_fieldmap_objects_load('drupal', $map['drupal']);
    $object = salesforce_api_fieldmap_objects_load('salesforce', $map['salesforce']);
    $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 $key => $value) {
      if (isset($drupal_object['fields'][$value]['export'])) {
        $drupal_value = $drupal_object['fields'][$value]['export']($account, $value);
      }
      elseif (isset($account->{$value})) {
        $drupal_value = $account->{$value};
      }
      else {
        $drupal_value = '';
      }
      $rows[] = array(
        $object['fields'][$key]['label'],
        $drupal_value,
        $data->{$key},
      );
    }
    $form['mapped'] = array(
      '#type' => 'fieldset',
      '#title' => t('Mapped field values'),
      '#description' => t('These fields have been mapped through <a href="!url">fieldmap @index</a>.', array(
        '!url' => url(SALESFORCE_PATH_FIELDMAPS . '/' . $account->salesforce['fieldmap'] . '/edit'),
        '@index' => $account->salesforce['fieldmap'],
      )),
    );
    $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',
      ),
    );

    // 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) $data as $key => $value) {
      if (!isset($map['fields'][$key]) && isset($object['fields'][$key])) {
        $rows[] = array(
          $object['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.'),
      );
      $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'],
        $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($data, TRUE) . '</pre>',
    );
  }
  return $form;
}