View source
<?php
function menu_block_perm() {
return array(
'administer menu block',
);
}
function menu_block_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array(
'path' => 'admin/build/menu-block',
'title' => t('Menu blocks'),
'description' => t('Enable blocks to appear on the <a href="!url">administer blocks page</a>.', array(
'!url' => url('admin/build/block'),
)),
'access' => user_access('administer menu block'),
'callback' => 'drupal_get_form',
'callback arguments' => 'menu_block_settings',
);
}
else {
drupal_add_css(drupal_get_path('module', 'menu_block') . '/menu_block.css');
}
return $items;
}
function menu_block_settings() {
include_once './' . drupal_get_path('module', 'menu_block') . '/menu_block.admin.inc';
return _menu_block_settings();
}
function menu_block_settings_submit($form_id, $form_values) {
include_once './' . drupal_get_path('module', 'menu_block') . '/menu_block.admin.inc';
return _menu_block_settings_submit($form_id, $form_values);
}
function menu_block_block($op = 'list', $delta = NULL, $edit = NULL) {
$function = '_menu_block_block_' . $op;
if (function_exists($function)) {
return $function($delta, $edit);
}
else {
include_once './' . drupal_get_path('module', 'menu_block') . '/menu_block.admin.inc';
if (function_exists($function)) {
return $function($delta, $edit);
}
}
}
function _menu_block_block_view($delta) {
$data = array();
$mid = variable_get("menu_block_{$delta}_mid", 1);
$level = variable_get("menu_block_{$delta}_level", 1);
if ($level == 1 || menu_in_active_trail($mid)) {
if ($level != 1) {
$active_trail = _menu_get_active_trail();
$mid = $active_trail[$level - 1];
}
$menu_item = menu_get_item($mid);
$data['subject'] = check_plain($menu_item['title']);
$depth = variable_get("menu_block_{$delta}_depth", 0);
if ($depth == 1) {
$data['content'] = theme('menu_block_list', $mid);
}
else {
$expanded = variable_get("menu_block_{$delta}_expanded", 0);
$data['content'] = theme('menu_block_tree', $mid, $depth, $expanded);
}
}
return $data;
}
function theme_menu_block_tree($pid = 1, $depth_limit = 0, $expanded = FALSE) {
if ($tree = menu_block_tree($pid, $depth_limit, $expanded)) {
return '<ul class="menu menu-tree menu-' . $pid . '">' . $tree . '</ul>';
}
else {
return '';
}
}
function theme_menu_block_list($pid = 1) {
if ($list = menu_block_tree($pid, 1, FALSE, TRUE)) {
return '<ul class="menu-list menu-' . $pid . '">' . $list . '</ul>';
}
else {
return '';
}
}
function menu_block_tree($pid = 1, $depth_limit = 0, $expanded = FALSE) {
$menu = menu_get_menu();
$output = '';
if (isset($menu['visible'][$pid]) && $menu['visible'][$pid]['children']) {
$active_id = menu_get_active_item();
$count = 1;
$total_children = count($menu['visible'][$pid]['children']);
foreach ($menu['visible'][$pid]['children'] as $mid) {
$in_active_trail = menu_in_active_trail_in_submenu($mid, $pid);
$item = menu_get_item($mid);
$item['attributes'] = array();
if (!empty($item['description'])) {
$item['attributes']['title'] = $item['description'];
}
if ($in_active_trail) {
$item['attributes']['class'] = 'active-trail';
}
while ($item['type'] & MENU_LINKS_TO_PARENT) {
$link_item = menu_get_item($item['pid']);
$item['path'] = $link_item['path'];
}
$link = theme('menu_block_item_link', $item);
$has_children = !empty($menu['visible'][$mid]['children']);
$children = '';
if ($has_children) {
$type = isset($menu['visible'][$mid]['type']) ? $menu['visible'][$mid]['type'] : FALSE;
if ($depth_limit != 1 && ($type & MENU_EXPANDED || $expanded || $in_active_trail)) {
$children = theme('menu_block_tree', $mid, $depth_limit ? $depth_limit - 1 : 0, $expanded);
}
}
$extra_class = "menu-{$mid}";
$extra_class .= $count == 1 ? ' first' : '';
$extra_class .= $count == $total_children ? ' last' : '';
$extra_class .= $mid == $active_id || $item['path'] == '<front>' && drupal_is_front_page() ? ' active' : '';
$output .= theme('menu_block_item', $link, $has_children, $children, $in_active_trail, $extra_class);
$count++;
}
}
return $output;
}
function theme_menu_block_item_link($link) {
if (empty($link['attributes'])) {
$link['attributes'] = array();
}
return l($link['title'], $link['path'], $link['attributes'], isset($link['query']) ? $link['query'] : NULL);
}
function theme_menu_block_item($link, $has_children, $menu = '', $in_active_trail = FALSE, $extra_class = NULL) {
$class = $menu ? 'expanded' : ($has_children ? 'collapsed' : 'leaf');
if (!empty($extra_class)) {
$class .= ' ' . $extra_class;
}
if ($in_active_trail) {
$class .= ' active-trail';
}
return '<li class="' . $class . '">' . $link . $menu . "</li>";
}