You are here

function fb_graph_publish_action in Drupal for Facebook 7.3

Same name and namespace in other branches
  1. 6.3 fb_graph.module \fb_graph_publish_action()

Helper function to publish user activity to Facebook's Open Graph.

1 call to fb_graph_publish_action()
fb_graph_fb in ./fb_graph.module
Implements hook_fb().
1 string reference to 'fb_graph_publish_action'
fb_graph_fb in ./fb_graph.module
Implements hook_fb().

File

./fb_graph.module, line 87
Open Graph Helpers

Code

function fb_graph_publish_action($action, $params, $options = array()) {
  if (!user_access(FB_GRAPH_PERM_PUBLISH)) {
    if (fb_verbose()) {

      // Because custom modules call this function and can get tripped up by permissions, log a message.
      watchdog('fb_graph', 'User does not have permission to publish facebook action.', array(), WATCHDOG_WARNING);
    }
    return;
  }

  // Defaults.
  if (isset($options['fb_app'])) {
    $fb_app = $options['fb_app'];
  }
  else {
    $fb_app = $GLOBALS['_fb_app'];
  }
  if (isset($options['fbu'])) {
    $fbu = $options['fbu'];
  }
  else {
    $fbu = fb_facebook_user();
  }
  if (!$fbu || !$fb_app) {

    // We don't have enough information.
    return;
  }
  if (isset($options['action_type'])) {
    $action_type = $options['action_type'];

    // Human readable verb.
  }
  else {
    $action_type = $action;
  }
  if (!isset($params['access_token'])) {
    $fb = fb_api_init($fb_app);
    $params['access_token'] = fb_get_token($fb, $fbu);
  }
  try {

    // @TODO: handle apps that have no canvas page.  how???
    // Note fb_graph() is in fb.module.
    $result = fb_graph($fbu . '/' . $fb_app->canvas . ':' . $action, $params, 'POST');
    if (isset($result['id'])) {
      $id = $result['id'];
      $message_args = array(
        '!delete_url' => url(FB_GRAPH_PATH_DELETE . '/' . $id, array(
          'destination' => request_path(),
        )),
        '%action_type' => $action_type,
        '!profile_url' => 'https://www.facebook.com/' . $fbu,
      );
      if (variable_get(FB_GRAPH_VAR_MESSAGES, TRUE)) {
        $message = t('Published %action_type activity to <a href=!profile_url>Facebook</a>.', $message_args);
        if (user_access(FB_GRAPH_PERM_DELETE_OWN)) {
          $message .= '&nbsp;' . t('[<a href=!delete_url>delete from timeline</a>]');
        }
        drupal_set_message($message);
      }
      return $id;
    }
    else {
      if (fb_verbose()) {
        watchdog('fb_graph', 'Failed to publish %action by user %fbu to facebook.  Facebook did not return an ID!', array(
          '%action' => $action,
          '%fbu' => $fbu,
        ), WATCHDOG_WARNING);
      }
    }
  } catch (Exception $e) {
    fb_log_exception($e, t('Failed to publish %action_type action', array(
      '%action_type' => $action_type,
    )));

    //$args = func_get_args();

    //watchdog('fb_graph', __FUNCTION__ . '<pre>' . dprint_r($args, 1) . '</pre>'); // debug
  }
}