dashboard.module 16.2 KB
Newer Older
1
2
3
<?php
// $Id$

4
5
6
7
8
9
10
11
/**
 * Implements hook_help().
 */
function dashboard_help($path, $arg) {
  switch ($path) {
    case 'admin/help#dashboard':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
12
      $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. For more information, see the online handbook entry for <a href="@handbook">Dashboard module</a>.', array('@handbook' => 'http://drupal.org/handbook/modules/dashboard', '@dashboard' => url('admin/dashboard'))) . '</p>';
13
14
15
16
17
18
19
20
21
22
23
      $output .= '<h3>' . t('Uses') . '</h3>';
      $output .= '<dl>';
      $output .= '<dt>' . t('Tracking user activity') . '</dt>';
      $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>';
      $output .= '<dt>' . t('Tracking content activity') . '</dt>';
      $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>';
      $output .= '</dl>';
      return $output;
  }
}

24
/**
25
 * Implements hook_menu().
26
27
28
29
 */
function dashboard_menu() {
  $items['admin/dashboard'] = array(
    'title' => 'Dashboard',
30
    'description' => 'View and customize your dashboard',
31
32
33
34
35
36
37
38
    'page callback' => 'dashboard_admin',
    'access arguments' => array('access dashboard'),
    // Make this appear first, so for example, in admin menus, it shows up on
    // the top corner of the window as a convinient "home link".
    'weight' => -100,
  );
  $items['admin/dashboard/customize'] = array(
    'title' => 'Dashboard',
39
    'description' => 'View and customize your dashboard',
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
    'page callback' => 'dashboard_admin',
    'page arguments' => array(TRUE),
    'access arguments' => array('access dashboard'),
    'type' => MENU_CALLBACK,
  );
  $items['admin/dashboard/drawer'] = array(
    'page callback' => 'dashboard_show_disabled',
    'access arguments' => array('administer blocks'),
    'type' => MENU_CALLBACK,
  );
  $items['admin/dashboard/block-content/%/%'] = array(
    'page callback' => 'dashboard_show_block_content',
    'page arguments' => array(3, 4),
    'access arguments' => array('administer blocks'),
    'type' => MENU_CALLBACK,
  );
  $items['admin/dashboard/update'] = array(
    'page callback' => 'dashboard_update',
    'access arguments' => array('administer blocks'),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
65
 * Implements hook_block_info_alter().
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
 *
 * Skip rendering dashboard blocks when not on the dashboard page itself. This
 * prevents expensive dashboard blocks from causing performance issues on pages
 * where they will never be displayed.
 */
function dashboard_block_info_alter(&$blocks) {
  if (!dashboard_is_visible()) {
    foreach ($blocks as $key => $block) {
      if (in_array($block->region, dashboard_regions())) {
        unset($blocks[$key]);
      }
    }
  }
}

/**
82
 * Implements hook_page_build().
83
84
85
 *
 * Display dashboard blocks in the main content region.
 */
86
function dashboard_page_build(&$page) {
87
88
89
90
  global $theme_key;

  if (dashboard_is_visible()) {
    $block_info = array();
91

92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
    // Create a wrapper for the dashboard itself, then insert each dashboard
    // region into it.
    $page['content']['dashboard'] = array('#theme_wrappers' => array('dashboard'));
    foreach (dashboard_regions() as $region) {
      // Insert regions even when they are empty, so that they will be
      // displayed when the dashboard is being configured.
      $page['content']['dashboard'][$region] = !empty($page[$region]) ? $page[$region] : array();
      $page['content']['dashboard'][$region]['#dashboard_region'] = $region;
      // Allow each dashboard region to be themed differently, or fall back on
      // the generic theme wrapper function for dashboard regions.
      $page['content']['dashboard'][$region]['#theme_wrappers'][] = array($region, 'dashboard_region');
      unset($page[$region]);
      $blocks_found = array();
      foreach ($page['content']['dashboard'][$region] as $item) {
        if (isset($item['#theme_wrappers']) && is_array($item['#theme_wrappers']) && in_array('block', $item['#theme_wrappers'])) {
          // If this item is a block, ensure it has a subject.
          if (empty($item['#block']->subject)) {
            // Locally cache info data for the object for all blocks, in case
            // we find a block similarly missing title from the same module.
            if (!isset($block_info[$item['#block']->module])) {
              $block_info[$item['#block']->module] = module_invoke($item['#block']->module, 'block_info');
            }
            $item['#block']->subject = $block_info[$item['#block']->module][$item['#block']->delta]['info'];
          }
          $blocks_found[$item['#block']->module . '_' . $item['#block']->delta] = TRUE;
        }
      }

      // Find blocks which were not yet displayed on the page (were empty), and
      // add placeholder items in their place for rendering.
      $block_list = db_select('block')
        ->condition('theme', $theme_key)
        ->condition('status', 1)
        ->condition('region', $region)
        ->fields('block')
        ->execute();
      foreach ($block_list as $block) {
        if (!isset($blocks_found[$block->module . '_' . $block->delta])) {
          $block->enabled = $block->page_match = TRUE;
          $block->content = array('#markup' => '<div class="dashboard-block-empty">(empty)</div>');
          if (!isset($block_info[$block->module])) {
            $block_info[$block->module] = module_invoke($block->module, 'block_info');
          }
          $block->subject = t('@title', array('@title' => $block_info[$block->module][$block->delta]['info']));
          $block_render = array($block->module . '_' . $block->delta => $block);
          $build = _block_get_renderable_array($block_render);
          $page['content']['dashboard'][$block->region][] = $build;
        }
      }
    }
  }
}

/**
146
 * Implements hook_permission().
147
148
149
150
151
 */
function dashboard_permission() {
  return array(
    'access dashboard' => array(
      'title' => t('View the administrative dashboard'),
152
      'description' => t('Note: modifying the dashboard requires the !administer_blocks permission.', array('!administer_blocks' => l(t('Administer blocks'), 'admin/config/people/permissions', array('fragment' => 'module-block')))),
153
154
155
156
157
    ),
  );
}

/**
158
 * Implements hook_system_info_alter().
159
160
161
162
163
164
165
166
167
168
169
170
171
 *
 * Add regions to each theme to store the dashboard blocks.
 */
function dashboard_system_info_alter(&$info, $file, $type) {
  if ($type == 'theme') {
    $info['regions'] += dashboard_region_descriptions();
    if (module_exists('overlay')) {
      $info['overlay_regions'] = !empty($info['overlay_regions']) ? array_merge($info['overlay_regions'], dashboard_regions()) : dashboard_regions();
    }
  }
}

/**
172
 * Implements hook_theme().
173
174
175
176
 */
function dashboard_theme() {
  return array(
    'dashboard' => array(
177
      'render element' => 'element',
178
    ),
179
    'dashboard_admin' => array(
180
      'render element' => 'element',
181
    ),
182
    'dashboard_region' => array(
183
      'render element' => 'element',
184
185
    ),
    'dashboard_disabled_blocks' => array(
186
      'variables' => array('blocks' => NULL),
187
188
    ),
    'dashboard_disabled_block' => array(
189
      'variables' => array('block' => NULL),
190
191
192
193
194
195
196
197
198
199
200
    ),
  );
}

/**
 * Dashboard page callback.
 *
 * @param $launch_customize
 *   Whether to launch in customization mode right away. TRUE or FALSE.
 */
function dashboard_admin($launch_customize = FALSE) {
201
202
203
204
205
206
207
208
  $js_settings = array(
    'dashboard' => array(
      'drawer' => url('admin/dashboard/drawer'),
      'blockContent' => url('admin/dashboard/block-content'),
      'updatePath' => url('admin/dashboard/update'),
      'formToken' => drupal_get_token('dashboard-update'),
      'launchCustomize' => $launch_customize,
      'dashboard' => url('admin/dashboard'),
209
      'emptyBlockText' => t('(empty)'),
210
      'emptyRegionTextInactive' => t('This dashboard region is empty. Click <em>Customize dashboard</em> to add blocks to it.'),
211
      'emptyRegionTextActive' => t('DRAG HERE'),
212
213
214
215
    ),
  );
  $build = array(
    '#theme' => 'dashboard_admin',
216
    '#message' => t('To customize the dashboard page, move blocks to the dashboard regions on the <a href="@blocks">Blocks administration page</a>, or enable JavaScript on this page to use the drag-and-drop interface.', array('!blocks' => url('admin/structure/block'))),
217
218
219
220
221
    '#access' => user_access('administer blocks'),
    '#attached' => array(
      'js' => array(
        drupal_get_path('module', 'dashboard') . '/dashboard.js',
        array('data' => $js_settings, 'type' => 'setting'),
222
      ),
223
224
225
226
      'library' => array(array('system', 'ui.sortable')),
    ),
  );
  return $build;
227
228
229
230
231
232
}

/**
 * Returns TRUE if the user is currently viewing the dashboard.
 */
function dashboard_is_visible() {
233
234
235
236
237
238
  static $is_visible;
  if (!isset($is_visible)) {
    $menu_item = menu_get_item();
    $is_visible = isset($menu_item['page_callback']) && $menu_item['page_callback'] == 'dashboard_admin';
  }
  return $is_visible;
239
240
241
242
243
244
}

/**
 * Return an array of dashboard region descriptions, keyed by region name.
 */
function dashboard_region_descriptions() {
245
246
247
  $regions = module_invoke_all('dashboard_regions');
  drupal_alter('dashboard_regions', $regions);
  return $regions;
248
249
250
251
252
253
}

/**
 * Return an array of dashboard region names.
 */
function dashboard_regions() {
254
255
256
257
258
  static $regions;
  if (!isset($regions)) {
    $regions = array_keys(dashboard_region_descriptions());
  }
  return $regions;
259
260
}

261
/**
262
 * Implements hook_dashboard_regions().
263
264
265
 */
function dashboard_dashboard_regions() {
  return array(
266
    'dashboard_main' => 'Dashboard main',
267
268
269
270
    'dashboard_sidebar' => 'Dashboard sidebar',
  );
}

271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/**
 * AJAX callback to show disabled blocks in the dashboard customization mode.
 */
function dashboard_show_disabled() {
  global $theme_key;

  // Blocks are not necessarily initialized at this point.
  $blocks = _block_rehash();

  // Limit the list to disabled blocks for the current theme.
  foreach ($blocks as $key => $block) {
    if ($block['theme'] != $theme_key || (!empty($block['status']) && !empty($block['region']))) {
      unset($blocks[$key]);
    }
  }

  // Theme the output and end the page request.
  print theme('dashboard_disabled_blocks', array('blocks' => $blocks));
289
  drupal_exit();
290
291
292
293
}

/**
 * AJAX callback to display the rendered contents of a specific block.
294
 *
295
296
 * @param $module
 *   The block's module name.
297
 * @param $delta
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
 *   The block's delta.
 */
function dashboard_show_block_content($module, $delta) {
  drupal_theme_initialize();
  global $theme_key;

  $blocks = array();
  $block_object = db_query("SELECT * FROM {block} WHERE theme = :theme AND module = :module AND delta = :delta", array(
    ":theme" => $theme_key,
    ":module" => $module,
    ":delta" => $delta,
    ))
    ->fetchObject();
  $block_object->enabled = $block_object->page_match = TRUE;
  $blocks[$module . "_" . $delta] = $block_object;
  $block_content = _block_render_blocks($blocks);
  $build = _block_get_renderable_array($block_content);
  $rendered_block = drupal_render($build);
  print $rendered_block;
317
  drupal_exit();
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
}

/**
 * Set the new weight of each region according to the drag-and-drop order.
 */
function dashboard_update() {
  drupal_theme_initialize();
  global $theme_key;
  // Check the form token to make sure we have a valid request.
  if (!empty($_REQUEST['form_token']) && drupal_valid_token($_REQUEST['form_token'], 'dashboard-update')) {
    parse_str($_REQUEST['regions'], $regions);
    foreach ($regions as $region_name => $blocks) {
      if ($region_name == 'disabled_blocks') {
        $region_name = '';
      }
      foreach ($blocks as $weight => $block_string) {
        // Parse the query string to determine the block's module and delta.
        preg_match('/block-([^-]+)-(.+)/', $block_string, $matches);
        $block = new stdClass;
        $block->module = $matches[1];
        $block->delta = $matches[2];

        $block->region = $region_name;
        $block->weight = $weight;
        if (empty($region_name)) {
          $block->status = 0;
        }
        else {
          $block->status = 1;
        }

        db_merge('block')
          ->key(array(
            'module' => $block->module,
            'delta' => $block->delta,
            'theme' => $theme_key,
          ))
          ->fields(array(
            'status' => $block->status,
            'weight' => $block->weight,
            'region' => $block->region,
            'pages' => '',
          ))
          ->execute();
      }
    }
  }
365
  drupal_exit();
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
}

/**
 * Theme the entire dashboard.
 *
 * @param $variables
 *   - element: An associative array containing the properties of the dashboard region
 *              element. Properties used: #dashboard_region, #children
 * @return
 *   A string representing the themed dashboard.
 *
 * @ingroup themeable
 */
function theme_dashboard($variables) {
  extract($variables);
  drupal_add_css(drupal_get_path('module', 'dashboard') . '/dashboard.css');
  return '<div id="dashboard">' . $element['#children'] . '</div>';
}

385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/**
 * Theme the page containing the dashboard.
 *
 * @param $variables
 *   An associative array containing:
 *   - elements: An associative array containing the properties of the element.
 *     Properties used: #message
 * @return
 *   A themed HTML string representing the non-customizable part of the
 *   dashboard page.
 *
 * @ingroup themeable
 */
function theme_dashboard_admin($variables) {
  // We only return a simple help message, since the actual content of the page
  // will be populated via the dashboard regions in dashboard_page_build().
  return '<div class="customize-dashboard js-hide">' . $variables['element']['#message'] . '</div>';
}

404
405
406
407
408
409
410
411
412
413
414
415
416
417
/**
 * Theme a generic dashboard region.
 *
 * @param $variables
 *   - element: An associative array containing the properties of the dashboard region
 *              element. Properties used: #dashboard_region, #children
 * @return
 *   A string representing the themed dashboard region.
 *
 * @ingroup themeable
 */
function theme_dashboard_region($variables) {
  extract($variables);
  $output = '<div id="' . $element['#dashboard_region'] . '" class="dashboard-region">';
418
  $output .= '<div class="region clearfix">';
419
  $output .= $element['#children'];
420
421
422
  // Closing div.region
  $output .= '</div>';
  // Closing div.dashboard-region
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
  $output .= '</div>';
  return $output;
}

/**
 * Theme a set of disabled blocks, for display in dashboard customization mode.
 *
 * @param $variables
 *   - blocks: An array of block objects from _block_rehash().
 * @return
 *   A string representing the disabled blocks region of the dashboard
 *   customization page.
 *
 * @ingroup themeable
 */
function theme_dashboard_disabled_blocks($variables) {
  extract($variables);
440
  $output = '<div class="canvas-content"><p>' . t('Drag and drop these to the columns below. Changes are automatically saved.') . '</p>';
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
  $output .= '<div id="disabled-blocks"><div class="section region disabled-blocks clearfix">';
  foreach ($blocks as $block) {
    $output .= theme('dashboard_disabled_block', array('block' => $block));
  }
  $output .= '</div></div></div>';
  return $output;
}

/**
 * Theme a disabled block, for display in dashboard customization mode.
 *
 * @param $variables
 *   - block: A block object from _block_rehash().
 * @return
 *   A string representing the disabled block.
 *
 * @ingroup themeable
 */
function theme_dashboard_disabled_block($variables) {
  extract($variables);
  $output = "";
  if (isset($block)) {
    $output .= '<div id="block-' . $block['module'] . '-' . $block['delta']
    . '" class="disabled-block block block-' . $block['module'] . '-' . $block['delta']
465
    . ' module-' . $block['module'] . ' delta-' . $block['delta'] . '">'
466
    . '<h2>' . (!empty($block['title']) && $block['title'] != '<none>' ? check_plain($block['title']) : check_plain($block['info'])) . '</h2>'
467
468
469
470
471
472
    . '<div class="content"></div>'
    . '</div>';
  }
  return $output;
}