You are here

social_content_facebook.module in Social Content 7

Same filename and directory in other branches
  1. 7.2 modules/facebook/social_content_facebook.module

Social Content: Facebook module.

File

modules/facebook/social_content_facebook.module
View source
<?php

/**
 * @file
 * Social Content: Facebook module.
 */
function social_content_facebook_social_content_info() {
  $info = array();
  $info['facebook'] = array(
    'title' => t('Facebook'),
    'settings_form' => 'social_content_facebook_settings_form',
    'content_type' => 'facebook',
    'external_id_field_mapping' => array(
      'id' => 'field_facebook_external_id',
    ),
    'data_callback' => 'social_content_facebook_data_callback',
    'post_callback' => 'social_content_facebook_post_callback',
    'additional_settings' => array(
      'user_id' => '',
      'graph_url' => 'https://graph.facebook.com',
      'access_token' => '',
      'min_resolution' => '',
    ),
  );
  return $info;
}

/**
 * Social content type Facebook settings form.
 *
 * @param $form
 *   Form API array, to be modified with additional settings.
 * @param $social_content_type
 *   Social content type object
 */
function social_content_facebook_settings_form(&$form, $social_content_type, $settings) {
  if ($social_content_type['name'] = 'facebook') {
    $form['user_id'] = array(
      '#type' => 'textfield',
      '#title' => t('Facebook Page User ID'),
      '#description' => t('Visit http://graph.facebook.com/<page_path> to obtain the user id.'),
      '#default_value' => $settings['user_id'],
      '#required' => TRUE,
    );
    $form['graph_url'] = array(
      '#type' => 'textfield',
      '#title' => t('Facebook Graph URL'),
      '#description' => t('The url to use to get facebook posts.'),
      '#default_value' => $settings['graph_url'],
      '#required' => TRUE,
    );
    $form['access_token'] = array(
      '#type' => 'textfield',
      '#title' => t('Access Token'),
      '#description' => t('This is required to interact with facebook. Use https://graph.facebook.com/oauth/access_token?client_id=APP_ID&client_secret=APP_SECRET&&grant_type=client_credentials to generate one.'),
      '#default_value' => $settings['access_token'],
      '#maxlength' => 255,
      '#required' => TRUE,
    );
    $form['min_resolution'] = array(
      '#type' => 'textfield',
      '#title' => t('Minimum image resolution'),
      '#description' => t('Only posts that have images that meet the minimum image resolution will be imported. WIDTHxHEIGHT'),
      '#default_value' => $settings['min_resolution'],
      '#required' => TRUE,
    );
  }
}
function social_content_facebook_data_callback($settings) {
  $params = array(
    'access_token' => $settings['access_token'],
  );
  $url = url($settings['graph_url'] . '/' . $settings['user_id'] . '/feed', array(
    'query' => $params,
    'external' => TRUE,
  ));
  $result = drupal_http_request($url);
  if ($result->code == 200) {
    $posts = json_decode($result->data);
    return $posts->data;
  }
  else {
    watchdog('social_content_facebook', 'Error fetching feed, data: !data', array(
      '!data' => $result->data,
    ), WATCHDOG_WARNING);
    return FALSE;
  }
}
function social_content_facebook_external_id_callback($post) {
  $id_parts = explode('_', $post->id);
  return array_pop($id_parts);
}
function social_content_facebook_post_callback(&$wrapper, $post, $external_id, $settings) {
  if (!social_content_facebook_valid_post($post)) {
    return FALSE;
  }
  $wrapper->created
    ->set(strtotime($post->created_time));
  $body_text = social_content_validate_text($post->message);
  $wrapper->body
    ->set(array(
    'value' => $body_text,
    'format' => 'filtered_html',
  ));
  $image_field_name = 'field_facebook_picture';
  $image_url = social_content_facebook_get_image_url($post, $settings);
  $image_file = social_content_get_image_file($image_url, $image_field_name, $settings['min_resolution']);
  if (!$image_file) {
    return FALSE;
  }
  $wrapper->{$image_field_name}
    ->set($image_file);
  $id_parts = explode('_', $external_id);
  $fblink = 'http://www.facebook.com/' . $post->from->id . '/posts/' . $id_parts[1];
  $wrapper->field_facebook_link
    ->set(array(
    'url' => $fblink,
  ));
  return TRUE;
}

/**
 * Callback function for obtaining the picture url.
 */
function social_content_facebook_get_image_url($post, $settings) {
  if (isset($post->picture)) {
    $picture_url = $post->picture;
  }
  if (isset($post->object_id)) {
    $picture_url = $settings['graph_url'] . '/' . $post->object_id . '/picture';
  }
  else {
    $picture_url_parts = drupal_parse_url($post->picture);
    if (isset($picture_url_parts['query']['url'])) {
      $picture_url = $picture_url_parts['query']['url'];
    }
    if (isset($picture_url_parts['query']['src'])) {
      $picture_url = $picture_url_parts['query']['src'];
    }
  }
  if (!isset($picture_url)) {
    return FALSE;
  }
  return $picture_url;
}

/**
 * Validate post.
 */
function social_content_facebook_valid_post($post) {

  // Only add direct wall posts.
  if (isset($post->to)) {
    return FALSE;
  }

  // Check post contains the required content.
  if (!isset($post->message) || !isset($post->picture)) {
    return FALSE;
  }
  return TRUE;
}