Simpletest Coverage - modules/block/block.admin.inc

1 <?php
2 // $Id: block.admin.inc,v 1.48 2009/08/12 11:43:09 dries Exp $
3
4 /**
5 * @file
6 * Admin page callbacks for the block module.
7 */
8
9 /**
10 * Implement hook_form_FORM_ID_alter().
11 */
12 function block_form_system_performance_settings_alter(&$form, &$form_state) {
13 $disabled = count(module_implements('node_grants'));
14 $form['caching']['block_cache'] = array(
15 '#type' => 'checkbox',
16 '#title' => t('Cache blocks'),
17 '#default_value' => variable_get('block_cache', FALSE),
18 '#disabled' => $disabled,
19 '#description' => $disabled ? t('Block caching is inactive because you have enabled modules defining content access restrictions.') : NULL,
20 '#weight' => -1,
21 );
22
23 // Check if the "Who's online" block is enabled.
24 $online_block_enabled = db_query_range("SELECT 1 FROM {block} b WHERE module = 'user' AND delta = 'online' AND status = 1", array(), 0, 1)->fetchField();
25
26 // If the "Who's online" block is enabled, append some descriptive text to
27 // the end of the form description.
28 if ($online_block_enabled) {
29 $form['page_cache']['cache']['#description'] .= '<p>' . t('When caching is enabled, anonymous user sessions are only saved to the database when needed, so the "Who\'s online" block does not display the number of anonymous users.') . '</p>';
30 }
31 }
32
33 /**
34 * Menu callback for admin/structure/block.
35 */
36 function block_admin_display($theme = NULL) {
37 global $custom_theme;
38
39 // If non-default theme configuration has been selected, set the custom theme.
40 $custom_theme = isset($theme) ? $theme : variable_get('theme_default', 'garland');
41
42 // Fetch and sort blocks.
43 $blocks = _block_rehash();
44 usort($blocks, '_block_compare');
45
46 return drupal_get_form('block_admin_display_form', $blocks, $theme);
47 }
48
49 /**
50 * Generate main blocks administration form.
51 */
52 function block_admin_display_form(&$form_state, $blocks, $theme = NULL) {
53 global $theme_key, $custom_theme;
54
55 drupal_add_css(drupal_get_path('module', 'block') . '/block.css', array('preprocess' => FALSE));
56
57 // If non-default theme configuration has been selected, set the custom theme.
58 $custom_theme = isset($theme) ? $theme : variable_get('theme_default', 'garland');
59 drupal_theme_initialize();
60
61 $block_regions = system_region_list($theme_key, REGIONS_VISIBLE) + array(BLOCK_REGION_NONE => '<' . t('none') . '>');
62
63 // Weights range from -delta to +delta, so delta should be at least half
64 // of the amount of blocks present. This makes sure all blocks in the same
65 // region get an unique weight.
66 $weight_delta = round(count($blocks) / 2);
67
68 // Build the form tree.
69 $form = array(
70 '#action' => arg(4) ? url('admin/structure/block/list/' . $theme_key) : url('admin/structure/block'),
71 '#tree' => TRUE,
72 );
73
74 foreach ($blocks as $i => $block) {
75 $key = $block['module'] . '_' . $block['delta'];
76 $form[$key]['module'] = array(
77 '#type' => 'value',
78 '#value' => $block['module'],
79 );
80 $form[$key]['delta'] = array(
81 '#type' => 'value',
82 '#value' => $block['delta'],
83 );
84 $form[$key]['info'] = array(
85 '#markup' => check_plain($block['info']),
86 );
87 $form[$key]['theme'] = array(
88 '#type' => 'hidden',
89 '#value' => $theme_key,
90 );
91 $form[$key]['weight'] = array(
92 '#type' => 'weight',
93 '#default_value' => $block['weight'],
94 '#delta' => $weight_delta,
95 );
96 $form[$key]['region'] = array(
97 '#type' => 'select',
98 '#default_value' => $block['region'],
99 '#options' => $block_regions,
100 );
101 $form[$key]['configure'] = array(
102 '#markup' => l(t('configure'),
103 'admin/structure/block/configure/' . $block['module'] . '/' . $block['delta']),
104 );
105 if ($block['module'] == 'block') {
106 $form[$key]['delete'] = array(
107 '#markup' => l(t('delete'),
108 'admin/structure/block/delete/' . $block['delta']),
109 );
110 }
111 }
112 // Do not allow disabling the main system content block.
113 unset($form['system_main']['region']['#options'][BLOCK_REGION_NONE]);
114
115 $form['submit'] = array(
116 '#type' => 'submit',
117 '#value' => t('Save blocks'),
118 );
119
120 return $form;
121 }
122
123 /**
124 * Process main blocks administration form submissions.
125 */
126 function block_admin_display_form_submit($form, &$form_state) {
127 foreach ($form_state['values'] as $block) {
128 $block['status'] = (int) ($block['region'] != BLOCK_REGION_NONE);
129 $block['region'] = $block['status'] ? $block['region'] : '';
130 db_update('block')
131 ->fields(array(
132 'status' => $block['status'],
133 'weight' => $block['weight'],
134 'region' => $block['region'],
135 ))
136 ->condition('module', $block['module'])
137 ->condition('delta', $block['delta'])
138 ->condition('theme', $block['theme'])
139 ->execute();
140 }
141 drupal_set_message(t('The block settings have been updated.'));
142 cache_clear_all();
143 }
144
145 /**
146 * Helper function for sorting blocks on admin/structure/block.
147 *
148 * Active blocks are sorted by region, then by weight.
149 * Disabled blocks are sorted by name.
150 */
151 function _block_compare($a, $b) {
152 global $theme_key;
153 $regions = &drupal_static(__FUNCTION__);
154
155 // We need the region list to correctly order by region.
156 if (!isset($regions)) {
157 $regions = array_flip(array_keys(system_region_list($theme_key)));
158 $regions[BLOCK_REGION_NONE] = count($regions);
159 }
160
161 // Separate enabled from disabled.
162 $status = $b['status'] - $a['status'];
163 if ($status) {
164 return $status;
165 }
166 // Sort by region (in the order defined by theme .info file).
167 if ((!empty($a['region']) && !empty($b['region'])) && ($place = ($regions[$a['region']] - $regions[$b['region']]))) {
168 return $place;
169 }
170 // Sort by weight.
171 $weight = $a['weight'] - $b['weight'];
172 if ($weight) {
173 return $weight;
174 }
175 // Sort by title.
176 return strcmp($a['info'], $b['info']);
177 }
178
179 /**
180 * Menu callback; displays the block configuration form.
181 */
182 function block_admin_configure(&$form_state, $module = NULL, $delta = 0) {
183 $form['module'] = array(
184 '#type' => 'value',
185 '#value' => $module,
186 );
187 $form['delta'] = array(
188 '#type' => 'value',
189 '#value' => $delta,
190 );
191
192 $edit = db_query("SELECT pages, visibility, custom, title FROM {block} WHERE module = :module AND delta = :delta", array(
193 ':module' => $module,
194 ':delta' => $delta,
195 ))->fetchAssoc();
196
197 $form['block_settings'] = array(
198 '#type' => 'fieldset',
199 '#title' => t('Block specific settings'),
200 '#collapsible' => TRUE,
201 );
202 $form['block_settings']['title'] = array(
203 '#type' => 'textfield',
204 '#title' => t('Block title'),
205 '#maxlength' => 64,
206 '#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.'),
207 '#default_value' => $edit['title'],
208 '#weight' => -18,
209 );
210
211 // Module-specific block configurations.
212 if ($settings = module_invoke($module, 'block_configure', $delta)) {
213 foreach ($settings as $k => $v) {
214 $form['block_settings'][$k] = $v;
215 }
216 }
217
218 // Get the block subject for the page title.
219 $info = module_invoke($module, 'block_list');
220 if (isset($info[$delta])) {
221 drupal_set_title(t("'%name' block", array('%name' => $info[$delta]['info'])), PASS_THROUGH);
222 }
223
224 $form['page_vis_settings'] = array(
225 '#type' => 'fieldset',
226 '#title' => t('Page specific visibility settings'),
227 '#collapsible' => TRUE,
228 '#collapsed' => TRUE,
229 );
230
231 $access = user_access('use PHP for settings');
232 if ($edit['visibility'] == 2 && !$access) {
233 $form['page_vis_settings'] = array();
234 $form['page_vis_settings']['visibility'] = array('#type' => 'value', '#value' => 2);
235 $form['page_vis_settings']['pages'] = array('#type' => 'value', '#value' => $edit['pages']);
236 }
237 else {
238 $options = array(t('Every page except those specified below.'), t('Only the pages specified below.'));
239 $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>'));
240
241 if (module_exists('php') && $access) {
242 $options[] = t('Show if the following PHP code returns <code>TRUE</code> (PHP-mode, experts only).');
243 $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 ?>'));
244 }
245 $form['page_vis_settings']['visibility'] = array(
246 '#type' => 'radios',
247 '#title' => t('Show block on specific pages'),
248 '#options' => $options,
249 '#default_value' => $edit['visibility'],
250 );
251 $form['page_vis_settings']['pages'] = array(
252 '#type' => 'textarea',
253 '#title' => t('Pages'),
254 '#default_value' => $edit['pages'],
255 '#description' => $description,
256 );
257 }
258
259 // Role-based visibility settings.
260 $default_role_options = db_query("SELECT rid FROM {block_role} WHERE module = :module AND delta = :delta", array(
261 ':module' => $module,
262 ':delta' => $delta,
263 ))->fetchCol();
264 $role_options = db_query('SELECT rid, name FROM {role} ORDER BY name')->fetchAllKeyed();
265 $form['role_vis_settings'] = array(
266 '#type' => 'fieldset',
267 '#title' => t('Role specific visibility settings'),
268 '#collapsible' => TRUE,
269 '#collapsed' => TRUE,
270 );
271 $form['role_vis_settings']['roles'] = array(
272 '#type' => 'checkboxes',
273 '#title' => t('Show block for specific roles'),
274 '#default_value' => $default_role_options,
275 '#options' => $role_options,
276 '#description' => t('Show this block only for the selected role(s). If you select no roles, the block will be visible to all users.'),
277 );
278
279 // Content type specific configuration.
280 $default_type_options = db_query("SELECT type FROM {block_node_type} WHERE module = :module AND delta = :delta", array(
281 ':module' => $module,
282 ':delta' => $delta,
283 ))->fetchCol();
284 $form['content_type_vis_settings'] = array(
285 '#type' => 'fieldset',
286 '#title' => t('Content type specific visibility settings'),
287 '#collapsible' => TRUE,
288 '#collapsed' => TRUE,
289 );
290 $form['content_type_vis_settings']['types'] = array(
291 '#type' => 'checkboxes',
292 '#title' => t('Show block for specific content types'),
293 '#default_value' => $default_type_options,
294 '#options' => node_type_get_names(),
295 '#description' => t('Show this block only when on a page displaying a post of the given type(s). If you select no types, there will be no type specific limitation.'),
296 );
297
298 // Standard block configurations.
299 $form['user_vis_settings'] = array(
300 '#type' => 'fieldset',
301 '#title' => t('User specific visibility settings'),
302 '#collapsible' => TRUE,
303 '#collapsed' => TRUE,
304 );
305 $form['user_vis_settings']['custom'] = array(
306 '#type' => 'radios',
307 '#title' => t('Custom visibility settings'),
308 '#options' => array(
309 t('Users cannot control whether or not they see this block.'),
310 t('Show this block by default, but let individual users hide it.'),
311 t('Hide this block by default but let individual users show it.')
312 ),
313 '#description' => t('Allow individual users to customize the visibility of this block in their account settings.'),
314 '#default_value' => $edit['custom'],
315 );
316
317 $form['submit'] = array(
318 '#type' => 'submit',
319 '#value' => t('Save block'),
320 );
321
322 return $form;
323 }
324
325 function block_admin_configure_validate($form, &$form_state) {
326 if ($form_state['values']['module'] == 'block') {
327 $box_exists = (bool) db_query_range('SELECT 1 FROM {box} WHERE bid <> :bid AND info = :info', array(
328 ':bid' => $form_state['values']['delta'],
329 ':info' => $form_state['values']['info'],
330 ), 0, 1)->fetchField();
331 if (empty($form_state['values']['info']) || $box_exists) {
332 form_set_error('info', t('Please ensure that each block description is unique.'));
333 }
334 }
335 }
336
337 function block_admin_configure_submit($form, &$form_state) {
338 if (!form_get_errors()) {
339 db_update('block')
340 ->fields(array(
341 'visibility' => (int) $form_state['values']['visibility'],
342 'pages' => trim($form_state['values']['pages']),
343 'custom' => (int) $form_state['values']['custom'],
344 'title' => $form_state['values']['title'],
345 ))
346 ->condition('module', $form_state['values']['module'])
347 ->condition('delta', $form_state['values']['delta'])
348 ->execute();
349
350 db_delete('block_role')
351 ->condition('module', $form_state['values']['module'])
352 ->condition('delta', $form_state['values']['delta'])
353 ->execute();
354 $query = db_insert('block_role')->fields(array('rid', 'module', 'delta'));
355 foreach (array_filter($form_state['values']['roles']) as $rid) {
356 $query->values(array(
357 'rid' => $rid,
358 'module' => $form_state['values']['module'],
359 'delta' => $form_state['values']['delta'],
360 ));
361 }
362 $query->execute();
363
364 db_delete('block_node_type')
365 ->condition('module', $form_state['values']['module'])
366 ->condition('delta', $form_state['values']['delta'])
367 ->execute();
368 $query = db_insert('block_node_type')->fields(array('type', 'module', 'delta'));
369 foreach (array_filter($form_state['values']['types']) as $type) {
370 $query->values(array(
371 'type' => $type,
372 'module' => $form_state['values']['module'],
373 'delta' => $form_state['values']['delta'],
374 ));
375 }
376 $query->execute();
377
378 module_invoke($form_state['values']['module'], 'block_save', $form_state['values']['delta'], $form_state['values']);
379 drupal_set_message(t('The block configuration has been saved.'));
380 cache_clear_all();
381 $form_state['redirect'] = 'admin/structure/block';
382 }
383 }
384
385 /**
386 * Menu callback: display the custom block addition form.
387 */
388 function block_add_block_form(&$form_state) {
389 return block_admin_configure($form_state, 'block', NULL);
390 }
391
392 function block_add_block_form_validate($form, &$form_state) {
393 $box_exists = (bool) db_query_range('SELECT 1 FROM {box} WHERE info = :info', array(':info' => $form_state['values']['info']), 0, 1)->fetchField();
394
395 if (empty($form_state['values']['info']) || $box_exists) {
396 form_set_error('info', t('Please ensure that each block description is unique.'));
397 }
398 }
399
400 /**
401 * Save the new custom block.
402 */
403 function block_add_block_form_submit($form, &$form_state) {
404 $delta = db_insert('box')
405 ->fields(array(
406 'body' => $form_state['values']['body'],
407 'info' => $form_state['values']['info'],
408 'format' => $form_state['values']['body_format'],
409 ))
410 ->execute();
411
412 $query = db_insert('block')->fields(array('visibility', 'pages', 'custom', 'title', 'module', 'theme', 'status', 'weight', 'delta', 'cache'));
413 foreach (list_themes() as $key => $theme) {
414 if ($theme->status) {
415 $query->values(array(
416 'visibility' => (int) $form_state['values']['visibility'],
417 'pages' => trim($form_state['values']['pages']),
418 'custom' => (int) $form_state['values']['custom'],
419 'title' => $form_state['values']['title'],
420 'module' => $form_state['values']['module'],
421 'theme' => $theme->name,
422 'status' => 0,
423 'weight' => 0,
424 'delta' => $delta,
425 'cache' => BLOCK_NO_CACHE,
426 ));
427 }
428 }
429 $query->execute();
430
431 $query = db_insert('block_role')->fields(array('rid', 'module', 'delta'));
432 foreach (array_filter($form_state['values']['roles']) as $rid) {
433 $query->values(array(
434 'rid' => $rid,
435 'module' => $form_state['values']['module'],
436 'delta' => $delta,
437 ));
438 }
439 $query->execute();
440
441 $query = db_insert('block_node_type')->fields(array('type', 'module', 'delta'));
442 foreach (array_filter($form_state['values']['types']) as $type) {
443 $query->values(array(
444 'type' => $type,
445 'module' => $form_state['values']['module'],
446 'delta' => $delta,
447 ));
448 }
449 $query->execute();
450
451 drupal_set_message(t('The block has been created.'));
452 cache_clear_all();
453 $form_state['redirect'] = 'admin/structure/block';
454 }
455
456 /**
457 * Menu callback; confirm deletion of custom blocks.
458 */
459 function block_box_delete(&$form_state, $bid = 0) {
460 $box = block_box_get($bid);
461 $form['info'] = array('#type' => 'hidden', '#value' => $box['info'] ? $box['info'] : $box['title']);
462 $form['bid'] = array('#type' => 'hidden', '#value' => $bid);
463
464 return confirm_form($form, t('Are you sure you want to delete the block %name?', array('%name' => $box['info'])), 'admin/structure/block', '', t('Delete'), t('Cancel'));
465 }
466
467 /**
468 * Deletion of custom blocks.
469 */
470 function block_box_delete_submit($form, &$form_state) {
471 db_delete('box')
472 ->condition('bid', $form_state['values']['bid'])
473 ->execute();
474 db_delete('block')
475 ->condition('module', 'block')
476 ->condition('delta', $form_state['values']['bid'])
477 ->execute();
478 drupal_set_message(t('The block %name has been removed.', array('%name' => $form_state['values']['info'])));
479 cache_clear_all();
480 $form_state['redirect'] = 'admin/structure/block';
481 return;
482 }
483
484 /**
485 * Process variables for block-admin-display.tpl.php.
486 *
487 * The $variables array contains the following arguments:
488 * - $form
489 *
490 * @see block-admin-display.tpl.php
491 * @see theme_block_admin_display()
492 */
493 function template_preprocess_block_admin_display_form(&$variables) {
494 global $theme_key;
495
496 $block_regions = system_region_list($theme_key, REGIONS_VISIBLE);
497 $variables['block_regions'] = $block_regions + array(BLOCK_REGION_NONE => t('Disabled'));
498
499 foreach ($block_regions as $key => $value) {
500 // Initialize an empty array for the region.
501 $variables['block_listing'][$key] = array();
502 }
503
504 // Initialize disabled blocks array.
505 $variables['block_listing'][BLOCK_REGION_NONE] = array();
506
507 // Set up to track previous region in loop.
508 $last_region = '';
509 foreach (element_children($variables['form']) as $i) {
510 $block = &$variables['form'][$i];
511
512 // Only take form elements that are blocks.
513 if (isset($block['info'])) {
514 // Fetch region for current block.
515 $region = $block['region']['#default_value'];
516
517 // Set special classes needed for table drag and drop.
518 $variables['form'][$i]['region']['#attributes']['class'] = 'block-region-select block-region-' . $region;
519 $variables['form'][$i]['weight']['#attributes']['class'] = 'block-weight block-weight-' . $region;
520
521 $variables['block_listing'][$region][$i] = new stdClass();
522 $variables['block_listing'][$region][$i]->row_class = isset($block['#attributes']['class']) ? $block['#attributes']['class'] : '';
523 $variables['block_listing'][$region][$i]->block_modified = isset($block['#attributes']['class']) && strpos($block['#attributes']['class'], 'block-modified') !== FALSE;
524 $variables['block_listing'][$region][$i]->block_title = drupal_render($block['info']);
525 $variables['block_listing'][$region][$i]->region_select = drupal_render($block['region']) . drupal_render($block['theme']);
526 $variables['block_listing'][$region][$i]->weight_select = drupal_render($block['weight']);
527 $variables['block_listing'][$region][$i]->configure_link = drupal_render($block['configure']);
528 $variables['block_listing'][$region][$i]->delete_link = !empty($block['delete']) ? drupal_render($block['delete']) : '';
529 $variables['block_listing'][$region][$i]->printed = FALSE;
530
531 $last_region = $region;
532 }
533 }
534
535 $variables['form_submit'] = drupal_render_children($variables['form']);
536 }
537

Legend

Missed
lines code that were not excersized during program execution.
Covered
lines code were excersized during program execution.
Comment/non executable
Comment or non-executable line of code.
Dead
lines of code that according to xdebug could not be executed. This is counted as coverage code because in almost all cases it is code that runnable.