You are here

function fb_element_validate_friend in Drupal for Facebook 7.4

File

./fb.module, line 657

Code

function fb_element_validate_friend(&$element, &$form_state, $form) {

  // @todo split by , for multiple names.
  if ($element['#value']) {

    // Parse names like "Full Name (user.name)"
    $split = preg_split('/[\\(\\)]/', $element['#value']);
    $name = trim($split[0]);
    $username = trim($split[1]);
    if (!$username) {
      form_error($element, t('Could not parse friend name %name.', array(
        '%name' => $name,
      )));
      return;
    }

    //$result = fb_fql('SELECT uid, name, username, is_app_user, pic_square FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me()) ORDER BY name ASC'

    // Facebook claims this will return a user object, but it returns only name and uid.

    //$result = fb_graph('me/friends/' . $username);
    $result = fb_graph_batch(array(
      $username,
      'me/friends/' . $username,
    ));
    $friend_data = $result['me/friends/' . $username]['data'];
    if (count($friend_data)) {

      // Yes, they are friends.
      // Array wrapper, because eventually this will support multiple names.
      form_set_value($element, array(
        $result[$username],
      ), $form_state);
    }
    else {
      form_error($element, t('Could not confirm friendship with %name.', array(
        '%name' => $name,
      )));
    }
  }
  else {

    // No value provided.
  }
}