Index: cvs.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/cvslog/cvs.module,v
retrieving revision 1.106.2.20
diff -u -r1.106.2.20 cvs.module
--- cvs.module	9 Nov 2006 09:58:12 -0000	1.106.2.20
+++ cvs.module	10 Nov 2006 15:24:25 -0000
@@ -174,18 +174,33 @@
 
   }
   else {
-    if (arg(0) == 'user' && is_numeric(arg(1)) && user_access('access CVS messages')) {
-      // If the user has a CVS account, add a 'track CVS messages' tab to the tracker page.
-      $result = db_query('SELECT uid FROM {cvs_accounts} WHERE uid = %d AND status = %d', arg(1), CVS_APPROVED);
+    if (arg(0) == 'user' && is_numeric(arg(1))) {
+      $result = db_query('SELECT uid, status FROM {cvs_accounts} WHERE uid = %d', arg(1));
       if ($account = db_fetch_object($result)) {
+        // If the user has a CVS account, add a 'track CVS messages' tab to the tracker page.
         $items[] = array(
           'path' => 'user/'. arg(1) .'/track/code',
           'title' => t('track code'),
           'callback' => 'cvs_account_tracker',
-          'access' => TRUE,
+          'access' => user_access('access CVS messages') && $result->status == CVS_APPROVED,
           'type' => MENU_LOCAL_TASK,
           'weight' => 2,
         );
+        // Insert the CVS account edit tab. Using hook_user is not an option
+        // here due to permission issues.
+        // Due to issues with hook_user not creating a MENU_DEFAULT_LOCAL_TASK
+        // unless other hook_user categories are defined, the following tab will
+        // be visible only if there is atleast one category defined elsewhere on
+        // the site.
+        $items[] = array(
+          'path' => 'user/'. arg(1) .'/edit/cvs',
+          'title' => t('CVS'),
+          'callback' => 'cvs_user_edit_form',
+          'callback arguments' => array($account->uid),
+          'access' => ($user->uid == $result->uid && $result->status == CVS_APPROVED) || user_access('administer CVS') ,
+          'type' => MENU_LOCAL_TASK,
+          'weight' => 3,
+        );
       }
     }
     if (arg(0) == 'node' && is_numeric(arg(1))) {
@@ -466,142 +481,141 @@
 }
 
 /**
- * Implementation of hook_user().
+ * Menu Callback: Display the CVS account edit form.
  */
-function cvs_user($type, $edit, &$account, $category = NULL) {
+function cvs_user_edit_form($uid) {
   global $user;
 
-  switch ($type) {
-    case 'form':
-      if($category == 'cvs') {
-        $result = db_fetch_object(db_query("SELECT name, motivation, status FROM {cvs_accounts} WHERE uid = %d", $account->uid));
-        $cvs_name = $result->name;
-        $cvs_status = $result->status;
-        $cvs_motivation = $result->motivation;
-
-        $form['cvs'] = array(
-          '#type' => 'fieldset',
-          '#title' => t('CVS account settings'),
-          '#weight' => 6,
-          '#collapsible' => TRUE,
-          '#collapsed' => FALSE,
-        );
-        if (user_access('administer CVS')) {
-          $form['cvs']['cvs_name'] = array(
-            '#type' => 'textfield',
-            '#title' => t('CVS username'),
-            '#default_value' => $cvs_name,
-            '#size' => 30,
-            '#maxlength' => 64,
-            '#description' => t('The CVS username associated with this account. This field is used to link CVS messages to user accounts.'),
-          );
-          $form['cvs']['cvs_pass'] = array(
-            '#type' => 'password_confirm',
-            '#title' => t('CVS password'),
-            '#description' => t('To change the current CVS password, enter the new password in both fields.'),
-          );
-          $form['cvs']['motivation'] = array(
-            '#type' => 'item',
-            '#title' => t('Motivation'),
-            '#value' => $cvs_motivation,
-            '#description' => t("The user's application letter."),
-          );
-          $form['cvs']['cvs_status'] = array(
-            '#type' => 'radios',
-            '#title' => t('CVS status'),
-            '#default_value' => $cvs_status,
-            '#options' => array(CVS_PENDING => t('Pending'), CVS_DECLINED => t('Declined / Disabled'), CVS_APPROVED => t('Approved')),
-            '#description' => t("You can change the status of the user's CVS account."),
-          );
-          $form['cvs']['send_mail'] = array(
-            '#type' => 'checkbox',
-            '#title' => t('Inform the user by e-mail.'),
-            '#default_value' => 1,
-          );
-          $form['cvs']['message'] = array(
-            '#type' => 'textarea',
-            '#title' => t('Reason/Message'),
-            '#cols' => 50,
-            '#rows' => 5,
-            '#description' => t('The message you want to send to the user. This can be the reason for declining the application or an additional message after approval (used in e-mail).'),
-          );
-        }
-        else if (strlen($cvs_name) && $account->uid == $user->uid) {
-          if($cvs_status) {
-            $form['cvs']['cvs_name'] = array(
-              '#type' => 'item',
-              '#title' => t('CVS username'),
-              '#value' => $cvs_name,
-              '#description' => t('Your CVS username. This field can only be edited by administrators and is used to link your CVS messages to your user account.'),
-            );
-            $form['cvs']['cvs_pass'] = array(
-              '#type' => 'password_confirm',
-              '#title' => t('CVS password'),
-              '#description' => t('To change your current CVS password, enter the new password in both fields.'),
-            );
-          }
-          else {
-            $form['cvs']['cvs_name'] = array(
-              '#type' => 'item',
-              '#title' => t('CVS username'),
-              '#value' => $cvs_name,
-              '#description' => t('Your CVS username associated with this account. Your CVS application has not been approved yet, it has been declined, or your account got blocked.'),
-            );
-          }
-        }
-        else {
-          $form['cvs']['cvs_name'] = array(
-            '#type' => 'item',
-            '#title' => t('CVS username'),
-            '#value' => $cvs_name,
-            '#description' => t('The CVS username associated with this account.'),
-          );
-        }
+  $result = db_fetch_object(db_query("SELECT name, motivation, status FROM {cvs_accounts} WHERE uid = %d", $uid));
+  $cvs_name = $result->name;
+  $cvs_status = $result->status;
+  $cvs_motivation = $result->motivation;
 
-        return $form;
-      }
-      break;
-    case 'validate':
-      if (isset($edit['cvs_name'])) {
-        // Check for duplicates:
-        $id = db_result(db_query("SELECT uid FROM {cvs_accounts} WHERE name = '%s' AND uid != %d", $edit['cvs_name'], $account->uid));
-        if ($id != 0) {
-          form_set_error('cvs_name', t('The specified CVS username is already in use by user #%id.', array('%id' => $id)));
-        }
-      }
-      if (!empty($edit['cvs_pass']) && strlen($edit['cvs_pass']) < CVS_MIN_PASS_LENGTH) {
-        form_set_error('cvs_pass', t('The CVS password you have chosen is too short (it must be at least %min characters long).', array('%min' => CVS_MIN_PASS_LENGTH)));
-      }
-      return $edit;
-      break;
-    case 'update':
-      $result = db_fetch_object(db_query("SELECT name, status FROM {cvs_accounts} WHERE uid = %d", $account->uid));
-      $cvs_name = $result->name;
-      $cvs_status = $result->status;
-
-      if (user_access('administer CVS') &&  isset($edit['cvs_status'])) {
-        if (isset($edit['cvs_pass'])  && strlen($edit['cvs_pass'])) {
-          db_query("UPDATE {cvs_accounts} SET name = '%s', status = %d, pass = '%s' WHERE uid = %d ", $edit['cvs_name'], $edit['cvs_status'], crypt($edit['cvs_pass']), $account->uid);
-        }
-        else {
-          db_query("UPDATE {cvs_accounts} SET name = '%s', status = %d WHERE uid = %d ", $edit['cvs_name'], $edit['cvs_status'], $account->uid);
-        }
-        db_query("UPDATE {cvs_messages} SET user = '%s' WHERE uid = %d", $edit['cvs_name'], $account->uid);
-        if ($edit['send_mail']) {
-          cvs_mail_user($account->uid, $edit['message']);
-        }
-      }
-      else if (strlen($cvs_name) && $account->uid == $user->uid && isset($edit['cvs_pass']) && strlen($edit['cvs_pass'])) {
-        if($cvs_status) {
-          db_query("UPDATE {cvs_accounts} SET pass = '%s' WHERE uid = %d ", crypt($edit['cvs_pass']), $account->uid);
-        }
-      }
-      break;
-    case 'categories':
-      // The following if block has been commented out pending the resolution of a bug in core.
-      //if (db_result(db_queryd("SELECT uid FROM {cvs_accounts} WHERE uid = %d", $account->uid))) {
-      return array(array('name' => 'cvs', 'title' => t('CVS'), 'weight' => 3));
-      //}
+  $form['cvs'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('CVS account settings'),
+    '#collapsible' => TRUE,
+    '#collapsed' => FALSE,
+  );
+  if (user_access('administer CVS')) {
+    $form['cvs']['cvs_name'] = array(
+      '#type' => 'textfield',
+      '#title' => t('CVS username'),
+      '#default_value' => $cvs_name,
+      '#size' => 30,
+      '#maxlength' => 64,
+      '#description' => t('The CVS username associated with this account. This field is used to link CVS messages to user accounts.'),
+    );
+    $form['cvs']['cvs_pass'] = array(
+      '#type' => 'password_confirm',
+      '#title' => t('CVS password'),
+      '#description' => t('To change the current CVS password, enter the new password in both fields.'),
+    );
+    $form['cvs']['motivation'] = array(
+      '#type' => 'item',
+      '#title' => t('Motivation'),
+      '#value' => $cvs_motivation,
+      '#description' => t("The user's application letter."),
+    );
+    $form['cvs']['cvs_status'] = array(
+      '#type' => 'radios',
+      '#title' => t('CVS status'),
+      '#default_value' => $cvs_status,
+      '#options' => array(CVS_PENDING => t('Pending'), CVS_DECLINED => t('Declined / Disabled'), CVS_APPROVED => t('Approved')),
+      '#description' => t("You can change the status of the user's CVS account."),
+    );
+    $form['cvs']['send_mail'] = array(
+      '#type' => 'checkbox',
+      '#title' => t('Inform the user by e-mail.'),
+      '#default_value' => 1,
+    );
+    $form['cvs']['message'] = array(
+      '#type' => 'textarea',
+      '#title' => t('Reason/Message'),
+      '#cols' => 50,
+      '#rows' => 5,
+      '#description' => t('The message you want to send to the user. This can be the reason for declining the application or an additional message after approval (used in e-mail).'),
+    );
+  }
+  elseif (strlen($cvs_name) && $uid == $user->uid) {
+    if($cvs_status) {
+      $form['cvs']['cvs_name'] = array(
+        '#type' => 'item',
+        '#title' => t('CVS username'),
+        '#value' => $cvs_name,
+        '#description' => t('Your CVS username. This field can only be edited by administrators and is used to link your CVS messages to your user account.'),
+      );
+      $form['cvs']['cvs_pass'] = array(
+        '#type' => 'password_confirm',
+        '#title' => t('CVS password'),
+        '#description' => t('To change your current CVS password, enter the new password in both fields.'),
+      );
+    }
+    else {
+      $form['cvs']['cvs_name'] = array(
+        '#type' => 'item',
+        '#title' => t('CVS username'),
+        '#value' => $cvs_name,
+        '#description' => t('Your CVS username associated with this account. Your CVS application has not been approved yet, it has been declined, or your account got blocked.'),
+      );
+    }
+  }
+  else {
+    $form['cvs']['cvs_name'] = array(
+      '#type' => 'item',
+      '#title' => t('CVS username'),
+      '#value' => $cvs_name,
+      '#description' => t('The CVS username associated with this account.'),
+    );
+  }
+  $form['cvs']['cvs_uid'] = array('#type' => 'value', '#value' => $uid);
+  $form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));
+
+  return drupal_get_form('cvs_user_edit_form', $form);
+}
+
+/**
+ * Validate CVS user edit form submission.
+ */
+function cvs_user_edit_form_validate($form_id, $form_values) {
+  if (isset($form_values['cvs_name'])) {
+    // Check for duplicates:
+    $id = db_result(db_query("SELECT uid FROM {cvs_accounts} WHERE name = '%s' AND uid != %d", $form_values['cvs_name'], $form_values['cvs_uid']));
+    if ($id != 0) {
+      form_set_error('cvs_name', t('The specified CVS username is already in use by user #%id.', array('%id' => $id)));
+    }
+  }
+  if (!empty($form_values['cvs_pass']) && strlen($form_values['cvs_pass']) < CVS_MIN_PASS_LENGTH) {
+    form_set_error('cvs_pass', t('The CVS password you have chosen is too short (it must be at least %min characters long).', array('%min' => CVS_MIN_PASS_LENGTH)));
+  }
+}
+
+/**
+ * Process CVS user edit form submission.
+ */
+function cvs_user_edit_form_submit($form_id, $form_values) {
+  global $user;
+
+  $result = db_fetch_object(db_query("SELECT name, status FROM {cvs_accounts} WHERE uid = %d", $form_values['cvs_uid']));
+  $cvs_name = $result->name;
+  $cvs_status = $result->status;
+
+  if (user_access('administer CVS') &&  isset($form_values['cvs_status'])) {
+    if (isset($form_values['cvs_pass'])  && strlen($form_values['cvs_pass'])) {
+      db_query("UPDATE {cvs_accounts} SET name = '%s', status = %d, pass = '%s' WHERE uid = %d", $form_values['cvs_name'], $form_values['cvs_status'], crypt($form_values['cvs_pass']), $form_values['cvs_uid']);
+    }
+    else {
+      db_query("UPDATE {cvs_accounts} SET name = '%s', status = %d WHERE uid = %d", $form_values['cvs_name'], $form_values['cvs_status'], $form_values['cvs_uid']);
+    }
+    db_query("UPDATE {cvs_messages} SET user = '%s' WHERE uid = %d", $form_values['cvs_name'], $form_values['cvs_uid']);
+    if ($form_values['send_mail']) {
+      cvs_mail_user($form_values['cvs_uid'], $form_values['message']);
+    }
+  }
+  elseif (strlen($cvs_name) && $form_values['cvs_uid'] == $user->uid && isset($form_values['cvs_pass']) && strlen($form_values['cvs_pass'])) {
+    if($cvs_status) {
+      db_query("UPDATE {cvs_accounts} SET pass = '%s' WHERE uid = %d", crypt($form_values['cvs_pass']), $form_values['cvs_uid']);
+    }
   }
 }
 
@@ -1765,4 +1779,3 @@
   print drupal_to_js($matches);
   exit();
 }
-
