comparison modules/block/block.admin.inc @ 1:c1f4ac30525a 6.0

Drupal 6.0
author Franck Deroche <webmaster@defr.org>
date Tue, 23 Dec 2008 14:28:28 +0100
parents
children 165d43f946a8
comparison
equal deleted inserted replaced
0:5a113a1c4740 1:c1f4ac30525a
1 <?php
2 // $Id: block.admin.inc,v 1.14 2007/12/22 23:24:24 goba Exp $
3
4 /**
5 * @file
6 * Admin page callbacks for the block module.
7 */
8
9 /**
10 * Menu callback for admin/build/block.
11 */
12 function block_admin_display($theme = NULL) {
13 global $custom_theme;
14
15 // If non-default theme configuration has been selected, set the custom theme.
16 $custom_theme = isset($theme) ? $theme : variable_get('theme_default', 'garland');
17
18 // Fetch and sort blocks
19 $blocks = _block_rehash();
20 usort($blocks, '_block_compare');
21
22 return drupal_get_form('block_admin_display_form', $blocks, $theme);
23 }
24
25 /**
26 * Generate main blocks administration form.
27 */
28 function block_admin_display_form(&$form_state, $blocks, $theme = NULL) {
29 global $theme_key, $custom_theme;
30
31 // Add CSS
32 drupal_add_css(drupal_get_path('module', 'block') .'/block.css', 'module', 'all', FALSE);
33
34 // If non-default theme configuration has been selected, set the custom theme.
35 $custom_theme = isset($theme) ? $theme : variable_get('theme_default', 'garland');
36 init_theme();
37
38 $throttle = module_exists('throttle');
39 $block_regions = system_region_list($theme_key) + array(BLOCK_REGION_NONE => '<'. t('none') .'>');
40
41 // Build form tree
42 $form = array(
43 '#action' => arg(3) ? url('admin/build/block/list/'. $theme_key) : url('admin/build/block'),
44 '#tree' => TRUE,
45 );
46
47 foreach ($blocks as $i => $block) {
48 $key = $block['module'] .'_'. $block['delta'];
49 $form[$key]['module'] = array(
50 '#type' => 'value',
51 '#value' => $block['module'],
52 );
53 $form[$key]['delta'] = array(
54 '#type' => 'value',
55 '#value' => $block['delta'],
56 );
57 $form[$key]['info'] = array(
58 '#value' => check_plain($block['info'])
59 );
60 $form[$key]['theme'] = array(
61 '#type' => 'hidden',
62 '#value' => $theme_key
63 );
64 $form[$key]['weight'] = array(
65 '#type' => 'weight',
66 '#default_value' => $block['weight'],
67 );
68 $form[$key]['region'] = array(
69 '#type' => 'select',
70 '#default_value' => $block['region'],
71 '#options' => $block_regions,
72 );
73
74 if ($throttle) {
75 $form[$key]['throttle'] = array('#type' => 'checkbox', '#default_value' => isset($block['throttle']) ? $block['throttle'] : FALSE);
76 }
77 $form[$key]['configure'] = array('#value' => l(t('configure'), 'admin/build/block/configure/'. $block['module'] .'/'. $block['delta']));
78 if ($block['module'] == 'block') {
79 $form[$key]['delete'] = array('#value' => l(t('delete'), 'admin/build/block/delete/'. $block['delta']));
80 }
81 }
82
83 $form['submit'] = array(
84 '#type' => 'submit',
85 '#value' => t('Save blocks'),
86 );
87
88 return $form;
89 }
90
91 /**
92 * Process main blocks administration form submission.
93 */
94 function block_admin_display_form_submit($form, &$form_state) {
95 foreach ($form_state['values'] as $block) {
96 $block['status'] = $block['region'] != BLOCK_REGION_NONE;
97 $block['region'] = $block['status'] ? $block['region'] : '';
98 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']);
99 }
100 drupal_set_message(t('The block settings have been updated.'));
101 cache_clear_all();
102 }
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 */
110 function _block_compare($a, $b) {
111 global $theme_key;
112 static $regions;
113
114 // We need the region list to correctly order by region.
115 if (!isset($regions)) {
116 $regions = array_flip(array_keys(system_region_list($theme_key)));
117 $regions[BLOCK_REGION_NONE] = count($regions);
118 }
119
120 // Separate enabled from disabled.
121 $status = $b['status'] - $a['status'];
122 if ($status) {
123 return $status;
124 }
125 // Sort by region (in the order defined by theme .info file).
126 $place = $regions[$a['region']] - $regions[$b['region']];
127 if ($place) {
128 return $place;
129 }
130 // Sort by weight.
131 $weight = $a['weight'] - $b['weight'];
132 if ($weight) {
133 return $weight;
134 }
135 // Sort by title.
136 return strcmp($a['info'], $b['info']);
137 }
138
139 /**
140 * Menu callback; displays the block configuration form.
141 */
142 function block_admin_configure(&$form_state, $module = NULL, $delta = 0) {
143
144 $form['module'] = array('#type' => 'value', '#value' => $module);
145 $form['delta'] = array('#type' => 'value', '#value' => $delta);
146
147 $edit = db_fetch_array(db_query("SELECT pages, visibility, custom, title FROM {blocks} WHERE module = '%s' AND delta = '%s'", $module, $delta));
148
149 $form['block_settings'] = array(
150 '#type' => 'fieldset',
151 '#title' => t('Block specific settings'),
152 '#collapsible' => TRUE,
153 );
154 $form['block_settings']['title'] = array(
155 '#type' => 'textfield',
156 '#title' => t('Block title'),
157 '#maxlength' => 64,
158 '#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.'),
159 '#default_value' => $edit['title'],
160 '#weight' => -18,
161 );
162
163
164 // Module-specific block configurations.
165 if ($settings = module_invoke($module, 'block', 'configure', $delta)) {
166 foreach ($settings as $k => $v) {
167 $form['block_settings'][$k] = $v;
168 }
169 }
170
171 // Get the block subject for the page title.
172 $info = module_invoke($module, 'block', 'list');
173 if (isset($info[$delta])) {
174 drupal_set_title(t("'%name' block", array('%name' => $info[$delta]['info'])));
175 }
176
177 // Standard block configurations.
178 $form['user_vis_settings'] = array(
179 '#type' => 'fieldset',
180 '#title' => t('User specific visibility settings'),
181 '#collapsible' => TRUE,
182 );
183 $form['user_vis_settings']['custom'] = array(
184 '#type' => 'radios',
185 '#title' => t('Custom visibility settings'),
186 '#options' => array(
187 t('Users cannot control whether or not they see this block.'),
188 t('Show this block by default, but let individual users hide it.'),
189 t('Hide this block by default but let individual users show it.')
190 ),
191 '#description' => t('Allow individual users to customize the visibility of this block in their account settings.'),
192 '#default_value' => $edit['custom'],
193 );
194
195 // Role-based visibility settings
196 $default_role_options = array();
197 $result = db_query("SELECT rid FROM {blocks_roles} WHERE module = '%s' AND delta = '%s'", $module, $delta);
198 while ($role = db_fetch_object($result)) {
199 $default_role_options[] = $role->rid;
200 }
201 $result = db_query('SELECT rid, name FROM {role} ORDER BY name');
202 $role_options = array();
203 while ($role = db_fetch_object($result)) {
204 $role_options[$role->rid] = $role->name;
205 }
206 $form['role_vis_settings'] = array(
207 '#type' => 'fieldset',
208 '#title' => t('Role specific visibility settings'),
209 '#collapsible' => TRUE,
210 );
211 $form['role_vis_settings']['roles'] = array(
212 '#type' => 'checkboxes',
213 '#title' => t('Show block for specific roles'),
214 '#default_value' => $default_role_options,
215 '#options' => $role_options,
216 '#description' => t('Show this block only for the selected role(s). If you select no roles, the block will be visible to all users.'),
217 );
218
219 $form['page_vis_settings'] = array(
220 '#type' => 'fieldset',
221 '#title' => t('Page specific visibility settings'),
222 '#collapsible' => TRUE,
223 );
224 $access = user_access('use PHP for block visibility');
225
226 if ($edit['visibility'] == 2 && !$access) {
227 $form['page_vis_settings'] = array();
228 $form['page_vis_settings']['visibility'] = array('#type' => 'value', '#value' => 2);
229 $form['page_vis_settings']['pages'] = array('#type' => 'value', '#value' => $edit['pages']);
230 }
231 else {
232 $options = array(t('Show on every page except the listed pages.'), t('Show on only the listed pages.'));
233 $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>'));
234
235 if ($access) {
236 $options[] = t('Show if the following PHP code returns <code>TRUE</code> (PHP-mode, experts only).');
237 $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 ?>'));
238 }
239 $form['page_vis_settings']['visibility'] = array(
240 '#type' => 'radios',
241 '#title' => t('Show block on specific pages'),
242 '#options' => $options,
243 '#default_value' => $edit['visibility'],
244 );
245 $form['page_vis_settings']['pages'] = array(
246 '#type' => 'textarea',
247 '#title' => t('Pages'),
248 '#default_value' => $edit['pages'],
249 '#description' => $description,
250 );
251 }
252
253 $form['submit'] = array(
254 '#type' => 'submit',
255 '#value' => t('Save block'),
256 );
257
258 return $form;
259 }
260
261 function block_admin_configure_validate($form, &$form_state) {
262 if ($form_state['values']['module'] == 'block') {
263 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']))) {
264 form_set_error('info', t('Please ensure that each block description is unique.'));
265 }
266 }
267 }
268
269 function block_admin_configure_submit($form, &$form_state) {
270 if (!form_get_errors()) {
271 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']);
272 db_query("DELETE FROM {blocks_roles} WHERE module = '%s' AND delta = '%s'", $form_state['values']['module'], $form_state['values']['delta']);
273 foreach (array_filter($form_state['values']['roles']) as $rid) {
274 db_query("INSERT INTO {blocks_roles} (rid, module, delta) VALUES (%d, '%s', '%s')", $rid, $form_state['values']['module'], $form_state['values']['delta']);
275 }
276 module_invoke($form_state['values']['module'], 'block', 'save', $form_state['values']['delta'], $form_state['values']);
277 drupal_set_message(t('The block configuration has been saved.'));
278 cache_clear_all();
279 $form_state['redirect'] = 'admin/build/block';
280 return;
281 }
282 }
283
284 /**
285 * Menu callback: display the custom block addition form.
286 */
287 function block_add_block_form(&$form_state) {
288 return block_admin_configure($form_state, 'block', NULL);
289 }
290
291 function block_add_block_form_validate($form, &$form_state) {
292 if (empty($form_state['values']['info']) || db_result(db_query("SELECT COUNT(*) FROM {boxes} WHERE info = '%s'", $form_state['values']['info']))) {
293 form_set_error('info', t('Please ensure that each block description is unique.'));
294 }
295 }
296
297 /**
298 * Save the new custom block.
299 */
300 function block_add_block_form_submit($form, &$form_state) {
301 db_query("INSERT INTO {boxes} (body, info, format) VALUES ('%s', '%s', %d)", $form_state['values']['body'], $form_state['values']['info'], $form_state['values']['format']);
302 $delta = db_last_insert_id('boxes', 'bid');
303
304 foreach (list_themes() as $key => $theme) {
305 if ($theme->status) {
306 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);
307 }
308 }
309
310 foreach (array_filter($form_state['values']['roles']) as $rid) {
311 db_query("INSERT INTO {blocks_roles} (rid, module, delta) VALUES (%d, '%s', '%s')", $rid, $form_state['values']['module'], $delta);
312 }
313
314 drupal_set_message(t('The block has been created.'));
315 cache_clear_all();
316
317 $form_state['redirect'] = 'admin/build/block';
318 return;
319 }
320
321 /**
322 * Menu callback; confirm deletion of custom blocks.
323 */
324 function block_box_delete(&$form_state, $bid = 0) {
325 $box = block_box_get($bid);
326 $form['info'] = array('#type' => 'hidden', '#value' => $box['info'] ? $box['info'] : $box['title']);
327 $form['bid'] = array('#type' => 'hidden', '#value' => $bid);
328
329 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'));
330 }
331
332 /**
333 * Deletion of custom blocks.
334 */
335 function block_box_delete_submit($form, &$form_state) {
336 db_query('DELETE FROM {boxes} WHERE bid = %d', $form_state['values']['bid']);
337 db_query("DELETE FROM {blocks} WHERE module = 'block' AND delta = %d", $form_state['values']['bid']);
338 drupal_set_message(t('The block %name has been removed.', array('%name' => $form_state['values']['info'])));
339 cache_clear_all();
340 $form_state['redirect'] = 'admin/build/block';
341 return;
342 }
343
344 /**
345 * Process variables for block-admin-display.tpl.php.
346 *
347 * The $variables array contains the following arguments:
348 * - $form
349 *
350 * @see block-admin-display.tpl.php
351 * @see theme_block_admin_display()
352 */
353 function template_preprocess_block_admin_display_form(&$variables) {
354 global $theme_key;
355
356 $block_regions = system_region_list($theme_key);
357 $variables['throttle'] = module_exists('throttle');
358 $variables['block_regions'] = $block_regions + array(BLOCK_REGION_NONE => t('Disabled'));
359
360 foreach ($block_regions as $key => $value) {
361 // Highlight regions on page to provide visual reference.
362 drupal_set_content($key, '<div class="block-region">'. $value .'</div>');
363 // Initialize an empty array for the region.
364 $variables['block_listing'][$key] = array();
365 }
366
367 // Initialize disabled blocks array.
368 $variables['block_listing'][BLOCK_REGION_NONE] = array();
369
370 // Set up to track previous region in loop.
371 $last_region = '';
372 foreach (element_children($variables['form']) as $i) {
373 $block = &$variables['form'][$i];
374
375 // Only take form elements that are blocks.
376 if (isset($block['info'])) {
377 // Fetch region for current block.
378 $region = $block['region']['#default_value'];
379
380 // Set special classes needed for table drag and drop.
381 $variables['form'][$i]['region']['#attributes']['class'] = 'block-region-select block-region-'. $region;
382 $variables['form'][$i]['weight']['#attributes']['class'] = 'block-weight block-weight-'. $region;
383
384 $variables['block_listing'][$region][$i]->row_class = isset($block['#attributes']['class']) ? $block['#attributes']['class'] : '';
385 $variables['block_listing'][$region][$i]->block_modified = isset($block['#attributes']['class']) && strpos($block['#attributes']['class'], 'block-modified') !== FALSE ? TRUE : FALSE;
386 $variables['block_listing'][$region][$i]->block_title = drupal_render($block['info']);
387 $variables['block_listing'][$region][$i]->region_select = drupal_render($block['region']) . drupal_render($block['theme']);
388 $variables['block_listing'][$region][$i]->weight_select = drupal_render($block['weight']);
389 $variables['block_listing'][$region][$i]->throttle_check = $variables['throttle'] ? drupal_render($block['throttle']) : '';
390 $variables['block_listing'][$region][$i]->configure_link = drupal_render($block['configure']);
391 $variables['block_listing'][$region][$i]->delete_link = !empty($block['delete']) ? drupal_render($block['delete']) : '';
392 $variables['block_listing'][$region][$i]->printed = FALSE;
393
394 $last_region = $region;
395 }
396 }
397
398 $variables['form_submit'] = drupal_render($variables['form']);
399 }