You are here

function fb_views_handler_author_is_friend in Drupal for Facebook 5.2

Same name and namespace in other branches
  1. 5 fb_views.module \fb_views_handler_author_is_friend()
1 string reference to 'fb_views_handler_author_is_friend'
fb_views_views_arguments in ./fb_views.module

File

./fb_views.module, line 51
Facebook-specific views arguments, filters, etc...

Code

function fb_views_handler_author_is_friend($op, &$query, $a1, $a2 = null) {

  //dpm(func_get_args(), "fb_views_handler_author_is_friend");

  // For convenience, allow 'current' for current user
  if ($a2 == 'current') {
    global $user;
    $a2 = $user->uid;
  }
  if ($op == 'summary') {

    // Ideally, we'd display all users who are known facebook users.  However, because the primary table is node, we can only show those who have actually posted content.
    $query
      ->add_table('fb_user_app', TRUE);
    $query
      ->add_where('fb_user_app.uid IS NOT NULL');
    $query
      ->add_table('users', true);
    $query
      ->add_field('name', 'users');
    $query
      ->add_field('uid', 'users');
    $fieldinfo['field'] = "users.name";
    return $fieldinfo;
  }
  else {
    if ($op == 'filter') {
      $fb_app = $GLOBALS['fb_app'];

      // Canvas pages only.
      if (!$fb_app) {
        $fb_app = fb_get_app(array(
          'nid' => $a1['options'],
        ));
      }
      $uid = intval($a2);
      $fbu = fb_get_fbu($uid, $fb_app);
      $friends = array();

      // On non-canvas pages, we have to work harder to get the facebook id.
      if ($uid && !$fbu) {

        // This code may never be reached.
        $result = db_query("SELECT fbu FROM {fb_user_app} WHERE uid='%d'", $uid);
        while ($data = db_fetch_object($result)) {
          $friends = array_merge($friends, fb_get_friends($data->fbu));
        }
      }
      else {
        $friends = fb_get_friends($fbu, $fb_app);
      }
      if (is_array($friends) && count($friends)) {
        $query
          ->add_table('fb_user_app', TRUE);
        $query
          ->add_where('fb_user_app.fbu IN (%s)', implode(',', $friends));
      }
      else {

        // No friends found.  How best to handle this?
        $query
          ->add_where('0');
      }
    }
    else {
      if ($op == 'link') {
        return l($query->name, "{$a2}/" . intval($query->uid));
      }
      else {
        if ($op == 'title') {
          if ($query == 'current') {
            global $user;
            $query = $user->uid;
          }
          $data = db_fetch_object(db_query("SELECT name FROM {users} WHERE uid = '%d'", $query));
          return check_plain($data->name);
        }
      }
    }
  }
}