Index: role_weights.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/role_weights/role_weights.module,v
retrieving revision 1.10
diff -u -r1.10 role_weights.module
--- role_weights.module	29 Jan 2007 20:30:54 -0000	1.10
+++ role_weights.module	30 Jan 2007 03:40:09 -0000
@@ -3,20 +3,10 @@
 
 /**
  * @file
- * A small utility module to allow site admins to specify certain weights for user roles.
+ * Allows users to specify weights for user roles.
  */
 
 /**
- * Implementation of hook_help().
- */
-function role_weights_help($section) {
-  switch ($section) {
-    case 'admin/modules#description':
-      return t('A small utility module to allow site admins to specify certain weights for user roles.');
-  }
-}
-
-/**
  * Implementation of hook_form_alter().
  *
  * User.module's role handling is not fully Forms API
@@ -25,21 +15,37 @@
  * array. Here we 'fix' this problem by altering the
  * submit behavior. This way, we can add to existing
  * role forms, and also act on role deletion (deleting
- * role_weights). 
+ * role_weights).
  */
 function role_weights_form_alter($form_id, &$form) {
   // Alter the 'user_admin_new_role' form to retheme.
   if ($form_id == 'user_admin_new_role') {
     $form['#pre_render'][] = 'role_weights_admin_new_role_set_theme';
   }
-  else if ($form_id == 'user_admin_edit_role') {
+  else if ($form_id == 'user_admin_role') {
     $rid = arg(4);
+    _role_weights_shift_rid($rid, FALSE);
     // Lock the anonymous and authenticated roles.
     if (in_array($rid, array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID))) {
-      $form['#prefix'] = t('<strong>%name</strong> (locked)', array('%name' => $form['name']['#default_value']));
+      switch ($rid) {
+        case DRUPAL_ANONYMOUS_RID:
+          $name = 'anonymous user';
+          break;
+        case DRUPAL_AUTHENTICATED_RID:
+          $name = 'authenticated user';
+          break;
+      }
+      // DRUPAL-5 TODO - t() call with params (check @ is correct)
+      // DRUPAL-5 TODO - Changes to how #prefix and #suffix are rendered in form arrays - order changed!
+      $form['#prefix'] = t('<strong>@name</strong> (locked)', array('@name' => $name));
+      // Pass the name and rid as values, so that they will register properly on submit.
       $form['name'] = array(
         '#type' => 'value',
-        '#value' => $form['name']['#default_value'],
+        '#value' => $name,
+      );
+      $form['rid'] = array(
+        '#type' => 'value',
+        '#value' => $rid,
       );
     }
 
@@ -48,7 +54,7 @@
       '#type' => 'weight',
       '#title' => t('Weight'),
       '#description' => t('Set a lower number to have this role take precedence over other roles.'),
-      '#default_value' => $weight
+      '#default_value' => $weight,
     );
     // By changing the values of the buttons, we force
     // user_admin_role() to bypass its custom handling of
@@ -82,22 +88,31 @@
  * links, as we need to be able to navigate to them
  * to provide weights. We will lock them in the role
  * admin form instead.
+ *
+ * DRUPAL-5 TODO: links to edit anon and authed user don't work
  */
 function theme_role_weights_admin_new_role($form) {
-  $header = array(t('Name'), t('Operations'));
+  $header = array(t('Name'), array('data' => t('Operations'), 'colspan' => 2));
   foreach (user_roles() as $rid => $name) {
-    $rows[] = array($name, l(t('edit'), 'admin/access/roles/edit/'. $rid));
+    $edit_permissions = l(t('edit permissions'), 'admin/user/access/'. $rid);
+    // Drupal core redirects if the rid is DRUPAL_ANONYMOUS_RID or DRUPAL_AUTHENTICATED_RID.
+    // We prevent this by shifting the rid.
+    _role_weights_shift_rid($rid);
+    $rows[] = array($name, l(t('edit role'), 'admin/user/roles/edit/'. $rid), $edit_permissions);
   }
-  $rows[] = array(form_render($form['name']), form_render($form['submit']));
+  $rows[] = array(drupal_render($form['name']), array('data' => drupal_render($form['submit']), colspan => 2));
 
-  return theme('table', $header, $rows) . form_render($form);
+  return drupal_render($form) . theme('table', $header, $rows);
 }
 
 /**
  * Submission from role_weights_admin form.
+ *
+ * DRUPAL-5 TODO: $_POST[op] deprecated in favor of $form_values[op] - relevant here?
  */
 function role_weights_admin_role_submit($form_id, $form_values) {
   $id = arg(4);
+  _role_weights_shift_rid($id, FALSE);
   switch ($_POST['op']) {
     case t('Save'):
       // Handle the role name update previously done in user_admin_role().
@@ -130,7 +145,7 @@
       drupal_set_message(t('The role has been deleted.'));
       break;
   }
-  return 'admin/access/roles';
+  return 'admin/user/roles';
 }
 
 /**
@@ -242,3 +257,15 @@
 
   return $tables;
 }
+
+/**
+ * Helper function to shift role ids up or down.
+ */
+function _role_weights_shift_rid(&$rid, $decrement = TRUE) {
+  if ($decrement === TRUE && in_array($rid, array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID))) {
+    $rid = $rid - 2;
+  }
+  elseif ($decrement === FALSE && $rid < 1) {
+    $rid = $rid + 2;
+  }
+}
\ No newline at end of file
