From 188b95f62e8f3f70a230909adfe6037f8cb6847d Mon Sep 17 00:00:00 2001
From: Stephen Tweeddale <stephen.tweeddale@computerminds.co.uk>
Date: Thu, 17 Mar 2011 15:01:45 +0000
Subject: [PATCH] Issue #1096140: Ported the fb_example submodule

---
 contrib/fb_example.info    |    2 +-
 contrib/fb_example.install |   25 +++-
 contrib/fb_example.module  |  281 ++++++++++++++++++++++++++++---------------
 3 files changed, 204 insertions(+), 104 deletions(-)

diff --git contrib/fb_example.info contrib/fb_example.info
index 40b48f7..8a36b17 100644
--- contrib/fb_example.info
+++ contrib/fb_example.info
@@ -1,7 +1,7 @@
 name = Example Customizations (fb_example.module)
 description = These hooks will make your site behave more like <a href=http://drupalforfcebook.org target=_blank>drupalforfacebook.org</a>.  Use this code as an example for your own customizations.
 package = Drupal for Facebook - contrib
-core = 6.x
+core = 7.x
 dependencies[] = fb
 dependencies[] = fb_form
 dependencies[] = fb_app
diff --git contrib/fb_example.install contrib/fb_example.install
index af7ba28..ce9590f 100644
--- contrib/fb_example.install
+++ contrib/fb_example.install
@@ -6,14 +6,29 @@
  */
 
 
-// Set weight so that fb_example comes after fb_user.
+/**
+ * Implements hook_install().
+ *
+ * Set weight so that fb_example comes after fb_user.
+ */
 function fb_example_install() {
   // So we fall after og_vocab and og
-  db_query("UPDATE {system} SET weight = 3 WHERE name='fb_example'");
+  db_update('system')
+  ->fields(array(
+    'weight' => 3,
+  ))
+  ->condition('name', 'fb_example')
+  ->execute();
 }
 
+/**
+ * Implements hook_update_N().
+ */
 function fb_example_update_1() {
-  $ret = array();
-  $ret[] = update_sql("UPDATE {system} SET weight = 3 WHERE name='fb_example'");
-  return $ret;
+  db_update('system')
+  ->fields(array(
+    'weight' => 3,
+  ))
+  ->condition('name', 'fb_example')
+  ->execute();
 }
diff --git contrib/fb_example.module contrib/fb_example.module
index 3d6560e..82db6ed 100644
--- contrib/fb_example.module
+++ contrib/fb_example.module
@@ -10,27 +10,6 @@
  */
 
 /**
- * Implements hook_link().
- *
- * Add an XFBML like button to all node pages.  The button will appear next to
- * other node links, such as "add new comment".
- */
-function fb_example_link($type, $object, $teaser = FALSE) {
-  if ($type == 'node' && !$teaser) {
-    $items = array();
-    if (variable_get('fb_example_link_add_like', TRUE)) { // Switch to control this behavior.
-      $url = fb_scrub_urls(url('node/' . $object->nid, array('absolute' => TRUE)));
-      $items['dff_like'] = array(
-        'title' => "<fb:like href={$url}></fb:like>",
-        'html' => TRUE,
-      );
-    }
-    return $items;
-  }
-}
-
-
-/**
  * Implements hook_fb_friend_invite_page_wrap_alter().
  *
  * @see modules/fb/contrib/fb_friend.module
@@ -63,6 +42,10 @@ function fb_example_fb_friend_invite_app_alter(&$fbml) {
   $fbml['selector']['#attributes']['email_invite'] = FALSE;
   $fbml['selector']['#attributes']['import_external_friends'] = FALSE;
 }
+
+/**
+ * Implements hook_fb_example_fb_friend_invite_app_wrap_alter().
+ */
 function fb_example_fb_friend_invite_app_wrap_alter(&$elem) {
   // Replace serverfbml with popup
   if ($elem['#type'] == 'fb_form_serverfbml') {
@@ -83,7 +66,7 @@ function fb_example_fb_friend_invite_app_wrap_alter(&$elem) {
 function fb_example_form_alter(&$form, $form_state, $form_id) {
   // Add stream publish option.
   if (isset($GLOBALS['_fb']) && fb_facebook_user()) {
-    if ($form['#id'] == 'node-form') {
+    if (!empty($form['#node_edit_form'])) {
       // Add checkbox to control feed publish.
       $form['fb_example']['stream_publish'] = array(
         '#type' => 'checkbox',
@@ -91,7 +74,7 @@ function fb_example_form_alter(&$form, $form_state, $form_id) {
         '#default_value' => TRUE,
       );
     }
-    elseif ($form['form_id']['#value'] == 'comment_form') {
+    elseif ($form['#id'] == 'comment-form') {
       // Add checkbox to control feed publish.
       $form['fb_example']['stream_publish'] = array(
         '#type' => 'checkbox',
@@ -103,113 +86,213 @@ function fb_example_form_alter(&$form, $form_state, $form_id) {
 }
 
 /**
- * Implements hook_nodeapi().
+ * Implements hook_node_insert().
  *
  * Publish to facebook Walls when users submit nodes.
  *
  * @see http://developers.facebook.com/docs/reference/rest/stream.publish
  * @see http://developers.facebook.com/docs/guides/attachments
  */
-function fb_example_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
-  if ($op == 'insert' || $op == 'update') {
-    if (isset($node->stream_publish) && $node->stream_publish) {
-      $attachment = array(
-        'name' => $node->title,
-        'href' => url('node/' . $node->nid, array('absolute' => TRUE)),
-        'description' => filter_xss($node->teaser, array()),
-      );
+function fb_example_node_insert($node) {
+  if (isset($node->stream_publish) && $node->stream_publish) {
+    $attachment = array(
+      'name' => $node->title,
+      'href' => url('node/' . $node->nid, array('absolute' => TRUE)),
+      'description' => filter_xss($node->teaser, array()),
+    );
 
-      /*
-        if ($picture = $GLOBALS['user']->picture) {
-        $url = url($picture, array('absolute' => TRUE));
-        $attachment['media'][] = array(
+    /*
+     if ($picture = $GLOBALS['user']->picture) {
+     $url = url($picture, array('absolute' => TRUE));
+     $attachment['media'][] = array(
+     'type' => 'image',
+     'src' => $url,
+     'href' => $url,
+     );
+     }
+     */
+    if ($logo_path = theme_get_setting('logo_path')) {
+      $url = url($logo_path, array('absolute' => TRUE));
+      //dpm($logo_path, "logo_path is $logo_path and url is $url");
+      $attachment['media'][] = array(
         'type' => 'image',
         'src' => $url,
         'href' => $url,
-        );
-        }
-      */
-      if ($logo_path = theme_get_setting('logo_path')) {
-        $url = url($logo_path, array('absolute' => TRUE));
-        //dpm($logo_path, "logo_path is $logo_path and url is $url");
-        $attachment['media'][] = array(
-          'type' => 'image',
-          'src' => $url,
-          'href' => $url,
-        );
-      }
+      );
+    }
+
 
+    $user_message = t('Check out my latest post on !site...',
+                      array('!site' => variable_get('site_name', t('my Drupal for Facebook powered site'))));
+    $actions = array();
+    $actions[] = array(
+      'text' => t('Read More'),
+      'href' => url('node/' . $node->nid, array('absolute' => TRUE)),
+    );
+    fb_stream_publish_dialog(array('message' => $user_message,
+                                   'attachment' => $attachment,
+                                   'action_links' => $actions,
+                             ));
+  }
+}
 
-      $user_message = t('Check out my latest post on !site...',
-                        array('!site' => variable_get('site_name', t('my Drupal for Facebook powered site'))));
-      $actions = array();
-      $actions[] = array(
-        'text' => t('Read More'),
-        'href' => url('node/' . $node->nid, array('absolute' => TRUE)),
+/**
+ * Implements hook_node_update().
+ *
+ * Publish to facebook Walls when users submit nodes.
+ *
+ * @see http://developers.facebook.com/docs/reference/rest/stream.publish
+ * @see http://developers.facebook.com/docs/guides/attachments
+ */
+function fb_example_node_update($node) {
+  if (!empty($node->stream_publish)) {
+    $teaser = node_view($node, 'teaser');
+    $attachment = array(
+      'name' => $node->title,
+      'href' => url('node/' . $node->nid, array('absolute' => TRUE)),
+      'description' => filter_xss($teaser['body'][0]['#markup'], array()),
+    );
+    /*
+     if ($picture = $GLOBALS['user']->picture) {
+     $url = url($picture, array('absolute' => TRUE));
+     $attachment['media'][] = array(
+     'type' => 'image',
+     'src' => $url,
+     'href' => $url,
+     );
+     }
+     */
+    if ($logo_path = theme_get_setting('logo_path')) {
+      $url = url($logo_path, array('absolute' => TRUE));
+      //dpm($logo_path, "logo_path is $logo_path and url is $url");
+      $attachment['media'][] = array(
+        'type' => 'image',
+        'src' => $url,
+        'href' => $url,
       );
-      fb_stream_publish_dialog(array('message' => $user_message,
-                                     'attachment' => $attachment,
-                                     'action_links' => $actions,
-                               ));
     }
+
+
+    $user_message = t('Check out my latest post on !site...',
+                      array('!site' => variable_get('site_name', t('my Drupal for Facebook powered site'))));
+    $actions = array();
+    $actions[] = array(
+      'text' => t('Read More'),
+      'href' => url('node/' . $node->nid, array('absolute' => TRUE)),
+    );
+    fb_stream_publish_dialog(array('message' => $user_message,
+                                   'attachment' => $attachment,
+                                   'action_links' => $actions,
+                             ));
   }
+}
 
-  // Another way to add like button, as part of a node.
-  if ($op == 'view' && variable_get('fb_example_nodeapi_add_like', FALSE)) {
+/**
+ * Implements hook_node_view().
+ */
+function fb_example_node_view($node, $view_mode = 'full') {
+  // This is an example of how you might add a 'like' button to the node links.
+  if (variable_get('fb_example_link_add_like', TRUE)) { // Switch to control this behavior.
+    $url = fb_scrub_urls(url('node/' . $node->nid, array('absolute' => TRUE)));
+    $node->content['links']['node']['#links']['fb_like'] = array(
+      'title' => "<fb:like layout='button_count' href={$url}></fb:like>",
+      'html' => TRUE,
+    );
+  }
+  // This is an example of how you might add a 'like' button into the content area.
+  if (variable_get('fb_example_nodeapi_add_like', TRUE)) {
     $url = fb_scrub_urls(url('node/' . $node->nid, array('absolute' => TRUE)));
-    $node->content['dff_like'] = array(
-      '#value' => "<fb:like href={$url}></fb:like>",
-      '#type' => 'markup',
-      '#prefix' => '<div class="dff_like_wrapper">',
+    $node->content['fb_like'] = array(
+      '#markup' => "<fb:like href={$url}></fb:like>",
+      '#weight' => -10,
+      '#prefix' => '<div class="fb_like_wrapper">',
       '#suffix' => '</div>',
     );
   }
 }
 
 /**
- * Implementation of hook_comment().
+ * Implements hook_comment_insert().
  *
  * Publish to facebook Walls when users submit comments.
  */
-function fb_example_comment(&$a1, $op) {
-  if ($op == 'insert' || $op == 'update') {
-    if ($a1['stream_publish']) {
-      //dpm($a1, "fb_example_comment, publishing to stream");
-      $node = node_load($a1['nid']);
+function fb_example_comment_insert($comment) {
+  if (!empty($comment->stream_publish)) {
+    //dpm($comment, "fb_example_comment, publishing to stream");
+    //$node = node_load($comment->nid);
 
-      // http://wiki.developers.facebook.com/index.php/Attachment_(Streams)
-      $attachment = array(
-        'name' => $a1['subject'],
-        'href' => url('node/' . $a1['nid'], array('absolute' => TRUE, 'fragment' => 'comment-' . $a1['cid'])),
-        'description' => $a1['comment'],
-        //'properties' => array(t('In reply to') => array('text' => $node->title, 'href' => url("node/" . $node->nid, array('absolute' => TRUE)))),
+    // http://wiki.developers.facebook.com/index.php/Attachment_(Streams)
+    $attachment = array(
+      'name' => $comment->subject,
+      'href' => url('node/' . $comment->nid, array('absolute' => TRUE, 'fragment' => 'comment-' . $comment->cid)),
+      'description' => $comment->comment_body[$comment->language][0]['value'],
+      //'properties' => array(t('In reply to') => array('text' => $node->title, 'href' => url("node/" . $node->nid, array('absolute' => TRUE)))),
+    );
+
+    if ($logo_path = theme_get_setting('logo_path')) {
+      $url = url($logo_path, array('absolute' => TRUE));
+      //dpm($logo_path, "logo_path is $logo_path and url is $url");
+      $attachment['media'][] = array(
+        'type' => 'image',
+        'src' => $url,
+        'href' => $url,
       );
+    }
 
-      if ($logo_path = theme_get_setting('logo_path')) {
-        $url = url($logo_path, array('absolute' => TRUE));
-        //dpm($logo_path, "logo_path is $logo_path and url is $url");
-        $attachment['media'][] = array(
-          'type' => 'image',
-          'src' => $url,
-          'href' => $url,
-        );
-      }
+    $user_message = t('Check out my latest comment on !site...',
+                      array('!site' => variable_get('site_name', t('my Drupal for Facebook powered site'))));
+    $actions = array();
+    $actions[] = array('text' => t('Read More'),
+                       'href' => url('node/' . $comment->nid, array('absolute' => TRUE)),
+    );
+    fb_stream_publish_dialog(array('message' => $user_message,
+                                   'attachment' => $attachment,
+                                   'action_links' => $actions,
+                             ));
+  }
+}
+
+/**
+ * Implements hook_comment_update().
+ *
+ * Publish to facebook Walls when users submit comments.
+ */
+function fb_example_comment_update($comment) {
+  if ($comment->stream_publish) {
+    //dpm($a1, "fb_example_comment, publishing to stream");
+    $node = node_load($comment->nid);
+
+    // http://wiki.developers.facebook.com/index.php/Attachment_(Streams)
+    $attachment = array(
+      'name' => $comment->subject,
+      'href' => url('node/' . $comment->nid, array('absolute' => TRUE, 'fragment' => 'comment-' . $comment->cid)),
+      'description' => $comment->comment_body[$comment->language][0]['value'],
+      //'properties' => array(t('In reply to') => array('text' => $node->title, 'href' => url("node/" . $node->nid, array('absolute' => TRUE)))),
+    );
 
-      $user_message = t('Check out my latest comment on !site...',
-                        array('!site' => variable_get('site_name', t('my Drupal for Facebook powered site'))));
-      $actions = array();
-      $actions[] = array('text' => t('Read More'),
-                         'href' => url('node/' . $a1['nid'], array('absolute' => TRUE)),
+    if ($logo_path = theme_get_setting('logo_path')) {
+      $url = url($logo_path, array('absolute' => TRUE));
+      //dpm($logo_path, "logo_path is $logo_path and url is $url");
+      $attachment['media'][] = array(
+        'type' => 'image',
+        'src' => $url,
+        'href' => $url,
       );
-      fb_stream_publish_dialog(array('message' => $user_message,
-                                     'attachment' => $attachment,
-                                     'action_links' => $actions,
-                               ));
     }
+
+    $user_message = t('Check out my latest comment on !site...',
+                      array('!site' => variable_get('site_name', t('my Drupal for Facebook powered site'))));
+    $actions = array();
+    $actions[] = array('text' => t('Read More'),
+                       'href' => url('node/' . $comment->nid, array('absolute' => TRUE)),
+    );
+    fb_stream_publish_dialog(array('message' => $user_message,
+                                   'attachment' => $attachment,
+                                   'action_links' => $actions,
+                             ));
   }
 }
 
-
 /**
  * Implements hook_fb_required_perms_alter().
  *
@@ -280,8 +363,10 @@ function fb_example_fb($op, $data, &$return) {
           // The user has connected (as opposed to logged out).  Let's redirect
           // them to our 'welcome' page.  Replace 'welcome' with the path you
           // really want.
-          $url = url('welcome',
-                     array('absolute' => TRUE, 'fb_canvas' => fb_is_canvas()));
+          $url = url('welcome', array(
+                                      'absolute' => TRUE,
+                                      'fb_canvas' => fb_is_canvas(),
+                                      ));
           // We return javascript to be evaluated by fb.js.
           $return[] = "FB_JS.reload('$url');";
         }
@@ -394,4 +479,4 @@ function fb_example_init() {
       }
     }
   }
-}
\ No newline at end of file
+}
-- 
1.7.0.4

