Only in /var/www/sites/all/modules/menu_trim: menu_trim-debug.module~ diff -up /var/www/sites/all/modules/menu_trim_bak/alpha3/menu_trim.info /var/www/sites/all/modules/menu_trim/menu_trim.info --- /var/www/sites/all/modules/menu_trim_bak/alpha3/menu_trim.info 2008-04-18 15:57:09.000000000 -0400 +++ /var/www/sites/all/modules/menu_trim/menu_trim.info 2008-05-02 23:08:38.000000000 -0400 @@ -1,4 +1,4 @@ -; $Id +; $Id$ name = Menu Trim description = "Allows menu hierarchies to be trimmed when navigated." Only in /var/www/sites/all/modules/menu_trim: menu_trim.info~ diff -up /var/www/sites/all/modules/menu_trim_bak/alpha3/menu_trim.install /var/www/sites/all/modules/menu_trim/menu_trim.install --- /var/www/sites/all/modules/menu_trim_bak/alpha3/menu_trim.install 2008-05-21 16:11:26.000000000 -0400 +++ /var/www/sites/all/modules/menu_trim/menu_trim.install 2008-05-14 12:30:38.000000000 -0400 @@ -71,7 +71,7 @@ function menu_trim_update_2() { $var_id = str_replace('menu_trim_menu_', '', $var->name); $var_data = variable_get($var->name, MENU_TRIM_DISABLED); // based on : menu_trim_admin_settings_submit and _menu_trim_settings_set - $menu[$var_id] = array('method' => $var_data, 'root_title' => 1, 'min_level' => '', 'min_limit' => 0); + $menu[$var_id] = array('method' => $var_data, 'root_title' => 1, 'min_level' => '', 'min_limit' => 0, 'dept' => ''); } variable_set('menu_trim', $menu); Only in /var/www/sites/all/modules/menu_trim: menu_trim.install~ diff -up /var/www/sites/all/modules/menu_trim_bak/alpha3/menu_trim.module /var/www/sites/all/modules/menu_trim/menu_trim.module --- /var/www/sites/all/modules/menu_trim_bak/alpha3/menu_trim.module 2008-04-21 23:30:33.000000000 -0400 +++ /var/www/sites/all/modules/menu_trim/menu_trim.module 2008-05-21 12:41:30.000000000 -0400 @@ -41,6 +41,10 @@ function menu_trim_help($section) { $output .= '
'. t('A minimum level from which starting displaying a menu by default.') .'
'; $output .= '
'. t('Limit') .'
'; $output .= '
'. t('Won\'t allow trimmable items to trim under its menu minimum level.') .'
'; + $output .= '
'. t('Dept') .'
'; + $output .= '
'. t('The maximum amount of level that a menu will display.') .'
'; + $output .= '
'. t('Exp.') .'
'; + $output .= '
'. t('Won\'t allow expanded menux to reach after the dept.') .'
'; $output .= ''; return $output; } @@ -84,8 +88,7 @@ function menu_trim_perm() { */ function menu_trim_admin_settings() { $menu_list = menu_get_root_menus(); - $menu_fetched = variable_get('menu_trim', NULL); - $menu = is_array($menu_fetched) ? $menu_fetched : array(); + $settings = variable_get('menu_trim', NULL); foreach ($menu_list as $mid => $title) { $form[$mid] = array( @@ -102,21 +105,27 @@ function menu_trim_admin_settings() { MENU_TRIM_ENABLED_SHOW_MENU_ALWAYS => t('Allow trimming'), MENU_TRIM_ENABLED_SHOW_MENU_ACTIVE => t('Allow trimming, hide when inactive'), ), - '#default_value' => $menu[$mid]['method'] ? $menu[$mid]['method'] : MENU_TRIM_DISABLED, + '#default_value' => $settings[$mid]['method'] ? $settings[$mid]['method'] : MENU_TRIM_DISABLED, ); $form[$mid]['root_title'] = array( '#type' => 'checkbox', - '#default_value' => $menu[$mid]['root_title'] ? $menu[$mid]['root_title'] : FALSE, + '#default_value' => $settings[$mid]['root_title'] ? $settings[$mid]['root_title'] : FALSE, ); $form[$mid]['min_level'] = array( '#type' => 'textfield', - '#default_value' => $menu[$mid]['min_level'], + '#default_value' => $settings[$mid]['min_level'], '#size' => 2, '#maxlength' => 2, ); $form[$mid]['min_level_limit'] = array( '#type' => 'checkbox', - '#default_value' => $menu[$mid]['min_level_limit'] ? $menu[$mid]['min_level_limit'] : FALSE, + '#default_value' => $settings[$mid]['min_level_limit'] ? $settings[$mid]['min_level_limit'] : FALSE, + ); + $form[$mid]['dept'] = array( + '#type' => 'textfield', + '#default_value' => $settings[$mid]['dept'], + '#size' => 2, + '#maxlength' => 2, ); } return system_settings_form($form); @@ -132,11 +141,11 @@ function theme_menu_trim_admin_settings( foreach ($form as $key => $element) { $title1 = $element['title']; if (is_array($title1) && $title1['#type'] == 'item') { - $rows[] = array(drupal_render($form[$key]['title']), drupal_render($form[$key]['method']), drupal_render($form[$key]['root_title']), drupal_render($form[$key]['min_level']), drupal_render($form[$key]['min_level_limit'])); + $rows[] = array(drupal_render($form[$key]['title']), drupal_render($form[$key]['method']), drupal_render($form[$key]['root_title']), drupal_render($form[$key]['min_level']), drupal_render($form[$key]['min_level_limit']), drupal_render($form[$key]['dept'])); } } // First item must have colspan to include hidden items - $header = array(array('data' => t('Menu')), array('data' => t('Trim Method')), array('data' => t('Root Title')), array('data' => t('Min. Level')), array('data' => t('Limit'))); + $header = array(array('data' => t('Menu')), array('data' => t('Trim Method')), array('data' => t('Root Title')), array('data' => t('Min. Level')), array('data' => t('Limit')), array('data' => t('Dept'))); $output = theme('table', $header, $rows); $output .= drupal_render($form); return $output; @@ -160,6 +169,15 @@ function menu_trim_admin_settings_valida elseif ($element['min_level']) { form_set_error($key .'][min_level', t('The minimum level must be an integer greater than zero.')); } + // Validation must be made in 2 steps or it might skip 0 + if (is_numeric($element['dept'])) { + if ($element['dept'] <= 0) { + form_set_error($key .'][dept', t('The dept must be an integer greater than zero.')); + } + } + elseif ($element['dept']) { + form_set_error($key .'][dept', t('The dept must be an integer greater than zero.')); + } } } } @@ -170,16 +188,15 @@ function menu_trim_admin_settings_valida * Save language settings in variable */ function menu_trim_admin_settings_submit($form_id, $form_values) { - $menu = array(); foreach ($form_values as $key => $element) { // Find appropriate element if (is_array($element)) { - $menu[$key] = array('method' => $element['method'], 'root_title' => $element['root_title'], 'min_level' => $element['min_level'], 'min_level_limit' => $element['min_level_limit']); + $settings[$key] = array('method' => $element['method'], 'root_title' => $element['root_title'], 'min_level' => $element['min_level'], 'min_level_limit' => $element['min_level_limit'], 'dept' => $element['dept']); } } - variable_set('menu_trim', $menu); - $menu_set = !db_error(); - if (!$menu_set) { + variable_set('menu_trim', $settings); + $settings_set = !db_error(); + if (!$settings_set) { drupal_set_message(t('The settings did not save properly.'), 'error'); } } @@ -193,12 +210,11 @@ function menu_trim_block($op = 'list', $ * Display the alternative menus for which respective methods aren't disabled. */ case 'list': - $blocks = array(); - $menu = variable_get('menu_trim', NULL); - if (is_array($menu)) { + $settings = variable_get('menu_trim', NULL); + if (is_array($settings)) { $root_menus = menu_get_root_menus(); foreach ($root_menus as $mid => $title) { - if ($menu[$mid] && $menu[$mid]['method'] != MENU_TRIM_DISABLED) { + if ($settings[$mid] && $settings[$mid]['method'] != MENU_TRIM_DISABLED) { $blocks[$mid]['info'] = check_plain($title) .' ('. t('Menu Trim') .')'; } } @@ -213,84 +229,60 @@ function menu_trim_block($op = 'list', $ * then go through as if it was the same as the minimum which will display if * it's a limit or if it isn't will note the minimum level values and search * for a selected item and if one is found it's displayed but if none was found - * the minimum level is displayed. + * the minimum level is displayed. Except for the default behavior, the + * displayed menus will be cropped to the dept value. * * In all cases if nothing is to be displayed and the chosen method is to have * the root menu displayed by default, it does that. */ case 'view': - $data = array(); - $menu = variable_get('menu_trim', NULL); - if (is_array($menu)) { - if ($menu[$delta]['method']) { // disabled is 0 so it won't pass - $trail = _menu_get_active_trail_in_submenu($delta); - if (is_array($trail)) { - array_shift($trail); // Remove trail root menu item - $level = isset($menu, $delta, $menu[$delta]['min_level']) ? $menu[$delta]['min_level'] : NULL; - // Verify if there's a minimum level - if (!is_numeric($level)) { - while ($mid = array_pop($trail)) { if (db_result(db_query("SELECT trim FROM {menu_trim} WHERE mid=%d", $mid))) { - $item = menu_get_item($mid); - $data['subject'] = check_plain($item['title']); - $data['content'] = theme('menu_tree', $mid); - break; + $settings = variable_get('menu_trim', NULL); + if (is_array($settings) && $settings[$delta]['method']) { // disabled method is 0 so it won't pass + $trail = _menu_get_active_trail_in_submenu($delta); + if (is_array($trail)) { + array_shift($trail); // Remove trail root menu item + $level = $settings[$delta]['min_level']; + // Verify if there's a minimum level + if (!is_numeric($level)) { + if ($first_mid = _get_first_trail($trail)) { + $data = _block_data_dept($first_mid, $settings[$delta]['dept'], $trail); + } + } + else { + // Verify if the level is lesser than its minimum + if (count($trail) < $level) { + // Verify if menu hasn't a limit, otherwise it reached an end + if (!$settings[$delta]['min_level_limit']) { + if ($first_mid = _get_first_trail($trail)) { + $data = _block_data_dept($first_mid, $settings[$delta]['dept'], $trail); } } } else { - // Verify if the level is lesser than its minimum - if (count($trail) < $level) { - // Verify if menu hasn't a limit - if (!$menu[$delta]['min_level_limit']) { - while ($mid = array_pop($trail)) { - if (db_result(db_query("SELECT trim FROM {menu_trim} WHERE mid=%d", $mid))) { - $item = menu_get_item($mid); - $data['subject'] = check_plain($item['title']); - $data['content'] = theme('menu_tree', $mid); - break; } - } - } + // Trail up to the minimum level + $min_trail = array_slice($trail, 0, $level); + // Retreive minimum level id + $min_mid = end($min_trail); + // Verify if menu has a limit + if ($settings[$delta]['min_level_limit']) { + $data = _block_data_dept($min_mid, $settings[$delta]['dept'], $trail); } else { - // Verify if the level is greater than its minimum - if (count($trail) > $level) { // Trail includes menu item - array_splice($trail, $level); + if ($first_mid = _get_first_trail($min_trail)) { + $data = _block_data_dept($first_mid, $settings[$delta]['dept'], $trail); } - // Level is at its minimum, by elimination - // Retreive minimum level id - $mid = array_pop($trail); - // Verify if menu has a limit - if ($menu[$delta]['min_level_limit']) { - $item = menu_get_item($mid); - $data['subject'] = check_plain($item['title']); - $data['content'] = theme('menu_tree', $mid); + else { // If there wasn't any selected item up the branch + $data = _block_data_dept($min_mid, $settings[$delta]['dept'], $trail); } - else { - // Save the minimum level id in case there's no selected item up the branch - $limit_mid = $mid; - do { - if (db_result(db_query("SELECT trim FROM {menu_trim} WHERE mid=%d", $mid))) { - $item = menu_get_item($mid); - $data['subject'] = check_plain($item['title']); - $data['content'] = theme('menu_tree', $mid); - break; } - } while ($mid = array_pop($trail)); - // Verify there wasn't any selected item up the branch - if (!$data) { - $item = menu_get_item($limit_mid); - $data['subject'] = check_plain($item['title']); - $data['content'] = theme('menu_tree', $limit_mid); - } - } - } + } } } - // Verify if nothing is to be displayed and the chosen method is to have the root menu displayed by default if (!$data && $menu[$delta]['method'] == MENU_TRIM_ENABLED_SHOW_MENU_ALWAYS) { - // Show the menu using root item as base - $item = menu_get_item($delta); - $data['subject'] = $menu[$delta]['root_title'] ? check_plain($item['title']) : ''; - $data['content'] = theme('menu_tree', $delta); - } + } + // Verify if nothing is to be displayed and the chosen method is to have the root menu displayed by default if (!$data && $settings[$delta]['method'] == MENU_TRIM_ENABLED_SHOW_MENU_ALWAYS) { + // Show the menu using root item as base + $item = menu_get_item($delta); + $data['subject'] = $settings[$delta]['root_title'] ? check_plain($item['title']) : ''; + $data['content'] = theme('menu_tree', $delta); } } return $data; @@ -306,16 +298,15 @@ function menu_trim_block($op = 'list', $ */ function menu_trim_form_alter($form_id, &$form) { if ($form_id == 'menu_edit_item_form') { - $menu = variable_get('menu_trim', NULL); + $settings = variable_get('menu_trim', NULL); $mid = $form['mid']['#value']; - // Initialisation verification if (isset($menu, $mid)) { + // Initialisation verification if (isset($settings, $mid)) { $item = menu_get_item($mid); - // Retreive root menu, based on _menu_get_active_trail() while ($item['pid']) { - $root = $item['pid']; + // Retreive root menu, based on _menu_get_active_trail() while ($item['pid']) { $root = $item['pid']; $item = menu_get_item($item['pid']); } // Verify if current item root has trimming enabled - if ($menu[$root]['method'] != MENU_TRIM_DISABLED) { + if ($settings[$root]['method'] != MENU_TRIM_DISABLED) { $form['trim'] = array( '#type' => 'checkbox', '#title' => t('Trim parent items'), @@ -324,12 +315,11 @@ function menu_trim_form_alter($form_id, '#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; - } + // Make sure the button remains at the form's bottom $form['submit']['#weight'] = 10; } } } } - + /** * Form submittion customization for edit_item_form. * The parameters are the same of the calling function menu_trim_form_alter. @@ -354,3 +344,70 @@ function menu_trim_edit_item_form_submit } } +/** + * Create block data limiting the trail to the dept. + * + * @param $mid Default menu ID. + * @param $dept Maximum dept of the menu to create. + * @param $trail Menu ID trail. + * + * @return Array containing the block data. + */ +function _block_data_dept($mid, $dept, $trail) { + if (is_numeric($dept) && is_array($trail) && $trail) { + $dept_search = 0; + foreach (array_reverse($trail) as $mid_search) { + if ($dept_search == $dept || $mid_search == $mid) { + break; + } + $dept_search++; + } + $item = menu_get_item($mid_search); + $data['subject'] = check_plain($item['title']); + $data['content'] = theme('menu_tree', $mid_search); + } + else { + $item = menu_get_item($mid); + $data['subject'] = check_plain($item['title']); + $data['content'] = theme('menu_tree', $mid); + } + return $data; +} + +/** + * Get the first menu marked as trimmed. + * + * @param $trail Menu ID trail. + * + * @return Menu ID or NULL if nothing was found. + * + * @see _get_trail_data() + */ +function _get_first_trail($trail) { + if ($trail_trims = _get_trail_data($trail)) { + foreach (array_reverse($trail) as $trail_mid) { + if ($trail_trims[$trail_mid]) { // Verify if marked as trimmed. + return $trail_mid; + } + } + } + return; +} + +/** + * Get the data from the trail menus. + * + * @param $trail Menu ID trail. + * + * @return Array with trim results keyed to their Menu ID or NULL if nothing was found. + */ +function _get_trail_data($trail) { + if ($trail) { // Empty IN argument would make an infinite query loop + $trail_trims_query = db_query("SELECT mid, trim FROM {menu_trim} WHERE mid IN (%s)", implode(",", $trail)); + while (($trail_trim = db_fetch_array($trail_trims_query)) !== FALSE) { + $trail_trims[$trail_trim['mid']] = $trail_trim['trim']; + } + } + return $trail_trims; // Could be NULL. +} + Only in /var/www/sites/all/modules/menu_trim: menu_trim.module~ Only in /var/www/sites/all/modules/menu_trim: .patch