/[drupal]/drupal/modules/block/block.admin.inc
ViewVC logotype

Contents of /drupal/modules/block/block.admin.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


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

  ViewVC Help
Powered by ViewVC 1.1.2