00001 <?php
00002
00003
00012 function block_admin_display($theme = NULL) {
00013 global $custom_theme;
00014
00015
00016 $custom_theme = isset($theme) ? $theme : variable_get('theme_default', 'garland');
00017
00018
00019 $blocks = _block_rehash();
00020 usort($blocks, '_block_compare');
00021
00022 return drupal_get_form('block_admin_display_form', $blocks, $theme);
00023 }
00024
00028 function block_admin_display_form(&$form_state, $blocks, $theme = NULL) {
00029 global $theme_key, $custom_theme;
00030
00031 drupal_add_css(drupal_get_path('module', 'block') . '/block.css', 'module', 'all', FALSE);
00032
00033
00034 $custom_theme = isset($theme) ? $theme : variable_get('theme_default', 'garland');
00035 init_theme();
00036
00037 $block_regions = system_region_list($theme_key) + array(BLOCK_REGION_NONE => '<' . t('none') . '>');
00038
00039
00040 $form = array(
00041 '#action' => arg(3) ? url('admin/build/block/list/' . $theme_key) : url('admin/build/block'),
00042 '#tree' => TRUE,
00043 );
00044
00045 foreach ($blocks as $i => $block) {
00046 $key = $block['module'] . '_' . $block['delta'];
00047 $form[$key]['module'] = array(
00048 '#type' => 'value',
00049 '#value' => $block['module'],
00050 );
00051 $form[$key]['delta'] = array(
00052 '#type' => 'value',
00053 '#value' => $block['delta'],
00054 );
00055 $form[$key]['info'] = array(
00056 '#value' => check_plain($block['info']),
00057 );
00058 $form[$key]['theme'] = array(
00059 '#type' => 'hidden',
00060 '#value' => $theme_key,
00061 );
00062 $form[$key]['weight'] = array(
00063 '#type' => 'weight',
00064 '#default_value' => $block['weight'],
00065 );
00066 $form[$key]['region'] = array(
00067 '#type' => 'select',
00068 '#default_value' => $block['region'],
00069 '#options' => $block_regions,
00070 );
00071 $form[$key]['configure'] = array(
00072 '#value' => l(t('configure'),
00073 'admin/build/block/configure/' . $block['module'] . '/' . $block['delta']),
00074 );
00075 if ($block['module'] == 'block') {
00076 $form[$key]['delete'] = array(
00077 '#value' => l(t('delete'),
00078 'admin/build/block/delete/' . $block['delta']),
00079 );
00080 }
00081 }
00082
00083 $form['submit'] = array(
00084 '#type' => 'submit',
00085 '#value' => t('Save blocks'),
00086 );
00087
00088 return $form;
00089 }
00090
00094 function block_admin_display_form_submit($form, &$form_state) {
00095 foreach ($form_state['values'] as $block) {
00096 $block['status'] = $block['region'] != BLOCK_REGION_NONE;
00097 $block['region'] = $block['status'] ? $block['region'] : '';
00098 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']);
00099 }
00100 drupal_set_message(t('The block settings have been updated.'));
00101 cache_clear_all();
00102 }
00103
00110 function _block_compare($a, $b) {
00111 global $theme_key;
00112 static $regions;
00113
00114
00115 if (!isset($regions)) {
00116 $regions = array_flip(array_keys(system_region_list($theme_key)));
00117 $regions[BLOCK_REGION_NONE] = count($regions);
00118 }
00119
00120
00121 $status = $b['status'] - $a['status'];
00122 if ($status) {
00123 return $status;
00124 }
00125
00126 $place = $regions[$a['region']] - $regions[$b['region']];
00127 if ($place) {
00128 return $place;
00129 }
00130
00131 $weight = $a['weight'] - $b['weight'];
00132 if ($weight) {
00133 return $weight;
00134 }
00135
00136 return strcmp($a['info'], $b['info']);
00137 }
00138
00142 function block_admin_configure(&$form_state, $module = NULL, $delta = 0) {
00143 $form['module'] = array(
00144 '#type' => 'value',
00145 '#value' => $module,
00146 );
00147 $form['delta'] = array(
00148 '#type' => 'value',
00149 '#value' => $delta,
00150 );
00151
00152 $edit = db_fetch_array(db_query("SELECT pages, visibility, custom, title FROM {blocks} WHERE module = '%s' AND delta = '%s'", $module, $delta));
00153
00154 $form['block_settings'] = array(
00155 '#type' => 'fieldset',
00156 '#title' => t('Block specific settings'),
00157 '#collapsible' => TRUE,
00158 );
00159 $form['block_settings']['title'] = array(
00160 '#type' => 'textfield',
00161 '#title' => t('Block title'),
00162 '#maxlength' => 64,
00163 '#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.'),
00164 '#default_value' => $edit['title'],
00165 '#weight' => -18,
00166 );
00167
00168
00169 if ($settings = module_invoke($module, 'block', 'configure', $delta)) {
00170 foreach ($settings as $k => $v) {
00171 $form['block_settings'][$k] = $v;
00172 }
00173 }
00174
00175
00176 $info = module_invoke($module, 'block', 'list');
00177 if (isset($info[$delta])) {
00178 drupal_set_title(t("'%name' block", array('%name' => $info[$delta]['info'])));
00179 }
00180
00181
00182 $form['user_vis_settings'] = array(
00183 '#type' => 'fieldset',
00184 '#title' => t('User specific visibility settings'),
00185 '#collapsible' => TRUE,
00186 );
00187 $form['user_vis_settings']['custom'] = array(
00188 '#type' => 'radios',
00189 '#title' => t('Custom visibility settings'),
00190 '#options' => array(
00191 t('Users cannot control whether or not they see this block.'),
00192 t('Show this block by default, but let individual users hide it.'),
00193 t('Hide this block by default but let individual users show it.')
00194 ),
00195 '#description' => t('Allow individual users to customize the visibility of this block in their account settings.'),
00196 '#default_value' => $edit['custom'],
00197 );
00198
00199
00200 $default_role_options = array();
00201 $result = db_query("SELECT rid FROM {blocks_roles} WHERE module = '%s' AND delta = '%s'", $module, $delta);
00202 while ($role = db_fetch_object($result)) {
00203 $default_role_options[] = $role->rid;
00204 }
00205 $result = db_query('SELECT rid, name FROM {role} ORDER BY name');
00206 $role_options = array();
00207 while ($role = db_fetch_object($result)) {
00208 $role_options[$role->rid] = $role->name;
00209 }
00210 $form['role_vis_settings'] = array(
00211 '#type' => 'fieldset',
00212 '#title' => t('Role specific visibility settings'),
00213 '#collapsible' => TRUE,
00214 );
00215 $form['role_vis_settings']['roles'] = array(
00216 '#type' => 'checkboxes',
00217 '#title' => t('Show block for specific roles'),
00218 '#default_value' => $default_role_options,
00219 '#options' => $role_options,
00220 '#description' => t('Show this block only for the selected role(s). If you select no roles, the block will be visible to all users.'),
00221 );
00222
00223 $form['page_vis_settings'] = array(
00224 '#type' => 'fieldset',
00225 '#title' => t('Page specific visibility settings'),
00226 '#collapsible' => TRUE,
00227 );
00228
00229 $access = user_access('use PHP for block visibility');
00230 if ($edit['visibility'] == 2 && !$access) {
00231 $form['page_vis_settings'] = array();
00232 $form['page_vis_settings']['visibility'] = array('#type' => 'value', '#value' => 2);
00233 $form['page_vis_settings']['pages'] = array('#type' => 'value', '#value' => $edit['pages']);
00234 }
00235 else {
00236 $options = array(t('Show on every page except the listed pages.'), t('Show on only the listed pages.'));
00237 $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>'));
00238
00239 if ($access) {
00240 $options[] = t('Show if the following PHP code returns <code>TRUE</code> (PHP-mode, experts only).');
00241 $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 ?>'));
00242 }
00243 $form['page_vis_settings']['visibility'] = array(
00244 '#type' => 'radios',
00245 '#title' => t('Show block on specific pages'),
00246 '#options' => $options,
00247 '#default_value' => $edit['visibility'],
00248 );
00249 $form['page_vis_settings']['pages'] = array(
00250 '#type' => 'textarea',
00251 '#title' => t('Pages'),
00252 '#default_value' => $edit['pages'],
00253 '#description' => $description,
00254 );
00255 }
00256
00257 $form['submit'] = array(
00258 '#type' => 'submit',
00259 '#value' => t('Save block'),
00260 );
00261
00262 return $form;
00263 }
00264
00265 function block_admin_configure_validate($form, &$form_state) {
00266 if ($form_state['values']['module'] == 'block') {
00267 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']))) {
00268 form_set_error('info', t('Please ensure that each block description is unique.'));
00269 }
00270 }
00271 }
00272
00273 function block_admin_configure_submit($form, &$form_state) {
00274 if (!form_get_errors()) {
00275 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']);
00276 db_query("DELETE FROM {blocks_roles} WHERE module = '%s' AND delta = '%s'", $form_state['values']['module'], $form_state['values']['delta']);
00277 foreach (array_filter($form_state['values']['roles']) as $rid) {
00278 db_query("INSERT INTO {blocks_roles} (rid, module, delta) VALUES (%d, '%s', '%s')", $rid, $form_state['values']['module'], $form_state['values']['delta']);
00279 }
00280 module_invoke($form_state['values']['module'], 'block', 'save', $form_state['values']['delta'], $form_state['values']);
00281 drupal_set_message(t('The block configuration has been saved.'));
00282 cache_clear_all();
00283 $form_state['redirect'] = 'admin/build/block';
00284 return;
00285 }
00286 }
00287
00291 function block_add_block_form(&$form_state) {
00292 return block_admin_configure($form_state, 'block', NULL);
00293 }
00294
00295 function block_add_block_form_validate($form, &$form_state) {
00296 if (empty($form_state['values']['info']) || db_result(db_query("SELECT COUNT(*) FROM {boxes} WHERE info = '%s'", $form_state['values']['info']))) {
00297 form_set_error('info', t('Please ensure that each block description is unique.'));
00298 }
00299 }
00300
00304 function block_add_block_form_submit($form, &$form_state) {
00305 db_query("INSERT INTO {boxes} (body, info, format) VALUES ('%s', '%s', %d)", $form_state['values']['body'], $form_state['values']['info'], $form_state['values']['format']);
00306 $delta = db_last_insert_id('boxes', 'bid');
00307
00308 foreach (list_themes() as $key => $theme) {
00309 if ($theme->status) {
00310 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);
00311 }
00312 }
00313
00314 foreach (array_filter($form_state['values']['roles']) as $rid) {
00315 db_query("INSERT INTO {blocks_roles} (rid, module, delta) VALUES (%d, '%s', '%s')", $rid, $form_state['values']['module'], $delta);
00316 }
00317
00318 drupal_set_message(t('The block has been created.'));
00319 cache_clear_all();
00320
00321 $form_state['redirect'] = 'admin/build/block';
00322
00323 return;
00324 }
00325
00329 function block_box_delete(&$form_state, $bid = 0) {
00330 $box = block_box_get($bid);
00331 $form['info'] = array('#type' => 'hidden', '#value' => $box['info'] ? $box['info'] : $box['title']);
00332 $form['bid'] = array('#type' => 'hidden', '#value' => $bid);
00333
00334 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'));
00335 }
00336
00340 function block_box_delete_submit($form, &$form_state) {
00341 db_query('DELETE FROM {boxes} WHERE bid = %d', $form_state['values']['bid']);
00342 db_query("DELETE FROM {blocks} WHERE module = 'block' AND delta = %d", $form_state['values']['bid']);
00343 drupal_set_message(t('The block %name has been removed.', array('%name' => $form_state['values']['info'])));
00344 cache_clear_all();
00345 $form_state['redirect'] = 'admin/build/block';
00346 return;
00347 }
00348
00358 function template_preprocess_block_admin_display_form(&$variables) {
00359 global $theme_key;
00360
00361 $block_regions = system_region_list($theme_key);
00362 $variables['block_regions'] = $block_regions + array(BLOCK_REGION_NONE => t('Disabled'));
00363
00364 foreach ($block_regions as $key => $value) {
00365
00366 drupal_set_content($key, '<div class="block-region">' . $value . '</div>');
00367
00368 $variables['block_listing'][$key] = array();
00369 }
00370
00371
00372 $variables['block_listing'][BLOCK_REGION_NONE] = array();
00373
00374
00375 $last_region = '';
00376 foreach (element_children($variables['form']) as $i) {
00377 $block = &$variables['form'][$i];
00378
00379
00380 if (isset($block['info'])) {
00381
00382 $region = $block['region']['#default_value'];
00383
00384
00385 $variables['form'][$i]['region']['#attributes']['class'] = 'block-region-select block-region-' . $region;
00386 $variables['form'][$i]['weight']['#attributes']['class'] = 'block-weight block-weight-' . $region;
00387
00388 $variables['block_listing'][$region][$i]->row_class = isset($block['#attributes']['class']) ? $block['#attributes']['class'] : '';
00389 $variables['block_listing'][$region][$i]->block_modified = isset($block['#attributes']['class']) && strpos($block['#attributes']['class'], 'block-modified') !== FALSE ? TRUE : FALSE;
00390 $variables['block_listing'][$region][$i]->block_title = drupal_render($block['info']);
00391 $variables['block_listing'][$region][$i]->region_select = drupal_render($block['region']) . drupal_render($block['theme']);
00392 $variables['block_listing'][$region][$i]->weight_select = drupal_render($block['weight']);
00393 $variables['block_listing'][$region][$i]->configure_link = drupal_render($block['configure']);
00394 $variables['block_listing'][$region][$i]->delete_link = !empty($block['delete']) ? drupal_render($block['delete']) : '';
00395 $variables['block_listing'][$region][$i]->printed = FALSE;
00396
00397 $last_region = $region;
00398 }
00399 }
00400
00401 $variables['form_submit'] = drupal_render($variables['form']);
00402 }