You are here

function fb_permission_form_alter in Drupal for Facebook 6.3

Same name and namespace in other branches
  1. 6.2 contrib/fb_permission.module \fb_permission_form_alter()
  2. 7.3 contrib/fb_permission.module \fb_permission_form_alter()

File

contrib/fb_permission.module, line 164
Code pertaining to Facebook's extended permissions. see http://wiki.developers.facebook.com/index.php/Extended_permissions

Code

function fb_permission_form_alter(&$form, $state, $id) {

  //dpm(func_get_args(), 'fb_permission_form_alter');
  if ($id == 'user_profile_form' && isset($form['_fb_permission'])) {
    unset($form['submit']);
    unset($form['delete']);
  }

  // Add settings to fb_app form
  if (isset($form['fb_app_data'])) {
    $fb_app = $form['#fb_app'];
    $fb = fb_api_init($fb_app);
    $fb_app_data = fb_get_app_data($fb_app);
    $fb_permission_data = isset($fb_app_data['fb_permission']) ? $fb_app_data['fb_permission'] : NULL;
    $form['fb_app_data']['fb_permission'] = array(
      '#type' => 'fieldset',
      '#title' => t('Facebook extended permissions'),
      '#tree' => TRUE,
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#description' => t('This list is generated automatically, by querying facebook for all known extended permissions.  Many of these will not actually work!  (When you have 500 million users, your code can suck.)<br/>  Choose only from <a target=_blank href=http://developers.facebook.com/docs/authentication/permissions/>the permissions your application is actually allowed to use</a>.  If you see an "invalid permission" error, return here and un-select that permission.'),
    );
    try {

      // http://developers.facebook.com/docs/reference/fql/permissions_info
      // Apparently, this query required an access token, so this won't work when creating an app, only when editing one.
      $result = fb_fql_query($fb, "SELECT permission_name, header, summary FROM permissions_info WHERE 1", array(
        'access_token' => fb_get_token($fb),
      ));
      foreach ($result as $data) {
        $options[$data['permission_name']] = $data['header'] . ' - ' . $data['summary'] . " ({$data['permission_name']})";
      }

      // First the perms that will be required.
      if (!isset($fb_permission_data['prompt'])) {
        $fb_permission_data['prompt'] = array();
      }
      $form['fb_app_data']['fb_permission']['prompt'] = array(
        '#type' => 'checkboxes',
        '#title' => t('Required extended permissions'),
        '#options' => $options,
        '#default_value' => $fb_permission_data['prompt'],
        '#description' => t('Prompt users when they first authorize the application.  Select only the most important features.'),
      );

      // Next the optional perms.
      if (!isset($fb_permission_data['map'])) {
        $fb_permission_data['map'] = array();
      }
      $form['fb_app_data']['fb_permission']['map'] = array(
        '#type' => 'checkboxes',
        '#title' => t('Optional extended permissions'),
        '#options' => $options,
        '#default_value' => $fb_permission_data['map'],
        '#description' => t('Which extended permissions does this application use?  Users will be able to grant these permissions on their user edit pages.'),
      );
    } catch (Exception $e) {
      if ($fb_app->apikey) {

        // If there's an apikey, this is an unexpected error.
        drupal_set_message(t('Failed to get extended permissions.  %msg', array(
          '%msg' => $e
            ->getMessage(),
        )), 'warning');
      }

      // Drupal will not display this properly in the collapsed fieldset.  Not sure how to fix!
      $form['fb_app_data']['fb_permission']['msg'] = array(
        '#type' => 'markup',
        '#value' => t('Failed to get extended permissions from facebook.   Submit this form, then click "edit" to see extended permission options.'),
      );
    }
  }
}