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

Contents of /contributions/modules/administration/administration.module

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


Revision 1.25 - (show annotations) (download) (as text)
Fri Jun 23 02:02:02 2006 UTC (3 years, 5 months ago) by der
Branch: MAIN
CVS Tags: HEAD
Changes since 1.24: +66 -48 lines
File MIME type: text/x-php
* fixes from recidive - node/70276
1 <?php
2 /**
3 * @file administration.module
4 * The administration module re-organizes a site's menus and provides
5 * some administrative shortcuts. It can utilize multiple config files,
6 * allowing for different administrative footprints to serve varying needs.
7 */
8
9 // ------------------------------------------------------------------------
10 // Drupal hooks
11
12 /**
13 * Implementation of hook_help().
14 */
15 function administration_help($section) {
16 switch ($section) {
17 case 'admin/modules#description':
18 return t('Site administration dashboard. <strong>Do not disable this module</strong> without first going to <a href="%settings">administration settings</a> and clicking "deactivate menus."', array('%settings' => url('admin/settings/administration')));
19 case 'admin/settings/administration':
20 return t('IMPORTANT! Before deactivating this module click the "Deactivate" button on this page. If you deactivate this module BEFORE clicking the "Deactivate" button on this page you will need to reactivate the administration module by going to url "admin/modules" and then return here and click "Deactivate".');
21 }
22 }
23 /**
24 * Implementation of hook_menu().
25 */
26 function administration_menu($may_cache) {
27 if ($may_cache) {
28 $access = user_access('administer site configuration');
29 $top_path = 'sadmin';
30
31 if (variable_get('administration_module_active', FALSE)) {
32 $config = administration_menu_config();
33 if ($config['top_path']) {
34 $top_path = $config['top_path'];
35 }
36 $items[] = array(
37 'path' => $top_path,
38 'title' => t('administer site'),
39 'description' => t('Manage your site'),
40 'access' => $access,
41 'callback' => '_administration_dashboard_page',
42 'weight' => 9
43 );
44 }
45 else {
46 $items[] = array(
47 'path' => $top_path,
48 'title' => t('administer site'),
49 'access' => $access,
50 'description' => t('Manage your site'),
51 'pid' => 0,
52 'type' => MENU_CUSTOM_MENU,
53 'callback' => '_administration_dashboard_page',
54 'weight' => 9
55 );
56 }
57 $items[] = array(
58 'path' => 'admin/settings/administration',
59 'title' => t('administration'),
60 'access' => $access,
61 'callback' => 'administration_form'
62 );
63 $items[] = array(
64 'path' => "$top_path/panelshift",
65 'access' => $access,
66 'callback' => 'administration_move_panel',
67 'type' => MENU_CALLBACK,
68 );
69
70 $items[] = array(
71 'path' => "$top_path/offsite",
72 'title' => t('administration'),
73 'access' => $access,
74 'callback' => 'administration_offsite',
75 'type' => MENU_CALLBACK
76 );
77
78 if (variable_get('administration_module_active', FALSE)) {
79 $menu_weight = -10;
80 // Redefine the 'admin' menu here as a callback to get rid of it in the menu
81 // we need to do this so 'logs' doesn't show up twice
82 $items[] = array(
83 'path' => 'admin',
84 'type' => MENU_CALLBACK
85 );
86
87 foreach ($config['section'] as $section_path => $section) {
88 if ($section['callback']) {
89 $items[] = array(
90 'path' => $section_path,
91 'title' => $section['title'],
92 'description' => $section['description'],
93 'access' => $access,
94 'callback' => $section['callback'],
95 'callback arguments' => $section['callback arguments'],
96 'type' => ($section['callback'] == '_administration_sub_dashboard_page' ? MENU_ITEM_GROUPING : MENU_NORMAL_ITEM),
97 'weight' => isset($section['weight']) ? $section['weight'] : $menu_weight++,
98 );
99 $item_weight = 0;
100 foreach ((array) $section['items'] as $item_path => $item_info) {
101 if ($item_info['callback']) {
102 $items[] = array(
103 'path' => $item_path,
104 'title' => $item_info['title'],
105 'description' => $item_info['description'],
106 'access' => $access,
107 'callback' => $item_info['callback'],
108 'callback arguments' => $item_info['callback arguments'],
109 'type' => MENU_NORMAL_ITEM,
110 'weight' => isset($item_info['weight']) ? $item_info['weight'] : $item_weight++,
111 );
112 }
113 }
114 }
115 }
116 }
117 }
118 return $items;
119 }
120
121 // ------------------------------------------------------------------------
122 // Menu callbacks
123
124 /**
125 * Display a 'sub' page which is a more in-depth look at a given section.
126 */
127 function _administration_sub_dashboard_page() {
128 _administration_set_head();
129 $config = administration_menu_config();
130
131 $mid = menu_get_active_item();
132 $menu = menu_get_menu();
133 $path = $menu['items'][$mid]['path'];
134
135 $panels[$path] = $config['section'][$path];
136
137 $output .= theme('administration_dashboard_container', $panels, $panels[$path]['sub_panel_css'], 'sub_panel_theme', $config['default']['sub_panel_theme']);
138
139 return $output;
140 }
141
142 /**
143 * Display a dashboard page.
144 */
145 function _administration_dashboard_page() {
146 $config = administration_menu_config();
147 _administration_set_head();
148 $panel_list = variable_get('administration_panels', $config['dashboard']);
149 foreach ($panel_list as $dashboard_id => $panel_ids) {
150 $panels = array();
151 foreach ($panel_ids as $panel_id) {
152 $panels[$panel_id] = $config['section'][$panel_id];
153 }
154 $output .= theme('administration_dashboard_container', $panels, $dashboard_id);
155 }
156 return $output;
157 }
158
159 /**
160 * Special link in order to have offsite links in the menu system.
161 */
162 function administration_offsite() {
163 $args = func_get_args();
164 $dest = implode('/', $args);
165
166 drupal_goto($dest);
167 }
168
169 /**
170 * Display the form that turns on and off the menu configuration.
171 */
172 function administration_form() {
173 if (!module_exist('menu')) {
174 drupal_set_message(t('IMPORTANT! The "Menu" must be enabled BEFORE you activate this module!'), 'error');
175 }
176 if (variable_get('administration_module_active', FALSE)) {
177 $config = administration_menu_config();
178 if (function_exists('administration_form_' . variable_get('administration_config', 'default'))) {
179 $form[variable_get('administration_config', 'default')] = call_user_func('administration_form_' . variable_get('administration_config', 'default'), $form);
180 }
181 }
182 else {
183 $files = system_listing('\.inc$', drupal_get_path('module', 'administration'), 'name', 0);
184 foreach ($files as $file => $info) {
185 $options[$file] = $file;
186 }
187 $form['administration_config'] = array(
188 '#type' => 'radios',
189 '#title' => t('Configuration profile'),
190 '#default_value' => variable_get('administration_config', 'default'),
191 '#options' => $options,
192 );
193 }
194 $form['administration_module_active'] = array(
195 '#type' => 'radios',
196 '#title' => t('Administration menus'),
197 '#default_value' => variable_get('administration_module_active', FALSE),
198 '#options' => array(t('Disabled'), t('Enabled')),
199 '#description' => t('Activate administration menus and dashboard.')
200 );
201
202 $form['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
203
204 return drupal_get_form('administration_form', $form);
205 }
206
207 /**
208 * Submit function for the administration form.
209 */
210 function administration_form_submit($formid, $form_values) {
211 // Retrieve module state before saving changes
212 $already_active = variable_get('administration_module_active', FALSE);
213
214 if ($form_values['administration_config']) {
215 variable_set('administration_config', $form_values['administration_config']);
216 }
217
218 if ($form_values['administration_module_active']) {
219 if (!$already_active) {
220 variable_set('administration_module_active', TRUE);
221 // force a menu_rebuild to get the right menu sections.
222 // Yes, this means menu_rebuild is performed twice.
223 menu_rebuild();
224 _administration_activate_menus();
225 }
226 }
227 else {
228 if ($already_active){
229 variable_set('administration_module_active', FALSE);
230 _administration_deactivate_menus();
231 }
232 }
233
234 if (function_exists('administration_form_submit_' . variable_get('administration_config', 'default'))) {
235 call_user_func('administration_form_submit_' . variable_get('administration_config', 'default'), $formid, $form_values);
236 }
237 }
238
239 /**
240 * De-activates custom menus
241 */
242 function _administration_deactivate_menus() {
243 _administration_unset_menus();
244 variable_del('administration_panels');
245 }
246
247 /**
248 * Activates custom menus
249 */
250 function _administration_activate_menus() {
251 _administration_set_menus();
252 }
253
254 /**
255 * Callback exists because we have some funky special handling with admin/settings.
256 */
257 function _administration_settings_page() {
258 if (arg(2) != NULL) {
259 $args = func_get_args();
260 return call_user_func_array('system_site_settings', $args);
261 }
262 else {
263 return _administration_sub_dashboard_page();
264 }
265 }
266
267 /**
268 * Menu callback to allow dashboard panels to be moved
269 */
270 function administration_move_panel($container, $panel, $direction) {
271 $config = administration_menu_config();
272 $panel_list = variable_get('administration_panels', $config['dashboard']);
273 $dashboard = "dashboard-container$container";
274 switch ($direction) {
275 case 'swap':
276 $dest = $container % 2 + 1; // silly math tricks
277 $panel_list["dashboard-container$dest"][] = $panel_list[$dashboard][$panel];
278 unset($panel_list[$dashboard][$panel]);
279 $panel_list[$dashboard] = array_values($panel_list[$dashboard]); // reindex
280 break;
281 case 'up':
282 _administration_swap($panel_list[$dashboard], $panel, $panel - 1);
283 break;
284 case 'down':
285 _administration_swap($panel_list[$dashboard], $panel, $panel + 1);
286 break;
287 }
288 variable_set('administration_panels', $panel_list);
289 drupal_goto(referer_uri());
290 }
291
292 // ------------------------------------------------------------------------
293 // Configuration
294
295 /**
296 * Gets the current menu config. Caching it for performance.
297 */
298 function administration_menu_config() {
299 static $config = NULL;
300 if (!$config) {
301 $config_file = variable_get('administration_config', 'default');
302 $filename = drupal_get_path('module', 'administration') . "/" . $config_file . ".inc";
303 include_once $filename;
304 // this'll be defined in the .inc
305 $config = _administration_menu_config();
306 }
307 return $config;
308 }
309
310 /**
311 * Add the CSS file for our menu system into the stream.
312 */
313 function _administration_set_head() {
314 $config = administration_menu_config();
315 $cssfile = $config['cssfile'] ? $config['cssfile'] : 'default.css';
316 $path = drupal_get_path('module','administration') . '/';
317 theme_add_style($path . $cssfile);
318 }
319
320 /**
321 * Helper function to swap places in an array
322 */
323 function _administration_swap(&$arr, $a, $b) {
324 $temp = $arr[$a];
325 $arr[$a] = $arr[$b];
326 $arr[$b] = $temp;
327 }
328
329 // ------------------------------------------------------------------------
330 // Menu re-organization
331
332 /**
333 * Reset the menus according to the config file.
334 */
335 function _administration_set_menus() {
336 $config = administration_menu_config();
337 foreach ($config['section'] as $section_path => $section) {
338 if (is_array($section['items'])) {
339 $weight = -10;
340 foreach ($section['items'] as $path => $info) {
341 _administration_reparent_menu_item($path, $section_path, $info['title'], $info['description'], isset($info['weight']) ? $info['weight'] : $weight++);
342 }
343 }
344 }
345 menu_rebuild();
346 if ($config['after_build']) {
347 call_user_func($config['after_build'], $config);
348 }
349 }
350
351 /**
352 * Return the menus to a default state.
353 */
354 function _administration_unset_menus() {
355 $menu = menu_get_menu();
356 $config = administration_menu_config();
357 $top_path = $config['top_path'] ? $config['top_path'] : 'sadmin';
358 _administration_unset_menu_item($menu['path index'][$top_path]);
359 menu_rebuild();
360 }
361
362 /**
363 * Move a menu item from its original location to our new administrative paths.
364 */
365 function _administration_reparent_menu_item ($path, $new_path, $title = NULL, $desc = NULL, $weight = NULL) {
366 $menu = menu_get_menu();
367 $pid = $menu['path index'][$new_path];
368 if (!empty($menu['path index'][$path])) {
369 $mid = $menu['path index'][$path];
370 $type = $menu['items'][$mid]['type'] | MENU_MODIFIED_BY_ADMIN;
371 if (empty($title)) {
372 $title = $menu['items'][$mid]['title'];
373 }
374 if (empty($desc) && isset($menu['items'][$mid]['description'])) {
375 $desc = $menu['items'][$mid]['description'];
376 }
377 if (empty($weight)) {
378 $weight = $menu['items'][$mid]['weight'];
379 }
380 db_query("UPDATE {menu} SET pid = %d, title = '%s', description = '%s', weight = %d, type = %d WHERE mid = %d", $pid, $title, $desc, $weight, $type, $mid);
381 }
382 else {
383 $mid = db_next_id('{menu}_mid');
384 $type = MENU_NORMAL_ITEM | MENU_MODIFIED_BY_ADMIN;
385 db_query("INSERT INTO {menu} (mid, pid, path, title, description, weight, type) VALUES (%d, %d, '%s', '%s', '%s', %d, %d)", $mid, $pid, $path, $title, $desc, $weight, $type);
386 }
387 return;
388 }
389
390 /**
391 * Return a single menu item to its basic state.
392 */
393 function _administration_unset_menu_item($pid){
394 db_query('DELETE FROM {menu} WHERE pid = %d', $pid);
395
396 $menu = menu_get_menu();
397 foreach ((array) $menu['items'][$pid]['children'] as $cid) {
398 if (!empty($menu['items'][$cid]) && $menu['items'][$cid]['children']) {
399 _administration_unset_menu_item($cid);
400 }
401 }
402 variable_set('administration_module_active', FALSE);
403 }
404
405
406 // ------------------------------------------------------------------------
407 // Themables and display.
408
409 /**
410 * Display a dashboard container. Relies heavily on callbacks from the .inc file.
411 */
412 function theme_administration_dashboard_container($panels, $css_class = NULL, $theme = 'panel_theme', $default_theme = NULL) {
413 if (!$default_theme) {
414 $default_theme = 'administration_sub_menu_panel';
415 }
416 $output = '<div class=' . $css_class . '>';
417 $i = 0;
418 $total = count($panels);
419 foreach ($panels as $id => $panel) {
420 $theme_func = $panel[$theme];
421 if (!$theme_func) {
422 $theme_func = $panel['panel_theme'];
423 }
424 if (!$theme_func) {
425 $theme_func = $default_theme;
426 }
427 $output .= theme($theme_func, $id, $css_class, $i, $total);
428 $i++;
429 }
430 $output .= '</div>';
431 return $output;
432 }
433

  ViewVC Help
Powered by ViewVC 1.1.2