2
// $Id: block.admin.inc,v 1.14.2.5 2008/11/24 06:00:02 dries Exp $
6
* Admin page callbacks for the block module.
10
* Menu callback for admin/build/block.
12
function block_admin_display($theme = NULL) {
15
// If non-default theme configuration has been selected, set the custom theme.
16
$custom_theme = isset($theme) ? $theme : variable_get('theme_default', 'garland');
18
// Fetch and sort blocks
19
$blocks = _block_rehash();
20
usort($blocks, '_block_compare');
22
return drupal_get_form('block_admin_display_form', $blocks, $theme);
26
* Generate main blocks administration form.
28
function block_admin_display_form(&$form_state, $blocks, $theme = NULL) {
29
global $theme_key, $custom_theme;
32
drupal_add_css(drupal_get_path('module', 'block') .'/block.css', 'module', 'all', FALSE);
34
// If non-default theme configuration has been selected, set the custom theme.
35
$custom_theme = isset($theme) ? $theme : variable_get('theme_default', 'garland');
38
$throttle = module_exists('throttle');
39
$block_regions = system_region_list($theme_key) + array(BLOCK_REGION_NONE => '<'. t('none') .'>');
41
// Weights range from -delta to +delta, so delta should be at least half
42
// of the amount of blocks present. This makes sure all blocks in the same
43
// region get an unique weight.
44
$weight_delta = round(count($blocks) / 2);
48
'#action' => arg(4) ? url('admin/build/block/list/'. $theme_key) : url('admin/build/block'),
52
foreach ($blocks as $i => $block) {
53
$key = $block['module'] .'_'. $block['delta'];
54
$form[$key]['module'] = array(
56
'#value' => $block['module'],
58
$form[$key]['delta'] = array(
60
'#value' => $block['delta'],
62
$form[$key]['info'] = array(
63
'#value' => check_plain($block['info'])
65
$form[$key]['theme'] = array(
67
'#value' => $theme_key
69
$form[$key]['weight'] = array(
71
'#default_value' => $block['weight'],
72
'#delta' => $weight_delta,
74
$form[$key]['region'] = array(
76
'#default_value' => $block['region'],
77
'#options' => $block_regions,
81
$form[$key]['throttle'] = array('#type' => 'checkbox', '#default_value' => isset($block['throttle']) ? $block['throttle'] : FALSE);
83
$form[$key]['configure'] = array('#value' => l(t('configure'), 'admin/build/block/configure/'. $block['module'] .'/'. $block['delta']));
84
if ($block['module'] == 'block') {
85
$form[$key]['delete'] = array('#value' => l(t('delete'), 'admin/build/block/delete/'. $block['delta']));
89
$form['submit'] = array(
91
'#value' => t('Save blocks'),
98
* Process main blocks administration form submission.
100
function block_admin_display_form_submit($form, &$form_state) {
101
foreach ($form_state['values'] as $block) {
102
$block['status'] = $block['region'] != BLOCK_REGION_NONE;
103
$block['region'] = $block['status'] ? $block['region'] : '';
104
db_query("UPDATE {blocks} SET status = %d, weight = %d, region = '%s', throttle = %d WHERE module = '%s' AND delta = '%s' AND theme = '%s'", $block['status'], $block['weight'], $block['region'], isset($block['throttle']) ? $block['throttle'] : 0, $block['module'], $block['delta'], $block['theme']);
106
drupal_set_message(t('The block settings have been updated.'));
111
* Helper function for sorting blocks on admin/build/block.
113
* Active blocks are sorted by region, then by weight.
114
* Disabled blocks are sorted by name.
116
function _block_compare($a, $b) {
120
// We need the region list to correctly order by region.
121
if (!isset($regions)) {
122
$regions = array_flip(array_keys(system_region_list($theme_key)));
123
$regions[BLOCK_REGION_NONE] = count($regions);
126
// Separate enabled from disabled.
127
$status = $b['status'] - $a['status'];
131
// Sort by region (in the order defined by theme .info file).
132
if ((!empty($a['region']) && !empty($b['region'])) && ($place = ($regions[$a['region']] - $regions[$b['region']]))) {
136
$weight = $a['weight'] - $b['weight'];
141
return strcmp($a['info'], $b['info']);
145
* Menu callback; displays the block configuration form.
147
function block_admin_configure(&$form_state, $module = NULL, $delta = 0) {
149
$form['module'] = array('#type' => 'value', '#value' => $module);
150
$form['delta'] = array('#type' => 'value', '#value' => $delta);
152
$edit = db_fetch_array(db_query("SELECT pages, visibility, custom, title FROM {blocks} WHERE module = '%s' AND delta = '%s'", $module, $delta));
154
$form['block_settings'] = array(
155
'#type' => 'fieldset',
156
'#title' => t('Block specific settings'),
157
'#collapsible' => TRUE,
159
$form['block_settings']['title'] = array(
160
'#type' => 'textfield',
161
'#title' => t('Block title'),
163
'#description' => $module == 'block' ? t('The title of the block as shown to the user.') : t('Override the default title for the block. Use <em><none></em> to display no title, or leave blank to use the default block title.'),
164
'#default_value' => $edit['title'],
169
// Module-specific block configurations.
170
if ($settings = module_invoke($module, 'block', 'configure', $delta)) {
171
foreach ($settings as $k => $v) {
172
$form['block_settings'][$k] = $v;
176
// Get the block subject for the page title.
177
$info = module_invoke($module, 'block', 'list');
178
if (isset($info[$delta])) {
179
drupal_set_title(t("'%name' block", array('%name' => $info[$delta]['info'])));
182
// Standard block configurations.
183
$form['user_vis_settings'] = array(
184
'#type' => 'fieldset',
185
'#title' => t('User specific visibility settings'),
186
'#collapsible' => TRUE,
188
$form['user_vis_settings']['custom'] = array(
190
'#title' => t('Custom visibility settings'),
192
t('Users cannot control whether or not they see this block.'),
193
t('Show this block by default, but let individual users hide it.'),
194
t('Hide this block by default but let individual users show it.')
196
'#description' => t('Allow individual users to customize the visibility of this block in their account settings.'),
197
'#default_value' => $edit['custom'],
200
// Role-based visibility settings
201
$default_role_options = array();
202
$result = db_query("SELECT rid FROM {blocks_roles} WHERE module = '%s' AND delta = '%s'", $module, $delta);
203
while ($role = db_fetch_object($result)) {
204
$default_role_options[] = $role->rid;
206
$result = db_query('SELECT rid, name FROM {role} ORDER BY name');
207
$role_options = array();
208
while ($role = db_fetch_object($result)) {
209
$role_options[$role->rid] = $role->name;
211
$form['role_vis_settings'] = array(
212
'#type' => 'fieldset',
213
'#title' => t('Role specific visibility settings'),
214
'#collapsible' => TRUE,
216
$form['role_vis_settings']['roles'] = array(
217
'#type' => 'checkboxes',
218
'#title' => t('Show block for specific roles'),
219
'#default_value' => $default_role_options,
220
'#options' => $role_options,
221
'#description' => t('Show this block only for the selected role(s). If you select no roles, the block will be visible to all users.'),
224
$form['page_vis_settings'] = array(
225
'#type' => 'fieldset',
226
'#title' => t('Page specific visibility settings'),
227
'#collapsible' => TRUE,
229
$access = user_access('use PHP for block visibility');
231
if ($edit['visibility'] == 2 && !$access) {
232
$form['page_vis_settings'] = array();
233
$form['page_vis_settings']['visibility'] = array('#type' => 'value', '#value' => 2);
234
$form['page_vis_settings']['pages'] = array('#type' => 'value', '#value' => $edit['pages']);
237
$options = array(t('Show on every page except the listed pages.'), t('Show on only the listed pages.'));
238
$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>'));
241
$options[] = t('Show if the following PHP code returns <code>TRUE</code> (PHP-mode, experts only).');
242
$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
$form['page_vis_settings']['visibility'] = array(
246
'#title' => t('Show block on specific pages'),
247
'#options' => $options,
248
'#default_value' => $edit['visibility'],
250
$form['page_vis_settings']['pages'] = array(
251
'#type' => 'textarea',
252
'#title' => t('Pages'),
253
'#default_value' => $edit['pages'],
254
'#description' => $description,
258
$form['submit'] = array(
260
'#value' => t('Save block'),
266
function block_admin_configure_validate($form, &$form_state) {
267
if ($form_state['values']['module'] == 'block') {
268
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']))) {
269
form_set_error('info', t('Please ensure that each block description is unique.'));
274
function block_admin_configure_submit($form, &$form_state) {
275
if (!form_get_errors()) {
276
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']);
277
db_query("DELETE FROM {blocks_roles} WHERE module = '%s' AND delta = '%s'", $form_state['values']['module'], $form_state['values']['delta']);
278
foreach (array_filter($form_state['values']['roles']) as $rid) {
279
db_query("INSERT INTO {blocks_roles} (rid, module, delta) VALUES (%d, '%s', '%s')", $rid, $form_state['values']['module'], $form_state['values']['delta']);
281
module_invoke($form_state['values']['module'], 'block', 'save', $form_state['values']['delta'], $form_state['values']);
282
drupal_set_message(t('The block configuration has been saved.'));
284
$form_state['redirect'] = 'admin/build/block';
290
* Menu callback: display the custom block addition form.
292
function block_add_block_form(&$form_state) {
293
return block_admin_configure($form_state, 'block', NULL);
296
function block_add_block_form_validate($form, &$form_state) {
297
if (empty($form_state['values']['info']) || db_result(db_query("SELECT COUNT(*) FROM {boxes} WHERE info = '%s'", $form_state['values']['info']))) {
298
form_set_error('info', t('Please ensure that each block description is unique.'));
303
* Save the new custom block.
305
function block_add_block_form_submit($form, &$form_state) {
306
db_query("INSERT INTO {boxes} (body, info, format) VALUES ('%s', '%s', %d)", $form_state['values']['body'], $form_state['values']['info'], $form_state['values']['format']);
307
$delta = db_last_insert_id('boxes', 'bid');
309
foreach (list_themes() as $key => $theme) {
310
if ($theme->status) {
311
db_query("INSERT INTO {blocks} (visibility, pages, custom, title, module, theme, status, weight, delta, cache) VALUES(%d, '%s', %d, '%s', '%s', '%s', %d, %d, '%s', %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);
315
foreach (array_filter($form_state['values']['roles']) as $rid) {
316
db_query("INSERT INTO {blocks_roles} (rid, module, delta) VALUES (%d, '%s', '%s')", $rid, $form_state['values']['module'], $delta);
319
drupal_set_message(t('The block has been created.'));
322
$form_state['redirect'] = 'admin/build/block';
327
* Menu callback; confirm deletion of custom blocks.
329
function block_box_delete(&$form_state, $bid = 0) {
330
$box = block_box_get($bid);
331
$form['info'] = array('#type' => 'hidden', '#value' => $box['info']);
332
$form['bid'] = array('#type' => 'hidden', '#value' => $bid);
334
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'));
338
* Deletion of custom blocks.
340
function block_box_delete_submit($form, &$form_state) {
341
db_query('DELETE FROM {boxes} WHERE bid = %d', $form_state['values']['bid']);
342
db_query("DELETE FROM {blocks} WHERE module = 'block' AND delta = '%s'", $form_state['values']['bid']);
343
drupal_set_message(t('The block %name has been removed.', array('%name' => $form_state['values']['info'])));
345
$form_state['redirect'] = 'admin/build/block';
350
* Process variables for block-admin-display.tpl.php.
352
* The $variables array contains the following arguments:
355
* @see block-admin-display.tpl.php
356
* @see theme_block_admin_display()
358
function template_preprocess_block_admin_display_form(&$variables) {
361
$block_regions = system_region_list($theme_key);
362
$variables['throttle'] = module_exists('throttle');
363
$variables['block_regions'] = $block_regions + array(BLOCK_REGION_NONE => t('Disabled'));
365
foreach ($block_regions as $key => $value) {
366
// Highlight regions on page to provide visual reference.
367
drupal_set_content($key, '<div class="block-region">'. $value .'</div>');
368
// Initialize an empty array for the region.
369
$variables['block_listing'][$key] = array();
372
// Initialize disabled blocks array.
373
$variables['block_listing'][BLOCK_REGION_NONE] = array();
375
// Set up to track previous region in loop.
377
foreach (element_children($variables['form']) as $i) {
378
$block = &$variables['form'][$i];
380
// Only take form elements that are blocks.
381
if (isset($block['info'])) {
382
// Fetch region for current block.
383
$region = $block['region']['#default_value'];
385
// Set special classes needed for table drag and drop.
386
$variables['form'][$i]['region']['#attributes']['class'] = 'block-region-select block-region-'. $region;
387
$variables['form'][$i]['weight']['#attributes']['class'] = 'block-weight block-weight-'. $region;
389
$variables['block_listing'][$region][$i]->row_class = isset($block['#attributes']['class']) ? $block['#attributes']['class'] : '';
390
$variables['block_listing'][$region][$i]->block_modified = isset($block['#attributes']['class']) && strpos($block['#attributes']['class'], 'block-modified') !== FALSE ? TRUE : FALSE;
391
$variables['block_listing'][$region][$i]->block_title = drupal_render($block['info']);
392
$variables['block_listing'][$region][$i]->region_select = drupal_render($block['region']) . drupal_render($block['theme']);
393
$variables['block_listing'][$region][$i]->weight_select = drupal_render($block['weight']);
394
$variables['block_listing'][$region][$i]->throttle_check = $variables['throttle'] ? drupal_render($block['throttle']) : '';
395
$variables['block_listing'][$region][$i]->configure_link = drupal_render($block['configure']);
396
$variables['block_listing'][$region][$i]->delete_link = !empty($block['delete']) ? drupal_render($block['delete']) : '';
397
$variables['block_listing'][$region][$i]->printed = FALSE;
399
$last_region = $region;
403
$variables['form_submit'] = drupal_render($variables['form']);