You are here

function fb_form_friend_selector_process in Drupal for Facebook 5.2

Same name and namespace in other branches
  1. 6.3 fb_form.module \fb_form_friend_selector_process()
  2. 6.2 fb_form.module \fb_form_friend_selector_process()
  3. 7.3 fb_form.module \fb_form_friend_selector_process()

A selector allowing the user to choose from their friends. This must behave differently depending on whether the form is displayed on an FBML canvas page, iframe canvas page, or regular HTML page.

File

./fb_form.module, line 349
This module defines facebook-specific form elements for use with Drupal's form API.

Code

function fb_form_friend_selector_process($orig) {

  // TODO: use fb:friend-selector on FBML pages.
  // TODO: support fb_app specified in element.  Perhaps using Facebook Connect.
  if (!$fb) {
    $fb = $GLOBALS['fb'];
  }

  // Global is set on canvas pages.
  if (!$fb) {

    // TODO: Generate an error.
    return;
  }
  static $options = NULL;
  if (!$options) {
    $query = "SELECT name, uid, pic_square FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1=" . fb_facebook_user() . ")";
    $result = $fb->api_client
      ->fql_query($query);

    // TODO: sort results by name

    //$options = array();

    //foreach ($result as $data) {

    //  $options[$data['uid']] = $data['first_name'] . ' ' . $data['last_name'];

    //}

    // Store list of friends in SESSION, so our autocomplete function will not
    // have to query it every time.
    $_SESSION['fb_form_friend_selector_result'] = $result;
  }
  $element = array(
    '#validate' => array(
      'fb_form_friend_selector_validate' => array(
        $orig,
      ),
    ),
  );
  foreach (array(
    '#title',
    '#parents',
    '#description',
    '#default_value',
    '#weight',
    '#multiple',
    '#required',
    '#name',
    '#value',
    '#id',
    '#size',
    '#rows',
    '#validate',
  ) as $key) {
    if (isset($orig[$key])) {
      $element[$key] = $orig[$key];
    }
  }

  // Allow use of textarea instead of textfield, autocomplete will not work.
  if (isset($orig['#rows']) && $orig['#rows'] > 0) {
    $element['#type'] = 'textarea';
  }
  else {
    $element['#type'] = 'textfield';
  }
  $element['#autocomplete_path'] = url('fb_form_friend_selector_autocomplete', NULL, NULL, TRUE);
  return $element;
}