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

Contents of /contributions/modules/menu_otf/menu_otf.module

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


Revision 1.6 - (show annotations) (download) (as text)
Fri Jun 17 01:57:01 2005 UTC (4 years, 5 months ago) by mathias
Branch: MAIN
CVS Tags: HEAD
Changes since 1.5: +2 -2 lines
File MIME type: text/x-php
* Patch #22077 by jjeff - Delete menu items upon node deletion.
1 <?php
2 // $Id: menu_otf.module,v 1.5 2005/04/29 22:09:58 tdobes Exp $
3 // Authored by: Matt Westgate <drupal at asitis dot org>
4
5 /********************************************************************
6 * Drupal Hooks
7 ********************************************************************/
8
9 /**
10 * Implementation of hook_help().
11 */
12 function menu_otf_help($section) {
13 switch ($section) {
14 case 'admin/modules#description':
15 return t('Creates a link to inject the current node into the menu system');
16 }
17 }
18
19 /**
20 * Implementation of hook_menu().
21 */
22 function menu_otf_menu($may_cache) {
23
24 // Only include this for the create content and edit content pages.
25 if (user_access('administer menu') && strstr($_GET['q'], 'node/') && strstr($_GET['q'], '/add') || strstr($_GET['q'], '/edit')) {
26 $head = "<script type=\"text/javascript\" src=\"". drupal_get_path('module', 'menu_otf'). "/menu_otf.js\"></script>";
27 $head .= '<style type="text/css">@import url('. drupal_get_path('module', 'menu_otf') ."/menu_otf.css);</style>\n";
28 drupal_set_html_head($head);
29 }
30 }
31
32 /**
33 * Implementation of hook_nodeapi().
34 */
35 function menu_otf_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
36 $edit = $_POST['edit'];
37
38 if (user_access('administer menu')) {
39 switch ($op) {
40 case 'form post':
41 if (!in_array($node->type, (array) variable_get('motf_no_nodes', ''))) {
42 $edit['nid'] = $node->nid;
43 if (_menu_module_check()) {
44 return menu_otf_form($edit);
45 }
46 }
47 break;
48
49 case 'insert':
50 case 'update':
51 if ($node->menu[delete]) {
52 menu_otf_delete($node);
53 menu_rebuild();
54 }
55 elseif ($node->menu[title]) {
56 menu_otf_save($node);
57 menu_rebuild();
58 }
59 break;
60
61 case 'delete':
62 menu_otf_delete($node);
63 menu_rebuild();
64 break;
65 }
66 }
67 }
68
69 /**
70 * Implementation of hook_settings().
71 */
72 function menu_otf_settings() {
73 drupal_set_title(t('Menu on-the-fly settings'));
74 $output = '';
75 _menu_module_check();
76
77 foreach (node_list() as $type) {
78 $nodetypes[$type] = node_invoke($type, 'node_name');
79 }
80 $output .= form_checkboxes(t('Hide \'menu_otf link\' for the following node types'), 'motf_no_nodes', variable_get('motf_no_nodes', array()), $nodetypes, t('Check the node types you <strong>do not</strong> want the ability to quickly add to the menu system.'), NULL, TRUE);
81 $options = menu_parent_options(0);
82 $output .= form_select(t('Default parent item'), 'motf_pid', variable_get('motf_pid', 1), $options, t('Choose a parent the will selected by default during menu item creation.'));
83
84 return $output;
85 }
86
87 /********************************************************************
88 * Module Functions
89 ********************************************************************/
90
91 /**
92 * Inject form fields into the node-form and load any values if necessary.
93 */
94 function menu_otf_form($edit = array()) {
95 $item = array();
96 if ($edit['nid'] > 0) {
97 $item = db_fetch_array(db_query("SELECT * FROM {menu} WHERE path = 'node/%d'", $edit['nid']));
98 if (!$item['path']) {
99 // I don't really have to do this, but it's probably good practice.
100 $item['path'] = 'node/'. $edit['nid'];
101 }
102 }
103 $item = ($_POST['op'] == t('Preview')) ? array_merge($item, $edit['menu']) : array_merge((array) $edit['menu'], $item);
104
105 $group = form_textfield(t('Menu item title'), 'menu][title', $item['title'], 60, 128, t('The name to display for this link.'), array('onclick' => 'setTitle(this.form);return false;'));
106 // Generate a list of possible parents (not including this item or descendants).
107 $options = menu_parent_options($edit['mid']);
108 $group .= form_select(t('Parent item'), 'menu][pid', ($item['pid']) ? $item['pid'] : variable_get('motf_pid', 1), $options);
109
110 if ($item['mid'] > 0) {
111 $group .= form_checkbox(t('Remove this link from the site navigation.'), 'menu][delete', 1, $item['delete'], null);
112 }
113 $form = menu_form_group(t('Menu navigation'), $group, NULL, TRUE);
114
115 $form .= form_hidden('menu][description', $item['description']);
116 $form .= form_hidden('menu][path', $item['path']);
117 $form .= form_hidden('menu][weight', ($item['weight']) ? $item['weight'] : 0);
118 $form .= form_hidden('menu][mid', ($item['mid']) ? $item['mid'] : 0);
119 $form .= form_hidden('menu][type', ($item['type']) ? $item['type'] : MENU_CUSTOM_ITEM);
120
121 return $form;
122 }
123
124 /**
125 * Format a group of form items.
126 *
127 * @param $legend
128 * The label for the form item group.
129 * @param $group
130 * The form items within the group, as an HTML string.
131 * @param $description
132 * Explanatory text to display after the form item group.
133 * @param $collapsed
134 * Whether the form item group should be collapsed by default.
135 * @return
136 * A themed HTML string representing the form item group.
137 */
138 function menu_form_group($legend, $group, $description = NULL, $collapsed = FALSE) {
139 return '<fieldset id="quick-menu"'. ($collapsed ? ' class="collapsed"':'') .'>' . ($legend ? '<legend>'. $legend .'</legend>' : '') . $group . ($description ? '<div class="description">'. $description .'</div>' : '') . "</fieldset>\n";
140 }
141
142 /**
143 * Prepare the data and send it to menu.module.
144 */
145 function menu_otf_save($node) {
146 $edit['title'] = $node->menu[title];
147 $edit['description'] = '';
148 $edit['path'] = ($node->menu[path]) ? $node->menu[path] : "node/$node->nid";
149 $edit['pid'] = $node->menu[pid];
150 $edit['weight'] = $node->menu[weight];
151 $edit['mid'] = $node->menu[mid];
152 $edit['type'] = $node->menu[type];
153
154 menu_edit_item_save($edit);
155 }
156
157 /**
158 * Remove the menu item.
159 */
160 function menu_otf_delete($node) {
161 if (db_query("DELETE FROM {menu} WHERE path = 'node/%s'", $node->nid)) {
162 drupal_set_message(t('Menu item deleted.'));
163 }
164 }
165
166 /**
167 * Return true if menu module is enabled, otherwise throw an error and return false.
168 */
169 function _menu_module_check() {
170 if (!function_exists('menu_help')) {
171 drupal_set_message(t('Menu_otf depends on menu module. Please enable menu module <a href="%modules-url">here</a>.', array('%modules-url' => url('admin/modules'))), 'error');
172 return FALSE;
173 }
174 return TRUE;
175 }
176 ?>

  ViewVC Help
Powered by ViewVC 1.1.2