You are here

function sf_node_salesforce_form in Salesforce Suite 6.2

Same name and namespace in other branches
  1. 5.2 sf_node/sf_node.module \sf_node_salesforce_form()
1 string reference to 'sf_node_salesforce_form'
sf_node_menu in sf_node/sf_node.module
Implementation of hook_menu().

File

sf_node/sf_node.module, line 395
Integrates the core node object and various node related modules with the Salesforce API.

Code

function sf_node_salesforce_form(&$form_state, $node) {

  // Fail out if the node didn't exist!
  if (!$node->nid) {
    drupal_not_found();
  }
  if (isset($form_state['storage']['confirm'])) {

    // ALSO do $form definition here. Your final submit handler (after user clicks Yes, I Confirm) will only see $form_state info defined here. Form you create here passed as param1 to confirm_form
    $form['node'] = array(
      '#type' => 'value',
      '#value' => $node,
    );
    return confirm_form($form, 'Are you sure you want to unlink this node from Salesforce?', 'node/' . $node->nid . '/salesforce', 'Unlinking this object will remove the connection between the Drupal object and the Salesforce record. This action will not delete the Drupal object or the Salesforce record. This cannot be undone.', 'Unlink', 'Cancel');

    //Had better luck leaving off last param 'name'
  }

  // Set the node page title.
  drupal_set_title(check_plain($node->title));
  $form = array();
  $form['nid'] = array(
    '#type' => 'value',
    '#value' => $node->nid,
  );
  if ($node->salesforce->sfid) {

    // Retrieve the object from Salesforce.
    $sf_data = salesforce_api_retrieve(array(
      $node->salesforce->sfid,
    ), $node->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' => $node->salesforce->sfid,
      )), 'warning');

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

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

    // Add the export form.
    $form['export']['fieldmap'] = array(
      '#type' => 'select',
      '#title' => t('Export fieldmap'),
      '#options' => salesforce_api_fieldmap_options('node_' . $node->type),
    );
    $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_node'] = array(
      '#type' => 'submit',
      '#value' => t('Export node'),
    );
  }
  else {

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

    // Load the fieldmap data.
    $map = salesforce_api_fieldmap_load($node->salesforce->name);
    $sf_object_definition = salesforce_api_fieldmap_objects_load('salesforce', $map->salesforce);
    $export_data = salesforce_api_fieldmap_export_create($node->salesforce->name, $node);
    $header = array(
      t('Field name'),
      t('Drupal @type value', array(
        '@type' => salesforce_api_fieldmap_object_label('drupal', $map->drupal),
      )),
      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 . '/' . $node->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.'),
        '#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;
}