You are here

function socialfeed_facebook_feeds in Social Feed 7

Same name and namespace in other branches
  1. 7.2 socialfeed.block.inc \socialfeed_facebook_feeds()

Returns HTML with feeds in required format.

Parameters

string $page_name: Array with parameters for this action: depends on the trigger.

1 call to socialfeed_facebook_feeds()
socialfeed_facebook_posts in ./socialfeed.module
Uses socialfeed_facebook_posts() for fetching Facebook feeds.

File

./socialfeed.module, line 233
Provide Facebook, Twitter and Instagram.

Code

function socialfeed_facebook_feeds($page_name = '') {
  $config = $message_feed = array();
  $display_time = $display_pic = $selected_type = $selected_type_key = $fb_type = $selected_type_value = '';
  $config['app_id'] = variable_get('socialfeed_facebook_app_id');
  $config['secret'] = variable_get('socialfeed_facebook_secret_key');
  $fb_page = $page_name;

  // PHP SDK 4.x
  libraries_load('facebook');
  FacebookSession::setDefaultApplication($config['app_id'], $config['secret']);
  if (isset($config['app_id']) && !empty($config['app_id']) && isset($config['secret']) && !empty($config['secret'])) {
    $session = FacebookSession::newAppSession();
    $request = new FacebookRequest($session, 'GET', '/' . $fb_page . '/feed');
    $response = $request
      ->execute();
    $graph_object = $response
      ->getGraphObject();
    $facebook_values = $graph_object
      ->asArray('data');
    if ($facebook_values) {
      $display_time = variable_get('socialfeed_facebook_time_stamp');
      $display_pic = variable_get('socialfeed_facebook_display_pic');
      $display_all_posts = variable_get('socialfeed_facebook_all_types');
      $selected_type = variable_get('socialfeed_facebook_post_type');
      $selected_type_key = $selected_type;
      $fb_type = array(
        'link',
        'status',
        'photo',
        'video',
      );
      $selected_type_value = $fb_type[$selected_type_key];
      $i = 0;
      foreach ($facebook_values['data'] as $facebook_value) {
        if ($facebook_value->type == $selected_type_value) {
          if (array_key_exists('message', $facebook_value)) {
            if ($facebook_value->type == 'photo') {
              if ($display_pic == 1) {
                $message_feed[$i]['picture'] = $facebook_value->picture;
              }
            }
            $message_feed[$i]['message'] = substr($facebook_value->message, 0, variable_get('socialfeed_facebook_trim_length'));
            if ($display_time == 1) {
              $formatted_date = new DateTime($facebook_value->created_time);
              $message_feed[$i]['created_stamp'] = $formatted_date
                ->format(variable_get('socialfeed_facebook_time_format'));
            }
          }
          $i++;
          if ($i == variable_get('socialfeed_facebook_no_feeds')) {
            break;
          }
        }
        elseif ($display_all_posts == 1) {
          if (array_key_exists('message', $facebook_value)) {
            if ($display_pic == 1) {
              $message_feed[$i]['picture'] = $facebook_value->picture;
            }
            $message_feed[$i]['message'] = substr($facebook_value->message, 0, variable_get('socialfeed_facebook_trim_length'));
            $message_feed[$i]['type'] = $facebook_value->type;
            if ($display_time == 1) {
              $formatted_date = new DateTime($facebook_value->created_time);
              $message_feed[$i]['created_stamp'] = $formatted_date
                ->format(variable_get('socialfeed_facebook_time_format'));
            }
          }
          $i++;
          if ($i == variable_get('socialfeed_facebook_no_feeds')) {
            break;
          }
        }
      }
      return theme('socialfeed_facebook', array(
        'facebook' => $message_feed,
      ));
    }
  }
  else {
    drupal_set_message(t('Please provide your credentials at the Facebook <a href="@configuration">configuration page</a>.', array(
      '@configuration' => 'admin/config/services/socialfeed',
    )), 'warning');
  }
}