You are here

function fb_feed_action_publish in Drupal for Facebook 5.2

File

./fb_feed.module, line 722
Helpers for Facebook feeds (http://wiki.developers.facebook.com/index.php/New_Design_Feed_Wall)

Code

function fb_feed_action_publish(&$context, $values = array()) {

  //dpm(func_get_args(), 'fb_actions_minifeed');

  // Get the objects we're acting upon.
  $objects = array();
  if ($values['hook'] == 'nodeapi') {
    $objects['node'] = $values['node'];
  }
  else {
    if ($values['hook'] == 'comment') {
      $objects['comment'] = $values['comment'];
      $objects['node'] = node_load($values['comment']->nid);
    }
    else {
      if ($values['hook'] == 'user') {
        $account = $values['user'];
        $objects['user'] = $account;
      }
    }
  }

  // Get the template
  $template = node_load($values['fb_feed_template_nid']);

  // TODO: Sanity check that bundle has been registered with Facebook.
  // And the app
  $fb_app = fb_get_app(array(
    'nid' => $template->fb_app_nid,
  ));
  $objects['fb_app'] = $fb_app;

  // TODO: Sanity check that apikeys match.
  // Log into facebook as the current user.
  if ($GLOBALS['fb_app'] && ($fb_app->nid = $GLOBALS['fb_app']->nid)) {

    // We're in a canvas page for the desired app.  We're already logged in.
    $fb = $GLOBALS['fb'];
  }
  else {
    global $user;

    // Must log in to publish user action.  This will only work if the user
    // have authorized us for offline access.
    $fbu = fb_get_fbu($user->uid, $fb_app);
    if ($fbu) {
      $fb = fb_api_init($fb_app, $fbu);
    }
  }

  // It's possible we have an $fb, but session may not be valid.  TODO: find a
  // way to test this?
  if ($fb) {

    // We need to pass a bunch of parameters to Feed.publishUserAction.
    $params = array(
      'bundle_id' => $template->bundle_id,
      'tokens' => array(),
      'target_ids' => array(),
      'body_general' => '',
      'do_publish' => TRUE,
    );
    $options = array(
      'fb_feed_template' => $template,
    );
    if ($values['token_enable']) {

      // Use tokens for every kind of object we know about
      foreach (array(
        'node',
        'comment',
        'user',
        'fb_app',
      ) as $type) {
        if ($objects[$type]) {
          $toks = token_get_values($type, $objects[$type], FALSE, $options);

          //watchdog('fb_feed_debug', "token_get_values($type) returned " . dprint_r($toks, 1));
          if ($toks && is_array($toks->tokens)) {
            foreach ($toks->tokens as $i => $key) {
              $params['tokens'][$key] = $toks->values[$i];
            }
          }
        }
      }
    }

    // Use naming conventions to allow tokens to provide every parameter.
    if ($params['tokens']['target_ids']) {
      $params['target_ids'][] = $params['tokens']['target_ids'];
    }
    if ($params['tokens']['body_general']) {
      $params['body_general'] = $params['tokens']['body_general'];
    }

    // Invoke a hook so that other modules have a chance to modify the params before we pass them to facebook.
    $params = fb_feed_invoke($fb_app, FB_FEED_OP_TOKEN_ALTER, $params, array(
      'fb_feed_template' => $template,
      'objects' => $objects,
      'context' => $context,
    ));
    if (fb_verbose()) {
      watchdog('fb_feed', t("Publish user action, app is %app, bundle is %bundle_id, params are !params.", array(
        '%app' => $fb_app->title,
        '%bundle_id' => $template->bundle_id,
        '!params' => dprint_r($params, 1),
      )));
    }
    if (is_array($params['tokens']) && $params['do_publish']) {
      if (is_array($params['target_ids'])) {
        $target_ids = implode(',', $params['target_ids']);
      }
      else {
        $target_ids = $params['target_ids'];
      }
      try {

        // http://wiki.developers.facebook.com/index.php/Feed.publishUserAction

        //if (FALSE) // disabled for debugging
        $fb->api_client
          ->feed_publishUserAction($template->bundle_id, json_encode($params['tokens']), $target_ids, $params['body_general']);
      } catch (Exception $e) {

        // We can get a lot of "Feed action limit reached" exceptions.  So
        // only log if fb_verbose is true.
        if ($e
          ->getCode() != 341 || fb_verbose()) {
          fb_log_exception($e, t('Failed to publish user action'));
        }
      }
    }
  }
}