You are here

function fb_permission_form_user_profile_form_alter in Drupal for Facebook 7.3

Implements hook_form_FORM_ID_alter()

File

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

Code

function fb_permission_form_user_profile_form_alter(&$form, &$form_state) {
  $category = $form['#user_category'];
  $account = $form_state['user'];

  // See if the category corresponds to a facebook app.
  $fb_app = fb_get_app(array(
    'label' => $category,
  ));
  if ($fb_app) {
    $fb = fb_api_init($fb_app);
    $map = fb_permission_map($fb);

    // All known permissions.
    // Show only permissions we've configured for this app.
    $fb_app_data = fb_get_app_data($fb_app);
    $fb_permission_data = $fb_app_data['fb_permission'];
    if (is_array($fb_permission_data['map'])) {
      foreach ($fb_permission_data['map'] as $key => $value) {
        if (!$value) {
          unset($map[$key]);
        }
      }
    }

    /* Should not be necessary to wrap in serverfbml, as prompt-permission is documented as XFBML.
       $form = array(
         'perms' => array(
           '#prefix' => '<fb:serverfbml><script type="text/fbml><fb:fbml>',
           '#suffix' => '</fb:fbml></script></fb:serverfbml>',
         ),
       );
       */
    $t_args = array(
      '%application' => $fb_app->title,
    );
    $weight = 0;
    foreach ($map as $key => $text) {
      $has_perm = FALSE;
      try {
        $has_perm = fb_call_method($fb, 'users.hasAppPermission', array(
          'ext_perm' => $key,
          'uid' => fb_get_fbu($account->uid, $fb_app),
        ));
      } catch (Exception $e) {
        fb_log_exception($e, t('Call to users.hasAppPermission(%key) failed.', array(
          '%key' => $key,
        )));
      }
      if (!$has_perm) {
        if ('facebook has fixed fb:prompt-permission bugs' == FALSE) {

          // Ideally, fb:prompt-permission would work.  But it doesn't.
          $form['perms'][$key] = array(
            '#type' => 'markup',
            '#value' => '<fb:prompt-permission perms="' . $key . '">' . t($text, $t_args) . '</fb:prompt-permission>',
            '#weight' => $weight++,
            '#suffix' => "<br/>\n",
          );
        }
        else {

          // Send user to facebook's extended permission page.
          // This is weak.  Would be better to use javascript or fb:prompt-permission.
          $url = url(current_path(), array(
            'absolute' => TRUE,
          ));
          $form['perms'][$key] = array(
            '#type' => 'markup',
            '#value' => l(t($text, $t_args), "http://www.facebook.com/authorize.php", array(
              'html' => TRUE,
              'query' => array(
                'api_key' => $fb_app->apikey,
                'v' => '1.0',
                'ext_perm' => $key,
                'next' => $url,
                'next_cancel' => $url,
              ),
            )),
            '#prefix' => '<p>',
            '#suffix' => '</p>',
            '#weight' => $weight++,
          );
        }
      }
    }
    $form['description'] = array(
      '#type' => 'markup',
      '#value' => l(t('All settings for %application (and other Facebook Applications).', array(
        '%application' => $fb_app->title,
      )), 'http://www.facebook.com/editapps.php', array(
        'html' => TRUE,
      )),
      '#prefix' => '<p>',
      '#suffix' => "</p>\n",
      '#weight' => $weight++,
    );
    unset($form['submit']);
    unset($form['delete']);
  }
}