dashboard.module 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. <?php
  2. /**
  3. * @file
  4. * Provides a dashboard page in the administrative interface.
  5. */
  6. /**
  7. * Implements hook_help().
  8. */
  9. function dashboard_help($path, $arg) {
  10. switch ($path) {
  11. case 'admin/help#dashboard':
  12. $output = '';
  13. $output .= '<h3>' . t('About') . '</h3>';
  14. $output .= '<p>' . t('The Dashboard module provides a <a href="@dashboard">Dashboard page</a> in the administrative interface for organizing administrative tasks and navigation, and tracking information within your site. The Dashboard page contains blocks, which you can add to and arrange using the drag-and-drop interface that appears when you click on the <em>Customize dashboard</em> link. Within this interface, blocks that are not primarily used for site administration do not appear by default, but can be added via the <em>Add other blocks</em> link. For more information, see the online handbook entry for <a href="@handbook">Dashboard module</a>.', array('@handbook' => 'http://drupal.org/documentation/modules/dashboard', '@dashboard' => url('admin/dashboard'))) . '</p>';
  15. $output .= '<h3>' . t('Uses') . '</h3>';
  16. $output .= '<dl>';
  17. $output .= '<dt>' . t('Tracking user activity') . '</dt>';
  18. $output .= '<dd>' . t("By enabling blocks such as <em>Who's online</em> and <em>Who's new</em>, site users can track who is logged in and new user signups at a centralized location.") . '</dd>';
  19. $output .= '<dt>' . t('Tracking content activity') . '</dt>';
  20. $output .= '<dd>' . t('By enabling blocks such as <em>Recent blog posts</em>, <em>New forum topics</em> and <em>Recent comments</em>, site users can view newly added site content at a glance.') . '</dd>';
  21. $output .= '</dl>';
  22. return $output;
  23. case 'admin/dashboard/configure':
  24. // @todo This assumes the current page is being displayed using the same
  25. // theme that the dashboard is displayed in.
  26. $output = '<p>' . t('Rearrange blocks for display on the <a href="@dashboard-url">Dashboard page</a>. Blocks placed in the <em>Dashboard (inactive)</em> region are not displayed when viewing the Dashboard page, but are available within its <em>Customize dashboard</em> interface. Removing a block from active dashboard display makes it available on the main <a href="@blocks-url">blocks administration page</a>.', array('@dashboard-url' => url('admin/dashboard'), '@blocks-url' => url("admin/structure/block/list/{$GLOBALS['theme_key']}"))) . '</p>';
  27. return $output;
  28. }
  29. }
  30. /**
  31. * Implements hook_menu().
  32. */
  33. function dashboard_menu() {
  34. $items['admin/dashboard'] = array(
  35. 'title' => 'Dashboard',
  36. 'description' => 'View and customize your dashboard.',
  37. 'page callback' => 'dashboard_admin',
  38. 'access arguments' => array('access dashboard'),
  39. // Make this appear first, so for example, in admin menus, it shows up on
  40. // the top corner of the window as a convenient "home link".
  41. 'weight' => -15,
  42. );
  43. $items['admin/dashboard/configure'] = array(
  44. 'title' => 'Configure available dashboard blocks',
  45. 'description' => 'Configure which blocks can be shown on the dashboard.',
  46. 'page callback' => 'dashboard_admin_blocks',
  47. 'access arguments' => array('administer blocks'),
  48. 'type' => MENU_VISIBLE_IN_BREADCRUMB,
  49. );
  50. $items['admin/dashboard/customize'] = array(
  51. 'title' => 'Customize dashboard',
  52. 'description' => 'Customize your dashboard.',
  53. 'page callback' => 'dashboard_admin',
  54. 'page arguments' => array(TRUE),
  55. 'access arguments' => array('access dashboard'),
  56. 'type' => MENU_VISIBLE_IN_BREADCRUMB,
  57. );
  58. $items['admin/dashboard/drawer'] = array(
  59. 'page callback' => 'dashboard_show_disabled',
  60. 'access arguments' => array('administer blocks'),
  61. 'type' => MENU_CALLBACK,
  62. );
  63. $items['admin/dashboard/block-content/%/%'] = array(
  64. 'page callback' => 'dashboard_show_block_content',
  65. 'page arguments' => array(3, 4),
  66. 'access arguments' => array('administer blocks'),
  67. 'type' => MENU_CALLBACK,
  68. );
  69. $items['admin/dashboard/update'] = array(
  70. 'page callback' => 'dashboard_update',
  71. 'access arguments' => array('administer blocks'),
  72. 'type' => MENU_CALLBACK,
  73. );
  74. return $items;
  75. }
  76. /**
  77. * Implements hook_permission().
  78. */
  79. function dashboard_permission() {
  80. return array(
  81. 'access dashboard' => array(
  82. 'title' => t('View the administrative dashboard'),
  83. // Note: We translate the 'Administer blocks' permission string here with
  84. // a separate t() call, to make sure it gets the same translation as when
  85. // it's in block_permission().
  86. 'description' => t('Customizing the dashboard requires the !permission-name permission.', array(
  87. '!permission-name' => l(t('Administer blocks'), 'admin/people/permissions', array('fragment' => 'module-block')),
  88. )),
  89. ),
  90. );
  91. }
  92. /**
  93. * Implements hook_block_info_alter().
  94. */
  95. function dashboard_block_info_alter(&$blocks, $theme, $code_blocks) {
  96. $admin_theme = variable_get('admin_theme');
  97. if (($admin_theme && $theme == $admin_theme) || (!$admin_theme && $theme == variable_get('theme_default', 'bartik'))) {
  98. foreach ($blocks as $module => &$module_blocks) {
  99. foreach ($module_blocks as $delta => &$block) {
  100. // Make administrative blocks that are not already in use elsewhere
  101. // available for the dashboard.
  102. if (empty($block['status']) && (empty($block['region']) || $block['region'] == BLOCK_REGION_NONE) && !empty($code_blocks[$module][$delta]['properties']['administrative'])) {
  103. $block['status'] = 1;
  104. $block['region'] = 'dashboard_inactive';
  105. }
  106. }
  107. }
  108. }
  109. }
  110. /**
  111. * Implements hook_block_list_alter().
  112. *
  113. * Skip rendering dashboard blocks when not on the dashboard page itself. This
  114. * prevents expensive dashboard blocks from causing performance issues on pages
  115. * where they will never be displayed.
  116. */
  117. function dashboard_block_list_alter(&$blocks) {
  118. if (!dashboard_is_visible()) {
  119. foreach ($blocks as $key => $block) {
  120. if (in_array($block->region, dashboard_regions())) {
  121. unset($blocks[$key]);
  122. }
  123. }
  124. }
  125. }
  126. /**
  127. * Implements hook_page_build().
  128. *
  129. * Display dashboard blocks in the main content region.
  130. */
  131. function dashboard_page_build(&$page) {
  132. global $theme_key;
  133. if (dashboard_is_visible()) {
  134. $block_info = array();
  135. // Create a wrapper for the dashboard itself, then insert each dashboard
  136. // region into it.
  137. $page['content']['dashboard'] = array('#theme_wrappers' => array('dashboard'));
  138. foreach (dashboard_regions() as $region) {
  139. // Do not show dashboard blocks that are disabled.
  140. if ($region == 'dashboard_inactive') {
  141. continue;
  142. }
  143. // Insert regions even when they are empty, so that they will be
  144. // displayed when the dashboard is being configured.
  145. $page['content']['dashboard'][$region] = !empty($page[$region]) ? $page[$region] : array();
  146. $page['content']['dashboard'][$region]['#dashboard_region'] = $region;
  147. // Allow each dashboard region to be themed differently, or fall back on
  148. // the generic theme wrapper function for dashboard regions.
  149. $page['content']['dashboard'][$region]['#theme_wrappers'][] = array($region, 'dashboard_region');
  150. unset($page[$region]);
  151. $blocks_found = array();
  152. foreach ($page['content']['dashboard'][$region] as $item) {
  153. if (isset($item['#theme_wrappers']) && is_array($item['#theme_wrappers']) && in_array('block', $item['#theme_wrappers'])) {
  154. // If this item is a block, ensure it has a subject.
  155. if (empty($item['#block']->subject)) {
  156. // Locally cache info data for the object for all blocks, in case
  157. // we find a block similarly missing title from the same module.
  158. if (!isset($block_info[$item['#block']->module])) {
  159. $block_info[$item['#block']->module] = module_invoke($item['#block']->module, 'block_info');
  160. }
  161. $item['#block']->subject = $block_info[$item['#block']->module][$item['#block']->delta]['info'];
  162. }
  163. $blocks_found[$item['#block']->module . '_' . $item['#block']->delta] = TRUE;
  164. }
  165. }
  166. // Find blocks which were not yet displayed on the page (were empty), and
  167. // add placeholder items in their place for rendering.
  168. $block_list = db_select('block')
  169. ->condition('theme', $theme_key)
  170. ->condition('status', 1)
  171. ->condition('region', $region)
  172. ->fields('block')
  173. ->execute();
  174. foreach ($block_list as $block) {
  175. if (!isset($blocks_found[$block->module . '_' . $block->delta])) {
  176. $block->enabled = $block->page_match = TRUE;
  177. $block->content = array('#markup' => '<div class="dashboard-block-empty">(empty)</div>');
  178. if (!isset($block_info[$block->module])) {
  179. $block_info[$block->module] = module_invoke($block->module, 'block_info');
  180. }
  181. $block->subject = t('@title', array('@title' => $block_info[$block->module][$block->delta]['info']));
  182. $block_render = array($block->module . '_' . $block->delta => $block);
  183. $build = _block_get_renderable_array($block_render);
  184. $page['content']['dashboard'][$block->region][] = $build;
  185. }
  186. }
  187. }
  188. }
  189. }
  190. /**
  191. * Implements hook_system_info_alter().
  192. *
  193. * Add regions to each theme to store the dashboard blocks.
  194. */
  195. function dashboard_system_info_alter(&$info, $file, $type) {
  196. if ($type == 'theme') {
  197. // Add the dashboard regions (the "inactive" region should always appear
  198. // last in the list, for usability reasons).
  199. $dashboard_regions = dashboard_region_descriptions();
  200. if (isset($dashboard_regions['dashboard_inactive'])) {
  201. $inactive_region = $dashboard_regions['dashboard_inactive'];
  202. unset($dashboard_regions['dashboard_inactive']);
  203. $dashboard_regions['dashboard_inactive'] = $inactive_region;
  204. }
  205. $info['regions'] += $dashboard_regions;
  206. // Indicate that these regions are intended to be displayed whenever the
  207. // dashboard is displayed in an overlay. This information is provided for
  208. // any module that might need to use it, not just the core Overlay module.
  209. $info['overlay_regions'] = !empty($info['overlay_regions']) ? array_merge($info['overlay_regions'], dashboard_regions()) : dashboard_regions();
  210. }
  211. }
  212. /**
  213. * Implements hook_theme().
  214. */
  215. function dashboard_theme() {
  216. return array(
  217. 'dashboard' => array(
  218. 'render element' => 'element',
  219. ),
  220. 'dashboard_admin' => array(
  221. 'render element' => 'element',
  222. ),
  223. 'dashboard_region' => array(
  224. 'render element' => 'element',
  225. ),
  226. 'dashboard_disabled_blocks' => array(
  227. 'variables' => array('blocks' => NULL),
  228. ),
  229. 'dashboard_disabled_block' => array(
  230. 'variables' => array('block' => NULL),
  231. ),
  232. 'dashboard_admin_display_form' => array(
  233. // When building the form for configuring dashboard blocks, reuse the
  234. // Block module's template for the main block configuration form.
  235. 'template' => 'block-admin-display-form',
  236. 'path' => drupal_get_path('module', 'block'),
  237. 'file' => 'block.admin.inc',
  238. 'render element' => 'form',
  239. ),
  240. );
  241. }
  242. /**
  243. * Implements hook_forms().
  244. */
  245. function dashboard_forms() {
  246. // Reroute the dashboard configuration form to the main blocks administration
  247. // form. This allows us to distinguish them by form ID in hook_form_alter().
  248. $forms['dashboard_admin_display_form'] = array(
  249. 'callback' => 'block_admin_display_form',
  250. );
  251. return $forms;
  252. }
  253. /**
  254. * Page callback: Displays the dashboard.
  255. *
  256. * @param $launch_customize
  257. * Whether to launch in customization mode right away. TRUE or FALSE.
  258. */
  259. function dashboard_admin($launch_customize = FALSE) {
  260. $js_settings = array(
  261. 'dashboard' => array(
  262. 'drawer' => url('admin/dashboard/drawer'),
  263. 'blockContent' => url('admin/dashboard/block-content'),
  264. 'updatePath' => url('admin/dashboard/update'),
  265. 'formToken' => drupal_get_token('dashboard-update'),
  266. 'launchCustomize' => $launch_customize,
  267. 'dashboard' => url('admin/dashboard'),
  268. 'emptyBlockText' => t('(empty)'),
  269. 'emptyRegionTextInactive' => t('This dashboard region is empty. Click <em>Customize dashboard</em> to add blocks to it.'),
  270. 'emptyRegionTextActive' => t('DRAG HERE'),
  271. ),
  272. );
  273. $build = array(
  274. '#theme' => 'dashboard_admin',
  275. '#message' => t('To customize the dashboard page, move blocks to the dashboard regions on the <a href="@dashboard">Dashboard administration page</a>, or enable JavaScript on this page to use the drag-and-drop interface.', array('@dashboard' => url('admin/dashboard/configure'))),
  276. '#access' => user_access('administer blocks'),
  277. '#attached' => array(
  278. 'js' => array(
  279. drupal_get_path('module', 'dashboard') . '/dashboard.js',
  280. array('data' => $js_settings, 'type' => 'setting'),
  281. ),
  282. 'library' => array(array('system', 'ui.sortable')),
  283. ),
  284. );
  285. return $build;
  286. }
  287. /**
  288. * Page callback: Builds the page for administering dashboard blocks.
  289. *
  290. * This page reuses the Block module's administration form but limits editing
  291. * to blocks that are available to appear on the dashboard.
  292. *
  293. * @see block_admin_display()
  294. * @see block_admin_display_form()
  295. * @see dashboard_form_dashboard_admin_display_form_alter()
  296. * @see template_preprocess_dashboard_admin_display_form()
  297. */
  298. function dashboard_admin_blocks() {
  299. global $theme_key;
  300. drupal_theme_initialize();
  301. module_load_include('inc', 'block', 'block.admin');
  302. // Prepare the blocks for the current theme, and remove those that are
  303. // currently displayed in non-dashboard regions.
  304. // @todo This assumes the current page is being displayed using the same
  305. // theme that the dashboard is displayed in.
  306. $blocks = block_admin_display_prepare_blocks($theme_key);
  307. $dashboard_regions = dashboard_region_descriptions();
  308. $regions_to_remove = array_diff_key(system_region_list($theme_key, REGIONS_VISIBLE), $dashboard_regions);
  309. foreach ($blocks as $id => $block) {
  310. if (isset($regions_to_remove[$block['region']])) {
  311. unset($blocks[$id]);
  312. }
  313. }
  314. // Pass in the above blocks and dashboard regions to the form, so that only
  315. // dashboard-related regions will be displayed.
  316. return drupal_get_form('dashboard_admin_display_form', $blocks, $theme_key, $dashboard_regions);
  317. }
  318. /**
  319. * Implements hook_form_FORM_ID_alter().
  320. */
  321. function dashboard_form_block_admin_display_form_alter(&$form, &$form_state, $form_id) {
  322. // Hide dashboard regions (and any blocks placed within them) from the block
  323. // administration form and from the options list on that form. This
  324. // function is called for both the dashboard block configuration form and the
  325. // standard block configuration form so that both forms can share the same
  326. // constructor. As a result the form_id must be checked.
  327. if ($form_id != 'dashboard_admin_display_form') {
  328. $dashboard_regions = dashboard_region_descriptions();
  329. $form['block_regions']['#value'] = array_diff_key($form['block_regions']['#value'], $dashboard_regions);
  330. foreach (element_children($form['blocks']) as $i) {
  331. $block = &$form['blocks'][$i];
  332. if (isset($block['region']['#default_value']) && isset($dashboard_regions[$block['region']['#default_value']]) && $block['region']['#default_value'] != 'dashboard_inactive') {
  333. $block['#access'] = FALSE;
  334. }
  335. elseif (isset($block['region']['#options'])) {
  336. $block['region']['#options'] = array_diff_key($block['region']['#options'], $dashboard_regions);
  337. }
  338. // Show inactive dashboard blocks as disabled on the main block
  339. // administration form, so that they are available to place in other
  340. // regions of the theme. Note that when the form is submitted, any such
  341. // blocks which still remain disabled will immediately be put back in the
  342. // 'dashboard_inactive' region, because dashboard_block_info_alter() is
  343. // called when the blocks are rehashed. Fortunately, this is the exact
  344. // behavior we want.
  345. if ($block['region']['#default_value'] == 'dashboard_inactive') {
  346. // @todo These do not wind up in correct alphabetical order.
  347. $block['region']['#default_value'] = NULL;
  348. }
  349. }
  350. }
  351. }
  352. /**
  353. * Implements hook_form_FORM_ID_alter().
  354. */
  355. function dashboard_form_dashboard_admin_display_form_alter(&$form, &$form_state) {
  356. // Redirect the 'configure' and 'delete' links on each block back to the
  357. // dashboard blocks administration page.
  358. foreach ($form['blocks'] as &$block) {
  359. if (isset($block['configure']['#href'])) {
  360. $block['configure']['#options']['query']['destination'] = 'admin/dashboard/configure';
  361. }
  362. if (isset($block['delete']['#href'])) {
  363. $block['delete']['#options']['query']['destination'] = 'admin/dashboard/configure';
  364. }
  365. }
  366. }
  367. /**
  368. * Implements hook_form_FORM_ID_alter().
  369. */
  370. function dashboard_form_block_admin_configure_alter(&$form, &$form_state) {
  371. global $theme_key;
  372. drupal_theme_initialize();
  373. // Hide the dashboard regions from the region select list on the block
  374. // configuration form, for all themes except the current theme (since the
  375. // other themes do not display the dashboard).
  376. // @todo This assumes the current page is being displayed using the same
  377. // theme that the dashboard is displayed in.
  378. $dashboard_regions = dashboard_region_descriptions();
  379. foreach (element_children($form['regions']) as $region_name) {
  380. $region = &$form['regions'][$region_name];
  381. if ($region_name != $theme_key && isset($region['#options'])) {
  382. $region['#options'] = array_diff_key($region['#options'], $dashboard_regions);
  383. }
  384. }
  385. }
  386. /**
  387. * Implements hook_form_FORM_ID_alter().
  388. */
  389. function dashboard_form_block_add_block_form_alter(&$form, &$form_state) {
  390. dashboard_form_block_admin_configure_alter($form, $form_state);
  391. }
  392. /**
  393. * Preprocesses variables for block-admin-display-form.tpl.php.
  394. */
  395. function template_preprocess_dashboard_admin_display_form(&$variables) {
  396. template_preprocess_block_admin_display_form($variables);
  397. if (isset($variables['block_regions'][BLOCK_REGION_NONE])) {
  398. $variables['block_regions'][BLOCK_REGION_NONE] = t('Other blocks');
  399. }
  400. }
  401. /**
  402. * Determines if the dashboard should be displayed on the current page.
  403. *
  404. * This function checks if the user is currently viewing the dashboard and has
  405. * access to see it. It is used by other functions in the dashboard module to
  406. * decide whether or not the dashboard content should be displayed to the
  407. * current user.
  408. *
  409. * Although the menu system normally handles the above tasks, it only does so
  410. * for the main page content. However, the dashboard is not part of the main
  411. * page content, but rather is displayed in special regions of the page (so it
  412. * can interface with the Block module's method of managing page regions). We
  413. * therefore need to maintain this separate function to check the menu item for
  414. * us.
  415. *
  416. * @return
  417. * TRUE if the dashboard should be visible on the current page, FALSE
  418. * otherwise.
  419. *
  420. * @see dashboard_block_list_alter()
  421. * @see dashboard_page_build()
  422. */
  423. function dashboard_is_visible() {
  424. static $is_visible;
  425. if (!isset($is_visible)) {
  426. // If the current menu item represents the page on which we want to display
  427. // the dashboard, and if the current user has access to see it, return
  428. // TRUE.
  429. $menu_item = menu_get_item();
  430. $is_visible = isset($menu_item['page_callback']) && $menu_item['page_callback'] == 'dashboard_admin' && !empty($menu_item['access']);
  431. }
  432. return $is_visible;
  433. }
  434. /**
  435. * Returns an array of dashboard region descriptions, keyed by region name.
  436. */
  437. function dashboard_region_descriptions() {
  438. $regions = module_invoke_all('dashboard_regions');
  439. drupal_alter('dashboard_regions', $regions);
  440. return $regions;
  441. }
  442. /**
  443. * Returns an array of dashboard region names.
  444. */
  445. function dashboard_regions() {
  446. $regions = &drupal_static(__FUNCTION__);
  447. if (!isset($regions)) {
  448. $regions = array_keys(dashboard_region_descriptions());
  449. }
  450. return $regions;
  451. }
  452. /**
  453. * Implements hook_dashboard_regions().
  454. */
  455. function dashboard_dashboard_regions() {
  456. return array(
  457. 'dashboard_main' => 'Dashboard (main)',
  458. 'dashboard_sidebar' => 'Dashboard (sidebar)',
  459. 'dashboard_inactive' => 'Dashboard (inactive)',
  460. );
  461. }
  462. /**
  463. * Ajax callback: Shows disabled blocks in the dashboard customization mode.
  464. */
  465. function dashboard_show_disabled() {
  466. global $theme_key;
  467. // Blocks are not necessarily initialized at this point.
  468. $blocks = _block_rehash();
  469. // Limit the list to blocks that are marked as disabled for the dashboard.
  470. foreach ($blocks as $key => $block) {
  471. if ($block['theme'] != $theme_key || $block['region'] != 'dashboard_inactive') {
  472. unset($blocks[$key]);
  473. }
  474. }
  475. // Theme the output and end the page request.
  476. print theme('dashboard_disabled_blocks', array('blocks' => $blocks));
  477. drupal_exit();
  478. }
  479. /**
  480. * Ajax callback: Displays the rendered contents of a specific block.
  481. *
  482. * @param $module
  483. * The block's module name.
  484. * @param $delta
  485. * The block's delta.
  486. */
  487. function dashboard_show_block_content($module, $delta) {
  488. drupal_theme_initialize();
  489. global $theme_key;
  490. $blocks = array();
  491. $block_object = db_query("SELECT * FROM {block} WHERE theme = :theme AND module = :module AND delta = :delta", array(
  492. ":theme" => $theme_key,
  493. ":module" => $module,
  494. ":delta" => $delta,
  495. ))
  496. ->fetchObject();
  497. $block_object->enabled = $block_object->page_match = TRUE;
  498. $blocks[$module . "_" . $delta] = $block_object;
  499. $block_content = _block_render_blocks($blocks);
  500. $build = _block_get_renderable_array($block_content);
  501. $rendered_block = drupal_render($build);
  502. print $rendered_block;
  503. drupal_exit();
  504. }
  505. /**
  506. * Sets the new weight of each region according to the drag-and-drop order.
  507. */
  508. function dashboard_update() {
  509. drupal_theme_initialize();
  510. global $theme_key;
  511. // Check the form token to make sure we have a valid request.
  512. if (!empty($_REQUEST['form_token']) && drupal_valid_token($_REQUEST['form_token'], 'dashboard-update')) {
  513. parse_str($_REQUEST['regions'], $regions);
  514. foreach ($regions as $region_name => $blocks) {
  515. if ($region_name == 'disabled_blocks') {
  516. $region_name = 'dashboard_inactive';
  517. }
  518. foreach ($blocks as $weight => $block_string) {
  519. // Parse the query string to determine the block's module and delta.
  520. preg_match('/block-([^-]+)-(.+)/', $block_string, $matches);
  521. $block = new stdClass();
  522. $block->module = $matches[1];
  523. $block->delta = $matches[2];
  524. $block->region = $region_name;
  525. $block->weight = $weight;
  526. $block->status = 1;
  527. db_merge('block')
  528. ->key(array(
  529. 'module' => $block->module,
  530. 'delta' => $block->delta,
  531. 'theme' => $theme_key,
  532. ))
  533. ->fields(array(
  534. 'status' => $block->status,
  535. 'weight' => $block->weight,
  536. 'region' => $block->region,
  537. 'pages' => '',
  538. ))
  539. ->execute();
  540. }
  541. }
  542. drupal_set_message(t('The configuration options have been saved.'), 'status', FALSE);
  543. }
  544. drupal_exit();
  545. }
  546. /**
  547. * Returns HTML for the entire dashboard.
  548. *
  549. * @param $variables
  550. * An associative array containing:
  551. * - element: A render element containing the properties of the dashboard
  552. * region element, #dashboard_region and #children.
  553. *
  554. * @ingroup themeable
  555. */
  556. function theme_dashboard($variables) {
  557. extract($variables);
  558. drupal_add_css(drupal_get_path('module', 'dashboard') . '/dashboard.css');
  559. return '<div id="dashboard" class="clearfix">' . $element['#children'] . '</div>';
  560. }
  561. /**
  562. * Returns HTML for the non-customizable part of the dashboard page.
  563. *
  564. * @param $variables
  565. * An associative array containing:
  566. * - element: A render element containing a #message.
  567. *
  568. * @ingroup themeable
  569. */
  570. function theme_dashboard_admin($variables) {
  571. // We only return a simple help message, since the actual content of the page
  572. // will be populated via the dashboard regions in dashboard_page_build().
  573. return '<div class="customize-dashboard js-hide">' . $variables['element']['#message'] . '</div>';
  574. }
  575. /**
  576. * Returns HTML for a generic dashboard region.
  577. *
  578. * @param $variables
  579. * An associative array containing:
  580. * - element: A render element containing the properties of the dashboard
  581. * region element, #dashboard_region and #children.
  582. *
  583. * @ingroup themeable
  584. */
  585. function theme_dashboard_region($variables) {
  586. extract($variables);
  587. $output = '<div id="' . $element['#dashboard_region'] . '" class="dashboard-region">';
  588. $output .= '<div class="region clearfix">';
  589. $output .= $element['#children'];
  590. // Closing div.region
  591. $output .= '</div>';
  592. // Closing div.dashboard-region
  593. $output .= '</div>';
  594. return $output;
  595. }
  596. /**
  597. * Returns HTML for disabled blocks, for use in dashboard customization mode.
  598. *
  599. * @param $variables
  600. * An associative array containing:
  601. * - blocks: An array of block objects from _block_rehash().
  602. *
  603. * @ingroup themeable
  604. */
  605. function theme_dashboard_disabled_blocks($variables) {
  606. extract($variables);
  607. $output = '<div class="canvas-content"><p>' . t('Drag and drop these blocks to the columns below. Changes are automatically saved. More options are available on the <a href="@dashboard-url">configuration page</a>.', array('@dashboard-url' => url('admin/dashboard/configure'))) . '</p>';
  608. $output .= '<div id="disabled-blocks"><div class="region disabled-blocks clearfix">';
  609. foreach ($blocks as $block) {
  610. $output .= theme('dashboard_disabled_block', array('block' => $block));
  611. }
  612. $output .= '<div class="clearfix"></div>';
  613. $output .= '<p class="dashboard-add-other-blocks">' . l(t('Add other blocks'), 'admin/dashboard/configure') . '</p>';
  614. $output .= '</div></div></div>';
  615. return $output;
  616. }
  617. /**
  618. * Returns HTML for disabled blocks, for use in dashboard customization mode.
  619. *
  620. * @param $variables
  621. * An associative array containing:
  622. * - block: A block object from _block_rehash().
  623. *
  624. * @ingroup themeable
  625. */
  626. function theme_dashboard_disabled_block($variables) {
  627. extract($variables);
  628. $output = "";
  629. if (isset($block)) {
  630. $output .= '<div id="block-' . $block['module'] . '-' . $block['delta']
  631. . '" class="disabled-block block block-' . $block['module'] . '-' . $block['delta']
  632. . ' module-' . $block['module'] . ' delta-' . $block['delta'] . '">'
  633. . '<h2>' . (!empty($block['title']) && $block['title'] != '<none>' ? check_plain($block['title']) : check_plain($block['info'])) . '</h2>'
  634. . '<div class="content"></div>'
  635. . '</div>';
  636. }
  637. return $output;
  638. }