You are here

function sf_import_settings_form in Salesforce Suite 7.2

Same name and namespace in other branches
  1. 6.2 sf_import/sf_import.admin.inc \sf_import_settings_form()

Main import settings form for Salesforce Import module.

1 string reference to 'sf_import_settings_form'
sf_import_menu in sf_import/sf_import.module
Implements hook_menu().

File

sf_import/sf_import.admin.inc, line 11
Admin settings for the Salesforce Import module.

Code

function sf_import_settings_form($form, $form_state) {
  $form = array();
  $form['overview'] = array(
    '#value' => 'Ongoing imports from Salesforce are possible by configuring the fieldmap(s) defined below. On each cron run, Salesforce Import will check for updated records and import them into Drupal.',
    '#prefix' => '<p>',
    '#suffix' => '</p>',
  );

  // Shows the time of last import.
  $last_import = variable_get('sf_import_queue_last_import', NULL);
  if ($last_import) {
    $date = date('M j Y H:i:s', $last_import);

    // Get the number of items remaining in the import queue
    $num_items = db_query("SELECT COUNT(sfid) FROM {sf_import_queue}")
      ->fetchField();
    $data = '<strong>Last import: </strong>' . $date . ' <br /><strong>Number of items imported:</strong> ' . variable_get('sf_import_queue_import_count', 0) . '<br /><strong>Number of items processed:</strong> ' . variable_get('sf_import_queue_processed_count', 0) . '<br /><strong>Number of items remaining in queue:</strong> ' . $num_items;
    $form['last_import'] = array(
      '#value' => $data,
      '#prefix' => '<p>',
      '#suffix' => '</p>',
    );
  }

  // Loads the fieldmaps.
  $fieldmaps = salesforce_api_salesforce_fieldmap_load_all();
  $maps = array();
  foreach ($fieldmaps as $map) {
    $maps[$map->name] = $map->name;
    if ($map->description) {
      $maps[$map->name] .= '<em> (' . $map->description . ')</em>';
    }
  }
  $form['sf_import_fieldmaps'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Fieldmaps'),
    '#description' => t('Select which fieldmaps should be used for ongoing imports from Salesforce to Drupal.'),
    '#options' => $maps,
    '#default_value' => variable_get('sf_import_fieldmaps', array(
      NULL,
    )),
  );

  // Adds advanced configuration settings on a per-fieldmap basis.
  $form['sf_import_advanced'] = array(
    '#type' => 'fieldset',
    '#title' => t('Advanced settings'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $import_maps = variable_get('sf_import_fieldmaps', array());
  $active_import_maps = array();
  if ($import_maps) {
    foreach ($import_maps as $import_map_key => $import_map_value) {

      // Provides fields to set custom import settings for this fieldmap.
      $form['sf_import_advanced'][$import_map_key]['update_settings'] = array(
        '#type' => 'fieldset',
        '#title' => t('Import settings for fieldmap %map', array(
          '%map' => $import_map_key,
        )),
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
      );
      $form['sf_import_advanced'][$import_map_key]['update_settings']['sf_import_' . $import_map_key . '_update_method'] = array(
        '#type' => 'radios',
        '#title' => 'Import method',
        '#description' => 'Select the method for selecting Salesforce IDs to import.',
        '#options' => array(
          'soql' => 'Custom SOQL query',
          'get_updated' => 'Salesforce API getUpdated() method',
        ),
        '#default_value' => variable_get('sf_import_' . $import_map_key . '_update_method', 'get_updated'),
      );
      $form['sf_import_advanced'][$import_map_key]['update_settings']['default_soql'] = array(
        '#value' => 'Default SOQL statement used for importing data: <code>SELECT Id, LastModifiedDate FROM ' . $fieldmaps[$import_map_key]->salesforce . ' WHERE LastModifiedDate > ' . gmdate(DATE_ATOM, variable_get('sf_import_queue_last_import', time())) . '</code>',
        '#prefix' => '<p>',
        '#suffix' => '</p>',
      );
      $form['sf_import_advanced'][$import_map_key]['update_settings']['sf_import_' . $import_map_key . '_soql'] = array(
        '#type' => 'textarea',
        '#title' => 'SOQL query',
        '#description' => t('Enter any additional SOQL WHERE clauses for querying Salesforce about updated records in the format of "Email != NULL AND FirstName != NULL".'),
        '#collapsible' => TRUE,
        '#collapsed' => FALSE,
        '#default_value' => variable_get('sf_import_' . $import_map_key . '_soql', NULL),
      );
    }
  }
  $form['sf_import_advanced']['sf_import_cron'] = array(
    '#type' => 'fieldset',
    '#title' => t('Drupal Cron Settings'),
    '#description' => t('You should only uncheck the boxes below if you intend to implement your own import and processing functions.'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['sf_import_advanced']['sf_import_cron']['sf_import_cron_import'] = array(
    '#type' => 'checkbox',
    '#title' => t('On cron run, import updated SFIDs to the {sf_import_queue} table.'),
    '#default_value' => variable_get('sf_import_cron_import', 1),
  );
  $form['sf_import_advanced']['sf_import_cron']['sf_import_cron_process'] = array(
    '#type' => 'checkbox',
    '#title' => t('On cron run, process SFIDs in the Salesforce import queue.'),
    '#default_value' => variable_get('sf_import_cron_process', 1),
  );

  // For fieldmaps that have import enabled, show some information about them.
  $import_maps = variable_get('sf_import_fieldmaps', array());
  $active_import_maps = array();
  if ($import_maps) {
    foreach ($import_maps as $import_map_key => $import_map_value) {
      if ($import_map_value !== 0) {
        $map = salesforce_api_salesforce_fieldmap_load($import_map_key);
        $start = variable_get('sf_import_queue_last_import', REQUEST_TIME);
        $end = REQUEST_TIME;

        // salesforce_api_get_updated requires the query window to be at least 1 minute
        if ($end - $start < 60) {
          $start = $end - 61;
        }
        $import_method = variable_get('sf_import_' . $map->name . '_update_method', 'get_updated');
        $import_function = $import_method == 'get_updated' ? 'salesforce_api_get_updated' : '_sf_import_get_soql_records';
        $sf_updated = $import_function($map, $start, $end);
        $form[$import_map_key . '_information'] = array(
          '#type' => 'fieldset',
          '#title' => t('Information for fieldmap %map', array(
            '%map' => $import_map_key,
          )),
          '#description' => t('Number of updated records and timestamp of last update, if information is available.'),
          '#collapsible' => TRUE,
          '#collapsed' => $sf_updated ? FALSE : TRUE,
        );
        if ($sf_updated) {
          $form[$import_map_key . '_information']['pending'] = array(
            '#markup' => t('@count updated record(s) in Salesforce pending import.<br />Newest object in Salesforce dated @date', array(
              '@count' => count($sf_updated->ids),
              '@date' => $sf_updated->latestDateCovered,
            )),
            '#prefix' => '<p>',
            '#suffix' => '</p>',
          );
          $active_import_maps[$import_map_key] = $import_map_value;

          // Show table of SFIDs pending import.
          $rows = array();
          $header = array(
            'Salesforce ID',
          );
          $sf_data = $sf_updated->ids;
          foreach ($sf_data as $key => $sfid) {
            $rows[] = array(
              $sfid,
            );
          }
          $form[$import_map_key . '_information']['data'] = array(
            '#markup' => theme('table', array(
              'header' => $header,
              'rows' => $rows,
            )),
            '#prefix' => '<div>',
            '#suffix' => '</div>',
          );
        }
        else {
          $form[$import_map_key . '_information']['nodata'] = array(
            '#markup' => 'There are no pending updates for this fieldmap.',
            '#prefix' => '<p>',
            '#suffix' => '</p>',
          );
        }
      }
    }

    // Allow the user to import records for fieldmaps that have updates.
    if ($active_import_maps) {
      $form['process_updates'] = array(
        '#type' => 'fieldset',
        '#title' => t('Process updates for all fieldmaps'),
        '#description' => t('Query Salesforce for updates for all fieldmaps and import updated data.'),
        '#collapsible' => TRUE,
        '#collapsed' => FALSE,
      );
      $form['process_updates']['get_updates'] = array(
        '#type' => 'submit',
        '#value' => t('Import updates'),
      );
      $form['#submit'][] = 'sf_import_settings_form_submit';
    }
  }
  return system_settings_form($form);
}