Index: modules/contact.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/contact.module,v
retrieving revision 1.39
diff -u -p -r1.39 contact.module
--- modules/contact.module	27 Dec 2005 18:11:27 -0000	1.39
+++ modules/contact.module	10 Jan 2006 21:51:29 -0000
@@ -45,9 +45,12 @@ function contact_menu($may_cache) {
     $items[] = array('path' => 'admin/contact/list', 'title' => t('list'),
       'callback' => 'contact_admin', 'access' => user_access('administer site configuration'),
       'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -1);
-    $items[] = array('path' => 'admin/contact/edit', 'title' => t('add category'),
+    $items[] = array('path' => 'admin/contact/add', 'title' => t('add category'),
       'callback' => 'contact_admin_edit', 'access' => user_access('administer site configuration'),
       'type' => MENU_LOCAL_TASK);
+    $items[] = array('path' => 'admin/contact/edit', 'title' => t('edit contact category'),
+      'callback' => 'contact_admin_edit', 'access' => user_access('administer site configuration'),
+      'type' => MENU_CALLBACK);
     $items[] = array('path' => 'admin/contact/delete', 'title' => t('delete contact'),
       'callback' => 'contact_admin_delete', 'access' => user_access('administer site configuration'),
       'type' => MENU_CALLBACK);
@@ -179,51 +182,67 @@ function contact_user_mail_submit($form_
 }
 
 function contact_admin_edit($cid = NULL) {
-  if (isset($_POST['edit'])) {
-    $edit = $_POST['edit'];
-
-    if (empty($edit['category'])) {
-      form_set_error('category', t('You must enter a category.'));
-    }
-    if (empty($edit['recipients'])) {
-      form_set_error('recipients', t('You must enter one or more recipients.'));
-    }
-    else {
-      $recipients = split(',', $edit['recipients']);
-      foreach($recipients as $recipient) {
-        if (!valid_email_address(trim($recipient))) {
-          form_set_error('recipients',t('%recipient is an invalid e-mail address.', array('%recipient' => theme('placeholder', $recipient))));
-        }
-      }
-    }
-
-    if (!form_get_errors()) {
-      db_query("DELETE FROM {contact} WHERE cid = %d", $cid);
-      db_query("INSERT INTO {contact} (category, recipients, reply, weight, selected) VALUES ('%s', '%s', '%s', %d, %d)", $edit['category'], $edit['recipients'], $edit['reply'], $edit['weight'], $edit['selected']);
-      drupal_set_message(t('Category %category has been updated.', array('%category' => theme('placeholder', $edit['category']))));
-      drupal_goto('admin/contact');
-    }
+  if (arg(2) == "edit" && $cid > 0) {
+    $category = db_fetch_object(db_query("SELECT * FROM {contact} WHERE cid = %d", $cid));
+    $edit['cid'] = $category->cid;
+    $edit['category'] = $category->category;
+    $edit['recipients'] = $category->recipients;
+    $edit['reply'] = $category->reply;
+    $edit['weight'] = $category->weight;
+    $edit['selected'] = $category->selected;
   }
   else {
-    $category           = db_fetch_object(db_query("SELECT * FROM {contact} WHERE cid = %d", $cid));
-    $edit['cid']        = $category->cid;
-    $edit['category']   = $category->category;
-    $edit['recipients'] = $category->recipients;
-    $edit['reply']      = $category->reply;
-    $edit['weight']     = $category->weight;
-    $edit['selected']   = $category->selected;
+    $edit['cid'] = $edit['selected'] = $edit['weight'] = 0;
+    $edit['category'] = $edit['recipients'] = $edit['reply'] = '';
   }
-
   $form['category'] = array('#type' => 'textfield', '#title' => t('Category'), '#maxlength' => 255, '#default_value' => $edit['category'], '#description' => t("Example: 'website feedback' or 'product information'."), '#required' => TRUE);
-  $form['recipients'] = array('#type' => 'textarea', '#title' => t('Recipients'), '#default_value' => $edit['recipients'], '#description' => t("Example: 'webmaster@yoursite.com' or 'sales@yoursite.com'.  To specify multiple repecients, separate each e-mail address with a comma."), '#required' => TRUE);
+  $form['recipients'] = array('#type' => 'textarea', '#title' => t('Recipients'), '#default_value' => $edit['recipients'], '#description' => t("Example: 'webmaster@yoursite.com' or 'sales@yoursite.com'.  To specify multiple recipients, separate each e-mail address with a comma."), '#required' => TRUE);
   $form['reply'] = array('#type' => 'textarea', '#title' => t('Auto-reply'), '#default_value' => $edit['reply'], '#description' => t("Optional auto-reply.  Leave empty if you don't want to send the user an auto-reply message."));
   $form['weight'] = array('#type' => 'weight', '#title' => t('Weight'), '#default_value' => $edit['weight'], '#description' => t('When listing categories, those with with light (small) weights get listed before categories with heavier (larger) weights. Categories with equal weights are sorted alphabetically.'));
   $form['selected'] = array('#type' => 'select', '#title' => t('Selected'), '#options' => array('0' => t('No'), '1' => t('Yes')), '#default_value' => $edit['selected'], '#description' => t('Set this to <em>Yes</em> if you would like this category to be selected by default.'));
+  $form['cid'] = array('#type' => 'hidden', '#value' => $edit['cid']);
   $form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));
 
   return drupal_get_form('contact_admin_edit', $form);
 }
 
+function contact_admin_edit_validate($form_id, $form_values) {
+  if (empty($form_values['category'])) {
+    form_set_error('category', t('You must enter a category.'));
+  }
+  if (empty($form_values['recipients'])) {
+    form_set_error('recipients', t('You must enter one or more recipients.'));
+  }
+  else {
+    $recipients = explode(',', $form_values['recipients']);
+    foreach($recipients as $recipient) {
+      if (!valid_email_address(trim($recipient))) {
+        form_set_error('recipients',t('%recipient is an invalid e-mail address.', array('%recipient' => theme('placeholder', $recipient))));
+      }
+    }
+  }
+}
+
+function contact_admin_edit_submit($form_id, $form_values) {
+  if ($form_values['selected']) {
+    //Unselect all other contact categories
+    db_query("UPDATE {contact} SET selected = 0");
+  }
+  $recipients = explode(',', $form_values['recipients']);
+  foreach($recipients as $key=>$recipient) {
+    $recipients[$key] = trim($recipient);
+  }
+  $form_values['recipients'] = implode(',', $recipients);
+  if (arg(2) == "add") {
+    db_query("INSERT INTO {contact} (category, recipients, reply, weight, selected) VALUES ('%s', '%s', '%s', %d, %d)", $form_values['category'], $form_values['recipients'], $form_values['reply'], $form_values['weight'], $form_values['selected']);
+  }
+  else {
+    db_query("UPDATE {contact} SET category = '%s', recipients = '%s', reply = '%s', weight = %d, selected = %d WHERE cid=%d", $form_values['category'], $form_values['recipients'], $form_values['reply'], $form_values['weight'], $form_values['selected'], $form_values['cid']);
+  }
+  drupal_set_message(t('Category %category has been updated.', array('%category' => theme('placeholder', $edit['category']))));
+  drupal_goto('admin/contact');
+}
+
 function contact_admin_delete($cid) {
   $info = db_fetch_object(db_query("SELECT cid, category FROM {contact} WHERE cid = %d", $cid));
   if ($_POST['op'] != t('Delete')) {
@@ -240,7 +259,6 @@ function contact_admin_delete($cid) {
   }
 }
 
-
 function contact_admin() {
   $result = db_query('SELECT cid, category, recipients, selected FROM {contact} ORDER BY weight, category');
   $rows = array();
@@ -264,7 +282,7 @@ function contact_mail_page() {
     }
 
     $result = db_query('SELECT cid, category, selected FROM {contact} ORDER BY weight, category');
-    $categories[] = '--';
+    $categories[] = t('--');
     while ($category = db_fetch_object($result)) {
       $categories[$category->cid] = $category->category;
       if ($category->selected) {
@@ -331,7 +349,7 @@ function contact_mail_page_submit($form_
   $contact = db_fetch_object(db_query("SELECT * FROM {contact} WHERE cid = %d", $edit['cid']));
 
   // Format the category:
-  $subject = '['. $contact->category .'] '. $edit['subject'];
+  $subject = t('[%category] %subject', array('%category' => $contact->category, '%subject' => $edit['subject']));
 
   // Prepare the body:
   $body = implode("\n\n", $message);
