Index: vud_node.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/vote_up_down/vud_node/Attic/vud_node.install,v
retrieving revision 1.1.2.4
diff -u -p -r1.1.2.4 vud_node.install
--- vud_node.install	15 Jan 2010 02:36:29 -0000	1.1.2.4
+++ vud_node.install	9 May 2010 12:08:49 -0000
@@ -31,6 +31,7 @@ function vud_node_uninstall() {
   variable_del('vud_node_votes');
   variable_del('vud_node_reset');
   variable_del('vud_node_widget_vote_on_teaser');
+  variable_del('vud_node_per_node');
 }
 
 /**
Index: vud_node.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/vote_up_down/vud_node/Attic/vud_node.module,v
retrieving revision 1.1.2.30
diff -u -p -r1.1.2.30 vud_node.module
--- vud_node.module	1 May 2010 08:18:23 -0000	1.1.2.30
+++ vud_node.module	9 May 2010 12:08:49 -0000
@@ -70,6 +70,12 @@ function vud_node_admin_settings() {
     '#default_value' => variable_get('vud_node_types', array()),
     '#options'       => node_get_types('names'),
   );
+  $form['vud_node_per_node'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Toggle on/off per node'),
+    '#description' => t('Check this if it should be possible to turn off voting on a per node basis.'),
+    '#default_value' => variable_get('vud_node_per_node', FALSE),
+  );
   $form['vud_node_widget'] = array(
     '#title'         => t('Widget selection'),
     '#description'   => t('Select the voting widget theme that will be displayed.'),
@@ -111,17 +117,28 @@ return system_settings_form($form);
  * Implementation of hook_nodeapi().
  */
 function vud_node_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
-  switch ($op) {
-    case 'view':
-      if (($can_edit=user_access('use vote up/down on nodes')) || user_access('view vote up/down count on nodes')) {
-        $node_type = in_array($node->type, variable_get('vud_node_types', array()), TRUE);
-        $widget_showmode = variable_get('vud_node_widget_show', VUD_NODE_DISPLAY_BOTH);
-        $tag = variable_get('vud_tag', 'vote');
-        $widget = variable_get('vud_node_widget', 'plain');
-        $vote_on_teaser = (bool)variable_get('vud_node_widget_vote_on_teaser', TRUE);
-        $teaser = $a3;
+  if (vud_node_is_valid_node($node)) {
+    switch ($op) {
+      case 'insert':
+      case 'update':
+        vud_status_save('node', $node->nid, $node->vud_status);
+        break;
+
+      case 'delete':
+        vud_status_delete('node', $node->nid);
+        break;
+
+      case 'load':
+        $node->vud_status = vud_status_get('node', $node->nid);
+        break;
 
-        if ($node_type) {
+      case 'view':
+        if (vud_status_get('node', $node->nid) && (($can_edit = user_access('use vote up/down on nodes')) || user_access('view vote up/down count on nodes'))) {
+          $widget_showmode = variable_get('vud_node_widget_show', VUD_NODE_DISPLAY_BOTH);
+          $tag = variable_get('vud_tag', 'vote');
+          $widget = variable_get('vud_node_widget', 'plain');
+          $vote_on_teaser = (bool)variable_get('vud_node_widget_vote_on_teaser', TRUE);
+          $teaser = $a3;
 
           switch ($widget_showmode) {
             case VUD_NODE_DISPLAY_TEASER_ONLY:
@@ -167,10 +184,9 @@ function vud_node_nodeapi(&$node, $op, $
             ),
             'setting'
           );
-
         }
+        break;
       }
-      break;
   }
 }
 
@@ -280,3 +296,113 @@ function vud_node_link($type, $object, $
   }
   return $links;
 }
+
+/**
+ * Helper function to check for valid individual nodes.
+ * Use vud_node_is_valid_type() if you only wan't to check
+ * for valid node types.
+ *
+ * @param $node
+ *   Either a full node object or a node ID.
+ *
+ * @param $reset
+ *   Whether or not to reset the static cache.
+ *
+ * @return
+ *   Return TRUE if voting is enabled for the node or FALSE
+ *   if voting was disabled for the node.
+ */
+function vud_node_is_valid_node($node, $reset = FALSE) {
+  static $cache = array();
+
+  // The argument was a node object.
+  if (is_object($node)) {
+    $nid = $node->nid;
+    $node_type = $node->type;
+  }
+  // The argument was a node id.
+  else {
+    $nid = $node;
+    // We have to limit the result if revisions is enabled.
+    // Because if revisions is enabled we will have multiple
+    // rows returned.
+    $node_type = db_result(db_query('SELECT type FROM {node} WHERE nid = %d LIMIT 1', $nid));
+  }
+
+  if (!isset($cache[$nid]) || $reset) {
+    // First, check if the node type is valid.
+    if (vud_node_is_valid_type($node_type, $reset)) {
+      // We've enabled the ability to toggle voting on/off per
+      // node, so we must check the status in the db.
+      if (variable_get('vud_node_per_node', FALSE)) {
+        $cache[$nid] = (bool)db_result(db_query("SELECT COUNT(*) FROM {vud_status} WHERE type = 'node' AND content_id = %d AND status = 1", $nid));
+      }
+      else {
+        $cache[$nid] = TRUE;
+      }
+    }
+    else {
+      $cache[$nid] = FALSE;
+    }
+  }
+  return $cache[$nid];
+}
+
+/**
+ * Helper function to check for valid node types.
+ *
+ * @param $type_name
+ *   The machine readable name of a node type.
+ *
+ * @param $reset
+ *   Whether or not to reset the static cache.
+ *
+ * @return
+ *   TRUE for node types that has voting enabled, otherwise FALSE. 
+ */
+function vud_node_is_valid_type($type_name, $reset = FALSE) {
+  static $cache = array();
+  if (!isset($cache[$type_name]) || $reset) {
+    // The parameter was a content type name string.
+    if (is_string($type_name) && in_array($type_name, array_filter(variable_get('vud_node_types', array())))) {
+      $cache[$type_name] = TRUE;
+    }
+    else {
+      $cache[$type_name] = FALSE;
+    }
+  }
+  return $cache[$type_name];
+}
+
+/**
+ * Implementation of hook_form_alter().
+ */
+function vud_node_form_alter(&$form, &$form_state, $form_id) {
+  if (isset($form['#node']) && $form_id == $form['#node']->type . '_node_form' && vud_node_is_valid_type($form['#node']->type)) {
+    $status = isset($form['#node']->vud_status) ? $form['#node']->vud_status : 0;
+    $form['vud'] = array(
+      '#type' => 'fieldset',
+      '#title' => t('Vote Up/Down'),
+      'vud_status' => array(
+        '#type' => 'checkbox',
+        '#title' => t('Enable up/down voting'),
+        '#default_value' => $status,
+      ),
+    );
+  }
+}
+
+/**
+ * Implementation of hook_content_extra_fields().
+ */
+function vud_node_content_extra_fields($type_name) {
+  if (vud_node_is_valid_type($type_name) && variable_get('vud_node_per_node', FALSE)) {
+    $extra = array();
+    $extra['vud'] = array(
+      'label' => t('Vote Up/Down'),
+      'description' => t('Vote Up/Down settings'),
+      'weight' => 0,
+    );
+  }
+  return $extra;
+}
