/[drupal]/drupal/modules/block.module
ViewVC logotype

Contents of /drupal/modules/block.module

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


Revision 1.206.2.3 - (hide annotations) (download) (as text)
Sun Aug 20 11:32:00 2006 UTC (3 years, 3 months ago) by killes
Branch: DRUPAL-4-7
Changes since 1.206.2.2: +12 -5 lines
File MIME type: text/x-php
#77924 by RobRoy: fixed race condition in block administration that might have caused data loss. Backport from HEAD
1 dries 1.8 <?php
2 killes 1.206.2.3 // $Id: block.module,v 1.206.2.2 2006/07/02 20:53:52 killes Exp $
3 dries 1.132
4     /**
5     * @file
6     * Controls the boxes that are displayed around the main content.
7     */
8 dries 1.69
9 dries 1.112 /**
10     * Implementation of hook_help().
11     */
12     function block_help($section) {
13 dries 1.69 switch ($section) {
14 dries 1.82 case 'admin/help#block':
15 killes 1.206.2.1 $output = '<p>'. t('Blocks are boxes of content that may be rendered into certain regions of your web pages, for example, into sidebars. Blocks are usually generated automatically by modules (e.g., Recent Forum Topics), but administrators can also define custom blocks.') .'</p>';
16     $output .= '<p>'. t('The region each block appears in depends on both which theme you are using (some themes allow greater control over block placement than others), and on the settings in the block administration section.') .'</p>';
17     $output .= '<p>'. t('The block administration screen lets you specify the vertical placement of the blocks within a region. You do this by assigning a weight to each block. Lighter blocks (those having a smaller weight) "float up" towards the top of the region; heavier ones "sink".') .'</p>';
18 dries 1.185 $output .= t('<p>A block\'s visibility depends on:</p>
19 dries 1.140 <ul>
20     <li>Its enabled checkbox. Disabled blocks are never shown.</li>
21     <li>Its throttle checkbox. Throttled blocks are hidden during high server loads.</li>
22 killes 1.206.2.1 <li>Its page visibility settings. Blocks can be configured to be visible/hidden on certain pages.</li>
23     <li>Its custom visibility settings. Blocks can be configured to be visible only when specific conditions are true.</li>
24     <li>Its user visibility settings. Administrators can choose to let users decide whether to show/hide certain blocks.</li>
25     <li>Its function. Some dynamic blocks, such as those generated by modules, will be displayed only on certain pages.</li>
26 dries 1.140 </ul>
27 dries 1.185 ');
28     $output .= '<h3>'. t('Module blocks') .'</h3>';
29 killes 1.206.2.1 $output .= '<p>'. t('Some modules generate blocks that become available when the modules are enabled. These blocks can be administered via the <a href="%admin-block">blocks administration page</a>.</p>', array('%admin-block' => url('admin/block'))) .'</p>';
30 dries 1.185 $output .= '<h3>'. t('Administrator defined blocks') .'</h3>';
31 killes 1.206.2.1 $output .= '<p>'. t('Administrators can also define custom blocks. These blocks consist of a title, a description, and a body which can be as long as you wish. Block content can be in any of the input formats supported for other content.') .'</p>';
32 dries 1.185 $output .= t('<p>You can</p>
33     <ul>
34 killes 1.206.2.1 <li>enable, throttle and configure blocks at <a href="%admin-block">administer &gt;&gt; blocks</a>.</li>
35     <li>add an administrator-defined block at <a href="%admin-block-add">administer &gt;&gt; blocks &gt;&gt; add block</a>.</li>
36 dries 1.185 </ul>
37     ', array('%admin-block' => url('admin/block'), '%admin-block-add' => url('admin/block/add')));
38 dries 1.202 $output .= '<p>'. t('For more information please read the configuration and customization handbook <a href="%block">Block page</a>.', array('%block' => 'http://drupal.org/handbook/modules/block/')) .'</p>';
39 dries 1.185 return $output;
40 dries 1.116 case 'admin/modules#description':
41 dries 1.112 return t('Controls the boxes that are displayed around the main content.');
42 dries 1.116 case 'admin/block':
43 dries 1.140 return t("
44 killes 1.206.2.1 <p>Blocks are boxes of content that may be rendered into certain regions of your web pages, for example, into sidebars. They are usually generated automatically by modules, but administrators can create blocks manually.</p>
45     <p>Only enabled blocks are shown. You can position blocks by specifying which area of the page they should appear in (e.g., a sidebar). Highlighted labels on this page show the regions into which blocks can be rendered. You can specify where within a region a block will appear by adjusting its weight.</p>
46 dries 1.168 <p>If you want certain blocks to disable themselves temporarily during high server loads, check the 'Throttle' box. You can configure the auto-throttle on the <a href=\"%throttle\">throttle configuration page</a> after having enabled the throttle module.</p>
47 killes 1.206.2.1 <p>You can configure the behaviour of each block (for example, specifying on which pages and for what users it will appear) by clicking the 'configure' link for each block.</p>
48 dries 1.140 ", array('%throttle' => url('admin/settings/throttle')));
49 dries 1.116 case 'admin/block/add':
50 unconed 1.160 return t('<p>Here you can create a new block. Once you have created this block you must make it active and give it a place on the page using <a href="%overview">blocks</a>. The title is used when displaying the block. The description is used in the "block" column on the <a href="%overview">blocks</a> page.</p>', array('%overview' => url('admin/block')));
51 dries 1.69 }
52 dries 1.86 }
53    
54 dries 1.112 /**
55     * Implementation of hook_perm().
56     */
57 dries 1.13 function block_perm() {
58 unconed 1.194 return array('administer blocks', 'use PHP for block visibility');
59 dries 1.15 }
60    
61 dries 1.111 /**
62 dries 1.116 * Implementation of hook_menu().
63 dries 1.111 */
64 dries 1.135 function block_menu($may_cache) {
65 dries 1.116 $items = array();
66 dries 1.135
67     if ($may_cache) {
68     $items[] = array('path' => 'admin/block', 'title' => t('blocks'),
69     'access' => user_access('administer blocks'),
70 dries 1.203 'callback' => 'block_admin_display');
71 dries 1.135 $items[] = array('path' => 'admin/block/list', 'title' => t('list'),
72     'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10);
73 dries 1.140 $items[] = array('path' => 'admin/block/configure', 'title' => t('configure block'),
74     'access' => user_access('administer blocks'),
75     'callback' => 'block_admin_configure',
76     'type' => MENU_CALLBACK);
77     $items[] = array('path' => 'admin/block/delete', 'title' => t('delete block'),
78 dries 1.135 'access' => user_access('administer blocks'),
79 dries 1.140 'callback' => 'block_box_delete',
80 dries 1.135 'type' => MENU_CALLBACK);
81 dries 1.143 $items[] = array('path' => 'admin/block/add', 'title' => t('add block'),
82 dries 1.135 'access' => user_access('administer blocks'),
83 dries 1.140 'callback' => 'block_box_add',
84 dries 1.135 'type' => MENU_LOCAL_TASK);
85 dries 1.177 foreach (list_themes() as $key => $theme) {
86     if ($theme->status) {
87     if ($key == variable_get('theme_default', 'bluemarine')) {
88     $items[] = array('path' => 'admin/block/list/' . $key, 'title' => t('%key settings', array('%key' => $key)),
89     'access' => user_access('administer blocks'), 'type' => MENU_DEFAULT_LOCAL_TASK, 'weight' => -10);
90     }
91     else {
92     $items[] = array('path' => 'admin/block/list/' . $key, 'title' => t('%key settings', array('%key' => $key)),
93     'access' => user_access('administer blocks'), 'type' => MENU_LOCAL_TASK);
94     }
95     }
96     }
97 dries 1.135 }
98    
99 dries 1.116 return $items;
100 dries 1.13 }
101    
102 dries 1.112 /**
103     * Implementation of hook_block().
104     *
105     * Generates the administrator-defined blocks for display.
106     */
107 dries 1.140 function block_block($op = 'list', $delta = 0, $edit = array()) {
108     switch ($op) {
109     case 'list':
110 dries 1.184 $blocks = array();
111    
112 dries 1.140 $result = db_query('SELECT bid, title, info FROM {boxes} ORDER BY title');
113     while ($block = db_fetch_object($result)) {
114 unconed 1.161 $blocks[$block->bid]['info'] = $block->info ? check_plain($block->info) : check_plain($block->title);
115 dries 1.140 }
116     return $blocks;
117    
118     case 'configure':
119     $box = block_box_get($delta);
120     if (filter_access($box['format'])) {
121     return block_box_form($box);
122     }
123     break;
124    
125     case 'save':
126     block_box_save($edit, $delta);
127     break;
128    
129     case 'view':
130     $block = db_fetch_object(db_query('SELECT * FROM {boxes} WHERE bid = %d', $delta));
131 unconed 1.161 $data['subject'] = check_plain($block->title);
132 unconed 1.175 $data['content'] = check_markup($block->body, $block->format, FALSE);
133 dries 1.140 return $data;
134 kjartan 1.25 }
135     }
136    
137 dries 1.39 /**
138 dries 1.112 * Update the 'blocks' DB table with the blocks currently exported by modules.
139 dries 1.40 *
140 dries 1.112 * @return
141 unconed 1.204 * Blocks currently exported by modules.
142 dries 1.39 */
143 unconed 1.204 function _block_rehash() {
144 dries 1.177 global $theme_key;
145    
146 dries 1.193 init_theme();
147 dries 1.177
148     $result = db_query("SELECT * FROM {blocks} WHERE theme = '%s'", $theme_key);
149 dries 1.38 while ($old_block = db_fetch_object($result)) {
150     $old_blocks[$old_block->module][$old_block->delta] = $old_block;
151     }
152    
153 killes 1.206.2.3 $blocks = array();
154 dries 1.38
155     foreach (module_list() as $module) {
156 dries 1.112 $module_blocks = module_invoke($module, 'block', 'list');
157 dries 1.38 if ($module_blocks) {
158     foreach ($module_blocks as $delta => $block) {
159 dries 1.112 $block['module'] = $module;
160     $block['delta'] = $delta;
161 dries 1.177 // If previously written to database, load values.
162 dries 1.38 if ($old_blocks[$module][$delta]) {
163 dries 1.112 $block['status'] = $old_blocks[$module][$delta]->status;
164     $block['weight'] = $old_blocks[$module][$delta]->weight;
165     $block['region'] = $old_blocks[$module][$delta]->region;
166 dries 1.140 $block['visibility'] = $old_blocks[$module][$delta]->visibility;
167     $block['pages'] = $old_blocks[$module][$delta]->pages;
168 dries 1.112 $block['custom'] = $old_blocks[$module][$delta]->custom;
169     $block['throttle'] = $old_blocks[$module][$delta]->throttle;
170 dries 1.38 }
171 dries 1.177 // Otherwise, use any set values, or else substitute defaults.
172 dries 1.38 else {
173 unconed 1.204 $properties = array('status' => 0, 'weight' => 0, 'region' => 'left', 'pages' => '', 'custom' => 0);
174 dries 1.177 foreach ($properties as $property => $default) {
175 unconed 1.204 if (!isset($block[$property])) {
176 dries 1.177 $block[$property] = $default;
177     }
178     }
179 dries 1.38 }
180    
181     $blocks[] = $block;
182     }
183     }
184     }
185    
186 killes 1.206.2.3 db_lock_table('blocks');
187     // Remove all blocks from table.
188     db_query("DELETE FROM {blocks} WHERE theme = '%s'", $theme_key);
189    
190     // Reinsert new set of blocks into table.
191     foreach ($blocks as $block) {
192     db_query("INSERT INTO {blocks} (module, delta, theme, status, weight, region, visibility, pages, custom, throttle) VALUES ('%s', '%s', '%s', %d, %d, '%s', %d, '%s', %d, %d)", $block['module'], $block['delta'], $theme_key, $block['status'], $block['weight'], $block['region'], $block['visibility'], $block['pages'], $block['custom'], $block['throttle']);
193     }
194     db_unlock_tables();
195    
196 dries 1.38 return $blocks;
197     }
198 dries 1.5
199 dries 1.112 /**
200 dries 1.203 * Generate main block administration form.
201 dries 1.112 */
202 dries 1.38 function block_admin_display() {
203 dries 1.188 global $theme_key, $custom_theme;
204 dries 1.181
205 dries 1.188 // If non-default theme configuration has been selected, set the custom theme.
206     if (arg(3)) {
207     $custom_theme = arg(3);
208     }
209     else {
210     $custom_theme = variable_get('theme_default', 'bluemarine');
211     }
212     init_theme();
213    
214 dries 1.205 // Fetch and sort blocks
215 unconed 1.204 $blocks = _block_rehash();
216     usort($blocks, '_block_compare');
217    
218 dries 1.203 $throttle = module_exist('throttle');
219 dries 1.181 $block_regions = system_region_list($theme_key);
220    
221 unconed 1.204 // Build form tree
222 dries 1.183 $form['#action'] = arg(3) ? url('admin/block/list/' . $theme_key) : url('admin/block');
223     $form['#tree'] = TRUE;
224 unconed 1.204 foreach ($blocks as $i => $block) {
225     $form[$i]['module'] = array('#type' => 'value', '#value' => $block['module']);
226     $form[$i]['delta'] = array('#type' => 'value', '#value' => $block['delta']);
227     $form[$i]['info'] = array('#value' => $block['info']);
228     $form[$i]['status'] = array('#type' => 'checkbox', '#default_value' => $block['status']);
229     $form[$i]['theme'] = array('#type' => 'hidden', '#value' => $theme_key);
230     $form[$i]['weight'] = array('#type' => 'weight', '#default_value' => $block['weight']);
231     $form[$i]['region'] = array('#type' => 'select',
232 killes 1.206.2.2 '#default_value' => isset($block['region']) ? $block['region'] : system_default_region($theme_key),
233 dries 1.203 '#options' => $block_regions,
234     );
235 dries 1.182
236 dries 1.181 if ($throttle) {
237 unconed 1.204 $form[$i]['throttle'] = array('#type' => 'checkbox', '#default_value' => $block['throttle']);
238 dries 1.181 }
239 unconed 1.204 $form[$i]['configure'] = array('#value' => l(t('configure'), 'admin/block/configure/'. $block['module'] .'/'. $block['delta']));
240 dries 1.181 if ($block['module'] == 'block') {
241 unconed 1.204 $form[$i]['delete'] = array('#value' => l(t('delete'), 'admin/block/delete/'. $block['delta']));
242 dries 1.181 }
243     }
244 dries 1.183 $form['submit'] = array('#type' => 'submit', '#value' => t('Save blocks'));
245 dries 1.181
246     return drupal_get_form('block_admin_display', $form);
247     }
248    
249 dries 1.203 /**
250 unconed 1.204 * Helper function for sorting blocks on admin/block.
251     *
252     * Active blocks are sorted by region, then by weight.
253     * Disabled blocks are sorted by name.
254     */
255     function _block_compare($a, $b) {
256     $status = $b['status'] - $a['status'];
257     // Separate enabled from disabled.
258     if ($status) {
259     return $status;
260     }
261     // Enabled blocks
262     if ($a['status']) {
263     $place = strcmp($a['region'], $b['region']);
264     return $place ? $place : ($a['weight'] - $b['weight']);
265     }
266     // Disabled blocks
267     else {
268     return strcmp($a['info'], $b['info']);
269     }
270     }
271    
272     /**
273 dries 1.203 * Process main block administration form submission.
274     */
275     function block_admin_display_submit($form_id, $form_values) {
276 unconed 1.204 foreach ($form_values as $block) {
277     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'], $block['throttle'], $block['module'], $block['delta'], $block['theme']);
278 dries 1.203 }
279     drupal_set_message(t('The block settings have been updated.'));
280     cache_clear_all();
281     }
282    
283     /**
284     * Theme main block administration form submission.
285 unconed 1.204 *
286     * Note: the blocks are already sorted in the right order,
287     * grouped by status, region and weight.
288 dries 1.203 */
289 dries 1.181 function theme_block_admin_display($form) {
290 dries 1.188 global $theme_key;
291 dries 1.203
292 dries 1.181 $throttle = module_exist('throttle');
293 dries 1.177 $block_regions = system_region_list($theme_key);
294 dries 1.182
295 dries 1.203 // Highlight regions on page to provide visual reference.
296 dries 1.177 foreach ($block_regions as $key => $value) {
297     drupal_set_content($key, '<div class="block-region">' . $value . '</div>');
298     }
299 dries 1.182
300 unconed 1.204 // Build rows
301 dries 1.157 $rows = array();
302 unconed 1.204 $last_region = '';
303     $last_status = 1;
304     foreach (element_children($form) as $i) {
305     $block = $form[$i];
306     // Only take form elements that are blocks.
307     if (is_array($block['info'])) {
308     // Fetch values
309     $region = $block['region']['#default_value'];
310     $status = $block['status']['#default_value'];
311    
312     // Output region header
313     if ($status && $region != $last_region) {
314     $region_title = t('%region', array('%region' => drupal_ucfirst($block_regions[$region])));
315     $rows[] = array(array('data' => $region_title, 'class' => 'region', 'colspan' => ($throttle ? 7 : 6)));
316 dries 1.205 $last_region = $region;
317 unconed 1.204 }
318     // Output disabled header
319     elseif ($status != $last_status) {
320     $rows[] = array(array('data' => t('Disabled'), 'class' => 'region', 'colspan' => ($throttle ? 7 : 6)));
321 dries 1.205 $last_status = $status;
322 unconed 1.204 }
323    
324     // Generate block row
325     $row = array(
326     array('data' => form_render($block['info']), 'class' => 'block'),
327     form_render($block['status']) . form_render($block['theme']),
328     form_render($block['weight']),
329     form_render($block['region'])
330     );
331     if ($throttle) {
332     $row[] = form_render($block['throttle']);
333     }
334     $row[] = form_render($block['configure']);
335     $row[] = $block['delete'] ? form_render($block['delete']) : '';
336     $rows[] = $row;
337 dries 1.177 }
338 dries 1.157 }
339 unconed 1.204
340     // Finish table
341 dries 1.181 $header = array(t('Block'), t('Enabled'), t('Weight'), t('Placement'));
342     if ($throttle) {
343     $header[] = t('Throttle');
344     }
345     $header[] = array('data' => t('Operations'), 'colspan' => 2);
346 unconed 1.204
347 dries 1.157 $output = theme('table', $header, $rows, array('id' => 'blocks'));
348 dries 1.181 $output .= form_render($form['submit']);
349 dries 1.203 // Also render the form_id as there is no form_render($form) call (as form_render does not appear to handle the
350     // multi-dimensional block form array very well).
351     $output .= form_render($form['form_id']);
352    
353 dries 1.181 return $output;
354 dries 1.1 }
355    
356 kjartan 1.25 function block_box_get($bid) {
357 dries 1.112 return db_fetch_array(db_query('SELECT * FROM {boxes} WHERE bid = %d', $bid));
358     }
359    
360     /**
361 dries 1.140 * Menu callback; displays the block configuration form.
362 dries 1.112 */
363 dries 1.140 function block_admin_configure($module = NULL, $delta = 0) {
364 unconed 1.138
365 dries 1.188 $form['module'] = array('#type' => 'value', '#value' => $module);
366     $form['delta'] = array('#type' => 'value', '#value' => $delta);
367 dries 1.140
368 dries 1.188 $edit = db_fetch_array(db_query("SELECT pages, visibility, custom FROM {blocks} WHERE module = '%s' AND delta = '%s'", $module, $delta));
369 dries 1.181
370 dries 1.188 // Module-specific block configurations.
371     if ($settings = module_invoke($module, 'block', 'configure', $delta)) {
372     $form['block_settings'] = array(
373     '#type' => 'fieldset',
374     '#title' => t('Block specific settings'),
375     '#collapsible' => true,
376     );
377 dries 1.140
378 dries 1.188 foreach ($settings as $k => $v) {
379     $form['block_settings'][$k] = $v;
380     }
381     }
382 dries 1.149
383 dries 1.188 // Get the block subject for the page title.
384     $info = module_invoke($module, 'block', 'list');
385     drupal_set_title(t("'%name' block", array('%name' => $info[$delta]['info'])));
386    
387     // Standard block configurations.
388    
389     $form['user_vis_settings'] = array(
390     '#type' => 'fieldset',
391     '#title' => t('User specific visibility settings'),
392     '#collapsible' => true,
393     );
394     $form['user_vis_settings']['custom'] = array(
395     '#type' => 'radios',
396     '#title' => t('Custom visibility settings'),
397 dries 1.199 '#options' => array(t('Users cannot control whether or not they see this block.'), t('Show this block by default, but let individual users hide it.'), t('Hide this block by default but let individual users show it.')),
398     '#description' => t('Allow individual users to customize the visibility of this block in their account settings.'),
399 dries 1.188 '#default_value' => $edit['custom'],
400     );
401     $form['page_vis_settings'] = array(
402     '#type' => 'fieldset',
403     '#title' => t('Page specific visibility settings'),
404     '#collapsible' => true,
405     );
406 dries 1.195 $access = user_access('use PHP for block visibility');
407 unconed 1.194
408     if ($edit['visibility'] == 2 && !$access) {
409 dries 1.195 $form['page_vis_settings'] = array();
410 unconed 1.194 $form['page_vis_settings']['visibility'] = array('#type' => 'value', '#value' => 2);
411     $form['page_vis_settings']['pages'] = array('#type' => 'value', '#value' => $edit['pages']);
412     }
413     else {
414     $options = array(t('Show on every page except the listed pages.'), t('Show on only the listed pages.'));
415 dries 1.195 $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' => theme('placeholder', 'blog'), '%blog-wildcard' => theme('placeholder', 'blog/*'), '%front' => theme('placeholder', '<front>')));
416    
417 unconed 1.194 if ($access) {
418     $options[] = t('Show if the following PHP code returns <code>TRUE</code> (PHP-mode, experts only).');
419 dries 1.195 $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' => theme('placeholder', '<?php ?>')));
420 unconed 1.194 }
421     $form['page_vis_settings']['visibility'] = array(
422     '#type' => 'radios',
423     '#title' => t('Show block on specific pages'),
424     '#options' => $options,
425     '#default_value' => $edit['visibility'],
426     );
427     $form['page_vis_settings']['pages'] = array(
428     '#type' => 'textarea',
429     '#title' => t('Pages'),
430     '#default_value' => $edit['pages'],
431 dries 1.195 '#description' => $description,
432 unconed 1.194 );
433     }
434    
435 dries 1.188 $form['submit'] = array(
436     '#type' => 'submit',
437     '#value' => t('Save block'),
438     );
439    
440     return drupal_get_form('block_admin_configure', $form);
441     }
442    
443     function block_admin_configure_validate($form_id, $form_values) {
444 dries 1.189 if ($form_values['module'] == 'block') {
445     if (empty($form_values['info']) || db_num_rows(db_query("SELECT bid FROM {boxes} WHERE bid != %d AND info = '%s'", $form_values['delta'], $form_values['info']))) {
446     form_set_error('info', t('Please ensure that each block description is unique.'));
447     }
448 dries 1.188 }
449     }
450 dries 1.140
451 dries 1.190 function block_admin_configure_submit($form_id, $form_values) {
452 dries 1.188 if (!form_get_errors()) {
453     db_query("UPDATE {blocks} SET visibility = %d, pages = '%s', custom = %d WHERE module = '%s' AND delta = '%s'", $form_values['visibility'], $form_values['pages'], $form_values['custom'], $form_values['module'], $form_values['delta']);
454     module_invoke($form_values['module'], 'block', 'save', $form_values['delta'], $form_values);
455     drupal_set_message(t('The block configuration has been saved.'));
456     cache_clear_all();
457 dries 1.201 return 'admin/block';
458 dries 1.140 }
459     }
460    
461     /**
462     * Menu callback; displays the block creation form.
463     */
464     function block_box_add() {
465 dries 1.188 $form = block_box_form();
466     $form['submit'] = array('#type' => 'submit', '#value' => t('Save block'));
467 dries 1.140
468 dries 1.188 return drupal_get_form('block_box_add', $form);
469     }
470    
471     function block_box_add_validate($form_id, $form_values) {
472     if (empty($form_values['info']) || db_num_rows(db_query("SELECT info FROM {boxes} WHERE info = '%s'", $form_values['info']))) {
473     form_set_error('info', t('Please ensure that each block description is unique.'));
474 dries 1.140 }
475 dries 1.188 }
476 dries 1.140
477 dries 1.190 function block_box_add_submit($form_id, $form_values) {
478 dries 1.188 if (!form_get_errors()) {
479     if (block_box_save($form_values)) {
480     drupal_set_message(t('The block has been created.'));
481 dries 1.201 return 'admin/block';
482 dries 1.188 }
483     }
484 dries 1.140 }
485    
486     /**
487 dries 1.181 * Menu callback; confirm deletion of custom blocks.
488 dries 1.140 */
489     function block_box_delete($bid = 0) {
490     $box = block_box_get($bid);
491 dries 1.183 $form['info'] = array('#type' => 'hidden', '#value' => $box['info'] ? $box['info'] : $box['title']);
492     $form['bid'] = array('#type' => 'hidden', '#value' => $bid);
493 dries 1.181
494 killes 1.206 return confirm_form('block_box_delete_confirm', $form, t('Are you sure you want to delete the block %name?', array('%name' => theme('placeholder', $box['info']))), 'admin/block', '', t('Delete'), t('Cancel'));
495 dries 1.181 }
496 unconed 1.138
497 dries 1.181 /**
498     * Deletion of custom blocks.
499     */
500 dries 1.190 function block_box_delete_confirm_submit($form_id, $form_values) {
501 dries 1.188 db_query('DELETE FROM {boxes} WHERE bid = %d', $form_values['bid']);
502     drupal_set_message(t('The block %name has been removed.', array('%name' => theme('placeholder', $form_values['info']))));
503 dries 1.181 cache_clear_all();
504 dries 1.201 return 'admin/block';
505 dries 1.181 };
506 dries 1.112
507 kjartan 1.25 function block_box_form($edit = array()) {
508 dries 1.198 $form['info'] = array(
509     '#type' => 'textfield',
510     '#title' => t('Block description'),
511     '#default_value' => $edit['info'],
512     '#maxlength' => 64,
513     '#description' => t('A brief description of your block. Used on the <a href="%overview">block overview page</a>.', array('%overview' => url('admin/block'))),
514     '#required' => TRUE,
515     '#weight' => -19,
516     );
517 dries 1.191 $form['title'] = array(
518     '#type' => 'textfield',
519     '#title' => t('Block title'),
520     '#default_value' => $edit['title'],
521     '#maxlength' => 64,
522     '#description' => t('The title of the block as shown to the user.'),
523     '#weight' => -18,
524     );
525 dries 1.196 $form['body_filter']['#weight'] = -17;
526     $form['body_filter']['body'] = array(
527 dries 1.191 '#type' => 'textarea',
528     '#title' => t('Block body'),
529     '#default_value' => $edit['body'],
530     '#rows' => 15,
531     '#description' => t('The content of the block as shown to the user.'),
532     '#weight' => -17,
533     );
534 dries 1.196 $form['body_filter']['format'] = filter_form($edit['format'], -16);
535 kjartan 1.25
536 dries 1.181 return $form;
537 kjartan 1.25 }
538    
539 dries 1.140 function block_box_save($edit, $delta = NULL) {
540 unconed 1.129 if (!filter_access($edit['format'])) {
541     $edit['format'] = FILTER_FORMAT_DEFAULT;
542 dries 1.38 }
543    
544 dries 1.140 if (isset($delta)) {
545     db_query("UPDATE {boxes} SET title = '%s', body = '%s', info = '%s', format = %d WHERE bid = %d", $edit['title'], $edit['body'], $edit['info'], $edit['format'], $delta);
546 kjartan 1.25 }
547     else {
548 unconed 1.129 db_query("INSERT INTO {boxes} (title, body, info, format) VALUES ('%s', '%s', '%s', %d)", $edit['title'], $edit['body'], $edit['info'], $edit['format']);
549 kjartan 1.25 }
550 dries 1.163 return true;
551 kjartan 1.25 }
552    
553 dries 1.112 /**
554     * Implementation of hook_user().
555     *
556     * Allow users to decide which custom blocks to display when they visit
557     * the site.
558     */
559 dries 1.120 function block_user($type, $edit, &$user, $category = NULL) {
560 kjartan 1.25 switch ($type) {
561 kjartan 1.113 case 'form':
562 dries 1.120 if ($category == 'account') {
563 dries 1.140 $result = db_query('SELECT * FROM {blocks} WHERE status = 1 AND custom != 0 ORDER BY weight, module, delta');
564 dries 1.187 $form['block'] = array('#type' => 'fieldset', '#title' => t('Block configuration'), '#weight' => 3, '#collapsible' => TRUE, '#tree' => TRUE);
565 dries 1.120 while ($block = db_fetch_object($result)) {
566     $data = module_invoke($block->module, 'block', 'list');
567     if ($data[$block->delta]['info']) {
568 dries 1.181 $return = TRUE;
569 dries 1.183 $form['block'][$block->module][$block->delta] = array('#type' => 'checkbox', '#title' => $data[$block->delta]['info'], '#default_value' => isset($user->block[$block->module][$block->delta]) ? $user->block[$block->module][$block->delta] : ($block->custom == 1));
570 dries 1.120 }
571 kjartan 1.30 }
572    
573 dries 1.181 if ($return) {
574     return $form;
575 dries 1.120 }
576 kjartan 1.25 }
577 dries 1.41
578     break;
579 dries 1.112 case 'validate':
580     if (!$edit['block']) {
581     $edit['block'] = array();
582 kjartan 1.36 }
583     return $edit;
584 dries 1.1 }
585     }
586 dries 1.20
587 dries 1.177 /**
588     * Return all blocks in the specified region for the current user.
589     *
590     * @param $region
591     * The name of a region.
592     *
593     * @return
594     * An array of block objects, indexed with <i>module</i>_<i>delta</i>.
595     * If you are displaying your blocks in one or two sidebars, you may check
596     * whether this array is empty to see how many columns are going to be
597     * displayed.
598     *
599     * @todo
600     * Add a proper primary key (bid) to the blocks table so we don't have
601     * to mess around with this <i>module</i>_<i>delta</i> construct.
602     * Currently, the blocks table has no primary key defined!
603     */
604     function block_list($region) {
605     global $user, $theme_key;
606    
607 dries 1.85 static $blocks = array();
608    
609 dries 1.179 if (!count($blocks)) {
610     $result = db_query("SELECT * FROM {blocks} WHERE theme = '%s' AND status = 1 ORDER BY region, weight, module", $theme_key);
611 dries 1.180 while ($block = db_fetch_object($result)) {
612 dries 1.184 if (!isset($blocks[$block->region])) {
613 dries 1.179 $blocks[$block->region] = array();
614     }
615 dries 1.140 // Use the user's block visibility setting, if necessary
616 dries 1.180 if ($block->custom != 0) {
617     if ($user->uid && isset($user->block[$block->module][$block->delta])) {
618     $enabled = $user->block[$block->module][$block->delta];
619 dries 1.140 }
620     else {
621 dries 1.180 $enabled = ($block->custom == 1);
622 dries 1.140 }
623     }
624     else {
625     $enabled = TRUE;
626 unconed 1.106 }
627 dries 1.89
628 unconed 1.126 // Match path if necessary
629 dries 1.180 if ($block->pages) {
630     if ($block->visibility < 2) {
631 dries 1.166 $path = drupal_get_path_alias($_GET['q']);
632 dries 1.180 $regexp = '/^('. preg_replace(array('/(\r\n?|\n)/', '/\\\\\*/', '/(^|\|)\\\\<front\\\\>($|\|)/'), array('|', '.*', '\1'. preg_quote(variable_get('site_frontpage', 'node'), '/') .'\2'), preg_quote($block->pages, '/')) .')$/';
633     $page_match = !($block->visibility xor preg_match($regexp, $path));
634 dries 1.166 }
635     else {
636 dries 1.180 $page_match = drupal_eval($block->pages);
637 dries 1.166 }
638 dries 1.149 }
639     else {
640     $page_match = TRUE;
641     }
642 dries 1.180
643 dries 1.166 if ($enabled && $page_match) {
644 dries 1.140 // Check the current throttle status and see if block should be displayed
645     // based on server load.
646 dries 1.180 if (!($block->throttle && (module_invoke('throttle', 'status') > 0))) {
647 dries 1.192 $array = module_invoke($block->module, 'block', 'view', $block->delta);
648     if (isset($array) && is_array($array)) {
649 dries 1.180 foreach ($array as $k => $v) {
650     $block->$k = $v;
651     }
652 unconed 1.133 }
653 dries 1.89 }
654 dries 1.180 if (isset($block->content) && $block->content) {
655     $blocks[$block->region]["{$block->module}_{$block->delta}"] = $block;
656 dries 1.85 }
657     }
658     }
659     }
660 dries 1.179 // Create an empty array if there were no entries
661 dries 1.184 if (!isset($blocks[$region])) {
662 dries 1.179 $blocks[$region] = array();
663     }
664 dries 1.85 return $blocks[$region];
665     }
666    
667 dries 1.178

  ViewVC Help
Powered by ViewVC 1.1.2