You are here

facebook_wall.module in Facebook Wall 7

Get the current facebook wall posts of the given user or page.

File

facebook_wall.module
View source
<?php

/**
 * @file
 * Get the current facebook wall posts of the given user or page.
 */

/**
 * Implements hook_help().
 */
function facebook_wall_help($path) {
  if ($path == 'admin/help#facebook_wall') {
    $help = '<p>' . t("This module allows you to fetch your facebook wall post from your given FB account username or userid. To get started, you must first connect with your FB account for the purpose of getting a valid access token from FB. Once connected, you can create a content block and show it on site anywhere you want and also it provide a default page (/faceboook_wall) to display the wall post.") . '</p>' . '<ul>' . '<li>' . l(t('Create a new FB App in Facebook'), 'https://developers.facebook.com/apps') . '</li>' . '<li>' . l(t('Get valid Access Token from Facebook'), 'https://developers.facebook.com/tools/explorer') . '</li>' . '<li>' . l(t('View Facebook Wall post'), 'facebook_wall') . '</li>' . '</ul>';
    return $help;
  }
}

/**
 * Implements hook_menu().
 */
function facebook_wall_menu() {
  $menu['admin/config/services/facebook_wall'] = array(
    'title' => 'Facebook Wall settings',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'facebook_wall_settings_form',
    ),
    'access callback' => 'user_access',
    'access arguments' => array(
      'administer facebook wall',
    ),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'facebook_wall.admin.inc',
  );
  $menu['admin/config/services/facebook_wall/settings'] = array(
    'title' => 'Facebook Settings',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'facebook_wall_settings_form',
    ),
    'access callback' => 'user_access',
    'access arguments' => array(
      'administer facebook wall',
    ),
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => 0,
    'file' => 'facebook_wall.admin.inc',
  );
  $menu['admin/config/services/facebook_wall/page_info'] = array(
    'title' => 'Page Information',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'facebook_wall_page_info',
    ),
    'access callback' => 'user_access',
    'access arguments' => array(
      'administer facebook wall',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => 2,
    'file' => 'facebook_wall.admin.inc',
  );
  $menu['facebook_wall'] = array(
    'title' => variable_get('facebook_wall_menu_name', 'Facebook Wall Post'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'facebook_wall_display_posts',
    ),
    'access callback' => 'user_access',
    'access arguments' => array(
      'facebook wall',
    ),
    'file' => 'facebook_wall.pages.inc',
    'type' => MENU_CALLBACK,
    'weight' => 3,
  );
  return $menu;
}

/**
 * Implements hook_permission().
 */
function facebook_wall_permission() {
  return array(
    'administer facebook wall' => array(
      'title' => t('Facebook Wall Settings'),
      'description' => t('Permission for Facebook Wall settings'),
    ),
    'facebook wall' => array(
      'title' => t('Facebook Wall Display'),
      'description' => t('Permission for Facebook Wall'),
    ),
  );
}

/**
 * Implements hook_block_info().
 */
function facebook_wall_block_info() {
  $block['facebook_wall'] = array(
    'info' => t('Facebook Wall'),
  );
  $block['facebook_wall_page_likes'] = array(
    'info' => t('Facebook Page Likes'),
  );
  return $block;
}

/**
 * The access token to use.
 *
 * @return string
 *   The access token to use.
 */
function facebook_wall_access_token() {
  $token = variable_get('facebook_wall_access_token');

  // If there's no token defined yet, use a temp token.
  if (empty($token)) {
    $token = variable_get('facebook_wall_access_token_temp');
  }
  return $token;
}

/**
 * The base URL for all Graph API requests.
 *
 * @return string
 *   The base URL for all Graph API requests.
 */
function facebook_wall_base_url() {
  return 'https://graph.facebook.com/v3.2/';
}

/**
 * Implements hook_block_view().
 */
function facebook_wall_block_view($data = '') {
  $block = array();
  if ($data == 'facebook_wall_page_likes') {
    $block['subject'] = t('Facebook Page Likes');
    $block['content'] = _facebook_wall_page_likes();
  }
  elseif ($data == 'facebook_wall') {
    $api_url = facebook_wall_build_feed_url();
    $block['subject'] = t('Facebook Wall');

    // HTML Contains FB current wall post.
    $block['content'] = _facebook_wall_theme_html_content($api_url);
  }
  return $block;
}

/**
 * Generate a Graph API URL string for a page's feed.
 *
 * @return string
 *   A complete Graph API URL for a page's feed.
 */
function facebook_wall_build_feed_url() {
  $url = facebook_wall_base_url() . variable_get('facebook_wall_page_url', 'me') . '/feed';
  $url .= '?access_token=' . facebook_wall_access_token();
  $url .= '&fields=';
  $url .= 'actions,';
  $url .= 'caption,';
  $url .= 'comments{from,message,created_time,fan_count},';
  $url .= 'created_time,';
  $url .= 'description,';
  $url .= 'id,';
  $url .= 'likes{id,name},';
  $url .= 'link,';
  $url .= 'message,';
  $url .= 'name,';
  $url .= 'picture,';
  $url .= 'source,';
  $url .= 'story,';
  $url .= 'type';
  $url .= '&limit=' . (int) variable_get('facebook_wall_post_limit', 10);
  return $url;
}

/**
 * Build the URL to obtain the picture for the requested user.
 *
 * @param string $user_id
 *   The user ID to check.
 *
 * @return string
 *   A complete Graph API URL for a user's picture.
 */
function facebook_wall_build_picture_url($user_id) {
  $url = facebook_wall_base_url() . $user_id;
  $url .= '?access_token=' . facebook_wall_access_token();
  $url .= '&fields=picture';
  return $url;
}

/**
 * Build the URL to obtain the number of links for the system page.
 *
 * @return string
 *   A complete Graph API URL for a page's like.
 */
function facebook_wall_build_likes_url() {
  $url = facebook_wall_base_url() . variable_get('facebook_wall_page_url', 'me') . '/likes';
  $url .= '?access_token=' . facebook_wall_access_token();
  $url .= 'fields=link,name,picture';
  return $url;
}

/**
 * Implements hook_theme().
 */
function facebook_wall_theme() {
  return array(
    'facebook_wall' => array(
      'variables' => array(
        'node' => NULL,
      ),
      'template' => 'facebook-wall',
    ),
  );
}

/**
 * Generate object array of FB Graph API content via CURL request.
 *
 * @param string $url
 *   FB graph api url with access token and fields.
 *
 * @return array
 *   Object array containing FB data.
 */
function _facebook_wall_graph_api_content($url) {
  $response = drupal_http_request($url);
  if ($response->code == 200) {
    return json_decode($response->data);
  }
  elseif (isset($response->data, $response->data->error, $response->data->error->message)) {
    watchdog('facebook wall', $response->data->error->message, array(), WATCHDOG_ERROR);
  }
  else {
    watchdog('facebook wall', '<pre>' . print_r($response, TRUE) . '</pre>', array(), WATCHDOG_ERROR);
  }
}

/**
 * Generate error message and fetchs FB content.
 *
 * @param string $api_url
 *   FB graph api url with access token and fields.
 *
 * @return array
 *   Array of FB content on success data fetch and FALSE if it isn't.
 */
function _facebook_wall_error_msg($api_url = FALSE) {

  // Checks for FB access token exists.
  $message_display = variable_get('facebook_wall_message_show', 1);
  if (facebook_wall_access_token() == '') {
    $message = t('Please login to your facebook account and generate a valid access token. Click here to get') . ' ' . l(t('valid Access Token.'), 'admin/config/content/facebook_wall/settings');
    watchdog('facebook wall', $message, array(), WATCHDOG_DEBUG);
    if ($message_display) {
      drupal_set_message($message, 'warning');
    }
  }
  elseif (variable_get('facebook_wall_page_url', 'me') == '') {
    $message = t('Please enter your Facebook page username or ID. Click here to get') . ' ' . l(t('Facebook Page URL.'), 'admin/config/content/facebook_wall/settings');
    watchdog('facebook wall', $message, array(), WATCHDOG_DEBUG);
    if ($message_display) {
      drupal_set_message($message, 'warning');
    }
  }
  elseif ($api_url != FALSE) {

    // Fetching content from FB via CURL request.
    $fb_content = _facebook_wall_graph_api_content($api_url);
    if (!isset($fb_content)) {
      $message = t('Not able to connect with Facebook');
      watchdog('facebook wall', $message, array(), WATCHDOG_DEBUG);
      if ($message_display) {
        drupal_set_message($message, 'warning');
      }
    }
    elseif (isset($fb_content->error)) {
      $message = $fb_content->error->type . ' : ' . $fb_content->error->code . ' ! ' . $fb_content->error->message;
      watchdog('facebook wall', $message, array(), WATCHDOG_DEBUG);
      if ($message_display) {
        drupal_set_message($message, 'warning');
      }
    }
    else {

      // Arrary return from FB on success.
      return $fb_content;
    }
  }
  return FALSE;
}

/**
 * Generate HTML content from template file.
 *
 * @param string $api_url
 *   FB graph api url with access token and fields.
 *
 * @return array
 *   Array of FB content on success data fetch and FALSE if it isn't.
 */
function _facebook_wall_theme_html_content($api_url) {

  // Contains FB current wall post.
  $wall_post = _facebook_wall_error_msg($api_url);
  if ($wall_post != FALSE && isset($wall_post->data[0])) {
    variable_set('facebook_wall_post_pagging_next', $wall_post->paging->next);

    // Contains FB user basic informaion.
    $basic_page = _facebook_wall_page_info_array();
    if ($basic_page != FALSE) {

      // Sending two FB array to template file.
      return theme('facebook_wall', array(
        'facebook_wall' => $wall_post,
        'facebook_page' => $basic_page,
      ));
    }
  }
  else {
    return FALSE;
  }
}

/**
 * Get FB user profile picture with given FB user id.
 *
 * @param string $user_id
 *   FB particular user ID.
 *
 * @return array
 *   Merge array of FB page basic with profile picture and FALSE if it isn't.
 */
function _facebook_wall_profile_picture($user_id) {

  // FB Graph API CURL calls request.
  $api_url = facebook_wall_build_picture_url($user_id);
  $profile_picture = _facebook_wall_error_msg($api_url);
  if ($profile_picture != FALSE) {
    return $profile_picture->picture->data->url;
  }
  else {
    return FALSE;
  }
}

/**
 * Admin configure form for Facebook Page Likes.
 */
function _facebook_wall_page_likes() {

  // FB Graph API CURL calls request.
  $user_likes = _facebook_wall_error_msg(facebook_wall_build_likes_url());
  if ($user_likes != FALSE) {

    // Gettings people likes list.
    for ($i = 0; $i < count($user_likes->data); $i++) {
      $rows[] = array(
        l($user_likes->data[$i]->name, $user_likes->data[$i]->link) . '<img src=' . $user_likes->data[$i]->picture->data->url . '" width="50" />',
      );
    }
    $header = array(
      'key' => array(
        'data' => t('Username'),
        'field' => 'role',
        'sort' => 'desc',
      ),
      'value' => array(
        'data' => t('Picture'),
      ),
    );
    $variables = array(
      'header' => $header,
      'rows' => $rows,
      'attributes' => array(),
      'sticky' => FALSE,
      'caption' => '',
      'colgroups' => array(),
      'empty' => t('No records available!'),
    );
    return theme('table', $variables);
  }
}

/**
 * Function to set video auto play setting .
 */
function _facebook_video_settings($video_scouce) {
  if (variable_get('facebook_wall_autoplay_video', '0')) {
    $op = 'true';
  }
  else {
    $op = 'false';
  }
  if (stristr($video_scouce, 'autoplay=1')) {
    $video_scouce = str_replace('autoplay=1', 'autoplay=' . variable_get('facebook_wall_autoplay_video', '0'), $video_scouce);
  }
  elseif (stristr($video_scouce, '?autoplay=0')) {
    $video_scouce = str_replace('?autoplay=0', '?autoplay=' . variable_get('facebook_wall_autoplay_video', '0'), $video_scouce);
  }
  elseif (stristr($video_scouce, 'auto_play=true')) {
    $video_scouce = str_replace("auto_play=true", "auto_play={$op}", $video_scouce);
  }
  elseif (stristr($video_scouce, 'auto_play=false')) {
    $video_scouce = str_replace("auto_play=false", "auto_play={$op}", $video_scouce);
  }
  else {
    $video_scouce = '';
  }
  return $video_scouce;
}

/**
 * Generate basic FB page or user information content.
 *
 * @return array
 *   Array of FB page basic on success and FALSE if it isn't.
 */
function _facebook_wall_page_info_array() {

  // FB Graph API CURL calls request.
  $api_url = facebook_wall_base_url() . variable_get('facebook_wall_page_url', 'me');
  $api_url .= '?access_token=' . facebook_wall_access_token();
  return _facebook_wall_error_msg($api_url);
}

Functions

Namesort descending Description
facebook_wall_access_token The access token to use.
facebook_wall_base_url The base URL for all Graph API requests.
facebook_wall_block_info Implements hook_block_info().
facebook_wall_block_view Implements hook_block_view().
facebook_wall_build_feed_url Generate a Graph API URL string for a page's feed.
facebook_wall_build_likes_url Build the URL to obtain the number of links for the system page.
facebook_wall_build_picture_url Build the URL to obtain the picture for the requested user.
facebook_wall_help Implements hook_help().
facebook_wall_menu Implements hook_menu().
facebook_wall_permission Implements hook_permission().
facebook_wall_theme Implements hook_theme().
_facebook_video_settings Function to set video auto play setting .
_facebook_wall_error_msg Generate error message and fetchs FB content.
_facebook_wall_graph_api_content Generate object array of FB Graph API content via CURL request.
_facebook_wall_page_info_array Generate basic FB page or user information content.
_facebook_wall_page_likes Admin configure form for Facebook Page Likes.
_facebook_wall_profile_picture Get FB user profile picture with given FB user id.
_facebook_wall_theme_html_content Generate HTML content from template file.