Skip to content

Instantly share code, notes, and snippets.

@timani
Created December 15, 2011 23:53
Show Gist options
  • Save timani/1483564 to your computer and use it in GitHub Desktop.
Save timani/1483564 to your computer and use it in GitHub Desktop.
Use the open graph API to post to the Drupal Facebook style status (FBSS) module
<?php
/**
* @file
* Allows posting statuses from Drupal to Facebook.
*
*/
/**
* Implementation of hook_menu().
*/
function fbss2fb_menu() {
$items = array();
$items['admin/settings/facebook_status/facebook'] = array(
'title' => 'Facebook',
'page callback' => 'drupal_get_form',
'page arguments' => array('fbss2fb_admin'),
'access arguments' => array('administer Facebook-style Statuses settings'),
'description' => 'Allows administrators to adjust Facebook integration settings for Facebook-style Statuses.',
'type' => MENU_LOCAL_TASK,
'file' => 'fbss2fb.admin.inc',
);
return $items;
}
/**
* Implementation of hook_perm().
*/
function fbss2fb_perm() {
return array('update status to Facebook');
}
/**
* Implementation of hook_user().
*/
function fbss2fb_user($op, &$edit, &$account, $category = NULL) {
global $user, $_fb_app;
if ($op == 'form' && $category == 'fb_user') {
$account = user_load(array('uid' => arg(1)));
if (!user_access('update status to Facebook', $account))
return; // hide from this user
$fb_user_data = _fb_user_get_config($_fb_app);
$fbu = _fb_user_get_fbu($account->uid);
if ($fbu) {
$data = unserialize($account->data);
$default_setting = variable_get('fbss2fb_default', 'off');
if ($default_setting != 'disallow') {
if ($default_setting == 'on-user' || $default_setting == 'off-user') {
if (!isset($data['fbss2fb_update_status'])) {
$data['fbss2fb_update_status'] = 0;
if ($default_setting == 'on-user') {
$data['fbss2fb_update_status'] = 1;
}
}
$form['fbss2fb_update_status'] = array(
'#type' => 'checkbox',
'#title' => t('Enable "Post to Facebook" checkbox for status updates by default'),
'#default_value' => $data['fbss2fb_update_status'],
'#weight' => 5,
);
}
}
}
return $form;
}
elseif ($op == 'update' && $category == 'fb_user' && isset($edit['fbss2fb_update_status'])) {
user_save($account, array(
'fbss2fb_update_status' => $edit['fbss2fb_update_status']));
}
}
/**
* Implementation of hook_form_FORM_ID_alter().
*/
function fbss2fb_form_facebook_status_box_alter(&$form, $form_state) {
global $user;
$account = $user;
// Only show option if user has linked local account to Facebook ID and has permission to update status to Facebook.
if (fb_facebook_user() && fb_api_check_session($GLOBALS['_fb']) && variable_get('fbss2fb_update_status', 'off') != 'disallow' && user_access('update status to Facebook')) {
// Add checkbox to control posting the status to Facebook.
$form['fbss2fb_publish'] = array(
'#type' => 'checkbox',
'#title' => t('Post to Facebook'),
'#default_value' => _fbss2fb_update_status_get_default($account),
);
$form['fbss-submit']['#submit'][] = 'fbss2fb_facebook_status_box_submit';
}
}
function fbss2fb_facebook_status_box_submit(&$form, &$form_state) {
global $user;
global $base_root;
$new_status = trim($form_state['values']['fbss-status']);
$default = $form_state['values']['sdefault'];
//Don't send the status if it wasn't changed from the default.
if ($new_status != $default) {
if (!empty($form_state['values']['fbss2fb_publish'])) {
$sid = NULL;
if (isset($form_state['facebook_status']['sid'])) {
$sid = $form_state['facebook_status']['sid'];
}
$actions = array();
$actions[] = array('text' => t('See Status Stream'),
'href' => url($base_root . '/user/' . $user->uid, array('absolute' => TRUE)),
);
try {
$fb = $GLOBALS['_fb'];
$facebook_access_token = $fb->getAccessToken();
$params = array("message" => $new_status,"access_token" => $facebook_access_token);
fb_graph("feed", $params, $method = 'POST',$fb);
} catch (Exception $e) {
drupal_set_message("There was a problem posting to your wall. Please logout and login to set permissions.");
// The following should bring up a dialog box if extended permissions aren't set. @TODO not working with facebook_status/js
// fb_stream_publish_dialog(array('user_message' => $new_status,
// 'attachment' => $attachment,
// 'action_links' => $actions,
// ));
// print drupal_json(fb_stream_get_stream_dialog_data());
// exit;
}
}
}
}
/**
* Gets the default value for the "Post to Facebook" checkbox on the status
* update form.
*
* @param $account
* The user account object for the user whose checkbox will be displayed.
* @return
* The default value for the checkbox (1 or 0).
*/
function _fbss2fb_update_status_get_default($account) {
$data = unserialize($account->data);
$default_setting = variable_get('fbss2fb_default', 'off');
$return = 0;
if ($default_setting == 'on-user' || $default_setting == 'on') {
$return = 1;
}
if (($default_setting == 'on-user' || $default_setting == 'off-user') && isset($data['fbss2fb_update_status'])) {
return $data['fbss2fb_update_status'];
}
return $return;
}
/**
* Implementation of hook_facebook_status_form_ahah_alter().
*/
function fbss2fb_facebook_status_form_ahah_alter(&$new_form, $form) {
$new_form['facebook'] = $form['facebook'];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment