Code coverage for /20080809/modules/block/block.admin.inc

Line #Times calledCode
1
<?php
2
// $Id: block.admin.inc,v 1.19 2008/07/16 21:59:25 dries Exp $
3
4
/**
5
 * @file
6
 * Admin page callbacks for the block module.
7
 */
8
9
/**
10
 * Menu callback for admin/build/block.
11
 */
12127
function block_admin_display($theme = NULL) {
1328
  global $custom_theme;
14
15
  // If non-default theme configuration has been selected, set the custom
theme.
1628
  $custom_theme = isset($theme) ? $theme : variable_get('theme_default',
'garland');
17
18
  // Fetch and sort blocks.
1928
  $blocks = _block_rehash();
2028
  usort($blocks, '_block_compare');
21
2228
  return drupal_get_form('block_admin_display_form', $blocks, $theme);
230
}
24
25
/**
26
 * Generate main blocks administration form.
27
 */
28127
function block_admin_display_form(&$form_state, $blocks, $theme = NULL) {
2928
  global $theme_key, $custom_theme;
30
3128
  drupal_add_css(drupal_get_path('module', 'block') . '/block.css',
'module', 'all', FALSE);
32
33
  // If non-default theme configuration has been selected, set the custom
theme.
3428
  $custom_theme = isset($theme) ? $theme : variable_get('theme_default',
'garland');
3528
  init_theme();
36
3728
  $block_regions = system_region_list($theme_key) + array(BLOCK_REGION_NONE
=> '<' . t('none') . '>');
38
39
  // Build the form tree.
40
  $form = array(
4128
    '#action' => arg(3) ? url('admin/build/block/list/' . $theme_key) :
url('admin/build/block'),
4228
    '#tree' => TRUE,
4328
  );
44
4528
  foreach ($blocks as $i => $block) {
4628
    $key = $block['module'] . '_' . $block['delta'];
4728
    $form[$key]['module'] = array(
4828
      '#type' => 'value',
4928
      '#value' => $block['module'],
50
    );
5128
    $form[$key]['delta'] = array(
5228
      '#type' => 'value',
5328
      '#value' => $block['delta'],
54
    );
5528
    $form[$key]['info'] = array(
5628
      '#markup' => check_plain($block['info']),
57
    );
5828
    $form[$key]['theme'] = array(
5928
      '#type' => 'hidden',
6028
      '#value' => $theme_key,
61
    );
6228
    $form[$key]['weight'] = array(
6328
      '#type' => 'weight',
6428
      '#default_value' => $block['weight'],
65
    );
6628
    $form[$key]['region'] = array(
6728
      '#type' => 'select',
6828
      '#default_value' => $block['region'],
6928
      '#options' => $block_regions,
70
    );
7128
    $form[$key]['configure'] = array(
7228
      '#markup' => l(t('configure'),
7328
      'admin/build/block/configure/' . $block['module'] . '/' .
$block['delta']),
74
    );
7528
    if ($block['module'] == 'block') {
764
      $form[$key]['delete'] = array(
774
        '#value' => l(t('delete'),
784
        'admin/build/block/delete/' . $block['delta']),
79
      );
804
    }
8128
  }
82
8328
  $form['submit'] = array(
8428
    '#type' => 'submit',
8528
    '#value' => t('Save blocks'),
86
  );
87
8828
  return $form;
890
}
90
91
/**
92
 * Process main blocks administration form submissions.
93
 */
94127
function block_admin_display_form_submit($form, &$form_state) {
958
  foreach ($form_state['values'] as $block) {
968
    $block['status'] = $block['region'] != BLOCK_REGION_NONE;
978
    $block['region'] = $block['status'] ? $block['region'] : '';
988
    db_query("UPDATE {blocks} SET status = %d, weight = %d, region = '%s'
WHERE module = '%s' AND delta = '%s' AND theme = '%s'", $block['status'],
$block['weight'], $block['region'], $block['module'], $block['delta'],
$block['theme']);
998
  }
1008
  drupal_set_message(t('The block settings have been updated.'));
1018
  cache_clear_all();
1028
}
103
104
/**
105
 * Helper function for sorting blocks on admin/build/block.
106
 *
107
 * Active blocks are sorted by region, then by weight.
108
 * Disabled blocks are sorted by name.
109
 */
110127
function _block_compare($a, $b) {
11128
  global $theme_key;
11228
  static $regions;
113
114
  // We need the region list to correctly order by region.
11528
  if (!isset($regions)) {
11628
    $regions = array_flip(array_keys(system_region_list($theme_key)));
11728
    $regions[BLOCK_REGION_NONE] = count($regions);
11828
  }
119
120
  // Separate enabled from disabled.
12128
  $status = $b['status'] - $a['status'];
12228
  if ($status) {
12328
    return $status;
1240
  }
125
  // Sort by region (in the order defined by theme .info file).
12628
  if ((!empty($a['region']) && !empty($b['region'])) && ($place =
($regions[$a['region']] - $regions[$b['region']]))) {
12728
    return $place;
1280
  }
129
  // Sort by weight.
13028
  $weight = $a['weight'] - $b['weight'];
13128
  if ($weight) {
1320
    return $weight;
1330
  }
134
  // Sort by title.
13528
  return strcmp($a['info'], $b['info']);
1360
}
137
138
/**
139
 * Menu callback; displays the block configuration form.
140
 */
141127
function block_admin_configure(&$form_state, $module = NULL, $delta = 0) {
1426
  $form['module'] = array(
1436
    '#type' => 'value',
1446
    '#value' => $module,
145
  );
1466
  $form['delta'] = array(
1476
    '#type' => 'value',
1486
    '#value' => $delta,
149
  );
150
1516
  $edit = db_fetch_array(db_query("SELECT pages, visibility, custom, title
FROM {blocks} WHERE module = '%s' AND delta = '%s'", $module, $delta));
152
1536
  $form['block_settings'] = array(
1546
    '#type' => 'fieldset',
1556
    '#title' => t('Block specific settings'),
1566
    '#collapsible' => TRUE,
157
  );
1586
  $form['block_settings']['title'] = array(
1596
    '#type' => 'textfield',
1606
    '#title' => t('Block title'),
1616
    '#maxlength' => 64,
1626
    '#description' => $module == 'block' ? t('The title of the block as
shown to the user.') : t('Override the default title for the block. Use
<em>&lt;none&gt;</em> to display no title, or leave blank to use the
default block title.'),
1636
    '#default_value' => $edit['title'],
1646
    '#weight' => -18,
165
  );
166
167
  // Module-specific block configurations.
1686
  if ($settings = module_invoke($module, 'block', 'configure', $delta)) {
1692
    foreach ($settings as $k => $v) {
1702
      $form['block_settings'][$k] = $v;
1712
    }
1722
  }
173
174
  // Get the block subject for the page title.
1756
  $info = module_invoke($module, 'block', 'list');
1766
  if (isset($info[$delta])) {
1774
    drupal_set_title(t("'%name' block", array('%name' =>
$info[$delta]['info'])));
1784
  }
179
180
  // Standard block configurations.
1816
  $form['user_vis_settings'] = array(
1826
    '#type' => 'fieldset',
1836
    '#title' => t('User specific visibility settings'),
1846
    '#collapsible' => TRUE,
185
  );
1866
  $form['user_vis_settings']['custom'] = array(
1876
    '#type' => 'radios',
1886
    '#title' => t('Custom visibility settings'),
189
    '#options' => array(
1906
      t('Users cannot control whether or not they see this block.'),
1916
      t('Show this block by default, but let individual users hide it.'),
1926
      t('Hide this block by default but let individual users show it.')
1936
    ),
1946
    '#description' => t('Allow individual users to customize the visibility
of this block in their account settings.'),
1956
    '#default_value' => $edit['custom'],
196
  );
197
198
  // Role-based visibility settings.
1996
  $default_role_options = array();
2006
  $result = db_query("SELECT rid FROM {blocks_roles} WHERE module = '%s'
AND delta = '%s'", $module, $delta);
2016
  while ($role = db_fetch_object($result)) {
2020
    $default_role_options[] = $role->rid;
2030
  }
2046
  $result = db_query('SELECT rid, name FROM {role} ORDER BY name');
2056
  $role_options = array();
2066
  while ($role = db_fetch_object($result)) {
2076
    $role_options[$role->rid] = $role->name;
2086
  }
2096
  $form['role_vis_settings'] = array(
2106
    '#type' => 'fieldset',
2116
    '#title' => t('Role specific visibility settings'),
2126
    '#collapsible' => TRUE,
213
  );
2146
  $form['role_vis_settings']['roles'] = array(
2156
    '#type' => 'checkboxes',
2166
    '#title' => t('Show block for specific roles'),
2176
    '#default_value' => $default_role_options,
2186
    '#options' => $role_options,
2196
    '#description' => t('Show this block only for the selected role(s). If
you select no roles, the block will be visible to all users.'),
220
  );
221
2226
  $form['page_vis_settings'] = array(
2236
    '#type' => 'fieldset',
2246
    '#title' => t('Page specific visibility settings'),
2256
    '#collapsible' => TRUE,
226
  );
227
2286
  $access = user_access('use PHP for block visibility');
2296
  if ($edit['visibility'] == 2 && !$access) {
2300
    $form['page_vis_settings'] = array();
2310
    $form['page_vis_settings']['visibility'] = array('#type' => 'value',
'#value' => 2);
2320
    $form['page_vis_settings']['pages'] = array('#type' => 'value',
'#value' => $edit['pages']);
2330
  }
234
  else {
2356
    $options = array(t('Show on every page except the listed pages.'),
t('Show on only the listed pages.'));
2366
    $description = t("Enter one page per line as Drupal paths. The '*'
character is a wildcard. Example paths are %blog for the blog page and
%blog-wildcard for every personal blog. %front is the front page.",
array('%blog' => 'blog', '%blog-wildcard' => 'blog/*', '%front' =>
'<front>'));
237
2386
    if ($access) {
2390
      $options[] = t('Show if the following PHP code returns
<code>TRUE</code> (PHP-mode, experts only).');
2400
      $description .= ' ' . t('If the PHP-mode is chosen, enter PHP code
between %php. Note that executing incorrect PHP-code can break your Drupal
site.', array('%php' => '<?php ?>'));
2410
    }
2426
    $form['page_vis_settings']['visibility'] = array(
2436
      '#type' => 'radios',
2446
      '#title' => t('Show block on specific pages'),
2456
      '#options' => $options,
2466
      '#default_value' => $edit['visibility'],
247
    );
2486
    $form['page_vis_settings']['pages'] = array(
2496
      '#type' => 'textarea',
2506
      '#title' => t('Pages'),
2516
      '#default_value' => $edit['pages'],
2526
      '#description' => $description,
253
    );
254
  }
255
2566
  $form['submit'] = array(
2576
    '#type' => 'submit',
2586
    '#value' => t('Save block'),
259
  );
260
2616
  return $form;
2620
}
263
264127
function block_admin_configure_validate($form, &$form_state) {
2652
  if ($form_state['values']['module'] == 'block') {
2660
    if (empty($form_state['values']['info']) || db_result(db_query("SELECT
COUNT(*) FROM {boxes} WHERE bid != %d AND info = '%s'",
$form_state['values']['delta'], $form_state['values']['info']))) {
2670
      form_set_error('info', t('Please ensure that each block description
is unique.'));
2680
    }
2690
  }
2702
}
271
272127
function block_admin_configure_submit($form, &$form_state) {
2732
  if (!form_get_errors()) {
2742
    db_query("UPDATE {blocks} SET visibility = %d, pages = '%s', custom =
%d, title = '%s' WHERE module = '%s' AND delta = '%s'",
$form_state['values']['visibility'], trim($form_state['values']['pages']),
$form_state['values']['custom'], $form_state['values']['title'],
$form_state['values']['module'], $form_state['values']['delta']);
2752
    db_query("DELETE FROM {blocks_roles} WHERE module = '%s' AND delta =
'%s'", $form_state['values']['module'], $form_state['values']['delta']);
2762
    foreach (array_filter($form_state['values']['roles']) as $rid) {
2770
      db_query("INSERT INTO {blocks_roles} (rid, module, delta) VALUES (%d,
'%s', '%s')", $rid, $form_state['values']['module'],
$form_state['values']['delta']);
2780
    }
2792
    module_invoke($form_state['values']['module'], 'block', 'save',
$form_state['values']['delta'], $form_state['values']);
2802
    drupal_set_message(t('The block configuration has been saved.'));
2812
    cache_clear_all();
2822
    $form_state['redirect'] = 'admin/build/block';
2832
    return;
2840
  }
2850
}
286
287
/**
288
 * Menu callback: display the custom block addition form.
289
 */
290127
function block_add_block_form(&$form_state) {
2912
  return block_admin_configure($form_state, 'block', NULL);
2920
}
293
294127
function block_add_block_form_validate($form, &$form_state) {
2951
  if (empty($form_state['values']['info']) || db_result(db_query("SELECT
COUNT(*) FROM {boxes} WHERE info = '%s'", $form_state['values']['info'])))
{
2960
    form_set_error('info', t('Please ensure that each block description is
unique.'));
2970
  }
2981
}
299
300
/**
301
 * Save the new custom block.
302
 */
303127
function block_add_block_form_submit($form, &$form_state) {
3041
  db_query("INSERT INTO {boxes} (body, info, format) VALUES ('%s', '%s',
%d)", $form_state['values']['body'], $form_state['values']['info'],
$form_state['values']['format']);
3051
  $delta = db_last_insert_id('boxes', 'bid');
306
3071
  foreach (list_themes() as $key => $theme) {
3081
    if ($theme->status) {
3091
      db_query("INSERT INTO {blocks} (visibility, pages, custom, title,
module, theme, status, weight, delta, cache) VALUES(%d, '%s', %d, '%s',
'%s', '%s', %d, %d, %d, %d)", $form_state['values']['visibility'],
trim($form_state['values']['pages']), $form_state['values']['custom'],
$form_state['values']['title'], $form_state['values']['module'],
$theme->name, 0, 0, $delta, BLOCK_NO_CACHE);
3101
    }
3111
  }
312
3131
  foreach (array_filter($form_state['values']['roles']) as $rid) {
3140
    db_query("INSERT INTO {blocks_roles} (rid, module, delta) VALUES (%d,
'%s', '%s')", $rid, $form_state['values']['module'], $delta);
3150
  }
316
3171
  drupal_set_message(t('The block has been created.'));
3181
  cache_clear_all();
319
3201
  $form_state['redirect'] = 'admin/build/block';
321
3221
  return;
3230
}
324
325
/**
326
 * Menu callback; confirm deletion of custom blocks.
327
 */
328127
function block_box_delete(&$form_state, $bid = 0) {
3292
  $box = block_box_get($bid);
3302
  $form['info'] = array('#type' => 'hidden', '#value' => $box['info'] ?
$box['info'] : $box['title']);
3312
  $form['bid'] = array('#type' => 'hidden', '#value' => $bid);
332
3332
  return confirm_form($form, t('Are you sure you want to delete the block
%name?', array('%name' => $box['info'])), 'admin/build/block', '',
t('Delete'), t('Cancel'));
3340
}
335
336
/**
337
 * Deletion of custom blocks.
338
 */
339127
function block_box_delete_submit($form, &$form_state) {
3401
  db_query('DELETE FROM {boxes} WHERE bid = %d',
$form_state['values']['bid']);
3411
  db_query("DELETE FROM {blocks} WHERE module = 'block' AND delta = %d",
$form_state['values']['bid']);
3421
  drupal_set_message(t('The block %name has been removed.', array('%name'
=> $form_state['values']['info'])));
3431
  cache_clear_all();
3441
  $form_state['redirect'] = 'admin/build/block';
3451
  return;
3460
}
347
348
/**
349
 * Process variables for block-admin-display.tpl.php.
350
 *
351
 * The $variables array contains the following arguments:
352
 * - $form
353
 *
354
 * @see block-admin-display.tpl.php
355
 * @see theme_block_admin_display()
356
 */
357127
function template_preprocess_block_admin_display_form(&$variables) {
35820
  global $theme_key;
359
36020
  $block_regions = system_region_list($theme_key);
36120
  $variables['block_regions'] = $block_regions + array(BLOCK_REGION_NONE =>
t('Disabled'));
362
36320
  foreach ($block_regions as $key => $value) {
364
    // Highlight regions on page to provide visual reference.
36520
    drupal_set_content($key, '<div class="block-region">' . $value .
'</div>');
366
    // Initialize an empty array for the region.
36720
    $variables['block_listing'][$key] = array();
36820
  }
369
370
  // Initialize disabled blocks array.
37120
  $variables['block_listing'][BLOCK_REGION_NONE] = array();
372
373
  // Set up to track previous region in loop.
37420
  $last_region = '';
37520
  foreach (element_children($variables['form']) as $i) {
37620
    $block = &$variables['form'][$i];
377
378
    // Only take form elements that are blocks.
37920
    if (isset($block['info'])) {
380
      // Fetch region for current block.
38120
      $region = $block['region']['#default_value'];
382
383
      // Set special classes needed for table drag and drop.
38420
      $variables['form'][$i]['region']['#attributes']['class'] =
'block-region-select block-region-' . $region;
38520
      $variables['form'][$i]['weight']['#attributes']['class'] =
'block-weight block-weight-' . $region;
386
38720
      $variables['block_listing'][$region][$i]->row_class =
isset($block['#attributes']['class']) ? $block['#attributes']['class'] :
'';
38820
      $variables['block_listing'][$region][$i]->block_modified =
isset($block['#attributes']['class']) &&
strpos($block['#attributes']['class'], 'block-modified') !== FALSE ? TRUE :
FALSE;
38920
      $variables['block_listing'][$region][$i]->block_title = 
drupal_render($block['info']);
39020
      $variables['block_listing'][$region][$i]->region_select =
drupal_render($block['region']) . drupal_render($block['theme']);
39120
      $variables['block_listing'][$region][$i]->weight_select =
drupal_render($block['weight']);
39220
      $variables['block_listing'][$region][$i]->configure_link =
drupal_render($block['configure']);
39320
      $variables['block_listing'][$region][$i]->delete_link =
!empty($block['delete']) ? drupal_render($block['delete']) : '';
39420
      $variables['block_listing'][$region][$i]->printed = FALSE;
395
39620
      $last_region = $region;
39720
    }
39820
  }
399
40020
  $variables['form_submit'] = drupal_render($variables['form']);
40120
}
402127