You are here

function fb_permission_user in Drupal for Facebook 6.3

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

Implementation of hook_user.

File

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

Code

function fb_permission_user($op, &$edit, &$account, $category = NULL) {
  global $user;
  if ($op == 'categories') {
    $items = array();

    // A tab for each application
    foreach (fb_get_all_apps() as $fb_app) {

      // TODO: limit only to apps which can be added to a user's account.
      $items[] = array(
        'name' => $fb_app->label,
        'title' => $fb_app->title,
        'access callback' => 'fb_permission_access_own',
        'access arguments' => array(
          1,
          'edit own extended permissions',
        ),
        'weight' => 2,
      );
    }
    return $items;
  }
  elseif ($op == 'form') {

    // 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($_GET['q'], 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' => "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++,
      );

      // A value for hook_form_alter to find.
      $form['_fb_permission'] = array(
        '#type' => 'value',
        '#value' => TRUE,
      );
      return $form;
    }
  }
}