You are here

function sf_entity_salesforce_form in Salesforce Suite 7

Same name and namespace in other branches
  1. 7.2 sf_entity/sf_entity.module \sf_entity_salesforce_form()
1 string reference to 'sf_entity_salesforce_form'
sf_entity_menu in sf_entity/sf_entity.module
Implements hook_menu(). Create a %/salesforce menu callback for all fieldable entities.

File

sf_entity/sf_entity.module, line 463
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($id, $vid, $bundle) = entity_extract_ids($entity_type, $entity);

  // Fail out if the node didn't exist!
  if (!$id || !$bundle) {
    drupal_not_found();
    exit;
  }
  drupal_set_title(t('SalesForce Export/Import'));
  $info = entity_get_info($entity_type);

  // Set the node page title.
  $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(
      $id,
      $vid,
      $bundle,
    ),
  );
  $options = salesforce_api_fieldmap_options($entity_type, $bundle);

  // Display an export button if the node hasn't been exported before.
  if (empty($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' => $info['label'],
      )),
    );
    if (!empty($options)) {

      // Add the export form.
      $form['export']['fieldmap'] = array(
        '#type' => 'select',
        '#title' => t('Export fieldmap'),
        '#options' => $options,
      );
      $form['export']['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Export'),
      );
    }
    else {
      $form['export']['notice'] = array(
        '#type' => 'item',
        '#title' => t('No @entity fieldmaps', array(
          '@entity' => $info['label'],
        )),
        '#markup' => t('You have not created any @entity fieldmaps. Please <a href="/!url">go create a fieldmap</a> and come back.', array(
          '@entity' => $info['label'],
          '!url' => SALESFORCE_PATH_FIELDMAPS,
        )),
      );
    }
  }
  else {

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

    // Retrieve the object from Salesforce.
    $sf = salesforce_api_connect();
    if (!$sf) {
      drupal_set_message(t('Unable to connect to Salesforce using <a href="!url">current credentials</a>.', array(
        '!url' => url(SALESFORCE_PATH_ADMIN),
      )));
      return array();
    }
    $sf_data = $sf
      ->retrieve(array(
      $entity->salesforce['sfid'],
    ), $entity->salesforce['fieldmap']);

    // Load the fieldmap data.
    $map = salesforce_api_fieldmap_load($entity->salesforce['fieldmap']);
    $sf_object_definition = salesforce_api_fieldmap_objects_load('salesforce', 'salesforce', $map['salesforce']);
    $export_data = salesforce_api_fieldmap_export_create($entity->salesforce['fieldmap'], $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', $map['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} : '&nbsp;';
      $row[] = isset($sf_data->{$sf_fieldname}) ? $sf_data->{$sf_fieldname} : '&nbsp;';
      $rows[] = $row;
    }
    $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 . '/' . $entity->salesforce['fieldmap'] . '/edit'),
        '@index' => $entity->salesforce['fieldmap'],
      )),
    );
    $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',
        ),
      ),
    );

    // 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'], $map['salesforce']),
      )),
    );
    $rows = array();
    foreach ((array) $sf_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 @entity.', array(
          '@entity' => $info['label'],
        )),
      );
      $form['unmapped']['unmmaped_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;
}