webmaster@1
|
1 <?php |
webmaster@1
|
2 // $Id: block.module,v 1.299 2008/02/03 19:12:57 goba Exp $ |
webmaster@1
|
3 |
webmaster@1
|
4 /** |
webmaster@1
|
5 * @file |
webmaster@1
|
6 * Controls the boxes that are displayed around the main content. |
webmaster@1
|
7 */ |
webmaster@1
|
8 |
webmaster@1
|
9 /** |
webmaster@1
|
10 * Denotes that a block is not enabled in any region and should not |
webmaster@1
|
11 * be shown. |
webmaster@1
|
12 */ |
webmaster@1
|
13 define('BLOCK_REGION_NONE', -1); |
webmaster@1
|
14 |
webmaster@1
|
15 /** |
webmaster@1
|
16 * Constants defining cache granularity for blocks. |
webmaster@1
|
17 * |
webmaster@1
|
18 * Modules specify the caching patterns for their blocks using binary |
webmaster@1
|
19 * combinations of these constants in their hook_block(op 'list'): |
webmaster@1
|
20 * $block[delta]['cache'] = BLOCK_CACHE_PER_ROLE | BLOCK_CACHE_PER_PAGE; |
webmaster@1
|
21 * BLOCK_CACHE_PER_ROLE is used as a default when no caching pattern is |
webmaster@1
|
22 * specified. |
webmaster@1
|
23 * |
webmaster@1
|
24 * The block cache is cleared in cache_clear_all(), and uses the same clearing |
webmaster@1
|
25 * policy than page cache (node, comment, user, taxonomy added or updated...). |
webmaster@1
|
26 * Blocks requiring more fine-grained clearing might consider disabling the |
webmaster@1
|
27 * built-in block cache (BLOCK_NO_CACHE) and roll their own. |
webmaster@1
|
28 * |
webmaster@1
|
29 * Note that user 1 is excluded from block caching. |
webmaster@1
|
30 */ |
webmaster@1
|
31 |
webmaster@1
|
32 /** |
webmaster@1
|
33 * The block should not get cached. This setting should be used: |
webmaster@1
|
34 * - for simple blocks (notably those that do not perform any db query), |
webmaster@1
|
35 * where querying the db cache would be more expensive than directly generating |
webmaster@1
|
36 * the content. |
webmaster@1
|
37 * - for blocks that change too frequently. |
webmaster@1
|
38 */ |
webmaster@1
|
39 define('BLOCK_NO_CACHE', -1); |
webmaster@1
|
40 |
webmaster@1
|
41 /** |
webmaster@1
|
42 * The block can change depending on the roles the user viewing the page belongs to. |
webmaster@1
|
43 * This is the default setting, used when the block does not specify anything. |
webmaster@1
|
44 */ |
webmaster@1
|
45 define('BLOCK_CACHE_PER_ROLE', 0x0001); |
webmaster@1
|
46 |
webmaster@1
|
47 /** |
webmaster@1
|
48 * The block can change depending on the user viewing the page. |
webmaster@1
|
49 * This setting can be resource-consuming for sites with large number of users, |
webmaster@1
|
50 * and thus should only be used when BLOCK_CACHE_PER_ROLE is not sufficient. |
webmaster@1
|
51 */ |
webmaster@1
|
52 define('BLOCK_CACHE_PER_USER', 0x0002); |
webmaster@1
|
53 |
webmaster@1
|
54 /** |
webmaster@1
|
55 * The block can change depending on the page being viewed. |
webmaster@1
|
56 */ |
webmaster@1
|
57 define('BLOCK_CACHE_PER_PAGE', 0x0004); |
webmaster@1
|
58 |
webmaster@1
|
59 /** |
webmaster@1
|
60 * The block is the same for every user on every page where it is visible. |
webmaster@1
|
61 */ |
webmaster@1
|
62 define('BLOCK_CACHE_GLOBAL', 0x0008); |
webmaster@1
|
63 |
webmaster@1
|
64 /** |
webmaster@1
|
65 * Implementation of hook_help(). |
webmaster@1
|
66 */ |
webmaster@1
|
67 function block_help($path, $arg) { |
webmaster@1
|
68 switch ($path) { |
webmaster@1
|
69 case 'admin/help#block': |
webmaster@1
|
70 $output = '<p>'. t('Blocks are boxes of content rendered into an area, or region, of a web page. The default theme Garland, for example, implements the regions "left sidebar", "right sidebar", "content", "header", and "footer", and a block may appear in any one of these areas. The <a href="@blocks">blocks administration page</a> provides a drag-and-drop interface for assigning a block to a region, and for controlling the order of blocks within regions.', array('@blocks' => url('admin/build/block'))) .'</p>'; |
webmaster@1
|
71 $output .= '<p>'. t('Although blocks are usually generated automatically by modules (like the <em>User login</em> block, for example), administrators can also define custom blocks. Custom blocks have a title, description, and body. The body of the block can be as long as necessary, and can contain content supported by any available <a href="@input-format">input format</a>.', array('@input-format' => url('admin/settings/filters'))) .'</p>'; |
webmaster@1
|
72 $output .= '<p>'. t('When working with blocks, remember that:') .'</p>'; |
webmaster@1
|
73 $output .= '<ul><li>'. t('since not all themes implement the same regions, or display regions in the same way, blocks are positioned on a per-theme basis.') .'</li>'; |
webmaster@1
|
74 $output .= '<li>'. t('disabled blocks, or blocks not in a region, are never shown.') .'</li>'; |
webmaster@1
|
75 $output .= '<li>'. t('when throttle module is enabled, throttled blocks (blocks with the <em>Throttle</em> checkbox selected) are hidden during high server loads.') .'</li>'; |
webmaster@1
|
76 $output .= '<li>'. t('blocks can be configured to be visible only on certain pages.') .'</li>'; |
webmaster@1
|
77 $output .= '<li>'. t('blocks can be configured to be visible only when specific conditions are true.') .'</li>'; |
webmaster@1
|
78 $output .= '<li>'. t('blocks can be configured to be visible only for certain user roles.') .'</li>'; |
webmaster@1
|
79 $output .= '<li>'. t('when allowed by an administrator, specific blocks may be enabled or disabled on a per-user basis using the <em>My account</em> page.') .'</li>'; |
webmaster@1
|
80 $output .= '<li>'. t('some dynamic blocks, such as those generated by modules, will be displayed only on certain pages.') .'</li></ul>'; |
webmaster@1
|
81 $output .= '<p>'. t('For more information, see the online handbook entry for <a href="@block">Block module</a>.', array('@block' => 'http://drupal.org/handbook/modules/block/')) .'</p>'; |
webmaster@1
|
82 return $output; |
webmaster@1
|
83 case 'admin/build/block': |
webmaster@1
|
84 $throttle = module_exists('throttle'); |
webmaster@1
|
85 $output = '<p>'. t('This page provides a drag-and-drop interface for assigning a block to a region, and for controlling the order of blocks within regions. To change the region or order of a block, grab a drag-and-drop handle under the <em>Block</em> column and drag the block to a new location in the list. (Grab a handle by clicking and holding the mouse while hovering over a handle icon.) Since not all themes implement the same regions, or display regions in the same way, blocks are positioned on a per-theme basis. Remember that your changes will not be saved until you click the <em>Save blocks</em> button at the bottom of the page.') .'</p>'; |
webmaster@1
|
86 if ($throttle) { |
webmaster@1
|
87 $output .= '<p>'. t('To reduce CPU usage, database traffic or bandwidth, blocks may be automatically disabled during high server loads by selecting their <em>Throttle</em> checkbox. Adjust throttle thresholds on the <a href="@throttleconfig">throttle configuration page</a>.', array('@throttleconfig' => url('admin/settings/throttle'))) .'</p>'; |
webmaster@1
|
88 } |
webmaster@1
|
89 $output .= '<p>'. t('Click the <em>configure</em> link next to each block to configure its specific title and visibility settings. Use the <a href="@add-block">add block page</a> to create a custom block.', array('@add-block' => url('admin/build/block/add'))) .'</p>'; |
webmaster@1
|
90 return $output; |
webmaster@1
|
91 case 'admin/build/block/add': |
webmaster@1
|
92 return '<p>'. t('Use this page to create a new custom block. New blocks are disabled by default, and must be moved to a region on the <a href="@blocks">blocks administration page</a> to be visible.', array('@blocks' => url('admin/build/block'))) .'</p>'; |
webmaster@1
|
93 } |
webmaster@1
|
94 } |
webmaster@1
|
95 |
webmaster@1
|
96 /** |
webmaster@1
|
97 * Implementation of hook_theme() |
webmaster@1
|
98 */ |
webmaster@1
|
99 function block_theme() { |
webmaster@1
|
100 return array( |
webmaster@1
|
101 'block_admin_display_form' => array( |
webmaster@1
|
102 'template' => 'block-admin-display-form', |
webmaster@1
|
103 'file' => 'block.admin.inc', |
webmaster@1
|
104 'arguments' => array('form' => NULL), |
webmaster@1
|
105 ), |
webmaster@1
|
106 ); |
webmaster@1
|
107 } |
webmaster@1
|
108 |
webmaster@1
|
109 /** |
webmaster@1
|
110 * Implementation of hook_perm(). |
webmaster@1
|
111 */ |
webmaster@1
|
112 function block_perm() { |
webmaster@1
|
113 return array('administer blocks', 'use PHP for block visibility'); |
webmaster@1
|
114 } |
webmaster@1
|
115 |
webmaster@1
|
116 /** |
webmaster@1
|
117 * Implementation of hook_menu(). |
webmaster@1
|
118 */ |
webmaster@1
|
119 function block_menu() { |
webmaster@1
|
120 $items['admin/build/block'] = array( |
webmaster@1
|
121 'title' => 'Blocks', |
webmaster@1
|
122 'description' => 'Configure what block content appears in your site\'s sidebars and other regions.', |
webmaster@1
|
123 'page callback' => 'block_admin_display', |
webmaster@1
|
124 'access arguments' => array('administer blocks'), |
webmaster@1
|
125 'file' => 'block.admin.inc', |
webmaster@1
|
126 ); |
webmaster@1
|
127 $items['admin/build/block/list'] = array( |
webmaster@1
|
128 'title' => 'List', |
webmaster@1
|
129 'type' => MENU_DEFAULT_LOCAL_TASK, |
webmaster@1
|
130 'weight' => -10, |
webmaster@1
|
131 ); |
webmaster@1
|
132 $items['admin/build/block/list/js'] = array( |
webmaster@1
|
133 'title' => 'JavaScript List Form', |
webmaster@1
|
134 'page callback' => 'block_admin_display_js', |
webmaster@1
|
135 'type' => MENU_CALLBACK, |
webmaster@1
|
136 'file' => 'block.admin.inc', |
webmaster@1
|
137 ); |
webmaster@1
|
138 $items['admin/build/block/configure'] = array( |
webmaster@1
|
139 'title' => 'Configure block', |
webmaster@1
|
140 'page callback' => 'drupal_get_form', |
webmaster@1
|
141 'page arguments' => array('block_admin_configure'), |
webmaster@1
|
142 'type' => MENU_CALLBACK, |
webmaster@1
|
143 'file' => 'block.admin.inc', |
webmaster@1
|
144 ); |
webmaster@1
|
145 $items['admin/build/block/delete'] = array( |
webmaster@1
|
146 'title' => 'Delete block', |
webmaster@1
|
147 'page callback' => 'drupal_get_form', |
webmaster@1
|
148 'page arguments' => array('block_box_delete'), |
webmaster@1
|
149 'type' => MENU_CALLBACK, |
webmaster@1
|
150 'file' => 'block.admin.inc', |
webmaster@1
|
151 ); |
webmaster@1
|
152 $items['admin/build/block/add'] = array( |
webmaster@1
|
153 'title' => 'Add block', |
webmaster@1
|
154 'page callback' => 'drupal_get_form', |
webmaster@1
|
155 'page arguments' => array('block_add_block_form'), |
webmaster@1
|
156 'type' => MENU_LOCAL_TASK, |
webmaster@1
|
157 'file' => 'block.admin.inc', |
webmaster@1
|
158 ); |
webmaster@1
|
159 $default = variable_get('theme_default', 'garland'); |
webmaster@1
|
160 foreach (list_themes() as $key => $theme) { |
webmaster@1
|
161 $items['admin/build/block/list/'. $key] = array( |
webmaster@1
|
162 'title' => check_plain($theme->info['name']), |
webmaster@1
|
163 'page arguments' => array($key), |
webmaster@1
|
164 'type' => $key == $default ? MENU_DEFAULT_LOCAL_TASK : MENU_LOCAL_TASK, |
webmaster@1
|
165 'weight' => $key == $default ? -10 : 0, |
webmaster@1
|
166 'file' => 'block.admin.inc', |
webmaster@1
|
167 'access callback' => '_block_themes_access', |
webmaster@1
|
168 'access arguments' => array($theme), |
webmaster@1
|
169 ); |
webmaster@1
|
170 } |
webmaster@1
|
171 return $items; |
webmaster@1
|
172 } |
webmaster@1
|
173 |
webmaster@1
|
174 /** |
webmaster@1
|
175 * Menu item access callback - only admin or enabled themes can be accessed |
webmaster@1
|
176 */ |
webmaster@1
|
177 function _block_themes_access($theme) { |
webmaster@1
|
178 return user_access('administer blocks') && ($theme->status || $theme->name == variable_get('admin_theme', '0')); |
webmaster@1
|
179 } |
webmaster@1
|
180 |
webmaster@1
|
181 /** |
webmaster@1
|
182 * Implementation of hook_block(). |
webmaster@1
|
183 * |
webmaster@1
|
184 * Generates the administrator-defined blocks for display. |
webmaster@1
|
185 */ |
webmaster@1
|
186 function block_block($op = 'list', $delta = 0, $edit = array()) { |
webmaster@1
|
187 switch ($op) { |
webmaster@1
|
188 case 'list': |
webmaster@1
|
189 $blocks = array(); |
webmaster@1
|
190 |
webmaster@1
|
191 $result = db_query('SELECT bid, info FROM {boxes} ORDER BY info'); |
webmaster@1
|
192 while ($block = db_fetch_object($result)) { |
webmaster@1
|
193 $blocks[$block->bid]['info'] = $block->info; |
webmaster@1
|
194 // Not worth caching. |
webmaster@1
|
195 $blocks[$block->bid]['cache'] = BLOCK_NO_CACHE; |
webmaster@1
|
196 } |
webmaster@1
|
197 return $blocks; |
webmaster@1
|
198 |
webmaster@1
|
199 case 'configure': |
webmaster@1
|
200 $box = array('format' => FILTER_FORMAT_DEFAULT); |
webmaster@1
|
201 if ($delta) { |
webmaster@1
|
202 $box = block_box_get($delta); |
webmaster@1
|
203 } |
webmaster@1
|
204 if (filter_access($box['format'])) { |
webmaster@1
|
205 return block_box_form($box); |
webmaster@1
|
206 } |
webmaster@1
|
207 break; |
webmaster@1
|
208 |
webmaster@1
|
209 case 'save': |
webmaster@1
|
210 block_box_save($edit, $delta); |
webmaster@1
|
211 break; |
webmaster@1
|
212 |
webmaster@1
|
213 case 'view': |
webmaster@1
|
214 $block = db_fetch_object(db_query('SELECT body, format FROM {boxes} WHERE bid = %d', $delta)); |
webmaster@1
|
215 $data['content'] = check_markup($block->body, $block->format, FALSE); |
webmaster@1
|
216 return $data; |
webmaster@1
|
217 } |
webmaster@1
|
218 } |
webmaster@1
|
219 |
webmaster@1
|
220 /** |
webmaster@1
|
221 * Update the 'blocks' DB table with the blocks currently exported by modules. |
webmaster@1
|
222 * |
webmaster@1
|
223 * @return |
webmaster@1
|
224 * Blocks currently exported by modules. |
webmaster@1
|
225 */ |
webmaster@1
|
226 function _block_rehash() { |
webmaster@1
|
227 global $theme_key; |
webmaster@1
|
228 |
webmaster@1
|
229 init_theme(); |
webmaster@1
|
230 |
webmaster@1
|
231 $result = db_query("SELECT * FROM {blocks} WHERE theme = '%s'", $theme_key); |
webmaster@1
|
232 $old_blocks = array(); |
webmaster@1
|
233 while ($old_block = db_fetch_array($result)) { |
webmaster@1
|
234 $old_blocks[$old_block['module']][$old_block['delta']] = $old_block; |
webmaster@1
|
235 } |
webmaster@1
|
236 |
webmaster@1
|
237 $blocks = array(); |
webmaster@1
|
238 // Valid region names for the theme. |
webmaster@1
|
239 $regions = system_region_list($theme_key); |
webmaster@1
|
240 |
webmaster@1
|
241 foreach (module_list() as $module) { |
webmaster@1
|
242 $module_blocks = module_invoke($module, 'block', 'list'); |
webmaster@1
|
243 if ($module_blocks) { |
webmaster@1
|
244 foreach ($module_blocks as $delta => $block) { |
webmaster@1
|
245 if (empty($old_blocks[$module][$delta])) { |
webmaster@1
|
246 // If it's a new block, add identifiers. |
webmaster@1
|
247 $block['module'] = $module; |
webmaster@1
|
248 $block['delta'] = $delta; |
webmaster@1
|
249 $block['theme'] = $theme_key; |
webmaster@1
|
250 if (!isset($block['pages'])) { |
webmaster@1
|
251 // {block}.pages is type 'text', so it cannot have a |
webmaster@1
|
252 // default value, and not null, so we need to provide |
webmaster@1
|
253 // value if the module did not. |
webmaster@1
|
254 $block['pages'] = ''; |
webmaster@1
|
255 } |
webmaster@1
|
256 // Add defaults and save it into the database. |
webmaster@1
|
257 drupal_write_record('blocks', $block); |
webmaster@1
|
258 // Set region to none if not enabled. |
webmaster@1
|
259 $block['region'] = $block['status'] ? $block['region'] : BLOCK_REGION_NONE; |
webmaster@1
|
260 // Add to the list of blocks we return. |
webmaster@1
|
261 $blocks[] = $block; |
webmaster@1
|
262 } |
webmaster@1
|
263 else { |
webmaster@1
|
264 // If it's an existing block, database settings should overwrite |
webmaster@1
|
265 // the code. But aside from 'info' everything that's definable in |
webmaster@1
|
266 // code is stored in the database and we do not store 'info', so we |
webmaster@1
|
267 // do not need to update the database here. |
webmaster@1
|
268 // Add 'info' to this block. |
webmaster@1
|
269 $old_blocks[$module][$delta]['info'] = $block['info']; |
webmaster@1
|
270 // If the region name does not exist, disable the block and assign it to none. |
webmaster@1
|
271 if (!empty($old_blocks[$module][$delta]['region']) && !isset($regions[$old_blocks[$module][$delta]['region']])) { |
webmaster@1
|
272 drupal_set_message(t('The block %info was assigned to the invalid region %region and has been disabled.', array('%info' => $old_blocks[$module][$delta]['info'], '%region' => $old_blocks[$module][$delta]['region'])), 'warning'); |
webmaster@1
|
273 $old_blocks[$module][$delta]['status'] = 0; |
webmaster@1
|
274 $old_blocks[$module][$delta]['region'] = BLOCK_REGION_NONE; |
webmaster@1
|
275 } |
webmaster@1
|
276 else { |
webmaster@1
|
277 $old_blocks[$module][$delta]['region'] = $old_blocks[$module][$delta]['status'] ? $old_blocks[$module][$delta]['region'] : BLOCK_REGION_NONE; |
webmaster@1
|
278 } |
webmaster@1
|
279 // Add this block to the list of blocks we return. |
webmaster@1
|
280 $blocks[] = $old_blocks[$module][$delta]; |
webmaster@1
|
281 // Remove this block from the list of blocks to be deleted. |
webmaster@1
|
282 unset($old_blocks[$module][$delta]); |
webmaster@1
|
283 } |
webmaster@1
|
284 } |
webmaster@1
|
285 } |
webmaster@1
|
286 } |
webmaster@1
|
287 |
webmaster@1
|
288 // Remove blocks that are no longer defined by the code from the database. |
webmaster@1
|
289 foreach ($old_blocks as $module => $old_module_blocks) { |
webmaster@1
|
290 foreach ($old_module_blocks as $delta => $block) { |
webmaster@1
|
291 db_query("DELETE FROM {blocks} WHERE module = '%s' AND delta = '%s' AND theme = '%s'", $module, $delta, $theme_key); |
webmaster@1
|
292 } |
webmaster@1
|
293 } |
webmaster@1
|
294 return $blocks; |
webmaster@1
|
295 } |
webmaster@1
|
296 |
webmaster@1
|
297 function block_box_get($bid) { |
webmaster@1
|
298 return db_fetch_array(db_query("SELECT bx.*, bl.title FROM {boxes} bx INNER JOIN {blocks} bl ON bx.bid = bl.delta WHERE bl.module = 'block' AND bx.bid = %d", $bid)); |
webmaster@1
|
299 } |
webmaster@1
|
300 |
webmaster@1
|
301 /** |
webmaster@1
|
302 * Define the custom block form. |
webmaster@1
|
303 */ |
webmaster@1
|
304 function block_box_form($edit = array()) { |
webmaster@1
|
305 $edit += array( |
webmaster@1
|
306 'info' => '', |
webmaster@1
|
307 'body' => '', |
webmaster@1
|
308 ); |
webmaster@1
|
309 $form['info'] = array( |
webmaster@1
|
310 '#type' => 'textfield', |
webmaster@1
|
311 '#title' => t('Block description'), |
webmaster@1
|
312 '#default_value' => $edit['info'], |
webmaster@1
|
313 '#maxlength' => 64, |
webmaster@1
|
314 '#description' => t('A brief description of your block. Used on the <a href="@overview">block overview page</a>.', array('@overview' => url('admin/build/block'))), |
webmaster@1
|
315 '#required' => TRUE, |
webmaster@1
|
316 '#weight' => -19, |
webmaster@1
|
317 ); |
webmaster@1
|
318 $form['body_field']['#weight'] = -17; |
webmaster@1
|
319 $form['body_field']['body'] = array( |
webmaster@1
|
320 '#type' => 'textarea', |
webmaster@1
|
321 '#title' => t('Block body'), |
webmaster@1
|
322 '#default_value' => $edit['body'], |
webmaster@1
|
323 '#rows' => 15, |
webmaster@1
|
324 '#description' => t('The content of the block as shown to the user.'), |
webmaster@1
|
325 '#weight' => -17, |
webmaster@1
|
326 ); |
webmaster@1
|
327 if (!isset($edit['format'])) { |
webmaster@1
|
328 $edit['format'] = FILTER_FORMAT_DEFAULT; |
webmaster@1
|
329 } |
webmaster@1
|
330 $form['body_field']['format'] = filter_form($edit['format'], -16); |
webmaster@1
|
331 |
webmaster@1
|
332 return $form; |
webmaster@1
|
333 } |
webmaster@1
|
334 |
webmaster@1
|
335 function block_box_save($edit, $delta) { |
webmaster@1
|
336 if (!filter_access($edit['format'])) { |
webmaster@1
|
337 $edit['format'] = FILTER_FORMAT_DEFAULT; |
webmaster@1
|
338 } |
webmaster@1
|
339 |
webmaster@1
|
340 db_query("UPDATE {boxes} SET body = '%s', info = '%s', format = %d WHERE bid = %d", $edit['body'], $edit['info'], $edit['format'], $delta); |
webmaster@1
|
341 |
webmaster@1
|
342 return TRUE; |
webmaster@1
|
343 } |
webmaster@1
|
344 |
webmaster@1
|
345 /** |
webmaster@1
|
346 * Implementation of hook_user(). |
webmaster@1
|
347 * |
webmaster@1
|
348 * Allow users to decide which custom blocks to display when they visit |
webmaster@1
|
349 * the site. |
webmaster@1
|
350 */ |
webmaster@1
|
351 function block_user($type, $edit, &$account, $category = NULL) { |
webmaster@1
|
352 switch ($type) { |
webmaster@1
|
353 case 'form': |
webmaster@1
|
354 if ($category == 'account') { |
webmaster@1
|
355 $rids = array_keys($account->roles); |
webmaster@1
|
356 $result = db_query("SELECT DISTINCT b.* FROM {blocks} b LEFT JOIN {blocks_roles} r ON b.module = r.module AND b.delta = r.delta WHERE b.status = 1 AND b.custom != 0 AND (r.rid IN (". db_placeholders($rids) .") OR r.rid IS NULL) ORDER BY b.weight, b.module", $rids); |
webmaster@1
|
357 $form['block'] = array('#type' => 'fieldset', '#title' => t('Block configuration'), '#weight' => 3, '#collapsible' => TRUE, '#tree' => TRUE); |
webmaster@1
|
358 while ($block = db_fetch_object($result)) { |
webmaster@1
|
359 $data = module_invoke($block->module, 'block', 'list'); |
webmaster@1
|
360 if ($data[$block->delta]['info']) { |
webmaster@1
|
361 $return = TRUE; |
webmaster@1
|
362 $form['block'][$block->module][$block->delta] = array('#type' => 'checkbox', '#title' => check_plain($data[$block->delta]['info']), '#default_value' => isset($account->block[$block->module][$block->delta]) ? $account->block[$block->module][$block->delta] : ($block->custom == 1)); |
webmaster@1
|
363 } |
webmaster@1
|
364 } |
webmaster@1
|
365 |
webmaster@1
|
366 if (!empty($return)) { |
webmaster@1
|
367 return $form; |
webmaster@1
|
368 } |
webmaster@1
|
369 } |
webmaster@1
|
370 |
webmaster@1
|
371 break; |
webmaster@1
|
372 case 'validate': |
webmaster@1
|
373 if (empty($edit['block'])) { |
webmaster@1
|
374 $edit['block'] = array(); |
webmaster@1
|
375 } |
webmaster@1
|
376 return $edit; |
webmaster@1
|
377 } |
webmaster@1
|
378 } |
webmaster@1
|
379 |
webmaster@1
|
380 /** |
webmaster@1
|
381 * Return all blocks in the specified region for the current user. |
webmaster@1
|
382 * |
webmaster@1
|
383 * @param $region |
webmaster@1
|
384 * The name of a region. |
webmaster@1
|
385 * |
webmaster@1
|
386 * @return |
webmaster@1
|
387 * An array of block objects, indexed with <i>module</i>_<i>delta</i>. |
webmaster@1
|
388 * If you are displaying your blocks in one or two sidebars, you may check |
webmaster@1
|
389 * whether this array is empty to see how many columns are going to be |
webmaster@1
|
390 * displayed. |
webmaster@1
|
391 * |
webmaster@1
|
392 * @todo |
webmaster@1
|
393 * Now that the blocks table has a primary key, we should use that as the |
webmaster@1
|
394 * array key instead of <i>module</i>_<i>delta</i>. |
webmaster@1
|
395 */ |
webmaster@1
|
396 function block_list($region) { |
webmaster@1
|
397 global $user, $theme_key; |
webmaster@1
|
398 |
webmaster@1
|
399 static $blocks = array(); |
webmaster@1
|
400 |
webmaster@1
|
401 if (!count($blocks)) { |
webmaster@1
|
402 $rids = array_keys($user->roles); |
webmaster@1
|
403 $result = db_query(db_rewrite_sql("SELECT DISTINCT b.* FROM {blocks} b LEFT JOIN {blocks_roles} r ON b.module = r.module AND b.delta = r.delta WHERE b.theme = '%s' AND b.status = 1 AND (r.rid IN (". db_placeholders($rids) .") OR r.rid IS NULL) ORDER BY b.region, b.weight, b.module", 'b', 'bid'), array_merge(array($theme_key), $rids)); |
webmaster@1
|
404 while ($block = db_fetch_object($result)) { |
webmaster@1
|
405 if (!isset($blocks[$block->region])) { |
webmaster@1
|
406 $blocks[$block->region] = array(); |
webmaster@1
|
407 } |
webmaster@1
|
408 // Use the user's block visibility setting, if necessary |
webmaster@1
|
409 if ($block->custom != 0) { |
webmaster@1
|
410 if ($user->uid && isset($user->block[$block->module][$block->delta])) { |
webmaster@1
|
411 $enabled = $user->block[$block->module][$block->delta]; |
webmaster@1
|
412 } |
webmaster@1
|
413 else { |
webmaster@1
|
414 $enabled = ($block->custom == 1); |
webmaster@1
|
415 } |
webmaster@1
|
416 } |
webmaster@1
|
417 else { |
webmaster@1
|
418 $enabled = TRUE; |
webmaster@1
|
419 } |
webmaster@1
|
420 |
webmaster@1
|
421 // Match path if necessary |
webmaster@1
|
422 if ($block->pages) { |
webmaster@1
|
423 if ($block->visibility < 2) { |
webmaster@1
|
424 $path = drupal_get_path_alias($_GET['q']); |
webmaster@1
|
425 // Compare with the internal and path alias (if any). |
webmaster@1
|
426 $page_match = drupal_match_path($path, $block->pages); |
webmaster@1
|
427 if ($path != $_GET['q']) { |
webmaster@1
|
428 $page_match = $page_match || drupal_match_path($_GET['q'], $block->pages); |
webmaster@1
|
429 } |
webmaster@1
|
430 // When $block->visibility has a value of 0, the block is displayed on |
webmaster@1
|
431 // all pages except those listed in $block->pages. When set to 1, it |
webmaster@1
|
432 // is displayed only on those pages listed in $block->pages. |
webmaster@1
|
433 $page_match = !($block->visibility xor $page_match); |
webmaster@1
|
434 } |
webmaster@1
|
435 else { |
webmaster@1
|
436 $page_match = drupal_eval($block->pages); |
webmaster@1
|
437 } |
webmaster@1
|
438 } |
webmaster@1
|
439 else { |
webmaster@1
|
440 $page_match = TRUE; |
webmaster@1
|
441 } |
webmaster@1
|
442 |
webmaster@1
|
443 if ($enabled && $page_match) { |
webmaster@1
|
444 // Check the current throttle status and see if block should be displayed |
webmaster@1
|
445 // based on server load. |
webmaster@1
|
446 if (!($block->throttle && (module_invoke('throttle', 'status') > 0))) { |
webmaster@1
|
447 // Try fetching the block from cache. Block caching is not compatible with |
webmaster@1
|
448 // node_access modules. We also preserve the submission of forms in blocks, |
webmaster@1
|
449 // by fetching from cache only if the request method is 'GET'. |
webmaster@1
|
450 if (!count(module_implements('node_grants')) && $_SERVER['REQUEST_METHOD'] == 'GET' && ($cid = _block_get_cache_id($block)) && ($cache = cache_get($cid, 'cache_block'))) { |
webmaster@1
|
451 $array = $cache->data; |
webmaster@1
|
452 } |
webmaster@1
|
453 else { |
webmaster@1
|
454 $array = module_invoke($block->module, 'block', 'view', $block->delta); |
webmaster@1
|
455 if (isset($cid)) { |
webmaster@1
|
456 cache_set($cid, $array, 'cache_block', CACHE_TEMPORARY); |
webmaster@1
|
457 } |
webmaster@1
|
458 } |
webmaster@1
|
459 |
webmaster@1
|
460 if (isset($array) && is_array($array)) { |
webmaster@1
|
461 foreach ($array as $k => $v) { |
webmaster@1
|
462 $block->$k = $v; |
webmaster@1
|
463 } |
webmaster@1
|
464 } |
webmaster@1
|
465 } |
webmaster@1
|
466 if (isset($block->content) && $block->content) { |
webmaster@1
|
467 // Override default block title if a custom display title is present. |
webmaster@1
|
468 if ($block->title) { |
webmaster@1
|
469 // Check plain here to allow module generated titles to keep any markup. |
webmaster@1
|
470 $block->subject = $block->title == '<none>' ? '' : check_plain($block->title); |
webmaster@1
|
471 } |
webmaster@1
|
472 if (!isset($block->subject)) { |
webmaster@1
|
473 $block->subject = ''; |
webmaster@1
|
474 } |
webmaster@1
|
475 $blocks[$block->region]["{$block->module}_{$block->delta}"] = $block; |
webmaster@1
|
476 } |
webmaster@1
|
477 } |
webmaster@1
|
478 } |
webmaster@1
|
479 } |
webmaster@1
|
480 // Create an empty array if there were no entries |
webmaster@1
|
481 if (!isset($blocks[$region])) { |
webmaster@1
|
482 $blocks[$region] = array(); |
webmaster@1
|
483 } |
webmaster@1
|
484 return $blocks[$region]; |
webmaster@1
|
485 } |
webmaster@1
|
486 |
webmaster@1
|
487 /** |
webmaster@1
|
488 * Assemble the cache_id to use for a given block. |
webmaster@1
|
489 * |
webmaster@1
|
490 * The cache_id string reflects the viewing context for the current block |
webmaster@1
|
491 * instance, obtained by concatenating the relevant context information |
webmaster@1
|
492 * (user, page, ...) according to the block's cache settings (BLOCK_CACHE_* |
webmaster@1
|
493 * constants). Two block instances can use the same cached content when |
webmaster@1
|
494 * they share the same cache_id. |
webmaster@1
|
495 * |
webmaster@1
|
496 * Theme and language contexts are automatically differenciated. |
webmaster@1
|
497 * |
webmaster@1
|
498 * @param $block |
webmaster@1
|
499 * @return |
webmaster@1
|
500 * The string used as cache_id for the block. |
webmaster@1
|
501 */ |
webmaster@1
|
502 function _block_get_cache_id($block) { |
webmaster@1
|
503 global $theme, $base_root, $user; |
webmaster@1
|
504 |
webmaster@1
|
505 // User 1 being out of the regular 'roles define permissions' schema, |
webmaster@1
|
506 // it brings too many chances of having unwanted output get in the cache |
webmaster@1
|
507 // and later be served to other users. We therefore exclude user 1 from |
webmaster@1
|
508 // block caching. |
webmaster@1
|
509 if (variable_get('block_cache', 0) && $block->cache != BLOCK_NO_CACHE && $user->uid != 1) { |
webmaster@1
|
510 $cid_parts = array(); |
webmaster@1
|
511 |
webmaster@1
|
512 // Start with common sub-patterns: block identification, theme, language. |
webmaster@1
|
513 $cid_parts[] = $block->module; |
webmaster@1
|
514 $cid_parts[] = $block->delta; |
webmaster@1
|
515 $cid_parts[] = $theme; |
webmaster@1
|
516 if (module_exists('locale')) { |
webmaster@1
|
517 global $language; |
webmaster@1
|
518 $cid_parts[] = $language->language; |
webmaster@1
|
519 } |
webmaster@1
|
520 |
webmaster@1
|
521 // 'PER_ROLE' and 'PER_USER' are mutually exclusive. 'PER_USER' can be a |
webmaster@1
|
522 // resource drag for sites with many users, so when a module is being |
webmaster@1
|
523 // equivocal, we favor the less expensive 'PER_ROLE' pattern. |
webmaster@1
|
524 if ($block->cache & BLOCK_CACHE_PER_ROLE) { |
webmaster@1
|
525 $cid_parts[] = 'r.'. implode(',', array_keys($user->roles)); |
webmaster@1
|
526 } |
webmaster@1
|
527 elseif ($block->cache & BLOCK_CACHE_PER_USER) { |
webmaster@1
|
528 $cid_parts[] = "u.$user->uid"; |
webmaster@1
|
529 } |
webmaster@1
|
530 |
webmaster@1
|
531 if ($block->cache & BLOCK_CACHE_PER_PAGE) { |
webmaster@1
|
532 $cid_parts[] = $base_root . request_uri(); |
webmaster@1
|
533 } |
webmaster@1
|
534 |
webmaster@1
|
535 return implode(':', $cid_parts); |
webmaster@1
|
536 } |
webmaster@1
|
537 } |