<?php

/**
 * @file
 * Get the current facebook wall posts of the given user or page.
 * @auther : Abhijeet Kalsi.
 */
module_load_include('inc', 'facebook_wall', 'facebook_wall.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_login'),
      'access callback' => 'user_access',
      'access arguments' => array('administer facebook wall'),
      'type' => MENU_NORMAL_ITEM,
      'file' => 'facebook_wall.page.inc',
  );
  $menu['admin/config/services/facebook_wall/settings'] = array(
      'title' => 'Facebook Settings',
      'page callback' => 'drupal_get_form',
      'page arguments' => array('_facebook_wall_login'),
      'access callback' => 'user_access',
      'access arguments' => array('administer facebook wall'),
      'type' => MENU_DEFAULT_LOCAL_TASK,
      'weight' => 0,
      'file' => 'facebook_wall.page.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.page.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_wall_posts'),
      'access callback' => 'user_access',
      'access arguments' => array('facebook wall'),
      'file' => 'facebook_wall.page.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;
}

/**
 * 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 = 'https://graph.facebook.com/'.FB_API_VER.'/' . FB_PAGE_NAME . '/feed?access_token=' . FB_ACCESS_TOKEN . '&limit=' . (int) variable_get('facebook_wall_post_limit', 10);


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

/**
 * 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) {
	//print $url;

  $response = drupal_http_request($url);
  //print '<pre>';
  //print_r($response);
  //print '</pre>';
  //exit;
  if ($response->code == 200) {
    return json_decode($response->data);
  }
}

/**
 * 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 (FB_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');
      }
  }
  // Checks for FB pabe name or id exists.
  elseif (FB_PAGE_NAME == '') {
      $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;
}
