You are here

function action_fb_write_minifeed in Drupal for Facebook 5

Implementation of an Action. See Actions module.

This action publishes to a user's mini-feed.

File

./fb_actions.module, line 443
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_write_minifeed($op, $edit = array(), $node) {
  if ($op == 'metadata') {
    return array(
      'description' => t('Post to Facebook mini-feed'),
      'type' => t('Facebook'),
      'batchable' => FALSE,
      'configurable' => TRUE,
    );
  }
  else {
    if ($op == 'form') {
      $options = fb_get_app_options(TRUE);
      $form['description'] = array(
        '#value' => t('Note that this action will only succeed when executed from a Facebook canvas page when the user is logged in, or on non-canvas pages when the logged in user has an infinite session key.  These are privacy restrictions enforced by the Facebook API.'),
      );
      $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 action?  If %current, action will only be performed only on canvas pages.', array(
          '%current' => $options[FB_APP_CURRENT],
        )),
      );
      $form['title'] = array(
        '#type' => 'textfield',
        '#title' => t('Title'),
        '#default_value' => $edit['title'],
        '#description' => t('Title line of mini-feed item.  (Most Facebook mini-feed items have only a title, and no message body).'),
        '#required' => TRUE,
      );
      $form['body'] = array(
        '#type' => 'textarea',
        '#title' => t('Body'),
        '#default_value' => $edit['body'],
        '#description' => t('Text to publish on mini-feed.  In many cases, only the title is required and this may be left blank.<br />The following token will be replaced: !token_help', array(
          '!token_help' => theme('token_help', 'node') . theme('token_help', 'fb_app'),
        )),
      );
      return $form;
    }
    else {
      if ($op == 'submit') {
        return array(
          'fb_app_nid' => $edit['fb_app_nid'],
          'body' => $edit['body'],
          'title' => $edit['title'],
        );
      }
      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) {
            global $user;

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

          // Even if we have an $fb now, it may not have permission to do all things
          // we ask of it.
          if ($fb) {

            // Replace both node and fb_app related tokens
            $title = token_replace($edit['title'], 'node', $node);
            $title = token_replace($title, 'fb_app', $fb_app);
            $body = token_replace($edit['body'], 'node', $node);
            $body = token_replace($body, 'fb_app', $fb_app);
            $fb->api_client
              ->feed_publishActionOfUser($title, $body);
            fb_report_errors($fb);
          }
        }
      }
    }
  }
}