? role_weights_210457-2.patch
? role_weights_210457-3.patch
Index: role_weights.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/role_weights/role_weights.module,v
retrieving revision 1.12.2.5
diff -u -r1.12.2.5 role_weights.module
--- role_weights.module	28 Jan 2009 13:11:47 -0000	1.12.2.5
+++ role_weights.module	28 Jan 2009 14:39:31 -0000
@@ -69,6 +69,19 @@
     $form['delete']['#weight'] = 10;
     $form['#submit']['role_weights_admin_role_submit'] = array();
   }
+  else if ($form_id == 'user_admin_perm' && variable_get('role_weights_reorder_forms', FALSE)) {
+    // For each role, fetch the weight, and add as a #weight on the row names and checkboxes
+    foreach ($form['checkboxes'] as $key => $value) {
+      if (is_numeric($key)) {
+        $weight = role_weights_get_weight($key);
+        $form['checkboxes'][$key]['#weight'] = $weight;
+        $form['role_names'][$key]['#weight'] = $weight;
+      }	
+    }
+    // Resort according to the adjusted weights
+    uasort($form['checkboxes'], "_element_sort");
+    uasort($form['role_names'], "_element_sort");
+  }
 }
 
 /**
@@ -93,7 +106,13 @@
  */
 function theme_role_weights_admin_new_role($form) {
   $header = array(t('Name'), array('data' => t('Operations'), 'colspan' => 2));
-  foreach (user_roles() as $rid => $name) {
+
+  $user_roles = user_roles();
+  if (variable_get('role_weights_reorder_forms', FALSE)) {
+    uksort($user_roles, '_role_weights_rid_compare');
+  }
+
+  foreach ($user_roles as $rid => $name) {
     $edit_permissions = l(t('edit permissions'), 'admin/user/access/'. $rid);
     
     // Drupal core redirects if the rid is DRUPAL_ANONYMOUS_RID or DRUPAL_AUTHENTICATED_RID.
@@ -259,6 +278,20 @@
 }
 
 /**
+ * compare function for sorting user_roles() by weight using uksort()
+ */
+function _role_weights_rid_compare($r1, $r2) {
+  $r1_weight = role_weights_get_weight($r1);
+  $r2_weight = role_weights_get_weight($r2);
+  
+  if ($r1_weight == $r2_weight) {
+    return 0;
+  }
+  
+  return ($r1_weight < $r2_weight) ? -1 : 1;
+}
+
+/**
  * Implementation of hook_views_tables()
  */
 function role_weights_views_tables() {
