--- automenu.module	Sat Oct 10 00:34:20 2009
+++ automenu.module.new	Wed Dec  2 09:01:53 2009
@@ -11,28 +11,34 @@ function automenu_nodeapi(&$node, $op, $
     case 'insert':
     case 'update':
       $parent_menu = explode(":", variable_get('parentmenu' . $node->language . '_' . $node->type, '0'));
-      if ($parent_menu[0] != '0') {
-        if (($node->menu['link_title'] == '' || $node->menu['delete']) && $node->status == 1) {
-          $new_menu = array(
-            'menu_name' => $parent_menu[0],
-            'link_path' => 'node/' . $node->nid,
-            'link_title' => $node->title,
-            'plid' => $parent_menu[1],
-            'hidden' => variable_get('automenu_hide_'.$node->type, 0),
-            //'customized' => true, // ?
-          );
-          if ($node->language) {
-            $new_menu['options'] = array('langcode'=> $node->language);
+      $parent_menu_id = $parent_menu[0];
+      
+      if ($parent_menu_id && ($node->menu['link_title'] == '' || $node->menu['delete']) && $node->status == 1) {
+        if ($node->path) {
+          $id = _automenu_parent_menu_id_by_path($node->path);
+          if ($id) {
+            $parent_menu_id = $id;
           }
-          if( $existing_item = db_fetch_array(db_query("SELECT mlid FROM {menu_links} WHERE link_path = '%s' AND plid = %d", 'node/' . $node->nid, $new_menu['plid']))) {
-            $new_menu['mlid']=$existing_item['mlid'];
-          }          
-          if (!menu_link_save($new_menu)) {
-            drupal_set_message(t('There was an error saving the auto-menu link.'), 'error');
-          }
-	  else {
-      drupal_set_message(t('The page was automatically added/updated to: !menu.', array('!menu' => $parent_menu[0])));
-	  }
+        } 
+        $new_menu = array(
+          'menu_name' => $parent_menu_id,
+          'link_path' => 'node/' . $node->nid,
+          'link_title' => $node->title,
+          'plid' => $parent_menu_id,
+          'hidden' => variable_get('automenu_hide_'.$node->type, 0),
+          //'customized' => true, // ?
+        );
+        if ($node->language) {
+          $new_menu['options'] = array('langcode'=> $node->language);
+        }
+        if( $existing_item = db_fetch_array(db_query("SELECT mlid FROM {menu_links} WHERE link_path = '%s' AND plid = %d", 'node/' . $node->nid, $new_menu['plid']))) {
+          $new_menu['mlid']=$existing_item['mlid'];
+        }          
+        if (!menu_link_save($new_menu)) {
+          drupal_set_message(t('There was an error saving the auto-menu link.'), 'error');
+        }
+         else {
+          drupal_set_message(t('The page was automatically added/updated to: !menu.', array('!menu' => $parent_menu[0])));
         }
       }
       break;
@@ -55,7 +61,6 @@ function automenu_form_alter(&$form, $fo
     // add a selection for "no language" selection... maybe we can find a
     // better way (I think this is what happens in language neutral situations?)
     
-    
     $form['workflow']['parentmenu'] = array(
       '#type' => 'select',
       '#title' => t('Default parent menu'),
@@ -80,8 +85,39 @@ function automenu_form_alter(&$form, $fo
       '#default_value' => variable_get('automenu_hide_' . $form['#node_type']->type, 0),
       '#description' => t('Set all auto generated menu items of this content type to hidden'),
     );
+  }
+}
 
-
+/**
+ * Tries to find a parent menu id based on a path.  It will recurse up the path until
+ * it finds a valid parent.  If it gets to the top level, it will return null.
+ * @return mixed null if no parent or a valid menu id 
+ */
+function _automenu_parent_menu_id_by_path($path) {
+  $path_parts = explode('/', $path);
+  if (count($path_parts) > 1) {
+    array_pop($path_parts);
+    $parent_path = implode('/', $path_parts);
+    $parent = _automenu_load_menuitem_by_path($parent_path);
+    if ($parent != null && $parent->mlid != null) {
+      return $parent->mlid;
+    }
+    else {
+      return _automenu_parent_menu_id_by_path($parent_path);
+    }
   }
+  return null;
+}
+
+/**
+ * Loads a menu_links details based on the path. 
+ * @return object a menu_link or null object
+ */
+function _automenu_load_menuitem_by_path($path){
+  $result = db_fetch_object(db_query("SELECT ml.* FROM {menu_links} ml 
+    JOIN {url_alias} ua ON ua.src = ml.link_path
+    WHERE ua.dst = '%s'", $path));
+  return $result;
 }
+
 
