? .DS_Store
? user_titles_tid.patch
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	20 Jul 2007 22:13:37 -0000
@@ -3,9 +3,16 @@
 
 function user_titles_install() {
   db_query("
+    CREATE TABLE {user_titles_posts} (
+      uid int(10) NOT NULL PRIMARY KEY,
+      posts integer
+    );
+  ");
+  db_query("
     CREATE TABLE {user_titles} (
-    uid int(10) NOT NULL PRIMARY KEY,
-    posts integer
-  );
+      tid int(10) NOT NULL PRIMARY KEY,
+      posts int(10),
+      title varchar(255),
+    );
   ");
 }
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	20 Jul 2007 22:13:37 -0000
@@ -60,31 +60,23 @@ function user_titles_settings_form() {
   );
 
   $titles = user_titles_get_titles();
-  $form['num_titles'] = array(
-    '#type' => 'hidden',
-    '#default_value' => count($titles),
-    '#parents' => array('num_titles'), // necessary because of the early building we do
-    '#post' => $_POST, // Necessary to get the right cache of data
-  );
-
-  // Build the form bit so we can get the actual number of titles already in
-  // the form, which may have been changed thanks to javascript.
-  $form['num_titles'] = form_builder('user_titles_settings_form', $form['num_titles']); 
-  $actual_titles = $form['num_titles']['#value'];
-
   $form['titles'] = array('#tree' => TRUE);
   $keys = array_keys($titles);
 
-  for ($i = 0; $i < $actual_titles; $i++) {
-    $form['titles'][$i]['value'] = array(
+  foreach($titles as $title) {
+    $form['titles'][$title['tid']]['posts'] = array(
       '#type' => 'textfield',
       '#size' => 2,
-      '#default_value' => isset($keys[$i]) ? ($keys[$i]) : '',
+      '#default_value' => $title['posts'],
     );
-    $form['titles'][$i]['title'] = array(
+    $form['titles'][$title['tid']]['title'] = array(
       '#type' => 'textfield',
       '#size' => 30,
-      '#default_value' => isset($keys[$i]) ? ($titles[$keys[$i]]) : '',
+      '#default_value' => $title['title'],
+    );
+    $form['titles'][$title['tid']]['tid'] = array(
+      '#type' => 'hidden',
+      '#default_value' => $title['tid'],
     );
   }
 
@@ -100,6 +92,12 @@ function user_titles_settings_form() {
     '#default_value' => '',
   );
 
+  // TODO: make this work
+  $form['add_tid'] = array(
+    '#type' => 'hidden',
+    '#value' => 1,
+  );
+
   $form['add_submit'] = array(
     '#type' => 'button',
     '#value' => t('Add title'),
@@ -132,16 +130,12 @@ function theme_user_titles_settings_form
 
   $output = '<div class="left">';
 
-  foreach ($form['titles'] as $id => $e) {
-    if (!is_numeric($id)) {
-      continue;
-    }
-    // set a reference so that the drupal_render gets remembered.
-    unset($elem);
-    $elem = &$form['titles'][$id];
+  foreach ($form['titles'] as $tid => $e) {
+    // Set a reference so that the drupal_render gets remembered.
+    $elem = &$form['titles'][$tid];
     $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['posts']), 'class' => 'num-posts'), 
+      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")),
     );
   }
@@ -149,8 +143,8 @@ function theme_user_titles_settings_form
   $output .= theme('table', $header, $rows, array('id' => 'user-titles-settings'));
   $add_rows = array(array(
     array('data' => drupal_render($form['add_value']), 'class' => 'num-posts'), 
-    array('data' => drupal_render($form['add_title']), 'class' => 'user-title'), 
-    drupal_render($form['add_submit'])
+    array('data' => drupal_render($form['add_title']), 'class' => 'user-title'),
+    drupal_render($form['add_tid']) . drupal_render($form['add_submit']),
   ));
   $output .= theme('table', array(), $add_rows, array('id' => 'user-titles-add'));
   $output .= '</div>';
@@ -217,7 +211,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 +221,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);
   }
 }
 
@@ -241,7 +235,7 @@ function user_titles_comment($a1, $op) {
 function user_titles_get_posts($uid) {
   static $cache = array();
   if (!array_key_exists($uid, $cache)) {
-    $cache[$uid] = db_result(db_query("SELECT posts FROM {user_titles} WHERE uid = %d", $uid));
+    $cache[$uid] = db_result(db_query("SELECT posts FROM {user_titles_posts} WHERE uid = %d", $uid));
     if ($cache[$uid] === FALSE) {
       $cache[$uid] = user_titles_update_post_count($uid);
     }
@@ -322,13 +316,31 @@ function user_titles_set_allowed_types($
  * Get titles from db
  */
 function user_titles_get_titles() {
-  return variable_get('user_titles', array());
+  if($query = db_query("SELECT * FROM {user_titles} ORDER BY posts")) {
+    $rows = array();
+    while($row = db_fetch_array($query)) {
+      $rows[] = $row;
+    }
+    usort($rows, "_user_titles_sort_titles");
+    return $rows;
+  }
+  return array();
+}
+
+function _user_titles_sort_titles($a, $b) {
+  if ($a['posts'] == $b['posts']) {
+      return 0;
+  }
+  return ($a['posts'] < $b['posts']) ? -1 : 1;
 }
 
 /**
  * Store titles in the db
  */
 function user_titles_set_titles($titles) {
-  krsort($titles);
-  return variable_set('user_titles', $titles);
+  // TODO: remove this and add ajax callbacks
+  db_query("TRUNCATE {user_titles}");
+  foreach($titles as $title) {
+    db_query("INSERT INTO {user_titles} VALUES(tid, posts, title)", $title['tid'], $title['posts'], $title['title']);
+  }
 }
