You are here

function oauthconnector_edit_form_provider in OAuth Connector 6

Same name and namespace in other branches
  1. 7 oauthconnector.admin.inc \oauthconnector_edit_form_provider()

Form to edit the settings of a provider.

1 string reference to 'oauthconnector_edit_form_provider'
oauthconnector_edit_provider in ./oauthconnector.admin.inc
Edit a provider.

File

./oauthconnector.admin.inc, line 123
Administrative functions for the OAuth Connector module.

Code

function oauthconnector_edit_form_provider(&$form_state, $provider) {
  $form = array();
  $form['pid'] = array(
    '#type' => 'value',
    '#value' => isset($provider->pid) ? $provider->pid : '',
  );
  $form['provider'] = array(
    '#type' => 'value',
    '#value' => $provider,
  );
  $form['name'] = array(
    '#type' => 'textfield',
    '#size' => 24,
    '#default_value' => $provider->name,
    '#title' => t('Name'),
    '#description' => t('A unique machine-readable name used to identify this provider internally. It may only contain lowercase alphanumeric characters and underscores. No spaces or uppercase characters.'),
    '#required' => TRUE,
  );
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#description' => t('A human-readable title for the provider.'),
    '#size' => 32,
    '#maxlength' => 255,
    '#required' => TRUE,
    '#default_value' => $provider->title,
  );
  $form['url'] = array(
    '#type' => 'textfield',
    '#title' => t('Base URL'),
    '#description' => t('A URL to the OAuth provider.'),
    '#size' => 32,
    '#maxlength' => 255,
    '#required' => TRUE,
    '#default_value' => $provider->url,
  );
  $form['consumer_key'] = array(
    '#type' => 'textfield',
    '#title' => t('OAuth Consumer Key'),
    '#description' => t('Your consumer key provided by the OAuth provider.'),
    '#size' => 32,
    '#maxlength' => 255,
    '#default_value' => $provider->consumer->key,
  );
  $form['consumer_secret'] = array(
    '#type' => 'textfield',
    '#title' => t('OAuth Consumer Secret'),
    '#description' => t('Your consumer secret provided by the OAuth provider.'),
    '#size' => 32,
    '#maxlength' => 255,
    '#default_value' => $provider->consumer->secret,
  );
  $form['consumer_advanced'] = array(
    '#type' => 'fieldset',
    '#title' => t('OAuth Consumer Advanced Settings'),
    '#tree' => TRUE,
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $sign_methods = array(
    'HMAC-SHA1' => 'HMAC-SHA1',
  );
  foreach (hash_algos() as $algo) {
    $sign_methods['HMAC-' . strtoupper($algo)] = 'HMAC-' . strtoupper($algo);
  }
  $sign_methods['PLAINTEXT'] = 'PLAINTEXT';
  $form['consumer_advanced']['signature method'] = array(
    '#type' => 'select',
    '#title' => t('Signature method'),
    '#options' => $sign_methods,
    '#required' => TRUE,
    '#default_value' => $provider->consumer_advanced['signature method'],
  );
  $form['consumer_advanced']['authentication realm'] = array(
    '#type' => 'textfield',
    '#title' => t('Authentication realm'),
    '#size' => 32,
    '#maxlength' => 255,
    '#default_value' => $provider->consumer_advanced['authentication realm'],
  );
  $form['consumer_advanced']['request token endpoint'] = array(
    '#type' => 'textfield',
    '#title' => t('Request token endpoint'),
    '#description' => t('Absolute path or path relative to base URL.'),
    '#size' => 32,
    '#maxlength' => 255,
    '#required' => TRUE,
    '#default_value' => $provider->consumer_advanced['request token endpoint'],
  );
  $form['consumer_advanced']['authorization endpoint'] = array(
    '#type' => 'textfield',
    '#title' => t('Authorization endpoint'),
    '#description' => t('Absolute path or path relative to base URL.'),
    '#size' => 32,
    '#maxlength' => 255,
    '#required' => TRUE,
    '#default_value' => $provider->consumer_advanced['authorization endpoint'],
  );
  $form['consumer_advanced']['access token endpoint'] = array(
    '#type' => 'textfield',
    '#title' => t('Access token endpoint'),
    '#description' => t('Absolute path or path relative to base URL.'),
    '#size' => 32,
    '#maxlength' => 255,
    '#required' => TRUE,
    '#default_value' => $provider->consumer_advanced['access token endpoint'],
  );
  $form['mapping'] = array(
    '#type' => 'fieldset',
    '#title' => t('Mapping'),
    '#description' => t('Map the attributes from the API response to the attributes useable by OAuth Connector.') . "<br/>" . t('If you have the !QueryPath module installed the field selection can be made by a CSS selector.', array(
      '!QueryPath' => l('QueryPath', 'http://drupal.org/project/querypath'),
    )),
    '#tree' => TRUE,
    'fields' => array(),
  );
  $form['mapping']['format'] = array(
    '#type' => 'radios',
    '#title' => t('Format'),
    '#options' => array(
      'json' => 'JSON',
      'php' => 'PHP',
      'xml' => 'XML',
    ),
    '#default_value' => empty($provider->mapping['format']) ? 'json' : $provider->mapping['format'],
  );
  $mappings = array(
    'uid' => array(
      'title' => t('User ID'),
      'description' => t('A resource containing a unique ID for the user.'),
      'required' => TRUE,
    ),
    'real name' => array(
      'title' => t('Name'),
      'description' => t('A resource containing the name of the user.'),
    ),
    'avatar' => array(
      'title' => t('Avatar'),
      'description' => t('A resource containing the URL of the users avatar.'),
    ),
  );
  foreach ($mappings as $key => $mapping) {
    $form['mapping']['fields'][$key] = array(
      '#type' => 'fieldset',
      '#title' => $mapping['title'],
      '#description' => $mapping['description'],
    );
    $form['mapping']['fields'][$key]['resource'] = array(
      '#type' => 'textfield',
      '#title' => t('Resource'),
      '#description' => t('The URL of the API resource representing the authorized user.'),
      '#size' => 32,
      '#maxlength' => 255,
      '#required' => !empty($mapping['required']),
      '#default_value' => empty($provider->mapping['fields'][$key]['resource']) ? '' : $provider->mapping['fields'][$key]['resource'],
    );
    $form['mapping']['fields'][$key]['method post'] = array(
      '#type' => 'checkbox',
      '#title' => t('POST request'),
      '#default_value' => !empty($provider->mapping['fields'][$key]['method post']),
    );
    $form['mapping']['fields'][$key]['field'] = array(
      '#type' => 'textfield',
      '#title' => t('Field'),
      '#size' => 32,
      '#maxlength' => 32,
      '#required' => !empty($mapping['required']),
      '#default_value' => empty($provider->mapping['fields'][$key]['field']) ? '' : $provider->mapping['fields'][$key]['field'],
    );
    $form['mapping']['fields'][$key]['querypath'] = array(
      '#type' => 'checkbox',
      '#title' => t('Field is a CSS selector'),
      '#default_value' => !empty($provider->mapping['fields'][$key]['querypath']),
      '#access' => module_exists('querypath'),
    );
  }
  $label = empty($provider->pid) ? t('Save and proceed') : t('Save');
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => $label,
  );
  return $form;
}