You are here

function fb_admin_tokens_form in Drupal for Facebook 7.4

1 string reference to 'fb_admin_tokens_form'
fb_admin_token_page in ./fb.admin.inc

File

./fb.admin.inc, line 542

Code

function fb_admin_tokens_form() {

  // Build the sortable table header.
  $header = array(
    // Sort disabled, because only numerical sort possible, not alphabetical
    'fba' => array(
      'data' => t('Application'),
    ),
    'fbu' => array(
      'data' => t('Account'),
    ),
    'status' => array(
      'data' => t('Status'),
      'field' => 'fbt.status',
    ),
    'token' => array(
      'data' => t('Token'),
      'style' => 'display:none;',
      'class' => 'fb_access_token',
    ),
    'changed' => array(
      'data' => t('Updated'),
      'field' => 'fbt.changed',
      'sort' => 'desc',
    ),
    'operations' => array(
      'data' => t('Operations'),
    ),
  );
  $query = db_select('fb_token', 'fbt')
    ->extend('PagerDefault')
    ->extend('TableSort');
  $query
    ->leftjoin('cache_fb', 'fbc_fba', 'fbc_fba.cid = fbt.fba');
  $query
    ->addField('fbc_fba', 'data', 'fba_data');
  $query
    ->leftjoin('cache_fb', 'fbc_fbu', 'fbc_fbu.cid = fbt.fbu');
  $query
    ->addField('fbc_fbu', 'data', 'fbu_data');
  $results = $query
    ->fields('fbt')
    ->limit(50)
    ->orderByHeader($header)
    ->execute();
  $options = array();
  while ($tdata = $results
    ->fetchObject()) {
    $options[$tdata->access_token] = array(
      // TODO: icon.
      // TODO: human readable name, link to fb.
      'fba' => $tdata->fba,
      'fbu' => $tdata->fbu,
      'status' => $tdata->status,
      'token' => array(
        'data' => $tdata->access_token,
        'class' => 'fb_access_token',
        'style' => 'display:none;',
      ),
      'changed' => format_date($tdata->changed, 'short'),
      'operations' => array(
        'data' => array(
          '#theme' => 'links',
          '#attributes' => array(
            'class' => array(
              'links',
              'inline',
            ),
          ),
          '#links' => array(
            'debug' => array(
              'title' => t('debug'),
              'href' => 'https://developers.facebook.com/tools/debug/access_token',
              'query' => array(
                'q' => $tdata->access_token,
              ),
              'attributes' => array(
                'target' => '_blank',
              ),
            ),
            'access_token_info' => array(
              'title' => t('info'),
              'href' => 'https://graph.facebook.com/oauth/access_token_info',
              'query' => array(
                'access_token' => $tdata->access_token,
                'client_id' => $tdata->fba,
              ),
            ),
          ),
        ),
      ),
    );

    // Operations for App tokens only.
    if ($tdata->status & FB_STATUS_FLAG_APP) {
      $options[$tdata->access_token]['operations']['data']['#links']['settings'] = array(
        'title' => t('settings'),
        'href' => 'https://developers.facebook.com/apps/' . $tdata->fba,
      );
      $options[$tdata->access_token]['operations']['data']['#links']['explore'] = array(
        'title' => t('explore'),
        'href' => 'https://developers.facebook.com/tools/explorer/' . $tdata->fba,
        'query' => array(
          'method' => 'GET',
          'path' => 'me',
        ),
      );
    }

    // Use details from cache, if available.
    $fba_data = unserialize($tdata->fba_data);
    $fbu_data = unserialize($tdata->fbu_data);
    if (!empty($fba_data)) {
      $options[$tdata->access_token]['fba'] = array(
        'data' => array(
          '#type' => 'link',
          '#title' => '<img src="' . $fba_data['icon_url'] . '"/>&nbsp;' . $fba_data['name'],
          '#href' => $fba_data['link'],
          '#suffix' => '&nbsp;(' . $tdata->fba . ')',
          '#options' => array(
            'html' => TRUE,
          ),
        ),
      );
    }
    if (!empty($fbu_data)) {
      $options[$tdata->access_token]['fbu'] = array(
        'data' => array(
          '#type' => 'link',
          '#title' => fb_get_name($fbu_data),
          '#href' => fb_get_link($fbu_data),
          '#suffix' => '&nbsp;(' . $tdata->fbu . ')',
        ),
      );
    }

    // Application tokens don't really have a user.
    if ($tdata->fba == $tdata->fbu) {
      $options[$tdata->access_token]['fbu'] = t('N/A');
    }
  }

  // Highlight current token.

  /*
  if (($user_token = fb_user_token()) &&
      !empty($options[$user_token])) {
    // TODO: find a better way to do this.
    $options[$user_token]['fbu'] .= '&nbsp;' . t('You!');
  }
  */
  $form['tokens'] = array(
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $options,
    '#empty' => t('No tokens found.'),
  );
  $form['toggle_tokens'] = array(
    '#type' => 'markup',
    '#markup' => '<a href=# onclick="jQuery(\'.fb_access_token\').toggle(); return false;">show/hide tokens</a>',
  );
  $form['pager'] = array(
    '#markup' => theme('pager'),
  );
  return $form;
}