/[drupal]/contributions/modules/simplemenu/simplemenu.module
ViewVC logotype

Contents of /contributions/modules/simplemenu/simplemenu.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.10 - (show annotations) (download) (as text)
Fri Dec 12 06:24:18 2008 UTC (11 months, 2 weeks ago) by rz
Branch: MAIN
CVS Tags: HEAD
Branch point for: DRUPAL-6--2
Changes since 1.9: +269 -105 lines
File MIME type: text/x-php
merging DRUPAL-6--1 into HEAD
1 <?php
2 // $Id: simplemenu.module,v 1.9.2.11.2.6 2008/12/12 05:25:07 rz Exp $
3
4 /**
5 * @file
6 * Creates a simplemenu.
7 */
8
9 /**
10 * Implementation of hook_menu().
11 */
12 function simplemenu_menu() {
13 $items = array();
14
15 $items['admin/settings/simplemenu'] = array(
16 'title' => 'SimpleMenu',
17 'description' => 'Select the menu to display.',
18 'page callback' => 'drupal_get_form',
19 'page arguments' => array('simplemenu_admin_settings'),
20 'access arguments' => array('administer simplemenu')
21 );
22
23 return $items;
24 }
25
26 /**
27 * Is simplemenu enabled for this page request?
28 */
29 function simplemenu_enabled() {
30 static $enabled;
31
32 if(!isset($enabled)) {
33 global $theme;
34 $exclusions = variable_get('simplemenu_exclusions', array());
35 $enabled = (user_access('view simplemenu')
36 && (!isset($exclusions[$theme]) || !$exclusions[$theme])
37 && _simplemenu_page_visibility());
38 }
39
40 return $enabled;
41 }
42
43 /**
44 * Implementation of hook_init().
45 */
46 function simplemenu_init() {
47 // do a simple access check here, since theme isn't available to check yet
48 if (user_access('view simplemenu')) {
49 $path = drupal_get_path('module', 'simplemenu');
50 $simplemenu_theme = variable_get('simplemenu_theme', 'original');
51 drupal_add_css($path .'/simplemenu.css');
52
53 if ($simplemenu_theme != 'custom') {
54 drupal_add_css($path .'/themes/'. $simplemenu_theme .'/'. $simplemenu_theme .'.css');
55 }
56
57 $settings = array(
58 'effect' => variable_get('simplemenu_effect', 'opacity'),
59 'effectSpeed' => variable_get('simplemenu_effect_speed', 'fast'),
60 'element' => variable_get('simplemenu_element', 'body'),
61 'hideDelay' => variable_get('simplemenu_hide_delay', 800),
62 'placement' => variable_get('simplemenu_element_method', 'prepend'),
63 'detectPopup' => variable_get('simplemenu_detect_popop', 1),
64 );
65
66 drupal_add_js(array('simplemenu' => $settings), 'setting');
67 drupal_add_js($path .'/simplemenu.js');
68 drupal_add_js($path .'/superfish.js');
69 }
70 }
71
72 /**
73 * Implementation of hook_footer().
74 *
75 * This has been broken off of simplemenu_init() because simplemenu_get_menu()
76 * calls simplemenu_menu_tree() which calls menu_tree_output() which has several
77 * calls to theme(). This initializes the theme system too early causing hard
78 * to track bugs.
79 *
80 * @see http://drupal.org/node/219910
81 */
82 function simplemenu_footer() {
83 if(simplemenu_enabled()) {
84 $simplemenu = drupal_to_js(simplemenu_get_menu());
85 $path = base_path() . drupal_get_path('module', 'simplemenu');
86
87 $output = "<script type=\"text/javascript\">var simplemenu = $simplemenu;</script>\n";
88
89 return $output;
90 }
91 }
92
93 /**
94 * Implementation of hook_perm().
95 */
96 function simplemenu_perm() {
97 return array('view simplemenu', 'administer simplemenu');
98 }
99
100 /**
101 * SimpleMenu settings page.
102 */
103 function simplemenu_admin_settings() {
104 if (module_exists('menu')) {
105 $form['default_menu']['simplemenu_menu'] = array(
106 '#type' => 'select',
107 '#title' => t('Menu'),
108 '#options' => menu_parent_options(menu_get_menus(), array( 'mlid' => 0 )), // return complete tree;
109 '#default_value' => variable_get('simplemenu_menu', 'navigation:0'),
110 '#description' => t('Select the menu to display.')
111 );
112 }
113
114 if (module_exists('devel')) {
115 $form['default_menu']['simplemenu_devel'] = array(
116 '#type' => 'checkbox',
117 '#title' => t('Add devel module links'),
118 '#default_value' => variable_get('simplemenu_devel', 0),
119 '#description' => t('Add devel module links for those users that can access the devel module.')
120 );
121 }
122
123 $form['default_menu']['simplemenu_theme'] = array(
124 '#type' => 'select',
125 '#title' => t('Theme'),
126 '#options' => array(
127 'original' => t('original'),
128 'blackblue' => t('black & blue'),
129 'custom' => t('custom'),
130 ),
131 '#default_value' => variable_get('simplemenu_theme', 'original'),
132 '#description' => t('Select which theme to use. If you specify custom, you need to define CSS in your theme.')
133 );
134
135 $form['default_menu']['advanced'] = array(
136 '#type' => 'fieldset',
137 '#title' => t('Advanced settings'),
138 '#collapsible' => TRUE,
139 '#collapsed' => TRUE
140 );
141
142 $form['default_menu']['advanced']['simplemenu_element'] = array(
143 '#type' => 'textfield',
144 '#title' => t('CSS selector to attach menu to'),
145 '#default_value' => variable_get('simplemenu_element', 'body'),
146 '#description' => t('A valid CSS selector to attach the menu to. <em>Example: body, #primary, div.my-class</em>'),
147 '#required' => TRUE
148 );
149
150 $form['default_menu']['advanced']['simplemenu_element_method'] = array(
151 '#type' => 'radios',
152 '#title' => t('Attach method'),
153 '#options' => array(
154 'prepend' => t('Prepend'),
155 'append' => t('Append'),
156 ),
157 '#default_value' => variable_get('simplemenu_element_method', 'prepend'),
158 '#description' => t('Choose how the menu should be attached to the above selector.'),
159 '#required' => TRUE
160 );
161
162 $form['default_menu']['advanced']['simplemenu_exclusions'] = array(
163 '#type' => 'checkboxes',
164 '#title' => t('Theme exclusions'),
165 '#options' => drupal_map_assoc(array_keys(list_themes())),
166 '#default_value' => variable_get('simplemenu_exclusions', array()),
167 '#description' => t('Select which themes to <strong>not</strong> display the menu. Use this when you have a theme that displays its own admin navigation.'),
168 );
169
170 $form['default_menu']['advanced']['simplemenu_hide_delay'] = array(
171 '#type' => 'textfield',
172 '#title' => t('Hide delay'),
173 '#size' => 4,
174 '#default_value' => variable_get('simplemenu_hide_delay', 800),
175 '#description' => t('How long (in milliseconds) should a menu still appear after losing focus.')
176 );
177
178 $form['default_menu']['advanced']['simplemenu_effect'] = array(
179 '#type' => 'radios',
180 '#title' => t('Show effect'),
181 '#options' => array('opacity' => t('Fade'), 'height' => t('Slide'), 'none' => t('None')),
182 '#default_value' => variable_get('simplemenu_effect', 'opacity'),
183 '#description' => t('The effect used when displaying a menu.')
184 );
185
186 $form['default_menu']['advanced']['simplemenu_effect_speed'] = array(
187 '#type' => 'radios',
188 '#title' => t('Show speed'),
189 '#options' => array('slow' => t('Slow'), 'medium' => t('Medium'), 'fast' => t('Fast')),
190 '#default_value' => variable_get('simplemenu_effect_speed', 'fast'),
191 '#description' => t('The speed of the effect, not used when "none" is set to show effect.')
192 );
193
194 $form['default_menu']['advanced']['simplemenu_detect_popop'] = array(
195 '#type' => 'checkbox',
196 '#title' => t('Detect pop-up windows'),
197 '#default_value' => variable_get('simplemenu_detect_popop', 1),
198 '#description' => t("Choose whether SimpleMenu should attempt to detect if it is inside of a pop-up window. If enabled, SimpleMenu will not display if it is inside of a pop-up window."),
199 );
200
201 $form['default_menu']['advanced']['simplemenu_visibility_operator'] = array(
202 '#type' => 'radios',
203 '#title' => t('Show block on specific pages'),
204 '#default_value' => variable_get('simplemenu_visibility_operator', 0),
205 '#options' => array(
206 0 => t('Show on every page except the listed pages.'),
207 1 => t('Show on only the listed pages.')
208 ),
209 );
210
211 $form['default_menu']['advanced']['simplemenu_visibility_pages'] = array(
212 '#type' => 'textarea',
213 '#title' => t('Pages'),
214 '#default_value' => variable_get('simplemenu_visibility_pages', ''),
215 '#description' => t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array('%blog' => 'blog', '%blog-wildcard' => 'blog/*', '%front' => '<front>')),
216 );
217
218 return system_settings_form($form);
219 }
220
221 /**
222 * Render an HTML list of links for a given menu.
223 */
224 function simplemenu_get_menu() {
225 $output = '';
226
227 // if a user turned off menu module but SimpleMenu was previously set
228 // reset variable so a menu appears
229 $menu_name = module_exists('menu') ? variable_get('simplemenu_menu', 'navigation:0') : 'navigation:0';
230 $menu = simplemenu_menu_tree($menu_name);
231
232 if (!$menu) {
233 $menu = '<li><a href="'. url('admin/settings/simplemenu') .'">'. t('No menu items found. Try a different menu as the default.') .'</a></li>';
234 }
235
236 // This is ugly, I know, but it is the only way I can see to get the additional
237 // links inside the <ul> tags
238 if($devel = simplemenu_get_devel()) {
239 $pos = strpos($menu, '>') + 1;
240 $menu = substr($menu, 0, $pos) . $devel .substr($menu, $pos);
241 }
242
243 $output .= $menu;
244
245 return $output;
246 }
247
248 /**
249 * Custom implementation of menu_tree().
250 * We want to retrieve the entire menu structure for a given menu,
251 * regardless of whether or not the menu item is expanded or not.
252 */
253
254 function simplemenu_menu_tree($menu_name = 'navigation:0') {
255 static $menu_output = array();
256
257 if (!isset($menu_output[$menu_name])) {
258 $tree = simplemenu_tree_all_data($menu_name);
259 $menu_output[$menu_name] = menu_tree_output($tree);
260 }
261 return $menu_output[$menu_name];
262 }
263
264 /**
265 * Modified menu_tree_all_data(), providing the complete menu tree below $root_menu
266 * (which can be *any* menu item, not just the root of a custom menu).
267 *
268 * @param $root_menu
269 * root menu item of the tree to return as "menu_name:mlid" (mlid = menu link id)
270 *
271 * @todo we don't actually need $menu_name, $mlid would be sufficient
272 */
273 function simplemenu_tree_all_data($root_menu = 'navigation:0') {
274 static $tree = array();
275
276 list($menu_name, $mlid) = explode(':', $root_menu);
277
278 // Generate the cache ID.
279 // "links:navigation:all:2" means "all from root to 2" (what the ...), so for "all from 2 down" we do "links:navigation:all:2:all"
280 $cid = "links:$menu_name:all:$mlid". ($mlid ? ':all' : '');
281
282 if (!isset($tree[$cid])) {
283 // If the static variable doesn't have the data, check {cache_menu}.
284 $cache = cache_get($cid, 'cache_menu');
285 if ($cache && isset($cache->data)) {
286 $data = $cache->data;
287 }
288 else {
289 // Build and run the query, and build the tree.
290 if ($mlid > 0) {
291 $item = menu_link_load($mlid);
292 // The tree is a subtree of $menu_name, so we need to restrict the query to
293 // this subtree.
294 $px = "p$item[depth]";
295 $where = " AND ml.$px = %d AND ml.mlid != %d";
296 $args = array($item[$px], $mlid);
297 }
298 else {
299 // Get all links in this menu.
300 $where = '';
301 $args = array();
302 }
303 array_unshift($args, $menu_name);
304 // Select the links from the table, and recursively build the tree. We
305 // LEFT JOIN since there is no match in {menu_router} for an external
306 // link.
307 $data['tree'] = menu_tree_data(db_query("
308 SELECT m.load_functions, m.to_arg_functions, m.access_callback, m.access_arguments, m.page_callback, m.page_arguments, m.title, m.title_callback, m.title_arguments, m.type, m.description, ml.*
309 FROM {menu_links} ml LEFT JOIN {menu_router} m ON m.path = ml.router_path
310 WHERE ml.menu_name = '%s'". $where ."
311 ORDER BY p1 ASC, p2 ASC, p3 ASC, p4 ASC, p5 ASC, p6 ASC, p7 ASC, p8 ASC, p9 ASC", $args));
312 $data['node_links'] = array();
313 menu_tree_collect_node_links($data['tree'], $data['node_links']);
314 // Cache the data.
315 cache_set($cid, $data, 'cache_menu');
316 }
317 // Check access for the current user to each item in the tree.
318 menu_tree_check_access($data['tree'], $data['node_links']);
319 $tree[$cid] = $data['tree'];
320 }
321
322 return $tree[$cid];
323 }
324
325 /**
326 * Return a list of devel module links if the module is enabled
327 * and the user has access to this module.
328 */
329 function simplemenu_get_devel() {
330 $output = '';
331
332 if (variable_get('simplemenu_devel', 0) && module_exists('devel')) {
333 if (user_access('access devel information')) {
334 $output = '<li class="expanded"><a href="'. url('admin/settings/devel') .'">'. t('Devel module') .'</a>';
335 $output .= simplemenu_menu_tree('devel');
336 $output .= '</li>';
337 }
338 }
339
340 return $output;
341 }
342
343 /**
344 * Determine if simplemenu should be displayed based on visibility settings.
345 *
346 * @return boolean
347 */
348 function _simplemenu_page_visibility() {
349 $operator = variable_get('simplemenu_visibility_operator', 0);
350 $pages = variable_get('simplemenu_visibility_pages', '');
351
352 if ($pages) {
353 $path = drupal_get_path_alias($_GET['q']);
354 // Compare with the internal and path alias (if any).
355 $page_match = drupal_match_path($path, $pages);
356 if ($path != $_GET['q']) {
357 $page_match = $page_match || drupal_match_path($_GET['q'], $pages);
358 }
359 // When $operator has a value of 0, the menu is displayed on
360 // all pages except those listed in $pages. When set to 1, it
361 // is displayed only on those pages listed in $pages.
362 $page_match = !($operator xor $page_match);
363 }
364 else {
365 $page_match = TRUE;
366 }
367
368 return $page_match;
369 }

  ViewVC Help
Powered by ViewVC 1.1.2