? .DS_Store
? user_titles_tid.patch
Index: admin.js
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/user_titles/admin.js,v
retrieving revision 1.8
diff -u -p -r1.8 admin.js
--- admin.js	20 Jul 2007 20:05:14 -0000	1.8
+++ admin.js	21 Jul 2007 01:20:52 -0000
@@ -35,7 +35,7 @@ Drupal.UserTitles.addTitle = function() 
   // Build the new title
   var html = '<tr class="' + (num % 2 ? 'odd' : 'even') + '">';
   html += '<td class="num-posts"><input type="text" name="titles[' + real_num + '][value]" size="2" value="' + value + '"></td>';
-  html += '<td class="user-title"><input type="text" name="titles[' + real_num + '][title]" size="30" value="' + title + '"></td>';
+  html += '<td class="user-title"><input type="text" name="titles[' + real_num + '][title]" size="30" value="' + title + '"><input type="hidden" name="titles[' + real_num + '][tid]" value="new" /></td>';
   html += '<td><img src="' + Drupal.settings.userTitles.imageURL + '" alt="Remove this title" title="Remove this title" width="16" height="16" class="title-delete" /></td>';
   html += '</tr>';
 
Index: user_titles.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/user_titles/user_titles.install,v
retrieving revision 1.2
diff -u -p -r1.2 user_titles.install
--- user_titles.install	20 Jul 2007 18:18:45 -0000	1.2
+++ user_titles.install	21 Jul 2007 01:20:52 -0000
@@ -3,9 +3,21 @@
 
 function user_titles_install() {
   db_query("
-    CREATE TABLE {user_titles} (
-    uid int(10) NOT NULL PRIMARY KEY,
-    posts integer
-  );
+      CREATE TABLE {user_titles} (
+      tid int(10) NOT NULL PRIMARY KEY,
+      title varchar(255) NOT NULL,
+      value int(10) NOT NULL
+    );
   ");
+  db_query("
+      CREATE TABLE {user_titles_posts} (
+      uid int(10) NOT NULL PRIMARY KEY,
+      posts integer
+    );
+  ");
+}
+
+function user_titles_uninstall() {
+  db_query("DROP TABLE {user_titles}");
+  db_query("DROP TABLE {user_titles_posts}");
 }
Index: user_titles.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/user_titles/user_titles.module,v
retrieving revision 1.4
diff -u -p -r1.4 user_titles.module
--- user_titles.module	20 Jul 2007 18:22:50 -0000	1.4
+++ user_titles.module	21 Jul 2007 01:20:52 -0000
@@ -73,18 +73,22 @@ function user_titles_settings_form() {
   $actual_titles = $form['num_titles']['#value'];
 
   $form['titles'] = array('#tree' => TRUE);
-  $keys = array_keys($titles);
-
+  
   for ($i = 0; $i < $actual_titles; $i++) {
+    $title = isset($titles[$i]) ? $titles[$i] : array('tid' => 'new', 'value' => '', 'title' => '');
     $form['titles'][$i]['value'] = array(
       '#type' => 'textfield',
       '#size' => 2,
-      '#default_value' => isset($keys[$i]) ? ($keys[$i]) : '',
+      '#default_value' => $title['value'],
     );
     $form['titles'][$i]['title'] = array(
       '#type' => 'textfield',
       '#size' => 30,
-      '#default_value' => isset($keys[$i]) ? ($titles[$keys[$i]]) : '',
+      '#default_value' => $title['title'],
+    );
+    $form['titles'][$i]['tid'] = array(
+      '#type' => 'hidden',
+      '#default_value' => $title['tid'],
     );
   }
 
@@ -132,16 +136,13 @@ function theme_user_titles_settings_form
 
   $output = '<div class="left">';
 
-  foreach ($form['titles'] as $id => $e) {
-    if (!is_numeric($id)) {
-      continue;
-    }
+  foreach (element_children($form['titles']) as $key) {
     // set a reference so that the drupal_render gets remembered.
-    unset($elem);
-    $elem = &$form['titles'][$id];
+    $elem = &$form['titles'][$key];
+    
     $rows[] = array(
       array('data' => drupal_render($elem['value']), 'class' => 'num-posts'), 
-      array('data' => drupal_render($elem['title']), 'class' => 'user-title'),
+      array('data' => drupal_render($elem['title']) . drupal_render($elem['tid']), 'class' => 'user-title'),
       theme('image', drupal_get_path('module', 'user_titles') . '/delete.gif', t('Remove this title'), t('Remove this title'), array('class' => "title-delete")),
     );
   }
@@ -195,14 +196,14 @@ function user_titles_settings_form_submi
     if (empty($title['value']) && empty($title['title'])) {
       continue;
     }
-    $titles[$title['value']] = $title['title'];
+    $titles[] = $title;
   }
   user_titles_set_titles($titles);
 
   // If they change the allowed types, wipe the existing counts so each one will be fresh.
   $types = user_titles_get_allowed_types();
   if ($types != $form_values['types']) {
-    db_query("DELETE FROM {user_titles}");
+    db_query("TRUNCATE {user_titles_posts}");
   }
 
   user_titles_set_allowed_types(array_keys(array_filter($form_values['types'])));
@@ -217,7 +218,7 @@ function user_titles_nodeapi(&$node, $op
     case 'insert':
       $allowed_types = user_titles_get_allowed_types();
       if (in_array($node->type, $allowed_types)) {
-        db_query("REPLACE INTO {user_titles} (uid, posts) VALUES (%d, %d)", $node->uid, user_titles_get_posts($node->uid) + 1);
+        db_query("REPLACE INTO {user_titles_posts} (uid, posts) VALUES (%d, %d)", $node->uid, user_titles_get_posts($node->uid) + 1);
       }
   }
 }
@@ -227,7 +228,7 @@ function user_titles_nodeapi(&$node, $op
  */
 function user_titles_comment($a1, $op) {
   if ($op == 'insert') {
-    db_query("REPLACE INTO {user_titles} (uid, posts) VALUES (%d, %d)", $a1->uid, user_titles_get_posts($a1->uid) + 1);
+    db_query("REPLACE INTO {user_titles_posts} (uid, posts) VALUES (%d, %d)", $a1->uid, user_titles_get_posts($a1->uid) + 1);
   }
 }
 
@@ -300,7 +301,7 @@ function user_titles_update_post_count($
     }
   }
   // mysql only query
-  db_query("REPLACE INTO {user_titles} (uid, posts) VALUES (%d, %d)", $uid, $count);
+  db_query("REPLACE INTO {user_titles_posts} (uid, posts) VALUES (%d, %d)", $uid, $count);
   return $count;
 }
 
@@ -322,13 +323,28 @@ function user_titles_set_allowed_types($
  * Get titles from db
  */
 function user_titles_get_titles() {
-  return variable_get('user_titles', array());
+  $query = db_query("SELECT title, value, tid FROM {user_titles} ORDER BY value DESC");
+  if(db_num_rows($query)) {
+    while($r = db_fetch_array($query)) {
+      $result[] = $r;
+    }
+  }
+  else {
+    $result = array();
+  }
+  return $result;
 }
 
 /**
  * Store titles in the db
  */
 function user_titles_set_titles($titles) {
-  krsort($titles);
+  db_query("TRUNCATE {user_titles}");
+  foreach($titles as $title) {
+    if($title['tid'] == 'new') {
+      $title['tid'] = db_next_id("{user_titles}_tid");
+    }
+    db_query("INSERT INTO {user_titles} (title, value, tid) VALUES ('%s', %d, %d)", $title['title'], $title['value'], $title['tid']);
+  }
   return variable_set('user_titles', $titles);
 }
