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

Contents of /contributions/modules/navigation/navigation.module

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


Revision 1.27 - (show annotations) (download) (as text)
Wed Nov 24 22:03:32 2004 UTC (5 years ago) by dries
Branch: MAIN
CVS Tags: HEAD
Changes since 1.26: +2 -2 lines
File MIME type: text/x-php
- Renamed check_query() to db_escape_string().  Commit announced on the mailing list.
1 <?php
2 // $Id: navigation.module,v 1.26 2004/10/01 00:08:34 uwe Exp $
3
4 /*
5 ** navigation.module:
6 ** For the creation of custom menus, tabs, ...
7 */
8
9 // API:
10 function navigation_display_tabs($mid) {
11 $output = "";
12 if(navigation_menu_allowed($mid) && ($menu = navigation_load_menu($mid))) {
13 $output = "<div class=\"tabs\" id=\"tabs-$mid\">\n";
14 $selected = navigation_load_selected_items($mid);
15 $output .= navigation_build_tabs($menu, 0, 0, $selected);
16 $output .= "</div>\n";
17 }
18 return $output;
19 }
20
21 function navigation_display_menu($mid) {
22 $output = "";
23 if(navigation_menu_allowed($mid) && ($menu = navigation_load_menu($mid))) {
24 $output = "<div class=\"menu\" id=\"menu-$mid\">\n";
25 $selected = navigation_load_selected_items($mid);
26 $output .= navigation_build_menu($menu, 0, 0, $selected);
27 $output .= "</div>\n";
28 }
29 return $output;
30 }
31
32 // Internal functions
33 function navigation_menu_allowed($mid) {
34 global $user;
35
36 if(variable_get("navigation_enable_menu_role", 0)) {
37 $menu = db_fetch_array(db_query("SELECT roles FROM {menu} WHERE mid=%d", $mid));
38 if($menu['roles'] != "") {
39 if(!in_array($user->rid, explode(",", $menu['roles']))) {
40 return 0;
41 }
42 }
43 }
44 return 1;
45 }
46
47 function navigation_item_allowed($mid, $iid) {
48 global $user;
49 static $menu;
50
51 if(variable_get("navigation_enable_item_role", 0)) {
52 // cache menu item roles for each menu to minimize db queries
53 if (empty($menu[$mid])) {
54 $result = db_query("SELECT roles,iid FROM {menu_item} WHERE mid=%d", $mid);
55 while($item = db_fetch_object($result)) {
56 $menu[$mid][$item->iid] = $item->roles;
57 }
58 }
59 if($menu[$mid][$iid] != "") {
60 if(!in_array($user->rid, explode(",", $menu[$mid][$iid]))) {
61 return 0;
62 }
63 }
64 }
65 return 1;
66 }
67
68 function navigation_l($text, $url, $attributes = array(), $query = NULL, $fragment = NULL, $absolute = NULL) {
69 if(ereg('^http://|^https://|^ftp://',"$url")) {
70 if (isset($fragment)) {
71 $fragment = "#$fragment";
72 }
73 return "<a href=\"$url$fragment\"". drupal_attributes($attributes) .">$text</a>";
74 }
75 else {
76 return l($text, $url, $attributes, $query, $fragment, $absolute);
77 }
78 }
79
80 function navigation_build_tabs($menu, $row, $active_parent, $selected) {
81 // only try and build the next row if it exists
82 if($menu[$row]) {
83 unset($tabrow);
84 unset($children);
85 foreach ($menu[$row] as $item) {
86 // only display items that are visible
87 if (navigation_item_allowed($menu[mid], $item->iid) && (in_array($active_parent, $item->parents) || !$active_parent)) {
88 if ($selected[$item->iid]) {
89 $tabrow .= " <li id=\"item-$item->iid\">". navigation_l($item->name, $item->url, array("title" => $item->description, "class" => "active-tab")) ."</li>\n";
90 $children[] = $item->iid;
91 }
92 else {
93 $tabrow .= " <li id=\"item-$item->iid\">". navigation_l($item->name, $item->url, array("title" => $item->description)) ."</li>\n";
94 }
95 }
96 }
97 if($tabrow) {
98 $output .= "\n<ul class=\"tab-row$row\">\n$tabrow</ul>\n";
99 }
100 if ($children) {
101 foreach($children as $child) {
102 $output .= navigation_build_tabs($menu, $row + 1, $child, $selected);
103 }
104 }
105 }
106
107 return $output;
108 }
109
110 function navigation_build_menu($menu, $row, $active_parent, $selected) {
111 // only try and build the next row if it exists
112 if($menu[$row]) {
113 $output .= "<ul class=\"menu-row$row\">\n";
114 foreach ($menu[$row] as $item) {
115 // only display items that are visible
116 if (navigation_item_allowed($menu[mid], $item->iid) && (in_array($active_parent, $item->parents) || !$active_parent)) {
117 if ($selected[$item->iid]) {
118 if(navigation_get_tree($menu[mid], $item->iid)) {
119 // this item has children, display expanded icon
120 $output .= "<li class=\"expanded\" id=\"item-$item->iid\">". navigation_l($item->name, $item->url, array("title" => $item->description)) ."\n";
121 // display children of active items
122 $output .= navigation_build_menu($menu, $row + 1, $item->iid, $selected);
123 $output .= "</li>\n";
124 }
125 else {
126 // this item has no children, display leaf icon
127 $output .= "<li class=\"leaf\" id=\"item-$item->iid\">". navigation_l($item->name, $item->url, array("title" => $item->description)) ."</li>\n";
128 }
129 }
130 else {
131 if(navigation_get_tree($menu[mid], $item->iid)) {
132 // this item has children, display collapsed icon
133 $output .= "<li class=\"collapsed\" id=\"item-$item->iid\">". navigation_l($item->name, $item->url, array("title" => $item->description)) ."</li>\n";
134 }
135 else {
136 // this item has no children, display leaf icon
137 $output .= "<li class=\"leaf\" id=\"item-$item->iid\">". navigation_l($item->name, $item->url, array("title" => $item->description)) ."</li>\n";
138 }
139 }
140 }
141 }
142 $output .= "</ul>\n";
143 }
144
145 return $output;
146 }
147
148 function navigation_load_selected_items($mid) {
149 $selected = array();
150
151 $path = (drupal_get_path_alias($_GET['q'])) ? drupal_get_path_alias($_GET['q']) : $_GET['q'];
152 foreach (array("SELECT iid FROM {menu_item} WHERE url='$path' and mid=$mid", "SELECT iid FROM {menu_item} WHERE enable='1' and mid=$mid") as $query) {
153 $result = db_query($query);
154 while($item = db_fetch_object($result)) {
155 $selected[$item->iid] = 1;
156 $iid = $item->iid;
157 while($parent = navigation_get_parent($iid)) {
158 $selected[$parent->iid] = 1;
159 $iid = $parent->iid;
160 }
161 }
162 }
163
164 // if enabled, see if there are any matches on taxonomy
165 if(variable_get("navigation_enable_taxonomy", 0)) {
166 if($tids = _navigation_path_to_term($path)) {
167 $result = db_query("SELECT iid,terms FROM {menu_item} WHERE mid=%d", $mid);
168 while ($item = db_fetch_object($result)) {
169 foreach(explode(",", $tids) as $tid) {
170 if (in_array($tid, explode(",", $item->terms))) {
171 $selected[$item->iid] = 1;
172 $iid = $item->iid;
173 while($parent = navigation_get_parent($iid)) {
174 $selected[$parent->iid] = 1;
175 $iid = $parent->iid;
176 }
177 break;
178 }
179 }
180 }
181 }
182 }
183
184 return $selected;
185 }
186
187 function navigation_load_menu($mid) {
188 $tree = navigation_get_tree($mid);
189 if ($tree) {
190 foreach ($tree as $item) {
191 /* Build a menu as follows:
192 ** $menu[0] is the first row,
193 ** $menu[0][0] is the first item of the first row,
194 ** $menu[0][1] is the second item of the first row, etc...
195 ** $menu[1] is the second row,
196 ** $menu[1][0] is the first item of the second row,
197 ** $menu[2] is the third row, etc...
198 */
199 $menu[$item->depth][] = $item;
200 }
201 }
202 // make it possible to get back to the $mid from the $menu
203 $menu[mid] = $mid;
204 return $menu;
205 }
206
207 function navigation_help($section){
208 $output = "";
209
210 switch ($section) {
211 case 'admin/system/modules#description':
212 $output = t("Enables the configuration of navigational menus and CSS-based tabs");
213 break;
214 case 'admin/system/modules/navigation':
215 $output = t("<p>The navigation module allows a site administrator to build custom menus that can be displayed in a block like the automatically generated 'Navigation' block, or as rows of CSS-based tabs. Menus are comprised of menu items, each of which can link to any destination, on site or off site. Menus can be flat or three-dimentional.</p><p>The following are advanced configuration options.</p>");
216 break;
217 case 'admin/system/menus/add/menu':
218 $output = t("To create a new menu, you must at least specify a menu name below. All other choices are optional.");
219 break;
220 case 'admin/system/menus':
221 $output = t("Below is an administrative view of the menu(s) that you have created. Clicking 'edit menu' next to the menu's name will allow you to make menu-level changes such as renaming a menu, or deleting a menu. Clicking 'add menu item' next to the menu's name will allow you to add a new menu item to your menu. Clicking 'edit menu item' next to the menu item's name will allow you to make item-level changes such as renaming the menu item, repositioning it in the menu, or deleting the menu item.");
222 break;
223 }
224 return $output;
225 }
226
227 function navigation_perm() {
228 return array("administer navigation");
229 }
230
231 function navigation_block($op = "list", $delta = 0) {
232 $menus = navigation_get_menus();
233 $id = 0;
234 if ($op == "list") {
235 foreach($menus as $menu) {
236 $blocks[$id++]["info"] = t($menu->name);
237 }
238 return $blocks;
239 }
240 else if (user_access("access content")) {
241 foreach($menus as $menu) {
242 if ($delta == $id++) {
243 $block["subject"] = t($menu->name);
244 $block["content"] = navigation_display_menu($menu->mid);
245 break;
246 }
247 }
248
249 return $block;
250 }
251 }
252
253 function navigation_link($type) {
254 if ($type == "system") {
255 drupal_set_html_head("<style type=\"text/css\">\n@import url(". variable_get("navigation_default_css", "modules/navigation/navigation.css") .");\n</style>");
256 if (user_access("administer navigation")) {
257 menu("admin/system/menus", t("menus"), "navigation_admin", 4);
258 menu("admin/system/menus/add/menu", t("create new menu"), "navigation_admin", 1);
259 }
260 return;
261 }
262
263 return $links ? $links : array();
264 }
265
266 function navigation_settings() {
267 if (!file_check_location(variable_get("navigation_default_css", "modules/navigation/navigation.css"))) {
268 $error['navigation_default_css'] = theme('error', t('File does not exist, or is not readable.'));
269 }
270 $group = form_textfield(t("Default style sheet"), "navigation_default_css", variable_get("navigation_default_css", "modules/navigation/navigation.css"), 70, 255, t("Specify the relative path to your default style sheet. The style sheet specified here will be used to generate your tabs, unless you manually override these CSS definitions in your theme's CSS file. ". $error['navigation_default_css']));
271 $output = form_group(t("CSS tabs"), $group);
272
273 $group = form_radios(t("Menu permissions"), "navigation_enable_menu_role", variable_get("navigation_enable_menu_role", 0), array("1" => t("Enabled"), "0" => t("Disabled")), t("Enabling menu permissions will add an additional configuration option when creating and configuring menus. This new option will allow you to associate each menu with one or more roles. If enabled and an association is defined for a given menu, the menu will only be displayed to the associated role(s). For example, if you associate a menu with the 'Adminstrator' role, only Administrators will be able to see the menu."));
274 $group .= form_radios(t("Menu item permissions"), "navigation_enable_item_role", variable_get("navigation_enable_item_role", 0), array("1" => t("Enabled"), "0" => t("Disabled")), t("Enabling menu item permissions will add an additional configuration option when creating and configuring menu items. This new option will allow you to associate each menu item with one or more roles. If enabled and an association is defined for a given menu item, the menu item and all its children will only be displayed to the associated role(s). For example, if you associate a menu item with the 'Adminstrator' role, only Administrators will be able to see the menu item and its children."));
275 $output .= form_group(t("Permissions"), $group);
276
277 $group = form_radios(t("Menu taxonomy"), "navigation_enable_taxonomy", variable_get("navigation_enable_taxonomy", 0), array("1" => t("Enabled"), "0" => t("Disabled")), t("Enabling menu taxonomy will add additional configuration options when creating and configuring menus and menu items. When creating and configuring menus, this new option will allow you to associate each menu with one or more taxonomy vocabularies. Once a menu is associated with one or more vocabularies, when creating and configuring menu items for that menu, a new option will allow you to associate each menu item with one or more terms from the selected vocabularies. This association makes it possible for the appropriate menu items to be visible when viewing specifc site content."));
278 $output .= form_group(t("Taxonomy"), $group);
279
280 return $output;
281 }
282
283 function navigation_admin() {
284 $op = $_POST["op"];
285 $edit = $_POST["edit"];
286
287 if (user_access("administer navigation")) {
288 if (empty($op)) {
289 $op = arg(3);
290 }
291
292 switch ($op) {
293 case "add":
294 if (arg(4) == "menu")
295 $output .= navigation_form_menu();
296 else if (arg(4) == "item")
297 $output .= navigation_form_item();
298 break;
299 case "edit":
300 if (arg(4) == "menu")
301 $output .= navigation_form_menu(object2array(navigation_get_menu(arg(5))));
302 else if (arg(4) == "item")
303 $output .= navigation_form_item(object2array(navigation_get_item(arg(5))));
304 break;
305 case t("Delete"):
306 if (!$edit["confirm"]) {
307 if (arg(4) == "menu") {
308 $output .= _navigation_confirm_del_menu($edit["mid"]);
309 }
310 else if (arg(4) == "item") {
311 $output .= _navigation_confirm_del_item($edit["iid"]);
312 }
313 break;
314 }
315 else {
316 $edit["name"] = 0;
317 // continue on to display the overview page
318 }
319 case t("Submit"):
320 if (arg(4) == "menu")
321 drupal_set_message(navigation_save_menu($edit));
322 else {
323 drupal_set_message(navigation_save_item($edit));
324 if (!$edit["iid"]) {
325 // admin is actively building a menu, so show form again
326 $output .= navigation_form_item();
327 break;
328 }
329 // otherwise continue on to display the overview page
330 }
331 // continue on to display the overview page
332 default:
333 $output .= navigation_overview();
334 }
335 }
336 else {
337 $output .= message_access();
338 }
339
340 print theme("page", $output);
341 }
342
343 function navigation_overview() {
344 $header = array(t("name"), array("data" => t("operations"), "colspan" => 2));
345
346 $menus = navigation_get_menus();
347
348 foreach ($menus as $menu) {
349 $links = array();
350 $rows[] = array($menu->name, l(t("edit menu"), "admin/system/menus/edit/menu/$menu->mid"), l(t("add menu item"), "admin/system/menus/add/item/$menu->mid"));
351
352 $tree = navigation_get_tree($menu->mid);
353 if ($tree) {
354 unset($data);
355 foreach ($tree as $item) {
356 $data .= _navigation_depth($item->depth) ." ". $item->name ." (". l(t("edit menu item"), "admin/system/menus/edit/item/$item->iid") .")<br />";
357 }
358 $rows[] = array(array("data" => $data, "colspan" => 5));
359 }
360 }
361
362 return theme("table", $header, $rows);
363 }
364
365 function navigation_get_menus($key = "mid") {
366 $result = db_query("SELECT * FROM {menu} ORDER BY weight, name");
367 $menus = array();
368 while ($menu = db_fetch_object($result)) {
369 $menus[$menu->$key] = $menu;
370 }
371
372 return $menus;
373 }
374
375 function navigation_get_menu($mid) {
376 return db_fetch_object(db_query("SELECT * FROM {menu} WHERE mid = %d", $mid));
377 }
378
379 function navigation_get_item($iid) {
380 return db_fetch_object(db_query("SELECT * FROM {menu_item} WHERE iid = %d", $iid));
381 }
382
383 function navigation_get_parent($iid) {
384 if ($iid) {
385 return db_fetch_object(db_query("SELECT i.* FROM {menu_hierarchy} h, {menu_item} i WHERE h.parent = i.iid AND h.iid = %d ORDER BY weight, name", $iid));
386 }
387 else {
388 return array();
389 }
390 }
391
392 function navigation_get_parents($iid, $key = "iid") {
393 if ($iid) {
394 $result = db_query("SELECT i.* FROM {menu_hierarchy} h, {menu_item} i WHERE h.parent = i.iid AND h.iid = %d ORDER BY weight, name", $iid);
395 $parents = array();
396 while ($parent = db_fetch_object($result)) {
397 $parents[$parent->$key] = $parent;
398 }
399 return $parents;
400 }
401 else {
402 return array();
403 }
404 }
405
406 function navigation_form_menu($edit = array()) {
407 $form = form_textfield(t("Menu name"), "name", $edit["name"], 50, 255, t("Required") .". ". t("The name for this menu. Example: 'Main'") .".");
408 $form .= form_textarea(t("Description"), "description", $edit["description"], 60, 5, t("Optional") .". ". t("Description of the menu. This description is also used to describe the menu block that will automatically be generated for this menu."));
409 $form .= form_weight(t("Weight"), "weight", $edit["weight"], 10, t("Optional. When editing menus, the heavier menus will sink and the lighter menus will be positioned nearer the top."));
410 if(variable_get("navigation_enable_menu_role", 0)) {
411 $form .= form_select(t("View permissions"), "roles", explode(",", $edit["roles"]), user_roles(), t("Optional. By selecting one or more user roles from this list, only the selected role(s) will be able to view this menu. If no roles are selected, the menu is visible to all."), "", 1);
412 }
413 if(variable_get("navigation_enable_taxonomy", 0)) {
414 $form .= form_select(t("Taxonomy vocabularies"), "vocabularies", explode(",", $edit["vocabularies"]), _navigation_get_vocabularies(), t("Optional. Associate this menu item with one or taxonomy term. Selecting terms from this list will cause this menu item to be visible when viewing site content that is associated with one or more of these terms."), "", 1);
415 }
416 $form .= form_submit(t("Submit"));
417
418 if ($edit["mid"]) {
419 $form .= form_submit(t("Delete"));
420 $form .= form_hidden("mid", $edit["mid"]);
421 }
422
423 return form($form);
424 }
425
426 function navigation_form_item($edit = array()) {
427 $mid = isset($edit["mid"]) ? $edit["mid"] : arg(5);
428 $menu = navigation_get_menu($mid);
429
430 $form = form_textfield(t("Menu item name"), "name", $edit["name"], 50, 255, t("Required") .". ". t("The name for this menu item. Example: 'FAQ'."));
431 $form .= form_textarea(t("Description"), "description", $edit["description"], 60, 5, t("Optional") .". ". t("A description for this menu item. This will be displayed in most web browsers when a user drags their mouse over the menu item."));
432 $form .= form_textfield(t("URL"), "url", $edit["url"], 50, 255, t("Required") .". ". t("Destination URL when clicking this menu item."));
433
434 $parent = array_keys(navigation_get_parents($edit["iid"]));
435 $children = navigation_get_tree($mid, $edit["iid"]);
436
437 // you can't be son of yourself nor of your children
438 foreach ($children as $child) {
439 $exclude[] = $child->iid;
440 }
441 $exclude[] = $edit["iid"];
442
443 $form .= _navigation_item_select(t("Parent"), "parent", $parent, $mid, t("Required") .". ". l(t("Parent menu item"), "admin/system/menus/help#parent") .".", 0, "<". t("root") .">", $exclude);
444
445 $form .= form_weight(t("Weight"), "weight", $edit["weight"], 10, t("Optional. When building vertical menus, the heavier items will sink and the lighter items will be positioned nearer the top. When building horizontal menus, the heavier items will slide to the right and the lighter items will be positioned nearer to the left."));
446
447 $form .= form_select(t("Active by default"), "enable", $edit["enable"], array(0 => "disabled", 1 => "enabled"), t("Optional. This option defines the default status of the menu item. If enabled, this menu item will default to always being active. If disabled, this menu item will only be active when it (or one if its children) is selected."));
448
449 // permissions
450 if(variable_get("navigation_enable_item_role", 0)) {
451 $form .= form_select(t("View permissions"), "roles", explode(",", $edit["roles"]), user_roles(), t("Optional") .". ". t("By selecting one or more user roles from this list, only the selected role(s) will be able to view this menu item and all its children. If no roles are selected, the menu item and its children are visible to all."), "", 1);
452 }
453
454 // taxonomy
455 if(variable_get("navigation_enable_taxonomy", 0)) {
456 $form .= form_select(t("Taxonomy terms"), "terms", explode(",", $edit["terms"]), _navigation_get_terms($menu->mid), t("Optional. Associate this menu item with one or more taxonomy terms. Items will not appear in this list until the menu is associated with one or more taxonomy vocabularies. Associating menu items with taxonomy terms makes it possible for the apropriate menu items to be visible when viewing arbitrary site content."), "", 1);
457 }
458
459 $form .= form_hidden("mid", $menu->mid);
460 $form .= form_submit(t("Submit"));
461
462 if ($edit["iid"]) {
463 $form .= form_submit(t("Delete"));
464 $form .= form_hidden("iid", $edit["iid"]);
465 }
466
467 return form($form);
468 }
469
470 function navigation_save_menu($edit) {
471 if (!$edit["roles"]) {
472 $edit["roles"] = array();
473 }
474 if (!$edit["vocabularies"]) {
475 $edit["vocabularies"] = array();
476 }
477
478 $data = array("name" => $edit["name"], "description" => $edit["description"], "weight" => $edit["weight"], "roles" => implode(",", $edit["roles"]), "vocabularies" => implode(",", $edit["vocabularies"]));
479 if ($edit["mid"] && $edit["name"]) {
480 db_query("UPDATE {menu} SET ". _prepare_update($data) ." WHERE mid = %d", $edit["mid"]);
481 $message = t("updated menu '%name'.", array("%name" => $edit["name"]));
482 cache_clear_all();
483 }
484 else if ($edit["mid"]) {
485 $message = navigation_del_menu($edit["mid"]);
486 cache_clear_all();
487 }
488 else {
489 $data["mid"] = $edit["mid"] = db_next_id("navigation_mid");
490 db_query("INSERT INTO {menu} ". navigation_prepare_insert($data, 1) ." VALUES ". navigation_prepare_insert($data, 2));
491 $message = t("created new menu '%name'.", array("%name" => $edit["name"]));
492 }
493
494 return $message;
495 }
496
497 function navigation_del_menu($mid) {
498 $menu = navigation_get_menu($mid);
499
500 db_query("DELETE FROM {menu} WHERE mid = %d", $mid);
501 $result = db_query("SELECT iid FROM {menu_item} WHERE mid = %d", $mid);
502 while ($item = db_fetch_object($result)) {
503 navigation_del_item($item->iid);
504 }
505
506 cache_clear_all();
507
508 return t("deleted menu '%name'.", array("%name" => $menu->name));
509 }
510
511 function navigation_save_item($edit) {
512 if (!$edit["roles"]) {
513 $edit["roles"] = array();
514 }
515 if (!$edit["terms"]) {
516 $edit["terms"] = array();
517 }
518
519 if ($edit['iid'] && $edit['name']) {
520 $data = array("name" => $edit["name"], "description" => str_replace('"', "'", $edit["description"]), "url" => $edit["url"], "weight" => $edit["weight"], "roles" => implode(",", $edit["roles"]), "terms" => implode(",", $edit["terms"]), "enable" => $edit["enable"]);
521
522 db_query("UPDATE {menu_item} SET ". _prepare_update($data) ." WHERE iid = %d", $edit["iid"]);
523 $message = t("the menu item '%a' has been updated.", array("%a" => $edit["name"]));
524 }
525 else if ($edit["iid"]) {
526 return navigation_del_item($edit["iid"]);
527 }
528 else {
529 $edit["iid"] = db_next_id("menu_item_iid");
530 $data = array("iid" => $edit["iid"], "name" => $edit["name"], "description" => str_replace('"', "'", $edit["description"]), "url" => $edit["url"], "mid" => $edit["mid"], "weight" => $edit["weight"], "roles" => implode(",", $edit["roles"]), "terms" => implode(",", $edit["terms"]), "enable" => $edit["enable"]);
531 db_query("INSERT INTO {menu_item} ". navigation_prepare_insert($data, 1) ." VALUES ". navigation_prepare_insert($data, 2));
532 $message = t("created new menu item '%name'.", array("%name" => $edit["name"]));
533 }
534
535 // hierarchy
536 db_query("DELETE FROM {menu_hierarchy} WHERE iid = %d", $edit["iid"]);
537 if (!isset($edit["parent"])) {
538 $edit["parent"] = 0;
539 }
540 if (is_array($edit["parent"])) {
541 foreach ($edit["parent"] as $parent) {
542 db_query("INSERT INTO {menu_hierarchy} (iid, parent) VALUES (%d, %d)", $edit["iid"], $parent);
543 }
544 }
545 else {
546 db_query("INSERT INTO {menu_hierarchy} (iid, parent) VALUES (%d, %d)", $edit["iid"], $edit["parent"][0]);
547 }
548
549 cache_clear_all();
550
551 return $message;
552 }
553
554 function navigation_del_item($iid) {
555 $item = navigation_get_item($iid);
556
557 db_query("DELETE FROM {menu_item} WHERE iid = %d", $iid);
558 db_query("DELETE FROM {menu_hierarchy} WHERE iid = %d", $iid);
559
560 cache_clear_all();
561
562 return t("deleted menu item '%name'.", array("%name" => $item->name));
563 }
564
565 function navigation_get_tree($mid, $parent = 0, $depth = -1, $key = "iid") {
566 // we build the tree ones, then cache it in statics...
567 static $children, $parents, $items;
568
569 $depth++;
570 if (!isset($children[$mid])) {
571 $children[$mid] = array();
572
573 $result = db_query("SELECT i.*, parent FROM {menu_item} i, {menu_hierarchy} h WHERE i.iid = h.iid AND i.mid = %d ORDER BY weight, name", $mid);
574 while ($item = db_fetch_object($result)) {
575 $children[$mid][$item->parent][] = $item->iid;
576 $parents[$mid][$item->iid][] = $item->parent;
577 $items[$mid][$item->iid] = $item;
578 }
579 }
580
581 if ($children[$mid][$parent]) {
582 foreach ($children[$mid][$parent] as $child) {
583 $items[$mid][$child]->depth = $depth;
584 unset($items[$mid][$child]->parent);
585 $items[$mid][$child]->parents = $parents[$mid][$child];
586 $tree[] = $items[$mid][$child];
587
588 $tree = array_merge($tree, navigation_get_tree($mid, $child, $depth));
589 }
590 }
591
592 return $tree ? $tree : array();
593 }
594
595 function navigation_prepare_insert($data, $stage) {
596 if ($stage == 1) {
597 $result = implode(", ", array_keys($data));
598 }
599 else {
600 foreach (array_values($data) as $value) {
601 $q[] = "'". db_escape_string($value) ."'";
602 }
603 $result = implode(", ", $q);
604 }
605 return "($result)";
606 }
607
608 function _navigation_get_vocabularies() {
609 $vocabularies = taxonomy_get_vocabularies();
610 while($vocab = array_pop($vocabularies)) {
611 $vocabulary[$vocab->vid] = $vocab->name;
612 }
613 return $vocabulary;
614 }
615
616 function _navigation_get_terms($mid) {
617 $result = db_fetch_object(db_query("SELECT vocabularies FROM {menu} WHERE mid = %d", $mid));
618 $vocabularies = explode(",", $result->vocabularies);
619 $options[0] = "<none>";
620 while ($vid = array_pop($vocabularies)) {
621 $tree = taxonomy_get_tree($vid);
622 if ($tree) {
623 foreach ($tree as $term) {
624 $options[$term->tid] .= _taxonomy_depth($term->depth, '-').$term->name;
625 }
626 }
627 }
628 return $options;
629 }
630
631 function _navigation_path_to_term($path) {
632 $args = explode("/", $path);
633 // we gather the nid differently for different modules
634 switch ($args[0]) {
635 case "node":
636 if($args[1] == "view" && $args[2]) {
637 $node = db_fetch_object(db_query("SELECT t.tid FROM {node} n, {term_node} t WHERE n.nid = t.nid AND t.nid=%d ORDER BY t.nid LIMIT 1", $args[2]));
638 return $node->tid;
639 }
640 break;
641 case "forum":
642 return $args[1];
643 break;
644 case "taxonomy":
645 if(($args[1] == "view" || $args[1] == "page")
646 && ($args[2] == "or" || $args[2] == "and")) {
647 return $args[3];
648 }
649 }
650 }
651
652 function _navigation_item_select($title, $name, $value, $mid, $description, $multiple, $blank, $exclude = array()) {
653 $tree = navigation_get_tree($mid);
654
655 if ($blank) {
656 $options[] = array("iid" => 0, "name" => $blank);
657 }
658
659 if ($tree) {
660 foreach ($tree as $item) {
661 if (!in_array($item->iid, $exclude)) {
662 $options[] = array("iid" => $item->iid, "name" => _navigation_depth($item->depth, '-').$item->name);
663 }
664 }
665 if (!$blank && !$value) {
666 // required but without a predefined value, so set first as predefined
667 $value = $tree[0]->iid;
668 }
669 }
670
671 if (count($options) > 0) {
672 foreach ($options as $option) {
673 $select .= "<option value=\"". $option["iid"] ."\"". (is_array($value) ? (in_array($option["iid"], $value) ? " selected=\"selected\"" : "") : ($option["iid"] == $value ? " selected=\"selected\"" : "")) .">". check_form($option["name"]) ."</option>";
674 }
675
676 $size = min(12, count($options));
677
678 return form_item($title, "<select name=\"edit[$name][]\"". ($multiple ? " multiple size=\"$size\"" : "") . ($extra ? " $extra" : "") .">$select</select>", $description);
679 }
680 }
681
682 function _navigation_confirm_del_menu($mid) {
683 $menu = navigation_get_menu($mid);
684
685 $form .= form_hidden("confirm", 1);
686 $form .= form_hidden("mid", $mid);
687 $form .= form_submit(t("Delete"));
688 $form .= form_submit(t("Cancel"));
689
690 return form(form_item(t("Delete menu '%name'", array("%name" => $menu->name)), $form, t("Are you sure you want to delete this menu and all of its menu items?")));
691 }
692
693 function _navigation_confirm_del_item($iid) {
694 $item = navigation_get_item($iid);
695
696 $form .= form_hidden("confirm", 1);
697 $form .= form_hidden("iid", $iid);
698 $form .= form_submit(t("Delete"));
699 $form .= form_submit(t("Cancel"));
700
701 return form(form_item(t("Delete menu item '%name'", array("%name" => $item->name)), $form, t("Are you sure you want to delete this menu item?")));
702 }
703
704 function _navigation_depth($depth, $graphic = '--') {
705 for ($n = 0; $n < $depth; $n++) {
706 $result .= $graphic;
707 }
708 return $result;
709 }
710
711 ?>

  ViewVC Help
Powered by ViewVC 1.1.2