You are here

function fb_permission_form_alter in Drupal for Facebook 7.3

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

Implements hook_form_alter().

File

contrib/fb_permission.module, line 258
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) {

  // 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('Many permissions on the lists below <em>will not actually work!</em> This list is generated automatically, by querying facebook for all known extended permissions.  Some of these are not supported or available only to privileged developers.<br/>  <strong>Choose only <a target=_blank href=http://developers.facebook.com/docs/authentication/permissions/>permissions your application is actually allowed to use</a>.  If you see an "invalid permission" error, return here and un-select that permission.</strong>'),
    );
    if ($fb_app && $fb_app->id) {
      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.'),
        );
      }
    }
    else {

      // App not yet configured.
      $form['fb_app_data']['fb_permission']['#description'] = t('First save this form, then click "edit" to return here and see extended permission options.');
      $form['fb_app_data']['fb_permission']['#collapsed'] = FALSE;
    }
  }
}