? radioactivity-module-247212-10.patch
? radioactivity-module-251656-3.patch
Index: radioactivity.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/radioactivity/radioactivity.module,v
retrieving revision 1.8
diff -u -p -r1.8 radioactivity.module
--- radioactivity.module	28 Apr 2008 06:55:30 -0000	1.8
+++ radioactivity.module	29 Apr 2008 15:20:48 -0000
@@ -104,6 +104,9 @@ function _radioactivity_get_decay_profil
     if (!isset($decay_profile["energy_view"])) $decay_profile["energy_view"]=1;
     if (!isset($decay_profile["energy_comment_insert"])) $decay_profile["energy_comment_insert"]=0;
     if (!isset($decay_profile["energy_comment_publish"])) $decay_profile["energy_comment_publish"]=0;
+
+    // defaults, backwards compatibility to 5.x-1.1
+    if (!isset($decay_profile["by_content_type"])) $decay_profile["by_content_type"]=0;
   }
   return $decay_profiles;
 }
@@ -202,7 +205,8 @@ function radioactivity_admin_profile_for
   } else {
     // defaults for new
     $decay_profile=array('half_life' => 6*3600, 'cut_off_energy' => 0.5,
-                         'energy_view' => 1, 'energy_comment_insert' => 0, 'energy_comment_publish' => 0);
+                         'energy_view' => 1, 'energy_comment_insert' => 0, 'energy_comment_publish' => 0,
+                         'by_content_type' => 0);
   }
 
   $form['label']=
@@ -240,26 +244,64 @@ function radioactivity_admin_profile_for
           '#tree' => TRUE,
           '#title' => t('Energy settings'));
 
-  $form['energy']['view']=
-    array('#type' => 'textfield',
-          '#title' => t('Incident energy from view'),
-          '#required' => TRUE,
-          '#description' => t('The amount of energy the node receives per page view.'),
-          '#default_value' => $decay_profile['energy_view']);
-
-  $form['energy']['comment_insert']=
-    array('#type' => 'textfield',
-          '#title' => t('Incident energy from submitted comment'),
-          '#required' => TRUE,
-          '#description' => t('The amount of energy the node receives when a comment is submitted.'),
-          '#default_value' => $decay_profile['energy_comment_insert']);
-
-  $form['energy']['comment_publish']=
-    array('#type' => 'textfield',
-          '#title' => t('Incident energy from published comment'),
-          '#required' => TRUE,
-          '#description' => t('The amount of energy the node receives when a comment is published by a moderator.'),
-          '#default_value' => $decay_profile['energy_comment_publish']);
+  $form['energy']['by_content_type']=
+    array('#type' => 'checkbox',
+          '#title' => t('Content type specific settings'),
+          '#description' => t('Set to enable content type specific settings.'),
+          '#default_value' => $decay_profile['by_content_type']);
+
+  if (!$decay_profile['by_content_type']) {
+    // values are not by content type
+    $form['energy']['view']=
+      array('#type' => 'textfield',
+            '#title' => t('Incident energy from view'),
+            '#required' => TRUE,
+            '#description' => t('The amount of energy the node receives per page view.'),
+            '#default_value' => $decay_profile['energy_view']);
+
+    $form['energy']['comment_insert']=
+      array('#type' => 'textfield',
+            '#title' => t('Incident energy from submitted comment'),
+            '#required' => TRUE,
+            '#description' => t('The amount of energy the node receives when a comment is submitted.'),
+            '#default_value' => $decay_profile['energy_comment_insert']);
+
+    $form['energy']['comment_publish']=
+      array('#type' => 'textfield',
+            '#title' => t('Incident energy from published comment'),
+            '#required' => TRUE,
+            '#description' => t('The amount of energy the node receives when a comment is published by a moderator.'),
+            '#default_value' => $decay_profile['energy_comment_publish']);
+  } else {
+    foreach (node_get_types('types') as $node_type) {
+      // values are by content type
+      $type = $node_type->type;
+      $form['energy'][$type]=
+        array('#type' => 'fieldset',
+              '#tree' => TRUE,
+              '#title' => check_plain($node_type->name));
+      $form['energy'][$type]['view']=
+        array('#type' => 'textfield',
+              '#title' => t('Incident energy from view'),
+              '#required' => TRUE,
+              '#description' => t('The amount of energy the node receives per page view.'),
+              '#default_value' => $decay_profile['energy_view'][$type]);
+
+      $form['energy'][$type]['comment_insert']=
+        array('#type' => 'textfield',
+              '#title' => t('Incident energy from submitted comment'),
+              '#required' => TRUE,
+              '#description' => t('The amount of energy the node receives when a comment is submitted.'),
+              '#default_value' => $decay_profile['energy_comment_insert'][$type]);
+
+      $form['energy'][$type]['comment_publish']=
+        array('#type' => 'textfield',
+              '#title' => t('Incident energy from published comment'),
+              '#required' => TRUE,
+              '#description' => t('The amount of energy the node receives when a comment is published by a moderator.'),
+              '#default_value' => $decay_profile['energy_comment_publish'][$type]);
+    }
+  }
 
   $form['buttons']['submit'] = array('#type' => 'submit', '#value' => t('Save decay profile') );
   $form['buttons']['delete'] = array('#value' => l(t('Delete profile'), 'admin/settings/radioactivity/profile_'.$dpid.'/delete'));
@@ -271,6 +313,49 @@ function radioactivity_admin_profile_for
   return $form;
 }
 
+
+/**
+ * Helper for radioactivity_admin_profile_form_submit
+ *
+ * Retrieves energy value from the form.
+ * Parameters:
+ *   $form         The form
+ *   $content_type Content type or NULL for not content-type specific
+ *   $energy_type  Energy type (e.g. view, comment_insert, comment_publish)
+ *
+ * If $content_type is not NULL and content type does not exist, try generic value.
+ * If $content_type is NULL and generic value does not exist, try first content_type specific value.
+ *
+ * These help when transitioning from content type specific and non specific settings.
+ */
+function _radioactivity_admin_profile_form_get_energy_value($form, $content_type, $energy_type) {
+  if ($content_type!==NULL) {
+    // content-type specific
+    if (isset($form['energy'][$content_type][$energy_type])) {
+      // ok, specific value found
+      return $form['energy'][$content_type][$energy_type];
+    } else {
+      // use generic value
+      return $form['energy'][$energy_type];
+    }
+  } else {
+    if (isset($form['energy'][$energy_type])) {
+      // ok, generic value found
+      return $form['energy'][$energy_type];
+    } else {
+      // use first specific value found
+      foreach (node_get_types('types') as $node_type) {
+        if (isset($form['energy'][$node_type->type][$energy_type])) {
+          return $form['energy'][$node_type->type][$energy_type];
+        }
+      }
+      // ok, none found
+      return FALSE;
+    }
+  }
+  // we should never reach here...
+}
+
 function radioactivity_admin_profile_form_submit($form_id, $form) {
   $dpid=(int)$form['decay_profile_id'];
 
@@ -302,10 +387,26 @@ function radioactivity_admin_profile_for
   $decay_profile['half_life']=$half_life;
 
   $decay_profile['cut_off_energy']=(double)$form['cut_off_energy'];
+  $decay_profile['by_content_type']=$form['energy']['by_content_type'];
 
-  $decay_profile['energy_view']=(double)$form['energy']['view'];
-  $decay_profile['energy_comment_insert']=(double)$form['energy']['comment_insert'];
-  $decay_profile['energy_comment_publish']=(double)$form['energy']['comment_publish'];
+  if (!$decay_profile['by_content_type']) {
+    // not by content type
+    $decay_profile['energy_view']=(double)_radioactivity_admin_profile_form_get_energy_value($form, NULL, 'view');
+    $decay_profile['energy_comment_insert']=(double)_radioactivity_admin_profile_form_get_energy_value($form, NULL, 'comment_insert');
+    $decay_profile['energy_comment_publish']=(double)_radioactivity_admin_profile_form_get_energy_value($form, NULL, 'comment_publish');
+  } else {
+    // by content type
+    $decay_profile['energy_view']=array();
+    $decay_profile['energy_comment_insert']=array();
+    $decay_profile['energy_comment_publish']=array();
+
+    foreach (node_get_types('types') as $node_type) {
+      $type=$node_type->type;
+      $decay_profile['energy_view'][$type]=(double)_radioactivity_admin_profile_form_get_energy_value($form, $type, 'view');
+      $decay_profile['energy_comment_insert'][$type]=(double)_radioactivity_admin_profile_form_get_energy_value($form, $type, 'comment_insert');
+      $decay_profile['energy_comment_publish'][$type]=(double)_radioactivity_admin_profile_form_get_energy_value($form, $type, 'comment_publish');
+    }
+  }
 
   $decay_profiles[$dpid]=$decay_profile;
 
@@ -400,6 +501,17 @@ function _radioactivity_add_energy_std($
   }
 }
 
+function _radioactivity_get_node_type($nid) {
+  static $cached_types=array();
+
+  if (!isset($cached_types[$nid])) {
+    $type = db_result(db_query("SELECT type FROM {node} WHERE nid = '$nid'"));
+    $cached_types[$nid]=$type;
+  }
+
+  return $cached_types[$nid];
+}
+
 /**
  * Add energy to nodes
  * @return TRUE if successful
@@ -411,7 +523,17 @@ function radioactivity_add_energy($nid, 
   $db_type=$GLOBALS['db_type'];
 
   foreach (_radioactivity_get_decay_profiles() as $dpid => $decay_profile) {
-    $amount=$decay_profile["energy_".$energy_type];
+    // determine amount
+    if (!$decay_profile["by_content_type"]) {
+      // not by content type
+      $amount=$decay_profile["energy_".$energy_type];
+    } else {
+      // by content type
+      $type=_radioactivity_get_node_type($nid);
+      if ($type) {
+        $amount=$decay_profile["energy_".$energy_type][$type];
+      }
+    }
     if ($amount==0) continue;
 
     switch ($db_type) {
