From 78fcbcebe20d31f4a64be1de4c9b71b8d74cae8d Mon Sep 17 00:00:00 2001
From: Bob Vincent <bobvin@pillars.net>
Date: Sat, 9 Jul 2011 13:59:28 -0400
Subject: [PATCH] Integrate with libraries API module, and enable installation via profile.

---
 facebook_stream.info    |    3 +-
 facebook_stream.install |   19 +++++----
 facebook_stream.module  |  102 +++++++++++++++++++++--------------------------
 3 files changed, 56 insertions(+), 68 deletions(-)

diff --git a/facebook_stream.info b/facebook_stream.info
index fcd170e9182936565a02c32b87f63b6f373d2f0e..23cc3424a05a52f8193da7b38f82d66a2c344b73 100644
--- a/facebook_stream.info
+++ b/facebook_stream.info
@@ -4,8 +4,7 @@ description = Allows users to import and export information from Facebook.
 package = Facebook
 core = 7.x
 
-files[] = facebook_stream.module
-files[] = stream.admin.inc
+dependencies[] = libraries
 
 configure = admin/settings/facebook_stream
 
diff --git a/facebook_stream.install b/facebook_stream.install
index 1554427b4f7511a4b3e8470b387b0cbff374cfd4..367c3a3ed6294e184f8bece4d08800be5617625c 100644
--- a/facebook_stream.install
+++ b/facebook_stream.install
@@ -4,22 +4,23 @@
  * Implementation of hook_requirements
  */
 function facebook_stream_requirements($phase) {
-  $t = get_t();
   $items = array();
-  $status = array('title' => $t('Facebook PHP SDK'));
-  $fb_sdk = drupal_get_path('module','facebook_stream').'/facebook-php-sdk';
-  if($phase == 'install') {
-    if (file_exists($fb_sdk) && is_dir($fb_sdk)) { 
-      $status['description'] = $t('Facebook SDK found at %path', array('%path' => $fb_sdk));
+  // Functions libraries_get_path(), url(), and l() may not be available
+  // during install phase.
+  if($phase == 'runtime') {
+    $status = array('title' => t('Facebook PHP SDK'));
+    $fb_sdk = libraries_get_path('facebook-php-sdk');
+    if ($fb_sdk) {
+      $status['description'] = t('Facebook SDK found at %path', array('%path' => $fb_sdk));
       $status['severity'] = REQUIREMENT_OK;
     }
     else {
-      $status['description'] = $t('Facebook PHP SDK not found. Download php sdk from !downloadurl and copy the extracted directory into facebook stream module as facebook-php-sdk', array('!downloadurl' => l('http://github.com/facebook/php-sdk', 'http://github.com/facebook/php-sdk')));
+      $status['description'] = t('Facebook PHP SDK not found. Download php sdk from !downloadurl and copy the extracted directory into your libraries folder as facebook-php-sdk', array('!downloadurl' => l('http://github.com/facebook/php-sdk', 'http://github.com/facebook/php-sdk')));
       $status['severity'] = REQUIREMENT_ERROR;
-      $status['value'] = $t('Not found');
+      $status['value'] = t('Not found');
     }
+    $items[] = $status;
   }
-  $items[] = $status;
   return $items;
 }
 
diff --git a/facebook_stream.module b/facebook_stream.module
index fc185acdfe56578e4e057832f8091eed2e1f888b..7fd1c07c72b80878c99a710fc469aaabae5a5ed4 100644
--- a/facebook_stream.module
+++ b/facebook_stream.module
@@ -20,33 +20,33 @@ function facebook_stream_init() {
     // check if fb module is enabled.
     if (isset($_fb) && !empty($_fb)) {
       $_facebook_stream_sdk = $_fb;
-    } 
-  } 
+    }
+  }
   elseif (isset($_fb) && !empty($_fb)) {
     drupal_set_message(t("Drupal for facebook module is already enabled, choose the drupal for facebook version"));
-  } 
+  }
   elseif (variable_get('facebook_stream_php_sdk', 0) == 3) {
     $_facebook_stream_sdk = facebook_client();
-  } 
+  }
   elseif (function_exists('facebook_client')) {
     drupal_set_message(t("fbconnect module is already enabled, choose the fbconnect module in the facebook stream settings page"));
   }
   else {
-    $fb_sdk = drupal_get_path('module','facebook_stream').'/facebook-php-sdk';
-    if(file_exists($fb_sdk) && is_dir($fb_sdk)) {
-      include $fb_sdk .'/src/facebook.php';
+    $fb_sdk = libraries_get_path('facebook-php-sdk');
+    if ($fb_sdk) {
+      include_once $fb_sdk .'/src/facebook.php';
       $api = variable_get('facebook_stream_facebook_api_id', NULL);
       $secret = variable_get('facebook_stream_facebook_secret_key', NULL);
       if (!$api || !$secret) {
 	drupal_set_message(l("Click Here", 'admin/config/facebook_stream/settings') . t(" to configure facebook stream module"));
-      } 
+      }
       $_facebook_stream_sdk = new Facebook(array(
         'appId'  => $api,
         'secret' => $secret,
         'cookie' => TRUE, // enable optional cookie support
       ));
     } else {
-      drupal_set_message(t("Add facebook-php-sdk folder in the facebook_stream module folder by downloading it from ") . l("http://github.com/facebook/php-sdk"));
+      drupal_set_message(t("Add the facebook-php-sdk folder to your libraries folder by downloading it from %url.", array('%url' => l("http://github.com/facebook/php-sdk"))));
     }
 
   }
@@ -55,7 +55,6 @@ function facebook_stream_init() {
   }
 }
 
-
 function call_facebook_api($method, $args) {
   global $_facebook_stream_sdk;
   if ($_facebook_stream_sdk->latest_php_sdk) {
@@ -71,7 +70,7 @@ function get_facebook_loggedin_user() {
   global $_facebook_stream_sdk;
   if ($_facebook_stream_sdk->latest_php_sdk == True) {
     $fbuid = $_facebook_stream_sdk->getUser();
-  } 
+  }
   else {
     $fbuid = $_facebook_stream_sdk->get_loggedin_user();
   }
@@ -91,21 +90,18 @@ function facebook_stream_menu() {
     'access arguments' => array(1),
     'type' => MENU_LOCAL_TASK,
   );
-  
   $items['fbstream/get/post/comments'] = array(
     'title' => t('Facebook Stream post comments'),
     "page callback" => "fb_stream_get_post_comments",
     'access arguments' => array('access content'),
     'type' => MENU_CALLBACK,
   );
-
   $items['fbstream/post/update'] = array(
     'title' => t('Facebook Stream update post'),
     "page callback" => "fb_stream_post_update",
     'access arguments' => array('access content'),
     'type' => MENU_CALLBACK,
   );
-
   $items['fbstream/post/comments'] = array(
     'title' => t('Facebook Stream posting comments to Facebook'),
     "page callback" => "fb_stream_post_comments",
@@ -136,8 +132,8 @@ function facebook_stream_menu() {
     'access arguments' => array('access content'),
     'type' => MENU_CALLBACK,
   );
-  //settings page 
 
+  // Settings page.
   $items['admin/config/facebook_stream'] = array(
     'title' => 'Facebook Stream',
     'description' => 'Facebook Stream Settings.',
@@ -148,7 +144,6 @@ function facebook_stream_menu() {
     'file' => 'system.admin.inc',
     'file path' => drupal_get_path('module' , 'system')
   );
-
   $items['admin/config/facebook_stream/settings'] = array(
     "title" => t("Facebook Stream Settings"),
     "description" => t("Configure the facebook stream to post comments, likes and status message."),
@@ -165,7 +160,7 @@ function facebook_stream_menu() {
     "access arguments" => array('administer site configuration'),
   );
 
-  return $items; 
+  return $items;
 }
 
 /**
@@ -176,7 +171,7 @@ function facebook_stream_theme() {
   return array(
     'posts_display' => array(
       'variables' => array('stream' => NULL),
-      'template' => 'posts_display', 
+      'template' => 'posts_display',
       ),
     'comments_display' => array(
       'variables' => array('comments' => NULL),
@@ -184,7 +179,7 @@ function facebook_stream_theme() {
       ),
     'likes_display' => array(
       'variables' => array('friends' => NULL, 'user_likes' => NULL, 'post_id' => NULL),
-      'template' => 'likes_display',      
+      'template' => 'likes_display',
       ),
     'post_display' => array(
       'variables' => array('post' => NULL, 'user_info' => NULL),
@@ -193,10 +188,9 @@ function facebook_stream_theme() {
     );
 }
 
-
 /**
  * Defination of stream configuration form
- * 
+ *
  * @ingroup forms
  * @see fb_stream_config_form_validate
  * @see fb_stream_config_form_submit
@@ -212,7 +206,7 @@ function fb_stream_config_form() {
     '#type' => 'textfield',
     '#title' => t('No of Items to display'),
     '#default_value' => $data['fbstream_user'] ? $data['fbstream_user'] : 5 ,
-    '#description' => t('NO of Feeds to display on the block.'),
+    '#description' => t('Number of Feeds to display on the block.'),
     '#required' => TRUE,
     );
   $form['submit'] = array('#type' => 'submit', '#value' => t('Save'));
@@ -238,7 +232,6 @@ function fb_stream_config_form_validate($form, &$form_state) {
   }
 }
 
-
 /**
  * Form submit of stream configurations
  *
@@ -274,15 +267,15 @@ function facebook_stream_login_render_button($attr = array()) {
       'text'       => t('Facebook Login'),
       'onlogin'    => 'facebook_onlogin_ready();',
       'background' => 'dark',
-    ); 
-  } 
+    );
+  }
   else {
     $defaulr_attr = array(
       'text'       => t('Facebook Login'),
       'onlogin'    => 'facebook_onlogin_ready();',
       'background' => 'dark',
       'v'          => 2,
-    ); 
+    );
   }
   $attr = array_merge($defaulr_attr, $attr);
   //drupal_alter('fbconnect_login_button', $attr);
@@ -314,7 +307,7 @@ function facebook_stream_block_configure($delta = '') {
       '#description' => t('Display the full information of the facebook stream post'),
       '#default_value' => variable_get('fbstream_block_post_display_more', 1),
     );
-      
+
     $form['fbstream_block_posts_update_time'] = array(
       '#type' => 'textfield',
       '#title' => t('Facebook stream posts update time in minutes'),
@@ -336,7 +329,6 @@ function facebook_stream_block_save($delta = '', $edit = array()) {
   return;
 }
 
-
 /**
  * Implements hook_block_view().
  */
@@ -359,7 +351,6 @@ function facebook_stream_block_view($delta = '') {
   }
 }
 
-
 /**
  * returns the permission of the string
  */
@@ -377,7 +368,6 @@ function get_perm($string, $uid) {
   return $perms;
 }
 
-
 /**
  * Get the facebook stream of the currently logged in user
  */
@@ -386,7 +376,7 @@ function _get_facebook_stream() {
   drupal_add_css(drupal_get_path('module', 'facebook_stream') .'/facebook_stream.css', 'theme');
   drupal_add_js(drupal_get_path('module', 'facebook_stream') .'/facebook_stream.js', array('weight' => JS_THEME));
   drupal_add_library('system', 'ui.dialog');
-  
+
   // facebbok client
   $fb = $_facebook_stream_sdk;
   $fbuid  = get_facebook_loggedin_user();
@@ -406,7 +396,7 @@ function _get_facebook_stream() {
     }
     $stream = call_facebook_api('stream.get', array('limit' => $count));
     $publish_perm = get_perm('publish_stream', $fbuid);
-    
+
     $confs = array(
       'fbstream_dispaly' => variable_get('fbstream_block_post_display_more', 1),
       'fbstream_update_time' => variable_get('fbstream_block_posts_update_time', 2),
@@ -423,7 +413,7 @@ function _get_facebook_stream() {
 
 /**
  * Gets user information from the stream profiles array
- * 
+ *
  * @param $profiles
  *  Profiles array came in the stream.get response
  * @param $uid
@@ -464,10 +454,9 @@ function _post_attachments($post) {
   return $output;
 }
 
-
 /**
  * Convert the time duration into readable way like 2hours, 3minute etc..
- * 
+ *
  * @param $seconds
  *  timestamp which needs to convert into readable way
  * @param $use
@@ -486,7 +475,7 @@ function time_duration($seconds, $use = NULL, $zeros = FALSE) {
     'minutes'   => 60,
     'seconds'   => 1
   );
-  
+
   // Break into periods
   $seconds = (float) $seconds;
   foreach ($periods as $period => $value) {
@@ -501,7 +490,7 @@ function time_duration($seconds, $use = NULL, $zeros = FALSE) {
     $segments[$array_key] = $count;
     $seconds = $seconds % $value;
   }
-  
+
   // Build the string
   foreach ($segments as $key => $value) {
     $segment_name = substr($key, 0, -1);
@@ -515,9 +504,8 @@ function time_duration($seconds, $use = NULL, $zeros = FALSE) {
   return $array[0];
 }
 
-
 function str_parse_url($str) {
-  // regular expression for possible links formats 
+  // regular expression for possible links formats
   $regexp = '/((http\:\/\/(\w{3}\.)?|https\:\/\/(\w{3}\.)?|www\.|ftp\.|ftps\.){1}\S+\.\S+)/i';
   return preg_replace($regexp, '<a href="$1" target="_new">$1</a>', $str);
 }
@@ -535,12 +523,12 @@ function fb_stream_get() {
 
 /**
  * call back for the url fbstream/get/post/comments
- * 
+ *
  * Get the comments for the post and return the html of the comments
  */
 function fb_stream_get_post_comments() {
   $post_id = trim($_POST['post_id']);
-  //$fb = facebook_client(); 
+  //$fb = facebook_client();
   global $_facebook_stream_sdk;
   $fb = $_facebook_stream_sdk;
   //$fbuid = fbconnect_get_fbuid();
@@ -558,12 +546,12 @@ function fb_stream_get_post_comments() {
 
   // ask the user to give permissions for our application to access his stream
   if (!$perm) {
-    $output .= '<div class = "post_comments_form hide">'. drupal_get_form('post_comment', $post_id) .'</div>'; 
+    $output .= '<div class = "post_comments_form hide">'. drupal_get_form('post_comment', $post_id) .'</div>';
     $output .= ' <br><div class = \'get_perm\'> Set the permissions for the Application to Publish <input type = "submit" value = "Get Perm" class = "form-submit" onclick = "prompt_for_permissions(\'publish_stream\');"><div>';
   }
   else{
-    if ( variable_get('stream_post_comments', 0)) 
-      $output .= '<div class = "post_comments_form">' . drupal_render(drupal_get_form('post_comment', $post_id)) . '</div>'; 
+    if ( variable_get('stream_post_comments', 0))
+      $output .= '<div class = "post_comments_form">' . drupal_render(drupal_get_form('post_comment', $post_id)) . '</div>';
   }
   echo $output;
   exit;
@@ -581,10 +569,10 @@ function post_comment($form, &$form_status, $post_id) {
     '#type' => 'textfield',
     '#title' => t(''),
     '#size' => 54,
-    '#description' => t('Write a comment')      
+    '#description' => t('Write a comment')
   );
   $form['submit'] = array(
-    '#type' => 'submit', 
+    '#type' => 'submit',
     '#value' => t('Save'),
   );
   $form['#attributes'] = array('class' => 'fb_stream_comment_add_form');
@@ -593,20 +581,20 @@ function post_comment($form, &$form_status, $post_id) {
 
 /**
  * call back for the url fbstream/get/post/likes
- * 
+ *
  * Get the likes for the post and return the html of the friends who likes this post
  */
 function fb_stream_get_post_likes() {
   $post_id = trim($_POST['post_id']);
-  //$fb = facebook_client(); 
+  //$fb = facebook_client();
   global $_facebook_stream_sdk;
   $fb = $_facebook_stream_sdk;
   $query = "SELECT likes FROM stream WHERE post_id ='". trim($post_id) ."'";
   $likes = call_facebook_api('fql.query', array('query' => $query));
   if (!$likes) {
-    echo "There is no likes information for this post "; 
+    echo "There is no likes information for this post ";
     exit;
-  } 
+  }
   $likes = $likes[0]['likes'];
   $friends = Array();
   if (!empty($likes['sample'])) {
@@ -626,7 +614,7 @@ function fb_stream_get_post_likes() {
 function fb_stream_post_comments() {
   $post_id = trim($_POST['id']);
   $comment = trim($_POST['comments']);
-  //$fb = facebook_client();	
+  //$fb = facebook_client();
   global $_facebook_stream_sdk;
   $fb = $_facebook_stream_sdk;
   $fbuid = get_facebook_loggedin_user();
@@ -636,7 +624,7 @@ function fb_stream_post_comments() {
   catch (FacebookRestClientException $e) {
     echo t('Comment not posted due to some error in application');
     exit;
-  } 
+  }
 
   $user_info = call_facebook_api('users.getInfo', array('uids' => $fbuid, 'fields' => 'name,pic_square,profile_url'));
   $user_info = $user_info[0];
@@ -666,17 +654,17 @@ function fb_stream_post_likes() {
   $fbuid = get_facebook_loggedin_user();
   if ($value == 'Like') {
     call_facebook_api('stream.addLike', array('uid' => $fbuid, 'post_id' => $post_id));
-    exit ;    
+    exit ;
  }
  else{
-   call_facebook_api('stream.removeLike', array('uid' => $fbuid, 'post_id' => $post_id));    
+   call_facebook_api('stream.removeLike', array('uid' => $fbuid, 'post_id' => $post_id));
    exit;
  }
 }
 
 function fb_stream_post_update() {
   $post_id = trim($_POST['post_id']);
-  //$fb = facebook_client(); 
+  //$fb = facebook_client();
   global $_facebook_stream_sdk;
   $fb = $_facebook_stream_sdk;
   $query = "SELECT post_id, viewer_id, source_id, type, actor_id, message, attachment, app_data, comments, likes, privacy, updated_time, created_time, filter_key, permalink  FROM stream WHERE post_id = '". $post_id ."'";
@@ -689,10 +677,10 @@ function fb_stream_post_update() {
   $post['post_id'] = trim($post['post_id']);
   echo theme('post_display', array('post' => $post, 'user_info' => $user_info));
 }
-//Callback: Posts staatus 
+//Callback: Posts staatus
 function fb_stream_post_status() {
   $post_value = trim($_POST['value']);
-  //$fb = facebook_client(); 
+  //$fb = facebook_client();
   global $_facebook_stream_sdk;
   $fb = $_facebook_stream_sdk;
   try {
-- 
1.7.4.1

