--- modules/menu.module	2006-03-22 19:25:14.000000000 +0530
+++ modules/menu.module.fapi	2006-03-22 19:24:56.000000000 +0530
@@ -54,9 +54,19 @@ function menu_menu($may_cache) {
       'title' => t('menus'),
       'callback' => 'menu_overview',
       'access' => user_access('administer menu'));
+    $items[] = array('path' => 'admin/menu/list',
+      'title' => t('list'),
+      'type' => MENU_DEFAULT_LOCAL_TASK,
+      'weight' => -10);
+
+    $items[] = array('path' => 'admin/menu/item/add',
+      'title' => t('add menu item'),
+      'callback' => 'menu_edit_item_form',
+      'access' => user_access('administer menu'),
+      'type' => MENU_LOCAL_TASK);
     $items[] = array('path' => 'admin/menu/item/edit',
       'title' => t('edit menu item'),
-      'callback' => 'menu_edit_item',
+      'callback' => 'menu_edit_item_form',
       'access' => user_access('administer menu'),
       'type' => MENU_CALLBACK);
     $items[] = array('path' => 'admin/menu/item/reset',
@@ -71,24 +81,25 @@ function menu_menu($may_cache) {
       'type' => MENU_CALLBACK);
     $items[] = array('path' => 'admin/menu/item/delete',
       'title' => t('delete menu item'),
-      'callback' => 'menu_edit_item_delete',
+      'callback' => 'menu_item_delete_form',
       'access' => user_access('administer menu'),
       'type' => MENU_CALLBACK);
 
-    $items[] = array('path' => 'admin/menu/list',
-      'title' => t('list'),
-      'type' => MENU_DEFAULT_LOCAL_TASK,
-      'weight' => -10);
     $items[] = array('path' => 'admin/menu/menu/add',
       'title' => t('add menu'),
-      'callback' => 'menu_add_menu',
+      'callback' => 'menu_edit_menu_form',
       'access' => user_access('administer menu'),
       'type' => MENU_LOCAL_TASK);
-    $items[] = array('path' => 'admin/menu/item/add',
-      'title' => t('add menu item'),
-      'callback' => 'menu_edit_item',
+    $items[] = array('path' => 'admin/menu/menu/edit',
+      'title' => t('add menu'),
+      'callback' => 'menu_edit_menu_form',
       'access' => user_access('administer menu'),
-      'type' => MENU_LOCAL_TASK);
+      'type' => MENU_CALLBACK);
+    $items[] = array('path' => 'admin/menu/menu/delete',
+      'title' => t('add menu'),
+      'callback' => 'menu_item_delete_form',
+      'access' => user_access('administer menu'),
+      'type' => MENU_CALLBACK);
 
     $items[] = array('path' => 'admin/settings/menu',
       'title' => t('menus'),
@@ -290,244 +301,182 @@ function menu_configure() {
 }
 
 /**
- * Menu callback; handle the adding of a new menu.
+ * Menu callback; handle the adding/editing of a new menu.
  */
-function menu_add_menu() {
-  $op = isset($_POST['op']) ? $_POST['op'] : '';
-  $edit = isset($_POST['edit']) ? $_POST['edit'] : '';
-  $output = '';
-
-  switch ($op) {
-    case t('Submit'):
-      menu_edit_item_validate($edit);
-      if (!form_get_errors()) {
-        menu_edit_item_save($edit);
-        drupal_goto('admin/menu');
-      }
-      // Fall through.
-    default:
-      $edit['pid'] = 0;
-      $edit['type'] = MENU_CUSTOM_MENU;
-      $output .= menu_edit_item_form($edit);
+function menu_edit_menu_form($mid = 0) {
+  if (arg(3) == 'edit') {
+    if (!($item = db_fetch_array(db_query('SELECT * FROM {menu} WHERE mid = %d', $mid)))) {
+      drupal_not_found();
+      return;
+    }
   }
-
-  return $output;
-}
-
-/**
- * Menu callback; dispatch to the appropriate menu item edit function.
- */
-function menu_edit_item($mid = 0) {
-  $op = isset($_POST['op']) ? $_POST['op'] : '';
-  $edit = isset($_POST['edit']) ? $_POST['edit'] : '';
-
-  $output = '';
-
-  switch ($op) {
-    case t('Submit'):
-      menu_edit_item_validate($edit);
-      if (!form_get_errors()) {
-        menu_edit_item_save($edit);
-        drupal_goto('admin/menu');
-      }
-      $output .= menu_edit_item_form($edit);
-      break;
-    default:
-      if ($mid > 0) {
-        $item = db_fetch_object(db_query('SELECT * FROM {menu} WHERE mid = %d', $mid));
-
-        $edit['mid'] = $item->mid;
-        $edit['pid'] = $item->pid;
-        $edit['path'] = $item->path;
-        $edit['title'] = $item->title;
-        $edit['description'] = $item->description;
-        $edit['weight'] = $item->weight;
-        $edit['type'] = $item->type;
-      }
-      else {
-        $edit['mid'] = 0; // In case a negative ID was passed in.
-        $edit['pid'] = 1; // Default to "Navigation" menu.
-        $edit['type'] = MENU_CUSTOM_ITEM;
-      }
-      $output .= menu_edit_item_form($edit);
+  else {
+    $item = array('mid' => 0, 'pid' => 0, 'path' => '', 'weight' => 0, 'type' => MENU_CUSTOM_MENU);
   }
+  $form['title'] = array('#type' => 'textfield',
+    '#title' => t('Title'),
+    '#default_value' => $item['title'],
+    '#description' => t('The name of the menu.'),
+    '#required' => TRUE,
+  );
+  $form['mid'] = array('#type' => 'value', '#value' => $item['mid']);
+  $form['pid'] = array('#type' => 'value', '#value' => $item['pid']);
+  $form['path'] = array('#type' => 'value', '#value' => $item['path']);
+  $form['weight'] = array('#type' => 'value', '#value' => $item['weight']);
+  $form['type'] = array('#type' => 'value', '#value' => $item['type']);
+  $form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));
 
-  return $output;
+  // Reuse the submit function of menu_edit_item_form.
+  return drupal_get_form('menu_edit_menu_form', $form, 'menu_edit_item_form');
 }
 
 /**
  * Present the menu item editing form.
  */
-function menu_edit_item_form($edit) {
-  if ($edit['pid'] == 0) {
-    // Display a limited set of fields for menus (not items).
-    $form['title'] = array('#type' => 'textfield',
-      '#title' => t('Title'),
-      '#default_value' => $edit['title'],
-      '#description' => t('The name of the menu.'),
-      '#required' => TRUE,
-    );
-    $form['path'] = array('#type' => 'hidden', '#value' => '');
-    $form['pid'] = array('#type' => 'hidden', '#value' => 0);
-    $form['weight'] = array('#type' => 'hidden', '#value' => 0);
+function menu_edit_item_form($mid = 0) {
+  if (arg(3) == 'edit') {
+    if (!($item = db_fetch_array(db_query('SELECT * FROM {menu} WHERE mid = %d', $mid)))) {
+      drupal_not_found();
+      return;
+    }
   }
   else {
-    $form['title'] = array('#type' => 'textfield',
-      '#title' => t('Title'),
-      '#default_value' => $edit['title'],
-      '#description' => t('The name of the menu item.'),
-      '#required' => TRUE,
-    );
-    $form['description'] = array('#type' => 'textfield',
-      '#title' => t('Description'),
-      '#default_value' => $edit['description'],
-      '#description' => t('The description displayed when hovering over a menu item.'),
-    );
-
-    $path_description = t('The Drupal path this menu item links to. Enter %front to link to the front page.', array('%front' => theme('placeholder', '<front>')));
-
-    if ($edit['type'] & MENU_CREATED_BY_ADMIN) {
-      $form['path'] = array('#type' => 'textfield',
-        '#title' => t('Path'),
-        '#default_value' => $edit['path'],
-        '#description' => $path_description,
-        '#required' => TRUE,
-      );
-    }
-    else {
-      $form['_path'] = array('#type' => 'item',
-        '#title' => t('Path'),
-        '#description' => l($edit['path'], $edit['path']),
-      );
-      $form['path'] = array('#type' => 'hidden', '#value' => $edit['path']);
-    }
-    $expanded = $edit['type'] & MENU_EXPANDED ? 1 : 0;
-    $form['expanded'] = array('#type' => 'checkbox',
-      '#title' => t('Expanded'),
-      '#default_value' => $expanded,
-      '#description' => t('If selected and this menu item has children, the menu will always appear expanded.'),
-    );
+    // This is an add form.
+    $item = array('mid' => 0, 'pid' => 1, 'weight' => 0, 'type' => MENU_CUSTOM_ITEM);
+  }
 
-    // Generate a list of possible parents (not including this item or descendants).
-    $options = menu_parent_options($edit['mid']);
-    $form['pid'] = array('#type' => 'select',
-      '#title' => t('Parent item'),
-      '#default_value' => $edit['pid'],
-      '#options' => $options,
-    );
+  $form['title'] = array('#type' => 'textfield',
+    '#title' => t('Title'),
+    '#default_value' => $item['title'],
+    '#description' => t('The name of the menu item.'),
+    '#required' => TRUE,
+  );
+  $form['description'] = array('#type' => 'textfield',
+    '#title' => t('Description'),
+    '#default_value' => $item['description'],
+    '#description' => t('The description displayed when hovering over a menu item.'),
+  );
 
-    $form['weight'] = array('#type' => 'weight',
-      '#title' => t('Weight'),
-      '#default_value' => $edit['weight'],
-      '#description' => t('Optional. In the menu, the heavier items will sink and the lighter items will be positioned nearer the top.'),
+  if ($item['type'] & MENU_CREATED_BY_ADMIN) {
+    $form['path'] = array('#type' => 'textfield',
+      '#title' => t('Path'),
+      '#default_value' => $item['path'],
+      '#description' => t('The Drupal path this menu item links to. Enter %front to link to the front page.', array('%front' => theme('placeholder', '<front>'))),
+      '#required' => TRUE,
     );
   }
+  else {
+    $form['_path'] = array('#type' => 'item',
+      '#title' => t('Path'),
+      '#description' => l($item['path'], $item['path']),
+    );
+    $form['path'] = array('#type' => 'value', '#value' => $item['path']);
+  }
 
-  $form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));
+  $expanded = $item['type'] & MENU_EXPANDED ? 1 : 0;
+  $form['expanded'] = array('#type' => 'checkbox',
+    '#title' => t('Expanded'),
+    '#default_value' => $expanded,
+    '#description' => t('If selected and this menu item has children, the menu will always appear expanded.'),
+  );
 
-  $form['mid'] = array('#type' => 'hidden', '#value' => $edit['mid']);
+  // Generate a list of possible parents (not including this item or descendants).
+  // Default to "Navigation" menu for new items.
+  $options = menu_parent_options($item['mid']);
+  $form['pid'] = array('#type' => 'select',
+    '#title' => t('Parent item'),
+    '#default_value' => $item['pid'],
+    '#options' => $options,
+  );
+  $form['weight'] = array('#type' => 'weight',
+    '#title' => t('Weight'),
+    '#default_value' => $item['weight'],
+    '#description' => t('Optional. In the menu, the heavier items will sink and the lighter items will be positioned nearer the top.'),
+  );
 
   // Always enable menu items (but not menus) when editing them.
-  if (!($edit['type'] & MENU_IS_ROOT)) {
-    $edit['type'] |= MENU_VISIBLE_IN_TREE | MENU_VISIBLE_IN_BREADCRUMB;
+  if (!($item['type'] & MENU_IS_ROOT)) {
+    $item['type'] |= MENU_VISIBLE_IN_TREE | MENU_VISIBLE_IN_BREADCRUMB;
   }
 
-  $form['type'] = array('#type' => 'hidden', '#value' => $edit['type']);
+  $form['type'] = array('#type' => 'value', '#value' => $item['type']);
+  $form['mid'] = array('#type' => 'value', '#value' => $item['mid']);
+  $form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));
 
   return drupal_get_form('menu_edit_item_form', $form);
 }
 
 /**
- * Confirm that an edited menu item has fields properly filled in.
+ * Process menu and menu item add/edit form submissions.
  */
-function menu_edit_item_validate($edit) {
-  if (empty($edit['title'])) {
-    form_set_error('title', t('You must specify a title.'));
-  }
-
-  if ($edit['pid'] != 0) {
-    if (empty($edit['path'])) {
-      form_set_error('path', t('You must specify a path.'));
-    }
-  }
+function menu_edit_item_form_submit($form_id, $form_values) {
+  menu_edit_item_save($form_values);
+  return 'admin/menu';
 }
 
 /**
- * Save changes to a menu item into the database.
- *
- * @return mid
+ * Menu callback; delete a single custom item.
  */
-function menu_edit_item_save($edit) {
-  if ($edit['expanded']) {
-    $edit['type'] |= MENU_EXPANDED;
+function menu_item_delete_form($mid) {
+  if (!($menu = db_fetch_object(db_query('SELECT type, title FROM {menu} WHERE mid = %d', $mid)))) {
+    drupal_not_found();
+    return;
+  }
+
+  $form['mid'] = array('#type' => 'value', '#value' => $mid);
+  $form['type'] = array('#type' => 'value', '#value' => $menu->type);
+  $form['title'] = array('#type' => 'value', '#value' => $menu->title);
+
+  if ($menu->type & MENU_IS_ROOT) {
+    $message = t('Are you sure you want to delete the menu %item?', array('%item' => theme('placeholder', $menu->title)));
   }
   else {
-    $edit['type'] &= ~MENU_EXPANDED;
+    $message = t('Are you sure you want to delete the custom menu item %item?', array('%item' => theme('placeholder', $menu->title)));
   }
 
-  $edit['type'] = $edit['type'] | MENU_MODIFIED_BY_ADMIN;
+  return confirm_form('menu_confirm_delete_form', $form, $message, 'admin/menu', t('This action cannot be undone.'), t('Delete'));
+}
 
-  $status = menu_save_item($edit);
+/**
+ * Process menu delete form submissions.
+ */
+function menu_confirm_delete_form_submit($form_id, $form_values) {
+  menu_delete_item($form_values['mid']);
 
-  if ($status == SAVED_UPDATED) {
-    drupal_set_message(t('The menu item %title has been updated.', array('%title' => theme('placeholder', $edit['title']))));
+  $t_args = array('%title' => theme('placeholder', $form_values['title']));
+  if ($form_values['type'] & MENU_IS_ROOT) {
+    drupal_set_message(t('The menu %title has been deleted.', $t_args));
+    watchdog('menu', t('Deleted menu %title.', $t_args), WATCHDOG_NOTICE);
   }
-  elseif ($status == SAVED_NEW) {
-    drupal_set_message(t('The menu item %title has been created.', array('%title' => theme('placeholder', $edit['title']))));
+  else {
+    drupal_set_message(t('The menu item %title has been deleted.', $t_args));
+    watchdog('menu', t('Deleted menu item %title.', $t_args), WATCHDOG_NOTICE);
   }
-  return $edit['mid'];
+
+  return 'admin/menu';
 }
 
 /**
  * Menu callback; reset a single modified item.
  */
 function menu_reset_item($mid) {
-  $op = isset($_POST['op']) ? $_POST['op'] : '';
-  switch ($op) {
-    case t('Reset'):
-      menu_delete_item($mid);
-      drupal_set_message(t("The menu item was reset to its default settings."));
-      drupal_goto('admin/menu');
-      break;
-    default:
-      $title = db_result(db_query('SELECT title FROM {menu} WHERE mid = %d', $mid));
-      return confirm_form('menu_item_confirm_reset', array(),
-        t('Are you sure you want to reset the item %item to its default values?', array('%item' => theme('placeholder', $title))),
-        'admin/menu', t('Any customizations will be lost. This action cannot be undone.'),
-        t('Reset'));
+  if (isset($mid) && $title = db_result(db_query('SELECT title FROM {menu} WHERE mid = %d', $mid))) {
+    $form['mid'] = array('#type' => 'value', '#value' => $mid);
+    return confirm_form('menu_reset_item_form', $form, t('Are you sure you want to reset the item %item to its default values?', array('%item' => theme('placeholder', $title))), 'admin/menu', t('Any customizations will be lost. This action cannot be undone.'), t('Reset'));
+  }
+  else {
+    drupal_not_found();
   }
 }
 
 /**
- * Menu callback; delete a single custom item.
+ * Process menu reset item form submissions.
  */
-function menu_edit_item_delete($mid) {
-  $op = isset($_POST['op']) ? $_POST['op'] : '';
-  $result = db_query('SELECT type, title FROM {menu} WHERE mid = %d', $mid);
-  $menu = db_fetch_object($result);
-  if (!$menu) {
-    drupal_goto('admin/menu');
-  }
-  switch ($op) {
-    case t('Delete'):
-      menu_delete_item($mid);
-      if ($menu->type & MENU_IS_ROOT) {
-        drupal_set_message(t('The menu has been removed.'));
-      }
-      else {
-        drupal_set_message(t('The menu item has been removed.'));
-      }
-      drupal_goto('admin/menu');
-      break;
-    default:
-      if ($menu->type & MENU_IS_ROOT) {
-        $message = t('Are you sure you want to delete the menu %item?', array('%item' => theme('placeholder', $menu->title)));
-      }
-      else {
-        $message = t('Are you sure you want to delete the custom menu item %item?', array('%item' => theme('placeholder', $menu->title)));
-      }
-      return confirm_form('menu_confirm_delete', $form, $message, 'admin/menu', t('This action cannot be undone.'), t('Delete'));
-  }
+function menu_reset_item_form_submit($form_id, $form_values) {
+  menu_delete_item($form_values['mid']);
+  drupal_set_message(t('The menu item was reset to its default settings.'));
+
+  return 'admin/menu';
 }
 
 /**
@@ -554,6 +503,34 @@ function menu_overview() {
 }
 
 /**
+ * Save changes to a menu item into the database.
+ *
+ * @return mid
+ */
+function menu_edit_item_save($edit) {
+  if ($edit['expanded']) {
+    $edit['type'] |= MENU_EXPANDED;
+  }
+  else {
+    $edit['type'] &= ~MENU_EXPANDED;
+  }
+
+  $edit['type'] = $edit['type'] | MENU_MODIFIED_BY_ADMIN;
+
+  $status = menu_save_item($edit);
+
+  $t_args = array('%title' => theme('placeholder', $edit['title']));
+  if ($status == SAVED_UPDATED) {
+    drupal_set_message(t('The menu item %title has been updated.', $t_args));
+  }
+  elseif ($status == SAVED_NEW) {
+    drupal_set_message(t('The menu item %title has been added.', $t_args));
+    watchdog('menu', t('Added menu item %title.', $t_args), WATCHDOG_NOTICE, l(t('view'), 'admin/menu'));
+  }
+  return $edit['mid'];
+}
+
+/**
  * Save a menu item to the database.
  *
  * @param $item
@@ -623,10 +600,10 @@ function menu_overview_tree() {
   foreach ($root_menus as $mid => $title) {
     $operations = array();
     if ($menu['items'][$mid]['type'] & MENU_MODIFIABLE_BY_ADMIN) {
-      $operations[] = l(t('edit'), 'admin/menu/item/edit/'. $mid);
+      $operations[] = l(t('edit'), 'admin/menu/menu/edit/'. $mid);
     }
     if ($menu['items'][$mid]['type'] & MENU_CREATED_BY_ADMIN) {
-      $operations[] = l(t('delete'), 'admin/menu/item/delete/'. $mid);
+      $operations[] = l(t('delete'), 'admin/menu/menu/delete/'. $mid);
     }
     $table = theme('item_list', $operations);
     $table .= theme('table', $header, menu_overview_tree_rows($mid));
