? LICENSE.txt
? diggthis.install
Index: diggthis.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/diggthis/diggthis.module,v
retrieving revision 1.2
diff -u -F^f -r1.2 diggthis.module
--- diggthis.module	24 Oct 2006 12:00:43 -0000	1.2
+++ diggthis.module	16 Nov 2006 16:13:23 -0000
@@ -10,6 +10,9 @@
  * The code of this module is partially based on the service links module by Fredrik Jonsson
  */
 
+
+
+
 /**
  * Implementation of hook_help().
  */
@@ -20,6 +23,9 @@ function diggthis_help($section) {
   }
 }
 
+
+
+
 /**
  * Implementation of hook_settings()
  */
@@ -35,96 +41,210 @@ function diggthis_settings() {
     '#default_value' => variable_get('diggthis_button_url',''),
     '#description' => t('The URL to the Digg button you want to use for stories that are not yet submitted to Digg.com, for example<br /> <strong>http://digg.com/img/badges/32x32-digg-guy.gif</strong><br />You can choose buttons here: ') . l(t('Digg buttons'), 'http://digg.com/tools/buttons') 
   );
+  
   $form['diggthis']['diggthis_node_types'] = array(
     '#type' => 'checkboxes',
     '#title' => t('Node Types'),
     '#default_value' => variable_get('diggthis_node_types', array()),
     '#options' => node_get_types(),
   );
-  $form['diggthis']['diggthis_button_display'] = array(
+  
+  $form['diggthis']['diggthis_button_teaser'] = array(
+    '#type' => 'select',
+    '#title' => t('Display button in teaser'),
+    '#default_value' => variable_get('diggthis_button_teaser', 0),
+    '#options' => array(0 => t('Disabled'),
+                        1 => t('Enabled')),
+    '#description' => t('Show digg button in teaser?'),
+  );
+  
+  $form['diggthis']['diggthis_button_fullview'] = array(
     '#type' => 'select',
-    '#title' => t('Display button in'),
-    '#default_value' => variable_get('diggthis_button_display', 0),
+    '#title' => t('Display button in full view'),
+    '#default_value' => variable_get('diggthis_button_fullview', 0),
     '#options' => array(0 => t('Disabled'),
-                        1 => t('Full-page view')),
-#                        2 => t('Teaser view'), 
-#                        3 => t('Teasers and full-page view')),
-    '#description' => t('When to display the Digg buttons.'),
+                        1 => t('Enabled')),
+    '#description' => t('Show digg button in full view?'),
   );
   return $form;
 }
 
+
+
+/**
+ * Implementation of hook_form_alter()
+ */
+function diggthis_form_alter($form_id, &$form) {
+  //Check we're altering the right form...
+  if (isset($form['type'])   &&   $form['type']['#value'] .'_node_form' == $form_id   &&   in_array($form['type']['#value'], variable_get('diggthis_node_types', array()), TRUE)) {
+    //Do we have a node value (ie editing or new?)
+    if($form['nid']['#value']) {
+      //We do - get the alternate URL (if there is one)
+      $alt_url = _diggthis_get_alt_url($form['nid']['#value']);
+    } else {
+      //Default to blank
+      $alt_url = "";
+    }
+    
+    //Create a fieldset - collapse it if there is no URL, expand it if there is one set
+    $form['diggthis'] = array(
+      '#type' => 'fieldset',
+      '#title' => t('Digg this'),
+      '#collapsible' => TRUE,
+      '#collapsed' => ($alt_url == ""),
+      '#tree' => TRUE,
+    );
+    
+    //Alternate URL field
+    $form['diggthis']['alternate_url'] = array(
+      '#type' => 'textfield',
+      '#title' => t('Alternate URL'),
+      '#default_value' => $alt_url,
+      '#description' => t('Enter an alternate URL or a digg story ID for the digg counter if required. If left blank, the current node URL will be used.'),
+    );
+  }
+}
+
+
+
 /**
  * Implementation of hook_nodeapi()
  */
-function diggthis_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
-  switch ($op) {
-    case 'view':
-      $node_type = in_array($node->type, variable_get('diggthis_node_types', array()), TRUE);
-      if ($node_type && user_access('access content') &&
-          variable_get('diggthis_button_display', 0)) {
-        $path = drupal_get_path('module', 'diggthis').'/';
-        theme_add_style($path . 'diggthis.css');
-        switch (variable_get('diggthis_button_display', 0)) {
-          case 1:
-            if ($page) {
-              $node->body = theme('diggthis_button', $node) . $node->body;
-            }
-            break;
-            /*
-          case 2:
-            if ($teaser) {
-              $node->teaser = theme('diggthis_button', $node) . $node->teaser;
-            }
-            break;
-          case 3:
-            if ($teaser) {
-              $node->teaser = theme('diggthis_button', $node) . $node->teaser;
-            } elseif ($page) {
-              $node->body = theme('diggthis_button', $node) . $node->body;
-            }
-            break;
-            */
+function diggthis_nodeapi($node, $op, $arg = 0) {
+  switch($op) {
+    case 'insert' : case 'update' :
+      //Get the alternate URL and trim spaces off (just in case)
+      $alt_url = trim($node->diggthis['alternate_url']);
+      
+      //If not empty, insert or update the table...
+      if(!empty($alt_url)) {
+        //a url is set - lets store it!
+        
+        if(is_null(_diggthis_get_alt_url($node->nid))) {
+          db_query("INSERT INTO {node_diggthis} (nid, diggurl) VALUES(%d, '%s')", $node->nid, $alt_url);
+        } else {
+          db_query("UPDATE {node_diggthis} dt SET dt.diggurl = '%s' WHERE dt.nid = %d", $alt_url, $node->nid);
         }
       }
+      //a url is not set - delete this nodes row from the table (if none exists, it just wont delete anything!
+      else {
+        db_query("DELETE FROM {node_diggthis} dt WHERE dt.nid = %d", $node->nid);
+      }
+      break;
+    
+    case 'load' :
+      //Load the alternate URL into the node
+      return array('diggthis' => array('alt_url' => _diggthis_get_alt_url($node->nid)));
+      break;
+      
+    case 'delete' :
+      //Delete the alternate node from the database
+      db_query("DELETE FROM {node_diggthis} dt WHERE dt.nid = %d", $node->nid);
       break;
   }
 }
 
-function digg_button_render($url, $title=NULL) {
-  if ($title) {
-    $link_text = t('Digg this!');
-    $img = '<img src="'. check_url(variable_get('diggthis_button_url','')) .'" alt="'. $link_text .'" />';
-    $href = 'http://digg.com/submit?phase=2&url='. $url .'&title='.$title;
-    $digg_button = l($img, $href, array('title' => $link_text), $query=NULL, $fragment=NULL, $absolute=FALSE, $html=TRUE);  
+
+//Private function to get the alternate URL of a node
+function _diggthis_get_alt_url($nid) {
+  $result = db_query("SELECT dt.* FROM {node_diggthis} dt WHERE dt.nid = %d", $nid);
+  if(db_num_rows($result)) {
+    $row = db_fetch_object($result);
+    return $row->diggurl;
   } else {
-$digg_button=<<<ENDJS
-<iframe src='http://digg.com/api/diggthis.php?u=$url' height='82' width='55' frameborder='0' scrolling='no'></iframe>
-ENDJS;
+    return NULL;
   }
-  return $digg_button;
 }
+
+
+
+/**
+ * Implementation of hook_link()
+ */
+function diggthis_link($type, $node = NULL, $teaser = FALSE) {
+  $links = array();
+  
+  //Only digg nodes, not comments
+  if($type == 'node') {
+    
+    //Check our node is one of the ok types
+    if(in_array($node->type, variable_get('diggthis_node_types', array()), TRUE)) {
+      
+      //Is this a teaser view and, if so, can we digg from the teaser?
+      if($teaser && variable_get('diggthis_button_teaser', 0)) {
+        $links[] = theme('diggthis_button', $node);
+      }
+      
+      //This isn't a teaser view - can we digg in fullview?
+      else {
+        if(variable_get('diggthis_button_fullview', 0)) {
+          $links[] = theme('diggthis_button', $node);
+        }
+      }
+    }
+  }
+  
+  return $links;
+}
+
+
+
+
+
+
+
 /**
  * Theme function
  */
 function theme_diggthis_button($node) {
-  $url = url("node/$node->nid", NULL, NULL, TRUE);
+  $url = isset($node->diggthis['alt_url']) ? drupal_urlencode($node->diggthis['alt_url']) : url("node/$node->nid", NULL, NULL, TRUE);
   $title = drupal_urlencode($node->title);
-  $digg_story_url = _request_diggthis($url);
-  if($digg_story_url) {
-     $digg_button = digg_button_render($digg_story_url,'');
+  $digg_story_data = _request_diggthis($url);
+  
+  if($digg_story_data) {
+    $digg_button = _digg_button_render($digg_story_data['HREF']);
   } else {
-    $digg_button = digg_button_render($url,$title);
+    $digg_button = _digg_button_render($url,$title);
   }
   return '<div class="diggthis_button">'. $digg_button .'</div>';
 }
 
+
+//Private function to render a button - NEEDS CLEANING UP
+function _digg_button_render($url, $title=NULL) {
+  if ($title) {
+    $link_text = t('Digg this!');
+    $img = '<img src="'. check_url(variable_get('diggthis_button_url','')) .'" alt="'. $link_text .'" />';
+    $href = 'http://digg.com/submit?phase=2&url='. $url .'&title='.$title;
+    $digg_button = l($img, $href, array('title' => $link_text), $query=NULL, $fragment=NULL, $absolute=FALSE, $html=TRUE);  
+  } else {
+    $digg_button = "<iframe src='http://digg.com/api/diggthis.php?u=$url' height='82' width='55' frameborder='0' scrolling='no'></iframe>";
+  }
+  return $digg_button;
+}
+
+
+
+
+
+
+
 /**
  * diggthis request and XML parser functions
  */
 function _request_diggthis($url) {
-  global $digg_story_url;
-  $request_url = 'http://services.digg.com/stories?link='. $url;
+  global $digg_story_data;
+  $digg_story_data = array();
+  
+  //Check which type of Digg URL we have...
+  if(preg_match("|[^0-9]|", $url)) {
+    //looks like a target URL - looks up diggs for that URL (but limit to 1 result)
+    $request_url = 'http://services.digg.com/stories?count=1&link=' . $url;
+  } else {
+    //Just numbers - must be an ID - lookup the ID
+    $request_url = 'http://services.digg.com/stories/' . $url;
+  }
+  
   $ch = curl_init($request_url);
   curl_setopt($ch, CURLOPT_HEADER, 0);
   ob_start();
@@ -138,19 +258,19 @@ function _request_diggthis($url) {
   xml_set_element_handler($xml_parser, "_startElement", "_endElement");
   xml_parse($xml_parser, $xml);
   xml_parser_free($xml_parser);
-  if($digg_story_url) {
-    return $digg_story_url;
+  if($digg_story_data) {
+    return $digg_story_data;
   }
   return FALSE;
 }
+
 function _startElement($parser, $name, $attrs) {
-  global $digg_story_url;
-  if ($name == 'STORY' && $attrs['HREF']) {
-    $digg_story_url = $attrs['HREF'];
+  global $digg_story_data;
+  
+  if ($name == 'STORY') {
+    $digg_story_data = $attrs;
   }
-  return;
 }
+
 function _endElement($parser, $name) {
-  return;
-}
-?>
\ No newline at end of file
+}
\ No newline at end of file
