| 1 |
<?php
|
| 2 |
// $Id: activemenu.module,v 1.3 2008/12/29 22:32:37 nedjo Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Add AJAX-based tree menu to navigation menu.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Implementation of hook_init().
|
| 11 |
*/
|
| 12 |
function activemenu_init() {
|
| 13 |
jstools_modules_includes('activemenu');
|
| 14 |
activemenu_load();
|
| 15 |
}
|
| 16 |
|
| 17 |
/**
|
| 18 |
* Implementation of hook_menu().
|
| 19 |
*/
|
| 20 |
function activemenu_menu() {
|
| 21 |
$items = array();
|
| 22 |
|
| 23 |
$items['activemenu/menu'] = array(
|
| 24 |
'title' => 'activemenu',
|
| 25 |
'access arguments' => array('access content'),
|
| 26 |
'type' => MENU_CALLBACK,
|
| 27 |
'page callback' => 'activemenu_js'
|
| 28 |
);
|
| 29 |
|
| 30 |
$items = array_merge($items, module_invoke_all('activemenu_menu'));
|
| 31 |
return $items;
|
| 32 |
}
|
| 33 |
|
| 34 |
/**
|
| 35 |
* Load needed files.
|
| 36 |
*/
|
| 37 |
function activemenu_load() {
|
| 38 |
static $loaded = FALSE;
|
| 39 |
if (!$loaded) {
|
| 40 |
$path = drupal_get_path('module', 'activemenu');
|
| 41 |
jstools_add_js($path . '/activemenu.js');
|
| 42 |
$activemenu = module_invoke_all('activemenu');
|
| 43 |
if (count($activemenu)) {
|
| 44 |
drupal_add_js(array('activemenu' => $activemenu), 'setting');
|
| 45 |
}
|
| 46 |
activemenu_theme_css();
|
| 47 |
$loaded = TRUE;
|
| 48 |
}
|
| 49 |
}
|
| 50 |
|
| 51 |
/**
|
| 52 |
* Menu callback. Output a themed menu tree for a requested menu item.
|
| 53 |
*/
|
| 54 |
function activemenu_js() {
|
| 55 |
if ($path = activemenu_get_request_path()) {
|
| 56 |
if ($plid = db_result(db_query("SELECT mlid FROM {menu_links} WHERE link_path = '%s'", $path))) {
|
| 57 |
$item = menu_link_load($plid);
|
| 58 |
// Retrieve the access checked tree
|
| 59 |
$tree = menu_tree_all_data($item['menu_name'], $item);
|
| 60 |
|
| 61 |
print drupal_to_js(array('status' => TRUE, 'content' => menu_tree_output($tree)));
|
| 62 |
}
|
| 63 |
}
|
| 64 |
|
| 65 |
exit();
|
| 66 |
}
|
| 67 |
|
| 68 |
/**
|
| 69 |
* Implementation of hook_footer().
|
| 70 |
*
|
| 71 |
* Add a theme-specific css file if needed. Pass Javascript data to the browser.
|
| 72 |
*/
|
| 73 |
function activemenu_theme_css() {
|
| 74 |
global $custom_theme, $theme, $user;
|
| 75 |
|
| 76 |
if (isset($user->theme) && $user->theme != '') {
|
| 77 |
$current_theme = $user->theme;
|
| 78 |
}
|
| 79 |
elseif (!empty($custom_theme)) {
|
| 80 |
$current_theme = $custom_theme;
|
| 81 |
}
|
| 82 |
else {
|
| 83 |
$current_theme = $theme ? $theme : variable_get('theme_default', 'garland');
|
| 84 |
}
|
| 85 |
|
| 86 |
$path = drupal_get_path('module', 'activemenu');
|
| 87 |
$file = $path .'/theme/'. $current_theme .'.css';
|
| 88 |
if (file_exists($file)) {
|
| 89 |
drupal_add_css($file);
|
| 90 |
}
|
| 91 |
else {
|
| 92 |
drupal_add_css($path .'/activemenu.css', 'theme');
|
| 93 |
}
|
| 94 |
}
|
| 95 |
|
| 96 |
/**
|
| 97 |
* Read the incoming request and return the correct path.
|
| 98 |
*/
|
| 99 |
function activemenu_get_request_path(){
|
| 100 |
global $language;
|
| 101 |
|
| 102 |
// Handle path prefixing.
|
| 103 |
$langcode = '';
|
| 104 |
if (module_exists('locale') && isset($_POST['path'])) {
|
| 105 |
$_GET['q'] = $_POST['path'];
|
| 106 |
// $language_initialize will remove a language prefix from $_GET['q'], if present.
|
| 107 |
if (variable_get('language_count', 1) != 1) {
|
| 108 |
$language = language_initialize();
|
| 109 |
}
|
| 110 |
|
| 111 |
// We can now use the correct path without a prefix.
|
| 112 |
$_POST['path'] = $_GET['q'];
|
| 113 |
$langcode = ($language != language_default()) ? $language->language : '';
|
| 114 |
}
|
| 115 |
return isset($_POST['path']) ? drupal_get_normal_path($_POST['path'], $langcode) : FALSE;
|
| 116 |
}
|