You are here

function fb_admin_token_select_process in Drupal for Facebook 7.4

Drupal form element process callback.

1 string reference to 'fb_admin_token_select_process'
fb_element_info in ./fb.module
Implements hook_element_info().

File

./fb.admin.inc, line 1413

Code

function fb_admin_token_select_process(&$element, &$form_state, &$form) {

  // defaults
  $element = $element + array(
    '#default_value' => -1,
  );
  $element['#tree'] = TRUE;
  $all_tokens = array();
  $i = 0;
  $option_default = -1;

  // Query all admin tokens to figure out our options.
  $result = db_query("SELECT * FROM {fb_token} WHERE access_token = :current_token OR (status & :valid_flag AND status & :admin_flag) ORDER BY changed DESC", array(
    ':current_token' => $element['#default_value'],
    ':valid_flag' => FB_STATUS_FLAG_VALID,
    ':admin_flag' => FB_STATUS_FLAG_ADMIN,
  ));

  // Loop over the saved tokens.
  while ($token_data = $result
    ->fetchAssoc()) {
    $i++;
    $all_tokens[$i] = $token_data;

    // In most cases, this call to fb_graph() hits the cache, not facebook.
    try {
      $fba_data = fb_graph($token_data['fba']);
      $fbu_data = fb_graph($token_data['fbu']);
    } catch (exception $e) {
      fb_log_exception($e, t('Failed to get token data.'));
    }
    if ($token_data['status'] & FB_STATUS_FLAG_ADMIN && !($token_data['status'] & FB_STATUS_FLAG_APP)) {
      $options[$i] = t('%name via %app', array(
        '%name' => fb_get_name($fbu_data),
        '%app' => fb_get_name($fba_data),
        '%time_ago' => format_interval(REQUEST_TIME - $token_data['changed']),
      ));
      if (!($token_data['status'] & FB_STATUS_FLAG_VALID)) {
        $options[$i] .= ' ' . t('(This token has expired!)');
      }
      elseif ($token_data['access_token'] == $element['#default_value']) {
        $option_default = $i;
      }
    }
  }

  // End of admin token loop.
  // Pass values to validate and submit hooks.
  $form_state['fb']['all_tokens'] = $all_tokens;
  if (!empty($options)) {
    $options[-1] = t('Do not use a saved token');

    // If a new token was generated from a code, the user probably intends to submit that new token rather than the current saved one, so change default.
    if (!empty($_REQUEST['code'])) {
      $option_default = -1;
    }

    // Note our options array uses index rather than token, to avoid embedding tokens in the form markup.
    $element['fb_token_index'] = array(
      '#type' => 'radios',
      //'#title' => t('Current and previous tokens'),
      '#options' => $options,
      '#default_value' => $option_default,
    );
  }
  return $element;
}