| 1 |
<?php
|
| 2 |
// $Id: local_menu.module,v 1.11 2009/07/21 19:50:19 davereid Exp $
|
| 3 |
|
| 4 |
define('LOCAL_MENU_TOP', 1);
|
| 5 |
define('LOCAL_MENU_PARENT', 10);
|
| 6 |
define('LOCAL_MENU_FAMILY', 11);
|
| 7 |
define('LOCAL_MENU_CHILDREN', 12);
|
| 8 |
define('LOCAL_MENU_UNLIMITED', 99);
|
| 9 |
|
| 10 |
/**
|
| 11 |
* Implementation of hook_block().
|
| 12 |
*/
|
| 13 |
function local_menu_block($op = 'list', $delta = 0, $edit = array()) {
|
| 14 |
if ($op == 'list') {
|
| 15 |
$blocks[0] = array(
|
| 16 |
'info' => t('Local Menu'),
|
| 17 |
'cache' => BLOCK_CACHE_PER_PAGE | BLOCK_CACHE_PER_USER,
|
| 18 |
);
|
| 19 |
return $blocks;
|
| 20 |
}
|
| 21 |
elseif ($op == 'configure') {
|
| 22 |
$form['local_menu_start'] = array(
|
| 23 |
'#type' => 'select',
|
| 24 |
'#title' => t('Starting depth'),
|
| 25 |
'#description' => t('At what depth we need to start draw the menu.'),
|
| 26 |
'#options' => array(
|
| 27 |
LOCAL_MENU_TOP => t('Top level (like a normal menu)'),
|
| 28 |
2 => t('Level #2'),
|
| 29 |
3 => t('Level #3'),
|
| 30 |
4 => t('Level #4'),
|
| 31 |
LOCAL_MENU_PARENT => t('Parent depth (of current page)'),
|
| 32 |
LOCAL_MENU_FAMILY => t('Current depth'),
|
| 33 |
LOCAL_MENU_CHILDREN => t('One level deeper (children)'),
|
| 34 |
),
|
| 35 |
'#default_value' => variable_get('local_menu_start', 2),
|
| 36 |
);
|
| 37 |
$form['local_menu_depth'] = array(
|
| 38 |
'#type' => 'select',
|
| 39 |
'#title' => t('Rendered depth'),
|
| 40 |
'#description' => t('How deep we should allow the menu to be.'),
|
| 41 |
'#options' => array(
|
| 42 |
1 => t('One level (like primary links)'),
|
| 43 |
2 => t('Two levels'),
|
| 44 |
3 => t('Three levels'),
|
| 45 |
LOCAL_MENU_UNLIMITED => t('All levels (unlimited)'),
|
| 46 |
),
|
| 47 |
'#default_value' => variable_get('local_menu_depth', LOCAL_MENU_UNLIMITED),
|
| 48 |
);
|
| 49 |
return $form;
|
| 50 |
}
|
| 51 |
elseif ($op == 'save') {
|
| 52 |
variable_set('local_menu_start', $edit['local_menu_start']);
|
| 53 |
variable_set('local_menu_depth', $edit['local_menu_depth']);
|
| 54 |
}
|
| 55 |
elseif ($op == 'view') {
|
| 56 |
$item = menu_get_item();
|
| 57 |
|
| 58 |
// Local Task
|
| 59 |
if ($item && $item['tab_parent'] && $item['tab_root']) {
|
| 60 |
$path_root = preg_replace('!^('. preg_replace('!%[^/]*!', '[^/]+', $item['tab_root']) .').*$!', '\\1', $item['href']);
|
| 61 |
$item = menu_get_item($path_root);
|
| 62 |
}
|
| 63 |
|
| 64 |
// Not found
|
| 65 |
if (!$item) {
|
| 66 |
return FALSE;
|
| 67 |
}
|
| 68 |
|
| 69 |
// Get the menu link and set the active menu to the menu it's in, so Drupal can find it's way and make correct breadcrumbs at the same time
|
| 70 |
$result = db_query("SELECT mlid FROM {menu_links} WHERE link_path = '%s' AND hidden <> 1 AND menu_name NOT IN ('admin_menu', 'devel')", $item['href']);
|
| 71 |
|
| 72 |
// In case there are multiple, let's find the most appropriate one
|
| 73 |
while ($link = menu_link_load(db_result($result))) {
|
| 74 |
|
| 75 |
if (in_array($link['menu_name'], array(
|
| 76 |
variable_get('menu_primary_links_source', 'primary-links'),
|
| 77 |
variable_get('menu_secondary_links_source', 'secondary-links'),
|
| 78 |
menu_get_active_menu_name(),
|
| 79 |
))) {
|
| 80 |
break;
|
| 81 |
}
|
| 82 |
}
|
| 83 |
|
| 84 |
menu_set_active_menu_name($link['menu_name']);
|
| 85 |
|
| 86 |
// Set current path temporarily to the root of the possible local task we might be, so Drupal finds it's way
|
| 87 |
$q = $_GET['q'];
|
| 88 |
$_GET['q'] = $item['href'];
|
| 89 |
$tree = menu_tree_page_data($link['menu_name']);
|
| 90 |
$_GET['q'] = $q;
|
| 91 |
|
| 92 |
$depth = variable_get('local_menu_depth', LOCAL_MENU_UNLIMITED);
|
| 93 |
$start = variable_get('local_menu_start', 2);
|
| 94 |
$title = db_result(db_query("SELECT title FROM {menu_custom} WHERE menu_name = '%s'", $link['menu_name']));
|
| 95 |
|
| 96 |
if ($start == LOCAL_MENU_TOP && $depth == LOCAL_MENU_UNLIMITED) {
|
| 97 |
$block['subject'] = check_plain($title);
|
| 98 |
$block['content'] = menu_tree_output($tree);
|
| 99 |
}
|
| 100 |
else {
|
| 101 |
|
| 102 |
if ($start == LOCAL_MENU_PARENT) {
|
| 103 |
$start = max(1, $link['depth'] - 1);
|
| 104 |
}
|
| 105 |
elseif ($start == LOCAL_MENU_FAMILY) {
|
| 106 |
$start = $link['depth'];
|
| 107 |
}
|
| 108 |
elseif ($start == LOCAL_MENU_CHILDREN) {
|
| 109 |
$start = $link['depth'] + 1;
|
| 110 |
}
|
| 111 |
|
| 112 |
$block['content'] = local_menu_content($tree, $start, $depth);
|
| 113 |
$block['subject'] = check_plain(($local_title = local_menu_title()) ? $local_title : $title);
|
| 114 |
}
|
| 115 |
|
| 116 |
return $block;
|
| 117 |
}
|
| 118 |
}
|
| 119 |
|
| 120 |
function local_menu_content(&$tree, $start, $depth) {
|
| 121 |
$items = array();
|
| 122 |
$output = '';
|
| 123 |
|
| 124 |
foreach ($tree as $data) {
|
| 125 |
if (!$data['link']['hidden']) {
|
| 126 |
$items[] = $data;
|
| 127 |
}
|
| 128 |
}
|
| 129 |
|
| 130 |
$num_items = count($items);
|
| 131 |
|
| 132 |
foreach ($items as $i => $data) {
|
| 133 |
|
| 134 |
if ($start <= $data['link']['depth']) {
|
| 135 |
$extra_class = NULL;
|
| 136 |
|
| 137 |
if ($i == 0) {
|
| 138 |
$extra_class = 'first';
|
| 139 |
}
|
| 140 |
|
| 141 |
if ($i == $num_items - 1) {
|
| 142 |
$extra_class .= ' last';
|
| 143 |
}
|
| 144 |
|
| 145 |
$link = theme('menu_item_link', $data['link']);
|
| 146 |
$below = ($depth > 1 && $data['below']) ? local_menu_content($data['below'], $start, $depth - 1) : '';
|
| 147 |
$output .= theme('menu_item', $link, $data['link']['has_children'], $below, $data['link']['in_active_trail'], $extra_class);
|
| 148 |
}
|
| 149 |
elseif ($data['link']['in_active_trail'] && $data['below']) {
|
| 150 |
|
| 151 |
if ($start == $data['link']['depth'] + 1) {
|
| 152 |
local_menu_title($data['link']['title']);
|
| 153 |
}
|
| 154 |
|
| 155 |
$output = local_menu_content($data['below'], $start, $depth - 1);
|
| 156 |
}
|
| 157 |
}
|
| 158 |
|
| 159 |
if (isset($data['link']['depth']) && $start <= $data['link']['depth']) {
|
| 160 |
$output = $output ? theme('menu_tree', $output) : '';
|
| 161 |
}
|
| 162 |
|
| 163 |
return $output;
|
| 164 |
}
|
| 165 |
|
| 166 |
function local_menu_title($set_title = '') {
|
| 167 |
static $title;
|
| 168 |
|
| 169 |
if ($set_title) {
|
| 170 |
$title = $set_title;
|
| 171 |
}
|
| 172 |
|
| 173 |
return $title;
|
| 174 |
}
|
| 175 |
|