? menu_block_split_drupal6_cleanup.patch
? mikl.patch
Index: menu_block_split.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/menu_block_split/menu_block_split.module,v
retrieving revision 1.5.2.2
diff -u -r1.5.2.2 menu_block_split.module
--- menu_block_split.module	6 Aug 2008 12:20:44 -0000	1.5.2.2
+++ menu_block_split.module	6 Nov 2008 11:16:30 -0000
@@ -8,62 +8,62 @@
  * Modified for Drupal 6.x by Frank Meyerer <meyerer@digi-info.de>
  * http://www.digi-info.de
  */
- 
+
 /**
- * Implementation of hook_perm()
+ * Implementation of hook_perm().
  */
 function menu_block_split_perm() {
   return array('administer menu block split');
 }
 
 /**
- * Implementation of hook_menu
+ * Implementation of hook_menu().
  */
 function menu_block_split_menu() {
   $items = array();
-  
+
   $items['admin/settings/menu_block_split'] = array(
     'title' => t('Menu block split'),
     'description' => t('Settings for Menu Block Split'),
     'page callback' => 'drupal_get_form',
     'page arguments' => array('menu_block_split_settings'),
     'access arguments' => array('administer menu block split'),
-    'type' => MENU_NORMAL_ITEM,
-  ); 
-    
+  );
+
   return $items;
 }
 
 /**
+ * Implementation of hook_theme().
+ */
+function menu_block_split_theme() {
+  return array(
+    'menu_block_split_menu' => array(
+      'arguments' => array('name', 'tree', 'level'),
+    ),
+  );
+}
+
+
+/**
  * Settings form
  */
 function menu_block_split_settings() {
-  $options = array(
-    1 => 1,
-    2 => 2,
-    3 => 3,
-    4 => 4,
-    5 => 5,
-    6 => 6,
-    7 => 7,
-    8 => 8,
-    9 => 9,
-    10 => 10
-  );
   $form['menu_block_split_howmany'] = array(
     '#type' => 'select',
     '#title' => t('How many blocks with first level menu do you need?'),
     '#default_value' => variable_get('menu_block_split_howmany', 1),
-    '#options' => $options,
+    '#options' => range(0, 10),
     '#description' => t('Set how many first menu level blocks do you need and click on the Save Configuration button to have the form available.')
   );
-  unset($options);
-  $menu_names = menu_get_menus();
-  foreach($menu_names as $name => $title) {
-    $menuinfo = menu_navigation_links($name);
-    if(count($menuinfo) > 0) {
-        $options[$name] = $title;
-    }  
+  // We don't want zero as an option
+  unset($form['menu_block_split_howmany']['#options'][0]);
+
+  $menu_select_options = array();
+  foreach(menu_get_menus() as $name => $title) {
+    if(count(menu_navigation_links($name)) > 0) {
+      $menu_select_options[$name] = $title;
+    }
   }
   for ($i = 1; $i <= variable_get('menu_block_split_howmany', 1); $i++) {
     $form['menu_block_split_fieldset_'. $i] = array(
@@ -75,168 +75,114 @@
     $form['menu_block_split_fieldset_'. $i]['menu_block_split_'. $i] = array(
       '#type' => 'select',
       '#title' => t('Block'. $i),
-      '#options' => $options,
+      '#options' => $menu_select_options,
       '#default_value' => variable_get('menu_block_split_'. $i, ''),
       '#description' => t('Choose a block as first level menu to split.'),
-      '#attributes' => array('onchange' => "auto=getElementById('edit-menu_block_splitinfo-". $i ."');menu_block_splitinfo=getElementById('edit-menu_block_split-". $i ."').value;auto.value=menu_block_splitinfo"),
     );
     $form['menu_block_split_fieldset_'. $i]['menu_block_splittitle_'. $i] = array(
       '#type' => 'textfield',
-      '#title' => t('Title'. $i),
+      '#title' => t('Title') .' '. $i,
       '#default_value' => variable_get('menu_block_splittitle_'. $i, ''),
       '#required' => FALSE,
-      '#description' => t('Set the title of you resulting block.')
-    ); 
+      '#description' => t('Set the title of the resulting block.')
+    );
   }
   return system_settings_form($form);
 }
 
-function menu_block_split_theme() {
-  $functions = array(
-    'menu_block_split_first_level_menu' => array('arguments' => array('menu_name' => ''))
-  );
-  return $functions;
-}
-
+/**
+ * Implementation of hook_block().
+ */
 function menu_block_split_block($op = 'list', $delta = 0, $edit = array()) {
-  
-  if ($op == 'list') {
-    for ($i = 1; $i <= variable_get('menu_block_split_howmany', 1); $i++) { 
-      $mid = variable_get('menu_block_split_'. $i, '');
-      $item = menu_load($mid);
-      $blocks[$i]['info'] = 'MBS-'. $item['title'] .'-1st';
-    }
-    $blocks[1000]['info'] = 'MBS-2nd';
-    return $blocks;
-  }
-  else if ($op == 'configure' && $delta == 0) {
-   
-  }
-  else if ($op == 'save' && $delta == 0) {
-    
-  }
-  else if ($op == 'view') {
-    switch ($delta) {
-      case $delta < 1000:
+
+  switch ($op) {
+    case 'list':
+      for ($i = 1; $i <= variable_get('menu_block_split_howmany', 1); $i++) {
+        $mid = variable_get('menu_block_split_'. $i, '');
+        $item = menu_load($mid);
+        $blocks[$i]['info'] = 'Menu block split 1st level ('. $item['title'] .')';
+      }
+      $blocks[0]['info'] = 'Menu block split 2nd level';
+      return $blocks;
+    break;
+    case 'view':
+      // Delta 0 is the 2nd level block
+      if ($delta > 0) {
+        $tree = menu_tree_page_data(variable_get('menu_block_split_'. $delta, ''));
         $block['subject'] = variable_get('menu_block_splittitle_'. $delta, '');
-        $block['content'] = theme('menu_block_split_first_level_menu', variable_get('menu_block_split_'. $delta, ''));
-        break;
-      case 1000:
+        $block['content'] = theme('menu_block_split_menu',
+                                  variable_get('menu_block_split_'. $delta, ''),
+                                  $tree, $delta);
+      }
+      else {
         $current_router_item = menu_get_item();
         for ($i = 1; $i <= variable_get('menu_block_split_howmany', 1); $i++) {
-          $menu_name = variable_get('menu_block_split_'. $i, '');
-          $module = ($menu_name == 'navigation') ? 'system' : 'menu';
-          $current_menu_item = menu_block_split_get_menu_link_info($current_router_item['href'], $module);
-          $parent = menu_block_split_get_parent($current_menu_item);
-          var_dump($parent);
-          if($menu_name == $current_menu_item['menu_name']) { 
-            if(menu_block_split_has_child($parent)) {// parent has a child?
-              $second_level_tree = menu_block_split_second_level_tree(menu_tree_page_data($menu_name), $parent);
-              //$block['subject'] = $current_menu_item['link_title'];
-              $block['subject'] = menu_block_split_get_menu_title($parent);
-              $block['content'] = menu_tree_output($second_level_tree);
-            }
-          }
+          $name = variable_get('menu_block_split_'. $i, '');
+          $active = menu_get_active_menu_name();
+          if ($active != $name) menu_set_active_menu_name($name);
+          $trail = menu_get_active_trail();
+          $tree = menu_tree_page_data($name);
+          $info = get_first_level($trail);
+          $block['subject'] = $info['title'];
+          $block['content'] = theme('menu_block_split_menu',
+                                    $name, $tree, $delta);
         }
-    }
-    return isset($block) ? $block : NULL;
+      }
+      break;
+    case 'cache':
+      return BLOCK_NO_CACHE;
   }
+  return $block;
 }
 
-function menu_block_split_first_level_menu_tree($menu_name = '') {
-  
-  $menu_items = menu_navigation_links($menu_name,0);
-  $output = '';
-  
-  $i = 1;
-  $menu_count = count($menu_items);
-  foreach ($menu_items as $k => $v) {
-    
-    $link = theme('menu_item_link', $v);
-    
-    $in_active_trail = FALSE;
-    if((isset($v['attributes']) && isset($v['attributes']['class'])) && ($v['attributes']['class'] == 'active-trail')) {
-      $in_active_trail = TRUE;
-    }
-    
-    if($i == 1) {
-     $extra_class = 'first';
-    }
-    elseif($i == $menu_count) {
-      $extra_class = 'last';
-    }
-    else {
-     $extra_class = ''; 
-    }
-    
-    $output .= theme('menu_item', $link, 0, '', $in_active_trail, $extra_class);
-    
-    $i++;
-  }
-  
-  return $output;
-}
 
-function theme_menu_block_split_first_level_menu($menu_name = '') {
-  if ($tree = menu_block_split_first_level_menu_tree($menu_name)) {
-    return "\n<ul class=\"menu\">\n". $tree ."\n</ul>\n";
+/**
+ * Theme menu
+ *
+ * @param string $name
+ * @param array $tree
+ * @return menu tree
+ */
+function theme_menu_block_split_menu($name = 'navigation', $tree, $level) {
+  if ($menu = menu_block_split_render_tree($tree, $level)) {
+    return $menu;
   }
 }
 
-function menu_block_split_has_child($parent) {
-  
-  $menu_info = db_fetch_object(db_query("SELECT has_children FROM {menu_links} WHERE mlid = '%d'", $parent));
-  if($menu_info->has_children == 0) {
-    return FALSE;
-  }
-  return TRUE;
-}
+/**
+ * Render menu
+ *
+ * @return menu tree
+ */
+function menu_block_split_render_tree($tree, $level) {
 
-function menu_block_split_get_menu_link_info($path, $module = 'menu') {
+  $output = '';
 
-  $link_info = db_fetch_array(db_query("SELECT menu_name, mlid, plid, link_path, router_path, link_title, options, module, hidden, external, has_children, expanded, weight, depth, customized, p1, p2, p3, p4, p5, p6, p7, p8, p9, updated FROM {menu_links} WHERE link_path = '%s' AND module = '%s'", $path, $module));
-  
-  return $link_info;
-}
-
-function menu_block_split_second_level_tree($tree, $mlid) {
-  
-  // why is this here, which is not being used in this function?  
-  //$menus = menu_get_menus();
-  //$current_menu = array($current_menu_item['menu_name'] => $menus[$current_menu_item['menu_name']]);
-  
-  foreach($tree as $key => $value) {
-    if($value['link']['mlid'] == $mlid)
-    {
-      $second_level_tree = $tree[$key]['below'];
+  $num_items = count($items);
+  foreach ($tree as $i => $mid) {
+    if ($mid['link']['hidden'] == 0) {
+      $link = theme('menu_item_link', $mid['link']);
+      // our first block
+      if ($level > 0) {
+        $output .= theme('menu_item', $link, '');
+      }
+      // sub block
+      else {
+        if ($mid['below']) {
+          $output .= menu_tree_output($mid['below'], $level);
+        }
+      }
     }
   }
-  
-  return $second_level_tree;
-}
-
-function menu_block_split_get_parent($current_menu_item) {
-  
-  if($current_menu_item['plid'] != 0) {
-    $menu_infos = db_fetch_array(db_query("SELECT mlid, plid FROM {menu_links} WHERE mlid = '%d'", $current_menu_item['plid']));
-    $parent_mlid = menu_block_split_get_parent($menu_infos);
-  }
-  else
-  {
-    $parent_mlid = $current_menu_item['mlid'];
-  }  
-  
-  return $parent_mlid;
+  if ($output) return "\n<ul class=\"menu\">\n". $output ."\n</ul>\n";
 }
 
 /**
- * returns the title of a given menu link id.
+ * Get info about first level.
  *
- * @param $
- *   int: menu link id.
- * @return
- *   string.
- */
-function menu_block_split_get_menu_title($mlid) {
-  return db_result(db_query("SELECT link_title FROM {menu_links} WHERE mlid = %d", $mlid));
-}
\ No newline at end of file
+ * @param array $trail current trail
+ * @return title
+ */
+function get_first_level($trail) {
+  return array('mlid' => $trail[1]['mlid'], 'title' => $trail[1]['title']);
+}
