--- profile.module	Wed Jan 10 21:31:18 2007
+++ profile.module	Mon Jan 15 18:45:53 2007
@@ -172,8 +172,9 @@ function profile_user($type, &$edit, &$u
     case 'register':
       return profile_form_profile($edit, $user, $category, TRUE);
     case 'update':
-    case 'insert':
       return profile_save_profile($edit, $user, $category);
+    case 'insert':
+	  return profile_save_profile_new($edit, $user, $category);
     case 'view':
       return profile_view_profile($user);
     case 'form':
@@ -440,7 +441,7 @@ function profile_browse() {
 
     // Compile a list of fields to show.
     $fields = array();
-    $result = db_query('SELECT name, title, type, weight, page FROM {profile_fields} WHERE fid != %d AND visibility = %d ORDER BY weight', $field->fid, PROFILE_PUBLIC_LISTINGS);
+    $result = db_query('SELECT name, title, type, weight, page FROM {profile_fields} WHERE visibility = %d ORDER BY category, weight', PROFILE_PUBLIC_LISTINGS);
     while ($record = db_fetch_object($result)) {
       $fields[] = $record;
     }
@@ -466,15 +467,9 @@ function profile_browse() {
     }
 
     // Extract the affected users:
-    $result = pager_query("SELECT u.uid, u.access FROM {users} u INNER JOIN {profile_values} v ON u.uid = v.uid WHERE v.fid = %d AND $query AND u.access != 0 AND u.status != 0 ORDER BY u.access DESC", 20, 0, NULL, $arguments);
+    $result = pager_query("SELECT u.uid, u.access FROM {users} u INNER JOIN {profile_values} v ON u.uid = v.uid WHERE v.fid = %d AND $query AND u.status != 0 ORDER BY u.access DESC", 20, 0, NULL, $arguments);
 
-    $output = '<div id="profile">';
-    while ($account = db_fetch_object($result)) {
-      $account = user_load(array('uid' => $account->uid));
-      $profile = _profile_update_user_fields($fields, $account);
-      $output .= theme('profile_listing', $account, $profile);
-    }
-    $output .= theme('pager', NULL, 20);
+	$output = theme('profile_table', $result, $fields);
 
     if ($field->type == 'selection' || $field->type == 'list' || $field->type == 'textfield') {
       $title = strtr(check_plain($field->page), array('%value' => theme('placeholder', $value)));
@@ -482,7 +477,6 @@ function profile_browse() {
     else {
       $title = check_plain($field->page);
     }
-    $output .= '</div>';
 
     drupal_set_title($title);
     return $output;
@@ -499,22 +493,27 @@ function profile_browse() {
     }
 
     // Extract the affected users:
-    $result = pager_query('SELECT uid, access FROM {users} WHERE uid > 0 AND status != 0 AND access != 0 ORDER BY access DESC', 20, 0, NULL);
+    $result = pager_query('SELECT uid, access FROM {users} WHERE uid > 0 AND status != 0 ORDER BY name ASC', 20, 0, NULL);
 
-    $output = '<div id="profile">';
-    while ($account = db_fetch_object($result)) {
-      $account = user_load(array('uid' => $account->uid));
-      $profile = _profile_update_user_fields($fields, $account);
-      $output .= theme('profile_listing', $account, $profile);
-    }
-    $output .= '</div>';
-    $output .= theme('pager', NULL, 20);
+	$output = theme('profile_table', $result, $fields);
 
-    drupal_set_title(t('user list'));
+    drupal_set_title(t('User list'));
     return $output;
   }
 }
 
+function theme_profile_table ($result, $fields=array()) {
+  $output = '<div id="profile">';
+  while ($account = db_fetch_object($result)) {
+    $account = user_load(array('uid' => $account->uid));
+    $profile = _profile_update_user_fields($fields, $account);
+    $output .= theme('profile_listing', $account, $profile);
+  }
+  $output .= '</div>';
+  $output .= theme('pager', NULL, 20);
+  return $output;
+}
+
 function profile_load_profile(&$user) {
   $result = db_query('SELECT f.name, f.type, v.value FROM {profile_fields} f INNER JOIN {profile_values} v ON f.fid = v.fid WHERE uid = %d', $user->uid);
   while ($field = db_fetch_object($result)) {
@@ -524,6 +523,13 @@ function profile_load_profile(&$user) {
   }
 }
 
+function profile_save_profile_new(&$edit, &$user, $category) {
+  $result = db_query('SELECT DISTINCT(category) from {profile_fields}');
+  while ($field = db_fetch_object($result)) {
+    profile_save_profile($edit, $user, $field->category);
+  }
+}
+
 function profile_save_profile(&$edit, &$user, $category) {
   $result = _profile_get_fields($category);
   while ($field = db_fetch_object($result)) {
@@ -830,4 +836,4 @@ function profile_admin_settings_autocomp
   }
   print drupal_to_js($matches);
   exit();
-}
+}
