admin.module 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. <?php
  2. /**
  3. * Implements hook_block_info().
  4. */
  5. function admin_block_info() {
  6. $blocks = array();
  7. $blocks['create'] = array(
  8. 'info' => t('Create content'),
  9. 'cache' => DRUPAL_NO_CACHE,
  10. 'admin' => TRUE,
  11. );
  12. $blocks['theme'] = array(
  13. 'info' => t('Theme switcher'),
  14. 'cache' => DRUPAL_CACHE_PER_ROLE,
  15. 'admin' => TRUE,
  16. );
  17. $blocks['account'] = array(
  18. 'info' => t('My Account'),
  19. 'cache' => DRUPAL_NO_CACHE,
  20. 'admin' => TRUE,
  21. );
  22. if (module_exists('menu')) {
  23. $blocks['menu'] = array(
  24. 'info' => t('Administration menu'),
  25. 'cache' => DRUPAL_CACHE_PER_ROLE,
  26. 'admin' => TRUE,
  27. );
  28. }
  29. if (module_exists('devel')) {
  30. $blocks['devel'] = array(
  31. 'info' => t('Devel'),
  32. 'cache' => DRUPAL_NO_CACHE,
  33. 'admin' => TRUE,
  34. );
  35. }
  36. return $blocks;
  37. }
  38. /**
  39. * Implements hook_block_view().
  40. */
  41. function admin_block_view($delta) {
  42. switch ($delta) {
  43. case 'create':
  44. $item = menu_get_item('node/add');
  45. $links = system_admin_menu_block($item);
  46. if (!empty($links)) {
  47. $menu = array();
  48. foreach ($links as $key => $link) {
  49. $menu[$key] = array(
  50. 'link' => $link + array('in_active_trail' => FALSE),
  51. 'below' => FALSE,
  52. );
  53. }
  54. return array(
  55. 'subject' => !empty($item['title']) ? $item['title'] : t('Create content'),
  56. 'content' => menu_tree_output($menu),
  57. );
  58. }
  59. break;
  60. case 'theme':
  61. if (user_access('select different theme')) {
  62. module_load_include('inc', 'admin', 'includes/admin.theme');
  63. return admin_block_theme();
  64. }
  65. return NULL;
  66. case 'account':
  67. return admin_account_block();
  68. case 'menu':
  69. $item = menu_get_item('admin');
  70. if ($item && $item['access']) {
  71. $tree = menu_tree_all_data('management');
  72. foreach ($tree as $key => $branch) {
  73. if ($branch['link']['link_path'] !== 'admin') {
  74. unset($tree[$key]);
  75. }
  76. }
  77. return array(
  78. 'subject' => !empty($item['title']) ? $item['title'] : t('Administer'),
  79. 'content' => menu_tree_output($tree),
  80. );
  81. }
  82. break;
  83. case 'devel':
  84. module_load_include('inc', 'admin', 'includes/admin.devel');
  85. return admin_block_devel();
  86. }
  87. }
  88. /**
  89. * Implements hook_element_info().
  90. */
  91. function admin_element_info() {
  92. return array(
  93. 'admin_panes' => array(
  94. '#value' => '',
  95. '#theme_wrappers' => array('admin_panes'),
  96. ),
  97. );
  98. }
  99. /**
  100. * Implements hook_menu().
  101. */
  102. function admin_menu() {
  103. $items = array();
  104. $items['admin/config/user-interface/admin'] =
  105. $items['admin/config/user-interface/admin/settings'] = array(
  106. 'title' => 'Administration tools',
  107. 'description' => 'Settings for site administration tools.',
  108. 'page callback' => 'drupal_get_form',
  109. 'page arguments' => array('admin_settings_form'),
  110. 'access callback' => 'user_access',
  111. 'access arguments' => array('administer site configuration'),
  112. 'type' => MENU_NORMAL_ITEM,
  113. 'file' => 'admin.admin.inc',
  114. 'module' => 'admin',
  115. );
  116. $items['admin/config/user-interface/admin/settings']['title'] = 'Settings';
  117. $items['admin/config/user-interface/admin/settings']['type'] = MENU_DEFAULT_LOCAL_TASK;
  118. $items['admin/config/user-interface/admin/rebuild'] = array(
  119. 'title' => 'Rebuild',
  120. 'description' => 'Wipe and rebuild the administrative menu.',
  121. 'page callback' => 'drupal_get_form',
  122. 'page arguments' => array('admin_settings_rebuild'),
  123. 'access callback' => 'user_access',
  124. 'access arguments' => array('administer site configuration'),
  125. 'type' => MENU_LOCAL_TASK,
  126. 'file' => 'admin.admin.inc',
  127. 'module' => 'admin',
  128. 'weight' => 10,
  129. );
  130. return $items;
  131. }
  132. /**
  133. * Implements hook_menu_alter().
  134. */
  135. function admin_menu_alter(&$items) {
  136. foreach ($items as $path => $item) {
  137. // Smarter access callback for poorly checked landing pages
  138. if (!empty($item['access arguments']) && !empty($item['page callback']) && $item['access arguments'] === array('access administration pages') && in_array($item['page callback'], array('system_admin_menu_block_page', 'system_settings_overview'))) {
  139. $items[$path]['access callback'] = 'admin_landing_page_access';
  140. $items[$path]['access arguments'] = array($path);
  141. }
  142. }
  143. }
  144. /**
  145. * Menu access callback for admin landing pages.
  146. *
  147. * For a given landing page, grant access if the current user has access
  148. * to any of its child menu items.
  149. */
  150. function admin_landing_page_access($path) {
  151. static $paths;
  152. if (!isset($paths[$path])) {
  153. $paths[$path] = FALSE;
  154. // Retrieve menu item but without menu_get_item()
  155. $item = db_select('menu_links')
  156. ->fields('menu_links')
  157. ->condition('module', 'system')
  158. ->condition('router_path', $path)
  159. ->execute()
  160. ->fetchAssoc();
  161. if ($item) {
  162. $query = db_select('menu_links', 'ml');
  163. $query->leftJoin('menu_router', 'm', 'm.path = ml.router_path');
  164. $query->fields('ml');
  165. $query->fields('m', array_diff(drupal_schema_fields_sql('menu_router'), array('weight')));
  166. $query->condition('ml.plid', $item['mlid']);
  167. $result = $query->execute();
  168. while ($item = $result->fetchAssoc()) {
  169. _menu_link_translate($item);
  170. if ($item['access']) {
  171. $paths[$path] = TRUE;
  172. break;
  173. }
  174. }
  175. }
  176. }
  177. return $paths[$path];
  178. }
  179. /**
  180. * Implements hook_permission().
  181. */
  182. function admin_permission() {
  183. return array(
  184. 'use admin toolbar' => array(
  185. 'title' => t('use admin toolbar'),
  186. 'description' => t('Use the admin toolbar'),
  187. ),
  188. 'select different theme' => array(
  189. 'title' => t('select different theme'),
  190. 'description' => t("Select different theme for the current user's session."),
  191. ),
  192. );
  193. }
  194. /**
  195. * Implements hook_theme().
  196. */
  197. function admin_theme($cache, $type, $theme, $path) {
  198. $path = drupal_get_path('module', 'admin');
  199. $items['admin_tab'] = array(
  200. 'variables' => array(
  201. 'tab' => array(),
  202. 'class' => '',
  203. ),
  204. 'path' => $path . '/theme',
  205. 'file' => 'theme.inc',
  206. );
  207. $items['admin_toolbar'] = array(
  208. 'variables' => array(
  209. 'blocks' => array(),
  210. 'position' => 'ne',
  211. 'layout' => 'horizontal',
  212. 'behavior' => 'df',
  213. ),
  214. 'template' => 'admin-toolbar',
  215. 'path' => $path . '/theme',
  216. 'file' => 'theme.inc',
  217. );
  218. $items['admin_drilldown_menu_tree_output'] = array(
  219. 'variables' => array('menu' => array()),
  220. 'path' => $path . '/theme',
  221. 'file' => 'theme.inc',
  222. );
  223. $items['admin_drilldown_menu_tree'] = array(
  224. 'variables' => array('menu' => array()),
  225. 'path' => $path . '/theme',
  226. 'file' => 'theme.inc',
  227. );
  228. $items['admin_drilldown_menu_item'] = array(
  229. 'variables' => array(
  230. 'link' => NULL,
  231. 'has_children' => NULL,
  232. 'menu' => '',
  233. 'in_active_trail' => FALSE,
  234. 'extra_class' => NULL,
  235. ),
  236. 'path' => $path . '/theme',
  237. 'file' => 'theme.inc',
  238. );
  239. $items['admin_drilldown_menu_item_link'] = array(
  240. 'variables' => array('item' => array()),
  241. 'path' => $path . '/theme',
  242. 'file' => 'theme.inc',
  243. );
  244. $items['admin_panes'] = array(
  245. 'render element' => 'element',
  246. 'template' => 'admin-panes',
  247. 'path' => $path . '/theme',
  248. 'file' => 'theme.inc',
  249. );
  250. $items['admin_settings_form'] = array(
  251. 'render element' => 'element',
  252. 'path' => $path,
  253. 'file' => 'admin.admin.inc',
  254. );
  255. return $items;
  256. }
  257. /**
  258. * Get variable settings or fallback to defaults.
  259. */
  260. function admin_get_settings($key = NULL) {
  261. static $settings;
  262. if (!isset($settings)) {
  263. // Try to gather what information we can from the cookie.
  264. // Note that this information should not be trusted in any
  265. // way for affecting *anything* but trivial changes to the site.
  266. $cookie = array();
  267. if (isset($_REQUEST['DrupalAdminToolbar'])) {
  268. parse_str($_REQUEST['DrupalAdminToolbar'], $cookie);
  269. }
  270. $settings = variable_get('admin_toolbar', array()) + array(
  271. 'layout' => 'vertical',
  272. 'position' => 'nw',
  273. 'behavior' => 'df',
  274. 'blocks' => admin_get_default_blocks(),
  275. 'expanded' => isset($cookie['expanded']) ? check_plain($cookie['expanded']) : 0,
  276. 'active_tab' => isset($cookie['activeTab']) ? check_plain($cookie['activeTab']) : 0,
  277. );
  278. // Ensure that if behavior is set to autohide the toolbar is collapsed.
  279. if ($settings['behavior'] === 'ah') {
  280. $settings['expanded'] = 0;
  281. }
  282. }
  283. if (isset($key)) {
  284. return isset($settings[$key]) ? $settings[$key] : FALSE;
  285. }
  286. return $settings;
  287. }
  288. /**
  289. * Get all blocks that have declared themselves visible in the admin toolbar by default.
  290. */
  291. function admin_get_default_blocks($reset = FALSE) {
  292. static $defaults;
  293. if (!isset($defaults) || $reset) {
  294. $cache = cache_get('admin_default_blocks');
  295. if ($cache && !$reset) {
  296. $defaults = $cache->data;
  297. }
  298. else {
  299. $defaults = array();
  300. foreach (module_implements('block_info') as $module) {
  301. $module_blocks = module_invoke($module, 'block_info');
  302. if ($module_blocks) {
  303. foreach ($module_blocks as $delta => $info) {
  304. if (isset($info['admin'])) {
  305. $defaults["{$module}-{$delta}"] = isset($info['cache']) ? $info['cache'] : DRUPAL_NO_CACHE;
  306. }
  307. }
  308. }
  309. }
  310. cache_set('admin_default_blocks', $defaults);
  311. }
  312. }
  313. return $defaults;
  314. }
  315. /**
  316. * Get blocks enabled for the admin toolbar.
  317. */
  318. function admin_get_admin_blocks() {
  319. $blocks = array();
  320. if (user_access('use admin toolbar') && $enabled_blocks = admin_get_settings('blocks')) {
  321. module_load_include('module', 'block', 'block');
  322. foreach ($enabled_blocks as $bid => $cache) {
  323. $block = NULL;
  324. $split = explode('-', $bid, 2);
  325. if (count($split) === 2) {
  326. list($module, $delta) = $split;
  327. $block = new stdClass();
  328. $block->module = $module;
  329. $block->delta = $delta;
  330. $block->bid = $bid;
  331. $block->title = NULL;
  332. $block->cache = is_numeric($cache) ? $cache : DRUPAL_NO_CACHE;
  333. $block->region = 'admin_toolbar'; // Fake the block region.
  334. $blocks[$bid] = $block;
  335. }
  336. }
  337. $blocks = _block_render_blocks($blocks);
  338. }
  339. return $blocks;
  340. }
  341. /**
  342. * Preprocessor that runs *before* template_preprocess_page().
  343. */
  344. function admin_page_build(&$page) {
  345. if (user_access('use admin toolbar') && $trail = menu_get_active_trail()) {
  346. do {
  347. $last = array_pop($trail);
  348. } while (count($trail) && !($last['type'] & MENU_VISIBLE_IN_TREE));
  349. if ($last) {
  350. drupal_add_js(array('activePath' => url($last['href'])), array('type' => 'setting', 'scope' => JS_DEFAULT));
  351. }
  352. }
  353. if (!admin_suppress(FALSE) && $blocks = admin_get_admin_blocks()) {
  354. $path = drupal_get_path('module', 'admin');
  355. drupal_add_js("misc/jquery.cookie.js");
  356. drupal_add_js("{$path}/includes/jquery.drilldown.js");
  357. drupal_add_js("{$path}/includes/admin.toolbar.js");
  358. drupal_add_js("{$path}/includes/admin.menu.js");
  359. drupal_add_css("{$path}/includes/admin.toolbar.base.css");
  360. drupal_add_css("{$path}/includes/admin.toolbar.css");
  361. drupal_add_css("{$path}/includes/admin.menu.css");
  362. $position = admin_get_settings('position');
  363. $layout = admin_get_settings('layout');
  364. $behavior = admin_get_settings('behavior');
  365. $class = admin_get_settings('expanded') ? 'admin-expanded' : '';
  366. $page['page_bottom']['admin_toolbar'] = array(
  367. '#type' => 'markup',
  368. '#markup' => theme('admin_toolbar', array('blocks' => $blocks, 'position' => $position, 'layout' => $layout, 'behavior' => $behavior)),
  369. );
  370. }
  371. }
  372. /**
  373. * Implements hook_preprocess_html().
  374. */
  375. function admin_preprocess_html(&$vars) {
  376. if (!admin_suppress(FALSE) && $blocks = admin_get_admin_blocks()) {
  377. $position = admin_get_settings('position');
  378. $layout = admin_get_settings('layout');
  379. $behavior = admin_get_settings('behavior');
  380. $class = admin_get_settings('expanded') ? 'admin-expanded' : '';
  381. $vars['classes_array'][] = " admin-{$position} admin-{$layout} admin-{$behavior} {$class} ";
  382. }
  383. }
  384. /**
  385. * Implements hook_suppress().
  386. *
  387. * Suppress display of administration toolbar.
  388. *
  389. * This function should be called from within another module's page callback
  390. * preferably using module_invoke_all('suppress') when the menu should not be
  391. * displayed. This is useful for modules that implement popup pages or other
  392. * special pages where the menu would be distracting or break the layout.
  393. *
  394. * @param $set
  395. * Defaults to TRUE. If called before hook_footer(), the menu will not be
  396. * displayed. If FALSE is passed, the suppression state is returned.
  397. */
  398. function admin_suppress($set = TRUE) {
  399. static $suppress = FALSE;
  400. if (!empty($set) && $suppress === FALSE) {
  401. $suppress = TRUE;
  402. }
  403. return $suppress;
  404. }
  405. /**
  406. * My Account block.
  407. */
  408. function admin_account_block() {
  409. $block = array(
  410. 'subject' => t('My Account'),
  411. 'content' => '',
  412. );
  413. global $user;
  414. if ($user->uid > 0) {
  415. $menu = array();
  416. $menu[] = array(
  417. 'data' => l(t('View my account'), 'user/' . $user->uid),
  418. 'class' => array('leaf'),
  419. );
  420. $menu[] = array(
  421. 'data' => l(t('Edit my account'), 'user/' . $user->uid . '/edit'),
  422. 'class' => array('leaf'),
  423. );
  424. $menu[] = array(
  425. 'data' => l(t('Logout'), 'user/logout'),
  426. 'class' => array('leaf'),
  427. );
  428. $block['content'] = theme('item_list', array('items' => $menu, 'attributes' => array('class' => array('menu'))));
  429. }
  430. return $block;
  431. }