Index: menu_trim.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/menu_trim/menu_trim.module,v
retrieving revision 1.3
diff -u -p -r1.3 menu_trim.module
--- menu_trim.module	6 Apr 2008 04:12:45 -0000	1.3
+++ menu_trim.module	3 Nov 2008 01:16:15 -0000
@@ -35,20 +35,15 @@ define ('MENU_TRIM_ENABLED_SHOW_MENU_ACT
 /**
  * Implementation for hook_menu().
  */
-function menu_trim_menu($may_cache) {
-  $items = array();
-
-  if ($may_cache) {
-    $items[] = array(
-      'path' => 'admin/settings/menu_trim',
-      'title' => t('Menu trim'),
-      'description' => t('Choose what menus need trimming.'),
-      'callback' => 'drupal_get_form',
-      'callback arguments' => array('menu_trim_admin_settings'),
-      'access' => user_access('administer site configuration'),
-      'type' => MENU_NORMAL_ITEM,
-    );
-  }
+function menu_trim_menu() {
+  $items['admin/settings/menu_trim'] = array(
+    'title' => t('Menu trim settings'),
+    'description' => t('Choose what menus need trimming.'),
+    'page callback' => 'drupal_get_form',
+    'page arguments' => array('menu_trim_admin_settings'),
+    'access arguments' => array('administer site configuration'),
+    'type' => MENU_NORMAL_ITEM,
+  );
 
   return $items;   
 }
@@ -63,17 +58,17 @@ function menu_trim_admin_settings() {
     '#description' => t('Choose the menus that need to be trimmed. Each of the menus where trimming is allowed will get a corresponding <em>Menu trim</em> block. You should then !enable, and !edit that need to have their parents trimmed.', array('!enable' => l(t('enable those blocks'), 'admin/build/block'), '!edit' => l(t('edit the menu items'), 'admin/build/menu'))),
   );
     
-  $menus = menu_get_root_menus();
-  foreach ($menus as $mid => $title) {
-    $form['menu_trim']['menu_trim_menu_'. $mid] = array(
+  $menus = _menu_trim_menu_overview();
+  foreach ($menus as $menu) {
+    $form['menu_trim']['menu_trim_menu_'. $menu['menu_name']] = array(
       '#type' => 'select',
-      '#title' => check_plain($title),
+      '#title' => check_plain($menu['title']),
       '#options' => array(
         MENU_TRIM_DISABLED => t('Never trim'),
         MENU_TRIM_ENABLED_SHOW_MENU_ALWAYS => t('Allow trimming'),
         MENU_TRIM_ENABLED_SHOW_MENU_ACTIVE => t('Allow trimming, hide when inactive'),
       ),
-      '#default_value' => variable_get('menu_trim_menu_'. $mid, MENU_TRIM_DISABLED),
+      '#default_value' => variable_get('menu_trim_menu_'. $menu['menu_name'], MENU_TRIM_DISABLED),
     );
   }
 
@@ -86,11 +81,14 @@ function menu_trim_admin_settings() {
 function menu_trim_block($op = 'list', $delta = 0, $edit = array()) {
   switch ($op) {
     case 'list':
-      $blocks = array();
-      $menus = menu_get_root_menus();
-      foreach ($menus as $mid => $title) {
-        if (variable_get('menu_trim_menu_'. $mid, MENU_TRIM_DISABLED) != MENU_TRIM_DISABLED) {
-          $blocks[$mid]['info'] = check_plain($title) .' ('. t('Menu trim') .')';
+      $menus = _menu_trim_menu_overview();
+      
+      foreach ($menus as $menu) {
+        $menu_name = $menu['menu_name'];
+        $title = $menu['title'];
+        
+        if (variable_get('menu_trim_menu_'. $menu_name, MENU_TRIM_DISABLED) != MENU_TRIM_DISABLED) {
+          $blocks[$menu_name]['info'] = check_plain($title) .' ('. t('Menu trim') .')';
         }
       }
       return $blocks;
@@ -99,68 +97,101 @@ function menu_trim_block($op = 'list', $
       if (($mode = variable_get('menu_trim_menu_'. $delta, MENU_TRIM_DISABLED)) != MENU_TRIM_DISABLED) {
         // Get the trail of the active item in the $delta menu. If the active
         // item is not in that menu, the menu won't be shown at all.
-        $trail = _menu_get_active_trail_in_submenu($delta);
-        if (is_array($trail)) {
-          // The active item is child of this menu, check for trimming
-          while ($mid = array_pop($trail)) {
-            if (variable_get('menu_trim_item_'. $mid, FALSE)) {
-              // Show the menu using the item as root
-              $item = menu_get_item($mid);
-              $data['subject'] = check_plain($item['title']);
-              $data['content'] = theme('menu_tree', $mid);
-              break;
+        // In d6, we need to set the active menu to $delta before doing anything.
+        menu_set_active_menu_name($delta);
+        $trail = menu_get_active_trail();
+
+        while ($menu_item = array_pop($trail)) {
+          // Show the menu using the item as root. No way to get a submenu 
+          // based off of a mlid in d6, so need to find the submenu.
+          $level = $menu_item['depth'];
+          $tree = menu_tree_page_data($delta);
+          
+          while ($level-- > 0 && $tree) {
+            // Loop through the current level's items until we find one that is in trail.
+            while ($item = array_shift($tree)) {
+              if ($item['link']['in_active_trail']) {
+                // The active item is child of this menu, check for trimming
+                if (variable_get('menu_trim_item_'. $item['link']['mlid'], FALSE)) {
+                  $parent = $item;
+                  // If the item is in the active trail, we continue in the subtree.
+                  $tree = empty($item['below']) ? array() : $item['below'];
+                  $blocks['subject'] = check_plain($parent['link']['title']);
+                  $blocks['content'] = menu_tree_output($tree);
+                }
+                // If the active item is a child but there isn't trimming, show the menu
+                // as normal
+                else {
+                  $root_menus = _menu_trim_menu_overview();
+                  
+                  while ($root_menu = array_pop($root_menus)) {
+                    if ($root_menu['menu_name'] == $delta) {
+                      $blocks['subject'] = check_plain($root_menu['title']);
+                      $blocks['content'] = menu_tree_output(menu_tree_page_data($delta));
+                    }
+                  }
+                }
+                return $blocks;
+              }
             }
           }
-          if (!$data) {
-            // No trim flag found in the trail, show the full menu
-            $item = menu_get_item($delta);
-            $data['subject'] = check_plain($item['title']);
-            $data['content'] = theme('menu_tree', $delta);
-          }
         }
-        elseif ($mode == MENU_TRIM_ENABLED_SHOW_MENU_ALWAYS) {
+        if ($mode == MENU_TRIM_ENABLED_SHOW_MENU_ALWAYS) {
           // The active item is not child of this menu, show the plain menu
-          $item = menu_get_item($delta);
-          $data['subject'] = check_plain($item['title']);
-          $data['content'] = theme('menu_tree', $delta);
+          $root_menus = _menu_trim_menu_overview();
+          
+          while ($root_menu = array_pop($root_menus)) {
+            if ($root_menu['menu_name'] == $delta) {
+              $blocks['subject'] = check_plain($root_menu['title']);
+              $blocks['content'] = menu_tree_output(menu_tree_page_data($delta));
+            }
+          }
         }
         // Otherwise, $mode == MENU_TRIM_ENABLED_MENU_SHOW_ACTIVE, do not show
         // the menu since it has to be shown only when it contains an active
         // item
       }
-      return $data;
+      return $blocks;
   }
 }
 
 /**
  * Implementation of hook_form_alter().
  */
-function menu_trim_form_alter($form_id, &$form) {
-  if ($form_id == 'menu_edit_item_form') {
-    $mid = $form['mid']['#value'];
-    $item = menu_get_item($mid);
+function menu_trim_form_alter(&$form, $form_state, $form_id) {
+  if ($form_id == 'menu_edit_item') {
+    $mlid = $form['menu']['#item']['mlid'];
+    
     // Check if this item is in a menu that is set for trimming
-    while ($item['pid']) {
-      $root_mid = $item['pid'];
-      $item = menu_get_item($item['pid']);
-    }
-    if (variable_get('menu_trim_menu_'. $root_mid, MENU_TRIM_DISABLED) != MENU_TRIM_DISABLED) {
-      $form['trim_item'] = array(
+    $menu_name = $form['menu']['#item']['menu_name'];
+    if (variable_get('menu_trim_menu_'. $menu_name, MENU_TRIM_DISABLED) != MENU_TRIM_DISABLED) {
+      $form['menu']['trim_item'] = array(
         '#type' => 'checkbox',
         '#title' => t('Trim parent items.'),
         '#description' => t('Check this option to trim the parent items when this item or any of its children becomes active.') .'<br />'. t('Trimming applies only to the menu\'s corresponding <em>Menu trim</em> !block, not to its normal block. When trimming occurs, this item\'s title will become the menu\'s title. Note also that if this item has no children, has its parents trimmed, and becomes active, then no menu will be displayed at all since such menu would be empty.', array('!block' => l(t('block'), 'admin/build/block'))),
-        '#default_value' => variable_get('menu_trim_item_'. $mid, FALSE),
+        '#default_value' => variable_get('menu_trim_item_'. $mlid, FALSE),
         '#weight' => 1,
       );
-      $form['#submit']['menu_trim_edit_item_form_submit'] = current($form['#submit']);
-
-      // Make sure the button remains at the form's bottom
-      $form['submit']['#weight'] = 10;
+      $form['#submit'][] = 'menu_trim_edit_item_submit';
     }
   }
 }
 
-function menu_trim_edit_item_form_submit($form_id, $form_values) {
-  variable_set('menu_trim_item_'. $form_values['mid'], $form_values['trim_item']);
+function menu_trim_edit_item_submit($form, &$form_state) {
+  variable_set('menu_trim_item_'. $form_state['values']['menu']['mlid'], $form_state['values']['menu']['trim_item']);
+}
+
+/**
+ * Helper function to replace menu_get_root_menus() functionality removed in d6
+ **/
+function _menu_trim_menu_overview() {
+  $result = db_query("SELECT * FROM {menu_custom} ORDER BY title");
+
+  $menu_overview = array();
+  while ($menu = db_fetch_array($result)) {
+    $menu_overview[] = $menu;
+  } 
+
+  return $menu_overview;
 }
 
