You are here

function action_fb_set_profile_fbml in Drupal for Facebook 5

Implementation of an Action. See Actions module.

This action updates a user's profile FBML.

File

./fb_actions.module, line 517
Actions defined here interact with Facebook's API. This makes it possible to notify facebook of various activities as they happen.

Code

function action_fb_set_profile_fbml($op, $edit = array(), $obj) {
  if ($op == 'metadata') {
    return array(
      'description' => t('Facebook set profile FBML'),
      'type' => t('Facebook'),
      'batchable' => FALSE,
      'configurable' => TRUE,
    );
  }
  else {
    if ($op == 'form') {
      $options = fb_get_app_options(TRUE);
      $form['description'] = array(
        '#value' => t('This action will update the current user\'s Facebook profile.  Suitable for canvas pages and cron jobs (if infinite session is configured).'),
      );
      $form['fb_app_nid'] = array(
        '#type' => 'select',
        '#title' => t('Application'),
        '#default_value' => $edit['fb_app_nid'],
        '#options' => $options,
        '#description' => t('Log into Facebook as which application?  %current is OK when invoked from canvas pages or cron jobs.', array(
          '%current' => $options[FB_APP_CURRENT],
        )),
      );
      $form['body'] = array(
        '#type' => 'textarea',
        '#title' => t('Profile FBML'),
        '#default_value' => $edit['body'],
        '#description' => t('FBML to write to user\'s profile.<br />The following token will be replaced: !token_help', array(
          '!token_help' => theme('token_help', 'fb_app'),
        )),
      );
      return $form;
    }
    else {
      if ($op == 'submit') {
        return array(
          'fb_app_nid' => $edit['fb_app_nid'],
          'body' => $edit['body'],
        );
      }
      else {
        if ($op == 'do') {

          // Log into facebook as the current user.
          if ($fb_app = $GLOBALS['fb_app']) {
            if ($edit['fb_app_nid'] == FB_APP_CURRENT || $fb_app->nid == $edit['fb_app_nid']) {

              // We're in a canvas page for the desired app.  We're already logged in.
              $fb = $GLOBALS['fb'];
            }
          }
          if (!$fb && $edit['fb_app_nid'] != FB_APP_CURRENT) {

            // Need to log into this app.
            $fb_app = fb_get_app(array(
              'nid' => $edit['fb_app_nid'],
            ));
            $fb = fb_api_init($fb_app, FB_FBU_INFINITE_SESSION);
          }
          if ($fb) {
            global $user;
            $fbu = fb_get_fbu($user->uid, $fb_app);

            // Replace fb_app related tokens
            $body = token_replace($edit['body'], 'fb_app', $fb_app);

            // TODO: enable filters for body.
            // print("Setting facebook profile markup for $user->name to " . dpr($body, 1));
            $fb->api_client
              ->profile_setFBML($body, $fbu);
            fb_report_errors($fb);
          }
        }
      }
    }
  }
}