? 348000_5x_comment_notify_user_settings_3.patch
? 348000_5x_comment_notify_user_settings_4.patch
Index: comment_notify.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/comment_notify/comment_notify.install,v
retrieving revision 1.4.2.5
diff -u -p -r1.4.2.5 comment_notify.install
--- comment_notify.install	4 Feb 2009 23:22:02 -0000	1.4.2.5
+++ comment_notify.install	5 Feb 2009 00:44:14 -0000
@@ -20,6 +20,13 @@ function comment_notify_install() {
         PRIMARY KEY (cid),
         KEY notify_hash (notify_hash)
       ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
+      // Create a table to store the per-user notification settings.
+      $status[] = db_query("CREATE TABLE {comment_notify_user_settings} (
+        uid int unsigned NOT NULL default 0,
+        node_notify tinyint unsigned NOT NULL default 0,
+        comment_notify tinyint unsigned NOT NULL default 0,
+        PRIMARY KEY (uid)
+      ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
       // Insert a record for each existing comment.
       $status[] = db_query("INSERT INTO {comment_notify} (cid, notify, notify_hash) SELECT c.cid, 0, md5(concat(c.mail, ifnull(u.mail, u.init), c.uid, c.name, c.nid)) FROM {comments} c LEFT OUTER JOIN {users} u on c.uid = u.uid");
       break;
@@ -31,6 +38,12 @@ function comment_notify_install() {
         notify_hash varchar(32) NOT NULL default '',
         PRIMARY KEY (cid)
       )");
+      $status[] = db_query("CREATE TABLE {comment_notify_user_settings} (
+        uid serial CHECK (uid >= 0),
+        node_notify smallint_unsigned NOT NULL default '0',
+        comment_notify smallint_unsigned NOT NULL default '0',
+        PRIMARY KEY (uid)
+      )");
       $status[] = db_query("CREATE INDEX {comment_notify}_notify_hash_idx ON {comment_notify} (notify_hash)");
       $status[] = db_query("INSERT INTO {comment_notify} (cid, notify, notify_hash) SELECT c.cid, 0, md5(concat(c.mail, ifnull(u.mail, u.init), c.uid, c.name, c.nid)) FROM {comments} c LEFT OUTER JOIN {users} u on c.uid = u.uid");
       break;
@@ -49,6 +62,9 @@ function comment_notify_uninstall() {
   if (db_table_exists('comment_notify')) {
     db_query("DROP TABLE {comment_notify}");
   }
+  if (db_table_exists('comment_notify_user_settings')) {
+    db_query("DROP TABLE {comment_notify_user_settings}");
+  }
   variable_del('node_notify_default_mailtext');
   db_query("DELETE FROM {variable} WHERE name LIKE 'comment_notify_%'");
 }
@@ -72,7 +88,7 @@ function comment_notify_update_2() {
   else {
     drupal_set_message(t('comment_notify module weight config update unsuccessful.'), 'error');
   }
-  
+
   $ret[] = $result;
   return $ret;
 }
@@ -166,4 +182,118 @@ function comment_notify_update_5200() {
       break;
   }
   return $ret;
+}
+
+/**
+ * Copy settings from {users}.data into {comment_notify_user_settings}.
+ */
+function comment_notify_update_5201() {
+  $ret = array();
+
+  // This determines how many users will be processed in each batch run.
+  $num_users_per_batch = 100;
+
+  // Multi-part update.
+  if (!isset($_SESSION['comment_notify_update_5202'])) {
+    // We need to start at uid 1, so initialize our variable
+    // to the value below that.
+    $_SESSION['comment_notify_update_5202'] = 1;
+    $_SESSION['comment_notify_update_5202_max'] = db_result(db_query("SELECT MAX(uid) FROM {users}"));
+
+    // Create the new table.  This will only happen in the first batch of this
+    // function.
+    switch ($GLOBALS['db_type']) {
+      case 'mysql':
+      case 'mysqli':
+        $ret[] = update_sql("CREATE TABLE {comment_notify_user_settings} (
+          uid int unsigned NOT NULL default 0,
+          node_notify tinyint unsigned NOT NULL default 0,
+          comment_notify tinyint unsigned NOT NULL default 0,
+          PRIMARY KEY (uid)
+        ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
+        break;
+      case 'pgsql':
+        $ret[] = update_sql("CREATE TABLE {comment_notify_user_settings} (
+          uid serial CHECK (uid >= 0),
+          node_notify smallint_unsigned NOT NULL default '0',
+          comment_notify smallint_unsigned NOT NULL default '0',
+          PRIMARY KEY (uid)
+        )");
+        break;
+    }
+  }
+
+  // Do the next batch of the deed.
+
+  // Find the next N records to update, or do the final batch.
+  $next = min($_SESSION['comment_notify_update_5202'] + $num_users_per_batch, $_SESSION['comment_notify_update_5202_max']);
+
+  // Check to make sure that the {comment_notify_user_settings} table exists.
+  // If for some reason it was not created above, we might lose data when
+  // we delete the comment_notify data that is currently in {users}.data.
+  // If the table doesn't exist, then alert the user and don't allow any
+  // more batches to be processed.
+  if (!db_table_exists('comment_notify_user_settings')) {
+    unset($_SESSION['comment_notify_update_5202']);
+    unset($_SESSION['comment_notify_update_5202_max']);
+
+    // Alert the user that there was an error.
+    $ret[] = array('success' => FALSE, 'query' => t('For some reason the {comment_notify_user_settings} table was not properly created, and so per-user comment_notify settings could not be copied from {users}.data.  You will need to run this update again.'));
+    return $ret;
+  }
+
+  // Transfer the data in our specified range of uid values.
+  $uid = $_SESSION['comment_notify_update_5202'];
+  while ($uid < $next) {
+    // Get the value of {users}.data.
+    $data = db_result(db_query('SELECT data FROM {users} WHERE uid = %d', $uid));
+    $settings = array('uid' => $uid);
+    if (!empty($data)) {
+      $data = unserialize($data);
+      if (isset($data['node_notify_mailalert'])) {
+        $settings['node_notify'] = $data['node_notify_mailalert'];
+        unset($data['node_notify_mailalert']);
+      }
+      if (isset($data['comment_notify_mailalert'])) {
+        $settings['comment_notify'] = $data['comment_notify_mailalert'];
+        unset($data['comment_notify_mailalert']);
+      }
+      $fields_sql = '';
+      $values_sql = '';
+      foreach ($settings as $field => $value) {
+        $fields_sql .= "$field, ";
+        $values_sql .= '%d, ';
+      }
+      // Trim off any trailing commas and spaces.
+      $fields_sql = rtrim($fields_sql, ', ');
+      $values_sql = rtrim($values_sql, ', ');
+
+      // Add this user and settings to {comment_notify_user_settings} only if
+      // at least one setting was found in {users}.data for this user.
+      if (count($settings) > 1) {
+        db_query("INSERT INTO {comment_notify_user_settings} ($fields_sql) VALUES ($values_sql)", $settings);
+
+        // Remove this comment_notify data from {users}.data.
+        db_query("UPDATE {users} SET data = '%s' WHERE uid = %d", serialize($data), $uid);
+      }
+    }
+    $uid++;
+  }
+
+  // Remember where we left off.
+  $_SESSION['comment_notify_update_5202'] = $next;
+
+  if ($_SESSION['comment_notify_update_5202'] == $_SESSION['comment_notify_update_5202_max']) {
+    // We're done, clear these out.
+    unset($_SESSION['comment_notify_update_5202']);
+    unset($_SESSION['comment_notify_update_5202_max']);
+
+    // Provide an explaination of what we did.
+    $ret[] = array('success' => TRUE, 'query' => t('Moved comment_notify user settings data from the {users} table into the {comment_notify_user_settings} table.'));
+  }
+  else {
+    // Report how much is left to complete.
+    $ret['#finished'] = $_SESSION['comment_notify_update_5202'] / $_SESSION['comment_notify_update_5202_max'];
+  }
+  return $ret;
 }
\ No newline at end of file
Index: comment_notify.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/comment_notify/comment_notify.module,v
retrieving revision 1.8.2.24
diff -u -p -r1.8.2.24 comment_notify.module
--- comment_notify.module	16 Dec 2008 13:20:42 -0000	1.8.2.24
+++ comment_notify.module	5 Feb 2009 00:44:14 -0000
@@ -132,7 +132,7 @@ function comment_notify_form_alter($form
       '#options' => $available_options,
     );
   }
-  // Always show the checkbox for registered users. 
+  // Always show the checkbox for registered users.
   // If you want to hide this on your site see http://drupal.org/node/322482
   $form['notify'] = array(
     '#type' => 'select',
@@ -306,8 +306,36 @@ function comment_notify_user($type, &$ed
         );
         return $form;
       }
+      break;
 
+    case 'submit':
+      // Save the values of node_notify_mailalert and comment_notify_mailalert
+      // to {comment_notify_user_settings}.
+      if (db_result(db_query('SELECT uid FROM {comment_notify_user_settings} WHERE uid = %d', $user->uid))) {
+        db_query('UPDATE {comment_notify_user_settings} SET node_notify = %d, comment_notify = %d WHERE uid = %d', $edit['node_notify_mailalert'], $edit['comment_notify_mailalert'], $user->uid);
+      }
+      else {
+        db_query('INSERT INTO {comment_notify_user_settings} (uid, node_notify, comment_notify) VALUES (%d, %d, %d)', $user->uid, $edit['node_notify_mailalert'], $edit['comment_notify_mailalert']);
+      }
+
+      // Unset them from $user so they don't also get saved into {users}.data.
+      unset($edit['node_notify_mailalert']);
+      unset($edit['comment_notify_mailalert']);
       break;
+
+    case 'load':
+      $user_settings = db_fetch_array(db_query('SELECT node_notify AS node_notify_mailalert, comment_notify AS comment_notify_mailalert FROM {comment_notify_user_settings} WHERE uid = %d', $user->uid));
+      if (count($user_settings)) {
+        foreach ($user_settings as $property => $value) {
+          $user->$property = $value;
+        }
+      }
+      break;
+
+    case 'delete':
+      db_query('DELETE FROM {comment_notify_user_settings} WHERE uid = %d', $user->uid);
+      break;
+
   }
 }
 
@@ -371,7 +399,7 @@ function _comment_notify_mailalert($comm
     drupal_mail('node_notify_mail', $author->mail, $subject, $message, $from, array());
     $sent_to[] = $author->mail;
   }
- 
+
   //Get the list of commenters to notify
   $result = db_query("SELECT DISTINCT c.cid, c.uid, c.name, c.nid, c.mail AS cmail, u.mail AS umail, u.init AS uinit, c.uid, c.name, cn.notify, cn.notify_hash
     FROM {comments} c INNER JOIN {comment_notify} cn on c.cid = cn.cid LEFT OUTER JOIN {users} u ON c.uid = u.uid
