You are here

function fb_form_multi_add_invite_form in Drupal for Facebook 7.3

Same name and namespace in other branches
  1. 5.2 fb_form.module \fb_form_multi_add_invite_form()
  2. 5 fb_form.module \fb_form_multi_add_invite_form()
  3. 6.3 fb_form.module \fb_form_multi_add_invite_form()
  4. 6.2 fb_form.module \fb_form_multi_add_invite_form()

Create a form allowing the user to invite friends to add the app.

Facebook provides a very specific way to build this form using FBML. This will only display properly on FBML canvas pages. The FBML for this form requires the <fb:request-form> tag where the <form> tag would normally be. Also the form provides its own buttons. These two things make it difficult to use Drupal's Form API to build the form. Still, we use FAPI, because we want modules to be able to alter the form (i.e. to add descriptive text). Alteration that rely on #submit or even additional input fields will probably not work properly, however.

1 string reference to 'fb_form_multi_add_invite_form'
fb_form_invite_page in ./fb_form.module
Create a page to invite friends to add an app.

File

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

Code

function fb_form_multi_add_invite_form() {
  global $_fb, $_fb_app;

  // TODO: confirm that we're displaying an FBML canvas page.
  if ($fbu = fb_facebook_user($_fb)) {

    // Exclude friends who have already installed app.
    // http://wiki.developers.facebook.com/index.php/Fb:request-form
    $rs = fb_fql_query($_fb, "SELECT uid FROM user WHERE has_added_app=1 and uid IN (SELECT uid2 FROM friend WHERE uid1 = {$fbu})");

    // FQL, no {curly_brackets}!
    $arFriends = "";

    //  Build an delimited list of users...
    if ($rs) {
      $arFriends .= $rs[0]["uid"];
      for ($i = 1; $i < count($rs); $i++) {
        if ($arFriends != "") {
          $arFriends .= ",";
        }
        $arFriends .= $rs[$i]["uid"];
      }
    }
  }

  // Use node body in invite message.
  $node = node_load($_fb_app->nid);
  $node = node_prepare($node);
  $content = $node->teaser;

  // Do we need to append &next=[someURL] to the url here?
  $content .= "<fb:req-choice url=\"http://www.facebook.com/add.php?api_key={$_fb_app->apikey}\" label=\"" . t('Add !title application.', array(
    '!title' => $_fb_app->label,
  )) . "\" />";

  // form type fb:request-form
  $form = array(
    '#fb_form_type_hack' => 'fb_form_request',
    /* becomes #type during form_alter */
    '#attributes' => array(
      'type' => $_fb_app->label,
      'content' => htmlentities($content),
      'invite' => 'true',
    ),
    '#action' => fb_protocol() . '://apps.facebook.com/' . $_fb_app->canvas,
  );
  $form['friends'] = array(
    '#type' => 'fb_form_request_selector',
    '#title' => t('Select the friends to invite.'),
    '#attributes' => array(
      'exclude_ids' => $arFriends,
    ),
  );
  return $form;
}