Index: menu.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/menu.module,v
retrieving revision 1.38
diff -u -p -r1.38 menu.module
--- menu.module	11 Oct 2005 19:44:34 -0000	1.38
+++ menu.module	13 Oct 2005 01:21:28 -0000
@@ -33,6 +33,11 @@ function menu_menu($may_cache) {
       'access' => user_access('administer menu'),
       'type' => MENU_CALLBACK);
 
+    $items[] = array('path' =>'admin/settings/menu',
+      'title' => t('menus'),
+      'callback' => 'menu_configure',
+      '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/menu/add', 'title' => t('add menu'),
@@ -65,10 +70,64 @@ function menu_help($section) {
       return t('<p>Enter the name for your new menu. Remember to enable the newly created block in the %blocks administration page.</p>', array('%blocks' => l(t('blocks'), 'admin/block')));
     case 'admin/menu/item/add':
       return t('<p>Enter the title, path, position and the weight for your new menu item.</p>');
+    case 'admin/settings/menu':
+      return t('<p>Customize the menu settings.</p>');
   }
 }
 
 /**
+ * Menu callback; presents menu configuration options.
+ */
+function menu_configure() {
+
+  $form['menu_node_form'] = array(
+    '#type' => 'fieldset',
+    '#collapsible' => TRUE, '#collapsed' => FALSE,
+    '#title' => ('Configure menu options available when editing nodes.')
+    );
+
+  foreach (node_get_types() as $type => $name) {
+    $nodetypes[$type] = $name;
+  }
+  $form['menu_node_form']['menu_nodes'] = array(
+    '#type' => 'checkboxes',
+    '#title' => t('Show menu item settings on the following node types'),
+    '#default_value' => variable_get('menu_nodes', array()),
+    '#options' => $nodetypes,
+    '#description' => t('Check the node types on which you want the menu items settings shown.')
+  );
+
+  $options = array(0 => t('Root'));
+  $options += menu_parent_options(0);
+
+  $form['menu_node_form']['menu_pid'] = array(
+    '#type' => 'select',
+    '#title' => t('Available menu items'),
+    '#default_value' => variable_get('menu_pid', 1),
+    '#options' => $options,
+    '#description' => t('Choose the top-level menu item that will be available as a parent. Only this menu item and its children will be shown.')
+  );
+
+  $form['menu_node_form']['menu_show_weight'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Show weight'),
+    '#default_value' => variable_get('menu_show_weight', 0),
+    '#return_value' => 1,
+    '#description' => t('Allow users to set the weight of menu items.')
+  );
+
+  $form['menu_node_form']['menu_default_expanded'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Default to expanded'),
+    '#default_value' => variable_get('menu_default_expanded', 0),
+    '#return_value' => 1,
+    '#description' => t('New menu items will default to showing submenus.')
+  );
+  
+  return system_settings_form('menu_configure', $form);
+}
+
+/**
  * Implementation of hook_block().
  */
 function menu_block($op = 'list', $delta = 0) {
@@ -99,9 +158,11 @@ function menu_nodeapi(&$node, $op) {
   if (user_access('administer menu')) {
     switch ($op) {
       case 'form':
-        $edit = $_POST['edit'];
-        $edit['nid'] = $node->nid;
-        return menu_node_form($edit);
+        if (in_array($node->type, (array) variable_get('menu_nodes', ''))) {
+          $edit = $_POST['edit'];
+          $edit['nid'] = $node->nid;
+          return menu_node_form($edit);
+	}
         break;
 
       case 'insert':
@@ -537,28 +598,102 @@ function menu_parent_options($mid, $pid 
 function menu_node_form($edit = array()) {
   $item = array();
   if ($edit['nid'] > 0) {
-    $item = db_fetch_array(db_query("SELECT * FROM {menu} WHERE path = 'node/%d'", $edit['nid']));
-    if (is_array($edit['menu'])) {
-      $item = !is_array($item) ? $edit['menu'] : (($_POST['op'] == t('Preview')) ? array_merge($item, $edit['menu']) : array_merge($edit['menu'], $item));
-    }
-  }
+    $where_string = "path = 'node/${edit['nid']}'";
+    // Also look for a menu pointing to an alias of this node.
+    $node_alias = db_fetch_array(db_query("SELECT dst FROM {url_alias} WHERE src = 'node/%d'", $edit['nid']));
+    if (is_array($node_alias)) {
+      $where_string .= " OR path = '${node_alias['dst']}'";
+    }
+    // Use the first result as the default settings.
+    // We could also analyse the result set and try to find an item within the menu_pid branch. Worth it?
+    $item = db_fetch_array(db_query("SELECT * FROM {menu} WHERE $where_string"));
+  }
+  if (is_array($edit['menu'])) {
+    $item = !is_array($item) ? $edit['menu'] : (($_POST['op'] == t('Preview')) ? array_merge($item, $edit['menu']) : array_merge($edit['menu'], $item));
+  }
+
+  $form['menu'] = array(
+    '#type' => 'fieldset',
+    '#title' => t('Menu item settings'),
+    '#collapsible' => TRUE, '#collapsed' => TRUE,
+    '#tree' => TRUE
+  );
+  $form['menu']['title'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Title'),
+    '#default_value' => $item['title'],
+    '#size' => 60, '#maxlength' => 128,
+    '#description' => t('The name to display for this link.')
+  );
+  $form['menu']['description'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Description'),
+    '#default_value' => $item['description'],
+    '#size' => 60, '#maxlength' => 128,
+    '#description' => t('The description displayed when hovering over a menu item.')
+  );
+  $form['menu']['path'] = array(
+    '#type' => 'hidden',
+    '#value' => $item['path']
+  );
+
+  $form['menu']['expanded'] = array(
+    '#type' => 'checkbox',
+    '#title' => t('Expanded'),
+    '#default_value' => $item['mid'] ? $item['type'] & MENU_EXPANDED : variable_get('menu_default_expanded', 0),
+    '#return_value' => 1,
+    '#description' => t('If selected and this menu item has children, the menu will always appear expanded.')
+  );
 
-  $form['menu'] = array('#type' => 'fieldset', '#title' => t('Menu item settings'), '#collapsible' => TRUE, '#collapsed' => TRUE, '#tree' => TRUE);
-
-  $form['menu']['title'] = array('#type' => 'textfield', '#title' => t('Title'), '#default_value' => $item['title'], '#size' => 60, '#maxlength' => 128, '#description' => t('The name to display for this link.'));
-
-  // Generate a list of possible parents (not including this item or descendants).
-  $options = menu_parent_options($edit['mid']);
-  $form['menu']['pid'] = array('#type' => select, '#title' => t('Parent item'), '#default_value' => $item['pid'], '#options' => $options);
-  $form['menu']['description'] = array('#type' => 'hidden', '#value' => $item['description']);
-  $form['menu']['path'] = array('#type' => 'hidden', '#value' => $item['path']);
-  $form['menu']['weight'] = array('#type' => 'hidden', '#value' => $item['weight'] ? $item['weight'] : 0);
+  // Generate a list of possible parents. Exclude this item and its children.
+  $pid = variable_get('menu_pid', 0);
+  $options = array();
+  $menu = menu_get_menu();
+  // if $pid is not root, add it to the option list
+  if ($pid > 0 && isset($menu['items'][$pid])) {
+    $title = $menu['items'][$pid]['title'];
+    if (!($menu['items'][$pid]['type'] & MENU_VISIBLE_IN_TREE)) {
+      $title .= ' ('. t('disabled') .')';
+    }
+    $options[$pid] = $title;
+  }
+  $options += menu_parent_options($edit['mid'], $pid, 1);
+  $form['menu']['pid'] = array(
+    '#type' => select,
+    '#title' => t('Parent item'),
+    '#default_value' => $item['pid'],
+    '#options' => $options
+  );
+  if (variable_get('menu_show_weight', 0)) {
+    $form['menu']['weight'] = array(
+      '#type' => 'weight',
+      '#title' => t('Weight'),
+      '#default_value' => $item['weight'],
+      '#delta' => 10,
+      '#description' => t('Optional. In the menu, the heavier items will sink and the lighter items will be positioned nearer the top.')
+    );
+  }
+  else {
+    $form['menu']['weight'] = array(
+      '#type' => 'hidden',
+      '#value' => $item['weight'] ? $item['weight'] : 0
+    );
+  }
 
-  $form['menu']['mid'] = array('#type' => 'hidden', '#value' => $item['mid'] ? $item['mid'] : 0);
-  $form['menu']['type'] = array('#type' => 'hidden', '#value' => $item['type'] ? $item['type'] : MENU_CUSTOM_ITEM);
+  $form['menu']['mid'] = array(
+    '#type' => 'hidden',
+    '#value' => $item['mid'] ? $item['mid'] : 0
+  );
+  $form['menu']['type'] = array('#type' => 'hidden',
+    '#value' => $item['type'] ? $item['type'] : MENU_CUSTOM_ITEM
+  );
 
   if ($item['mid'] > 0) {
-    $form['menu']['delete'] = array('#type' => 'checkbox', '#title' => t('Check to delete this menu item.'), '#default_value' => $item['delete']);
+    $form['menu']['delete'] = array(
+      '#type' => 'checkbox',
+      '#title' => t('Check to delete this menu item.'),
+      '#default_value' => $item['delete']
+    );
   }
 
   return $form;
