You are here

function salesforce_oauth_form in Salesforce Suite 7.3

Generate the Salesforce authorization form.

Return value

array The Salesforce authorization form.

1 string reference to 'salesforce_oauth_form'
salesforce_menu in ./salesforce.module
Implements hook_menu().

File

./salesforce.module, line 166
API and module for Salesforce integration.

Code

function salesforce_oauth_form() {

  // Overlay doesn't work. See https://www.drupal.org/node/2268401.
  if (module_exists('overlay') && overlay_get_mode() == 'child') {
    $current_path = current_path();
    overlay_close_dialog($current_path);
  }
  $form = array();
  $consumer_key = variable_get('salesforce_consumer_key', FALSE);
  $consumer_secret = variable_get('salesforce_consumer_secret', FALSE);
  $salesforce_endpoint = variable_get('salesforce_endpoint', SALESFORCE_DEFAULT_ENDPOINT);
  $form['message'] = array(
    '#type' => 'item',
    '#markup' => t('Authorize this website to communicate with Salesforce by entering the consumer key and secret from a remote application. Clicking authorize will redirect you to Salesforce where you will be asked to grant access.'),
  );
  $form['salesforce_consumer_key'] = array(
    '#title' => t('Salesforce consumer key'),
    '#type' => 'textfield',
    '#description' => t('Consumer key of the Salesforce remote application you want to grant access to'),
    '#required' => TRUE,
    '#default_value' => $consumer_key,
  );
  $form['salesforce_consumer_secret'] = array(
    '#title' => t('Salesforce consumer secret'),
    '#type' => 'textfield',
    '#description' => t('Consumer secret of the Salesforce remote application you want to grant access to'),
    '#required' => TRUE,
    '#default_value' => $consumer_secret,
  );
  $form['advanced'] = array(
    '#title' => t('Advanced'),
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['advanced']['salesforce_endpoint'] = array(
    '#title' => t('Salesforce endpoint'),
    '#type' => 'textfield',
    '#description' => t('Enter the URL of your Salesforce environment (for example, <code>https://test.salesforce.com</code>). <strong>Caution:</strong> Note that switching this setting after you have already synchronised data between your Drupal site and Salesforce will render any existing links between Salesforce objects and Drupal objects invalid!'),
    '#default_value' => $salesforce_endpoint,
  );
  $form['submit'] = array(
    '#value' => t('Authorize'),
    '#type' => 'submit',
  );

  // If we're authenticated, show a list of available REST resources.
  if ($consumer_key && $consumer_secret) {
    $sfapi = new Salesforce($consumer_key, $consumer_secret);

    // If fully configured, attempt to connect to Salesforce and return a list
    // of resources.
    if ($sfapi
      ->isAuthorized()) {
      try {
        $resources = $sfapi
          ->listResources();
        foreach ($resources as $key => $path) {
          $items[] = $key . ': ' . $path;
        }
        $form['resources'] = array(
          '#title' => t('Your Salesforce instance is authorized and has access to the following resources:'),
          '#type' => 'item',
          '#markup' => theme('item_list', array(
            'items' => $items,
          )),
        );
      } catch (SalesforceException $e) {
        salesforce_set_message(t('Salesforce Exception during @event : @exception', array(
          '@event' => 'oauth',
          '@exception' => $e
            ->getMessage(),
        )), 'warning');
      }
    }
    else {
      salesforce_set_message(t('Salesforce needs to be authorized to connect to this website.'), 'error');
    }
  }
  return $form;
}