| 1 |
<?php
|
| 2 |
|
| 3 |
// $Id: spajax_mm.module,v 1.5 2006/02/04 23:43:11 jjeff Exp $
|
| 4 |
|
| 5 |
// spajax_mm.module by Jeff Robbins
|
| 6 |
// Sriptaculous Magic Menus module for Drupal
|
| 7 |
// adds DHTML/AJAX menu functionality
|
| 8 |
|
| 9 |
/**
|
| 10 |
* @todo
|
| 11 |
* - improve wording on settings page (too much "expandable")
|
| 12 |
* - remove spajax_mm_block_list() function and used cached block_list()
|
| 13 |
*
|
| 14 |
*/
|
| 15 |
|
| 16 |
/**
|
| 17 |
* Implementation of hook_menu()
|
| 18 |
*/
|
| 19 |
|
| 20 |
function spajax_mm_menu($may_cache){
|
| 21 |
$items = array();
|
| 22 |
if ($may_cache){
|
| 23 |
$items[] = array('path' => 'admin/settings/spajax/magicmenus', 'access' => user_access('administer site configuration'), 'title' => 'magic menus', 'callback' => 'spajax_mm_settings_page', 'type' => MENU_LOCAL_TASK);
|
| 24 |
|
| 25 |
$items[] = array('path' => 'admin/settings/spajax/magicmenus/expand', 'access' => user_access('administer site configuration'), 'title' => 'expand menus', 'callback' => 'spajax_mm_expand_menus_confirm', 'type' => MENU_CALLBACK);
|
| 26 |
}
|
| 27 |
return $items;
|
| 28 |
}
|
| 29 |
|
| 30 |
function spajax_mm_help($section){
|
| 31 |
switch($section){
|
| 32 |
case 'admin/modules#name':
|
| 33 |
return t('S/P Magic Menus');
|
| 34 |
case 'admin/modules#description':
|
| 35 |
return t('S/P Magic Menus: AJAX menu blocks (requires spajax.module)');
|
| 36 |
}
|
| 37 |
}
|
| 38 |
|
| 39 |
function spajax_mm_settings_page() {
|
| 40 |
drupal_set_title(t('Scriptaculous/Prototype/AJAX Settings'));
|
| 41 |
$user_blocks = user_block('list');
|
| 42 |
$menu_blocks = menu_block('list');
|
| 43 |
$themes = list_themes();
|
| 44 |
//drupal_set_message('<pre>'. print_r($themes, true) .'</pre>');
|
| 45 |
|
| 46 |
$result = db_query("SELECT * FROM {blocks} WHERE status = 1 AND ((module = 'user' AND delta = 1) OR (module = 'menu')) ORDER BY theme, weight, module");
|
| 47 |
while ($r = db_fetch_object($result)){
|
| 48 |
if ($themes[$r->theme]->status){
|
| 49 |
$themeblocks[$r->theme]['block-'.$r->module.'-'.$r->delta] = ${$r->module.'_blocks'}[$r->delta]['info'];
|
| 50 |
// save 'em for the expand links
|
| 51 |
$menus[$r->delta] = ${$r->module.'_blocks'}[$r->delta]['info'];
|
| 52 |
}
|
| 53 |
}
|
| 54 |
|
| 55 |
$form['float_menus'] = array('#type' => 'fieldset', '#title' => t('Floating Menu Blocks'));
|
| 56 |
$form['float_menus']['intro'] = array('#type' => 'markup', '#value' => t('<p>The Magic Menus can create JavaScript animated toggling Drupal menu blocks. It also can float these blocks, pulling them out of the page structure and allowing users to drag them anywhere on the screen. Listed are the enabled menu blocks for each enabled theme. Select which you would like to affect.</p>'));
|
| 57 |
$value = variable_get('float_menus', array());
|
| 58 |
|
| 59 |
$options = spajax_mm_menublock_options();
|
| 60 |
|
| 61 |
foreach($themeblocks as $theme => $blocks){
|
| 62 |
$form['float_menus'][$theme] = array('#type' => 'fieldset', '#title' => $theme, '#collapsible' => TRUE, '#collapsed' => TRUE);
|
| 63 |
foreach($blocks as $block_name => $block_title){
|
| 64 |
$form['float_menus'][$theme][$block_name] = array(
|
| 65 |
'#type' => 'radios',
|
| 66 |
'#default_value' => $value[$theme][$block_name] ? $value[$theme][$block_name] : '',
|
| 67 |
'#options' => $options,
|
| 68 |
'#title' => $block_title,
|
| 69 |
);
|
| 70 |
if(strlen($value[$theme][$block_name])) {
|
| 71 |
$form['float_menus'][$theme]['#collapsed'] = FALSE;
|
| 72 |
}
|
| 73 |
}
|
| 74 |
}
|
| 75 |
|
| 76 |
$form['#tree'] = TRUE;
|
| 77 |
|
| 78 |
$form['expand'] = array('#type' => 'fieldset', '#title' => t('Expand Menus'), '#collapsible' => TRUE, '#collapsed' => TRUE);
|
| 79 |
$form['expand'][] = array('#type' => 'markup', '#value' => t('<p>In order to use Magic Menus, all submenu items need to be set to "expanded". Select a displayed menu block to expand all of its items.</p>'));
|
| 80 |
foreach($menus as $mid => $title){
|
| 81 |
$expand = spajax_mm_menu_needs_expanding($mid);
|
| 82 |
if ($expand) {
|
| 83 |
$form['expand']['#collapsed'] = FALSE;
|
| 84 |
}
|
| 85 |
$note = $expand ? '<strong>'. t(' <-- This menu needs to be expanded.') .'</strong>' : t(' - already expanded');
|
| 86 |
$list[] = l($title, 'admin/settings/spajax/magicmenus/expand/'. $mid) . $note;
|
| 87 |
}
|
| 88 |
$form['expand'][] = array('#type' => 'markup', '#value' => theme('item_list', $list));
|
| 89 |
$form[] = array('#type' => 'submit', '#value' => t('Submit'));
|
| 90 |
$output = drupal_get_form('spajax_mm_settings', $form);
|
| 91 |
$output .= spajax_mm_version();
|
| 92 |
return $output;
|
| 93 |
}
|
| 94 |
|
| 95 |
function spajax_mm_menublock_options(){
|
| 96 |
$options[''] = t('<strong>Normal</strong>: no change to behavior');
|
| 97 |
$options['dhtml'] = t('<strong>Expandable</strong>: simple expandable menu');
|
| 98 |
$options['absolute'] = t('<strong>Expandable and Floating - Absolute</strong>: menu block is draggable by grabbing title');
|
| 99 |
$options['fixed'] = t('<strong>Expandable and Floating - Fixed</strong>: menu block is draggable and does not scroll with page');
|
| 100 |
return $options;
|
| 101 |
}
|
| 102 |
|
| 103 |
function spajax_mm_settings_submit($form_id, $form_values){
|
| 104 |
variable_set('float_menus', $form_values['float_menus']);
|
| 105 |
drupal_set_message(t('Settings have been saved.'));
|
| 106 |
// reload the page so that new settings are read
|
| 107 |
drupal_goto('admin/settings/spajax/magicmenus');
|
| 108 |
}
|
| 109 |
|
| 110 |
function spajax_mm_expand_menus_confirm($mid = NULL){
|
| 111 |
if (!is_numeric($mid)){
|
| 112 |
drupal_goto('admin/settings/spajax/magicmenus');
|
| 113 |
}
|
| 114 |
$form['mid'] = array('#type' => 'value', '#value' => $mid);
|
| 115 |
$output = confirm_form('spajax_mm_expand_menus', $form, t('Are you sure you want to expand this menu and all of its submenus?'), 'admin/settings/spajax/magicmenus');
|
| 116 |
return $output;
|
| 117 |
}
|
| 118 |
|
| 119 |
function spajax_mm_expand_menus_submit($form_id, $form_values){
|
| 120 |
if ($form_values['confirm']) {
|
| 121 |
spajax_mm_expand_menus($form_values['mid']);
|
| 122 |
drupal_set_message(t('Menu items have been expanded.'));
|
| 123 |
menu_rebuild();
|
| 124 |
}
|
| 125 |
drupal_goto('admin/settings/spajax/magicmenus');
|
| 126 |
}
|
| 127 |
|
| 128 |
/**
|
| 129 |
* Expands a menu and all of its children
|
| 130 |
*
|
| 131 |
* @param integer $mid
|
| 132 |
*/
|
| 133 |
function spajax_mm_expand_menus($mid){
|
| 134 |
if (!$mid) {
|
| 135 |
return;
|
| 136 |
}
|
| 137 |
$menus = menu_get_menu();
|
| 138 |
|
| 139 |
$menu = $menus['items'][$mid];
|
| 140 |
|
| 141 |
$edit = $menus['items'][$mid];
|
| 142 |
if (($edit['type'] & MENU_MODIFIABLE_BY_ADMIN)){ // only save modifiable
|
| 143 |
$edit['mid'] = $mid;
|
| 144 |
//$edit['weight'] = $menus['items'][$mid]['weight'];
|
| 145 |
$edit['expanded'] = 1;
|
| 146 |
menu_edit_item_save($edit);
|
| 147 |
}
|
| 148 |
// uncomment next line to resets all submenus
|
| 149 |
// db_query('DELETE FROM {menu} WHERE mid = %d', $mid);
|
| 150 |
|
| 151 |
// recurse through the children
|
| 152 |
$children = $menu['children'];
|
| 153 |
if(is_array($children)){
|
| 154 |
foreach($children as $cmid){
|
| 155 |
spajax_mm_expand_menus($cmid);
|
| 156 |
}
|
| 157 |
}
|
| 158 |
}
|
| 159 |
|
| 160 |
/**
|
| 161 |
* Tests submenus to see if any need expanding
|
| 162 |
*
|
| 163 |
* @param $mid
|
| 164 |
* the menu id of the menu to check
|
| 165 |
* @return
|
| 166 |
* true if any child is not expanded
|
| 167 |
*/
|
| 168 |
|
| 169 |
function spajax_mm_menu_needs_expanding($mid, $child = FALSE){
|
| 170 |
$menus = menu_get_menu();
|
| 171 |
$menu = $menus['visible'][$mid];
|
| 172 |
$expandme = (!($menu['type'] & MENU_EXPANDED) && ($menu['type'] & MENU_MODIFIABLE_BY_ADMIN));
|
| 173 |
if ($expandme && $child){
|
| 174 |
$bool = TRUE;
|
| 175 |
}
|
| 176 |
else {
|
| 177 |
$children = $menu['children'];
|
| 178 |
if(is_array($children)){
|
| 179 |
foreach($children as $cmid){
|
| 180 |
$bool = spajax_mm_menu_needs_expanding($cmid, TRUE);
|
| 181 |
if ($bool) {
|
| 182 |
break;
|
| 183 |
}
|
| 184 |
else {
|
| 185 |
$bool = FALSE;
|
| 186 |
}
|
| 187 |
}
|
| 188 |
}
|
| 189 |
}
|
| 190 |
return $bool;
|
| 191 |
}
|
| 192 |
|
| 193 |
/**
|
| 194 |
* This calls the float_menu() javascript function for every
|
| 195 |
* menu block that has been enabled in the settings
|
| 196 |
*
|
| 197 |
*/
|
| 198 |
|
| 199 |
function spajax_mm_menu_float(){
|
| 200 |
global $theme_key;
|
| 201 |
if (empty($theme_key)) {
|
| 202 |
init_theme();
|
| 203 |
}
|
| 204 |
$block_list = spajax_mm_block_list();
|
| 205 |
$blocks = variable_get('float_menus', array());
|
| 206 |
if(is_array($blocks[$theme_key])){
|
| 207 |
$callback = url('spajax/set', NULL, NULL, TRUE);
|
| 208 |
foreach($blocks[$theme_key] as $block_id => $type){
|
| 209 |
// check to see if this block appears on this page
|
| 210 |
// (there's got to be a better way to do this...)
|
| 211 |
$rename = str_replace(array('block-menu', 'block-user', '-'), array('menu', 'user', '_'), $block_id);
|
| 212 |
if ($type != '' && isset($block_list[$rename])){
|
| 213 |
switch($type) {
|
| 214 |
case 'fixed':
|
| 215 |
case 'absolute':
|
| 216 |
$position = $type;
|
| 217 |
$block_pos = spajax_get($block_id);
|
| 218 |
$block_pos['top'] = isset($block_pos['top']) ? $block_pos['top'] : variable_get($block_id.'_top_default', '20px');
|
| 219 |
$block_pos['left'] = isset($block_pos['left']) ? $block_pos['left'] : variable_get($block_id.'_left_default', '20px');
|
| 220 |
break;
|
| 221 |
|
| 222 |
case 'dhtml':
|
| 223 |
$position = 'normal';
|
| 224 |
break;
|
| 225 |
|
| 226 |
}
|
| 227 |
spajax_head();
|
| 228 |
spajax_output("float_menu('$block_id', '$position', '{$block_pos['top']}', '{$block_pos['left']}', '$callback');");
|
| 229 |
}
|
| 230 |
}
|
| 231 |
}
|
| 232 |
}
|
| 233 |
|
| 234 |
function spajax_mm_version(){
|
| 235 |
return str_replace(array('$RCSf'.'ile:', ',v', '$Re'.'vision: ', '$Da'.'te: ', '$'), '', '<p style="font-size:x-small">$RCSfile: spajax_mm.module,v $ version: <b>$Revision: 1.5 $</b>, $Date: 2006/02/04 23:43:11 $</p>');
|
| 236 |
}
|
| 237 |
|
| 238 |
/**
|
| 239 |
* Rewrite of block_list()
|
| 240 |
* returns ALL blocks enabled for this page
|
| 241 |
*/
|
| 242 |
|
| 243 |
function spajax_mm_block_list() {
|
| 244 |
global $user, $theme_key;
|
| 245 |
|
| 246 |
if (!count($blocks)) {
|
| 247 |
$result = db_query("SELECT * FROM {blocks} WHERE theme = '%s' AND status = 1 ORDER BY region, weight, module", $theme_key);
|
| 248 |
while ($block = db_fetch_object($result)) {
|
| 249 |
if (!isset($blocks)) {
|
| 250 |
$blocks = array();
|
| 251 |
}
|
| 252 |
// Use the user's block visibility setting, if necessary
|
| 253 |
if ($block->custom != 0) {
|
| 254 |
if ($user->uid && isset($user->block[$block->module][$block->delta])) {
|
| 255 |
$enabled = $user->block[$block->module][$block->delta];
|
| 256 |
}
|
| 257 |
else {
|
| 258 |
$enabled = ($block->custom == 1);
|
| 259 |
}
|
| 260 |
}
|
| 261 |
else {
|
| 262 |
$enabled = TRUE;
|
| 263 |
}
|
| 264 |
|
| 265 |
// Match path if necessary
|
| 266 |
if ($block->pages) {
|
| 267 |
if ($block->visibility < 2) {
|
| 268 |
$path = drupal_get_path_alias($_GET['q']);
|
| 269 |
$regexp = '/^('. preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\<front\\\\>($|\|)/'), array('|', '.*', '\1'. preg_quote(variable_get('site_frontpage', 'node'), '/') .'\2'), preg_quote($block->pages, '/')) .')$/';
|
| 270 |
$page_match = !($block->visibility xor preg_match($regexp, $path));
|
| 271 |
}
|
| 272 |
else {
|
| 273 |
$page_match = drupal_eval($block->pages);
|
| 274 |
}
|
| 275 |
}
|
| 276 |
else {
|
| 277 |
$page_match = TRUE;
|
| 278 |
}
|
| 279 |
|
| 280 |
if ($enabled && $page_match) {
|
| 281 |
$blocks["{$block->module}_{$block->delta}"] = $block;
|
| 282 |
}
|
| 283 |
}
|
| 284 |
}
|
| 285 |
// Create an empty array if there were no entries
|
| 286 |
if (!isset($blocks)) {
|
| 287 |
$blocks = array();
|
| 288 |
}
|
| 289 |
return $blocks;
|
| 290 |
}
|
| 291 |
|
| 292 |
/**
|
| 293 |
* Implementation of hook_spajax()
|
| 294 |
*
|
| 295 |
*/
|
| 296 |
|
| 297 |
function spajax_mm_spajax(){
|
| 298 |
spajax_mm_menu_float();
|
| 299 |
}
|