Projet

Général

Profil

Paste
Télécharger (16,8 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / block / block.install @ 76597ebf

1 85ad3d82 Assos Assos
<?php
2
3
/**
4
 * @file
5
 * Install, update and uninstall functions for the block module.
6
 */
7
8
/**
9
 * Implements hook_schema().
10
 */
11
function block_schema() {
12
  $schema['block'] = array(
13
    'description' => 'Stores block settings, such as region and visibility settings.',
14
    'fields' => array(
15
      'bid' => array(
16
        'type' => 'serial',
17
        'not null' => TRUE,
18
        'description' => 'Primary Key: Unique block ID.',
19
      ),
20
      'module' => array(
21
        'type' => 'varchar',
22
        'length' => 64,
23
        'not null' => TRUE,
24
        'default' => '',
25
        'description' => "The module from which the block originates; for example, 'user' for the Who's Online block, and 'block' for any custom blocks.",
26
      ),
27
      'delta' => array(
28
        'type' => 'varchar',
29
        'length' => 32,
30
        'not null' => TRUE,
31
        'default' => '0',
32
        'description' => 'Unique ID for block within a module.',
33
      ),
34
      'theme' => array(
35
        'type' => 'varchar',
36
        'length' => 64,
37
        'not null' => TRUE,
38
        'default' => '',
39
        'description' => 'The theme under which the block settings apply.',
40
      ),
41
      'status' => array(
42
        'type' => 'int',
43
        'not null' => TRUE,
44
        'default' => 0,
45
        'size' => 'tiny',
46
        'description' => 'Block enabled status. (1 = enabled, 0 = disabled)',
47
      ),
48
      'weight' => array(
49
        'type' => 'int',
50
        'not null' => TRUE,
51
        'default' => 0,
52
        'description' => 'Block weight within region.',
53
      ),
54
      'region' => array(
55
        'type' => 'varchar',
56
        'length' => 64,
57
        'not null' => TRUE,
58
        'default' => '',
59
        'description' => 'Theme region within which the block is set.',
60
      ),
61
      'custom' => array(
62
        'type' => 'int',
63
        'not null' => TRUE,
64
        'default' => 0,
65
        'size' => 'tiny',
66
        'description' => 'Flag to indicate how users may control visibility of the block. (0 = Users cannot control, 1 = On by default, but can be hidden, 2 = Hidden by default, but can be shown)',
67
      ),
68
      'visibility' => array(
69
        'type' => 'int',
70
        'not null' => TRUE,
71
        'default' => 0,
72
        'size' => 'tiny',
73
        'description' => 'Flag to indicate how to show blocks on pages. (0 = Show on all pages except listed pages, 1 = Show only on listed pages, 2 = Use custom PHP code to determine visibility)',
74
      ),
75
      'pages' => array(
76
        'type' => 'text',
77
        'not null' => TRUE,
78
        'description' => 'Contents of the "Pages" block; contains either a list of paths on which to include/exclude the block or PHP code, depending on "visibility" setting.',
79
      ),
80
      'title' => array(
81
        'type' => 'varchar',
82 b4adf10d Assos Assos
        'length' => 255,
83 85ad3d82 Assos Assos
        'not null' => TRUE,
84
        'default' => '',
85
        'description' => 'Custom title for the block. (Empty string will use block default title, <none> will remove the title, text will cause block to use specified title.)',
86
        'translatable' => TRUE,
87
      ),
88
      'cache' => array(
89
        'type' => 'int',
90
        'not null' => TRUE,
91
        'default' => 1,
92
        'size' => 'tiny',
93
        'description' => 'Binary flag to indicate block cache mode. (-2: Custom cache, -1: Do not cache, 1: Cache per role, 2: Cache per user, 4: Cache per page, 8: Block cache global) See DRUPAL_CACHE_* constants in ../includes/common.inc for more detailed information.',
94
      ),
95
    ),
96
    'primary key' => array('bid'),
97
    'unique keys' => array(
98
      'tmd' => array('theme', 'module', 'delta'),
99
    ),
100
    'indexes' => array(
101
      'list' => array('theme', 'status', 'region', 'weight', 'module'),
102
    ),
103
  );
104
105
  $schema['block_role'] = array(
106
    'description' => 'Sets up access permissions for blocks based on user roles',
107
    'fields' => array(
108
      'module' => array(
109
        'type' => 'varchar',
110
        'length' => 64,
111
        'not null' => TRUE,
112
        'description' => "The block's origin module, from {block}.module.",
113
      ),
114
      'delta' => array(
115
        'type' => 'varchar',
116
        'length' => 32,
117
        'not null' => TRUE,
118
        'description' => "The block's unique delta within module, from {block}.delta.",
119
      ),
120
      'rid' => array(
121
        'type' => 'int',
122
        'unsigned' => TRUE,
123
        'not null' => TRUE,
124
        'description' => "The user's role ID from {users_roles}.rid.",
125
      ),
126
    ),
127
    'primary key' => array('module', 'delta', 'rid'),
128
    'indexes' => array(
129
      'rid' => array('rid'),
130
    ),
131
  );
132
133
  $schema['block_custom'] = array(
134
    'description' => 'Stores contents of custom-made blocks.',
135
    'fields' => array(
136
      'bid' => array(
137
        'type' => 'serial',
138
        'unsigned' => TRUE,
139
        'not null' => TRUE,
140
        'description' => "The block's {block}.bid.",
141
      ),
142
      'body' => array(
143
        'type' => 'text',
144
        'not null' => FALSE,
145
        'size' => 'big',
146
        'description' => 'Block contents.',
147
        'translatable' => TRUE,
148
      ),
149
      'info' => array(
150
        'type' => 'varchar',
151
        'length' => 128,
152
        'not null' => TRUE,
153
        'default' => '',
154
        'description' => 'Block description.',
155
      ),
156
      'format' => array(
157
        'type' => 'varchar',
158
        'length' => 255,
159
        'not null' => FALSE,
160
        'description' => 'The {filter_format}.format of the block body.',
161
      ),
162
    ),
163
    'unique keys' => array(
164
      'info' => array('info'),
165
    ),
166
    'primary key' => array('bid'),
167
  );
168
169
  $schema['cache_block'] = drupal_get_schema_unprocessed('system', 'cache');
170
  $schema['cache_block']['description'] = 'Cache table for the Block module to store already built blocks, identified by module, delta, and various contexts which may change the block, such as theme, locale, and caching mode defined for the block.';
171
172
  return $schema;
173
}
174
175
/**
176
 * Implements hook_install().
177
 */
178
function block_install() {
179
180
  // Block should go first so that other modules can alter its output
181
  // during hook_page_alter(). Almost everything on the page is a block,
182
  // so before block module runs, there will not be much to alter.
183
  db_update('system')
184
    ->fields(array('weight' => -5))
185
    ->condition('name', 'block')
186
    ->execute();
187
}
188
189
/**
190
 * Implements hook_update_dependencies().
191
 */
192
function block_update_dependencies() {
193
  // block_update_7005() needs to query the {filter_format} table to get a list
194
  // of existing text formats, so it must run after filter_update_7000(), which
195
  // creates that table.
196
  $dependencies['block'][7005] = array(
197
    'filter' => 7000,
198
  );
199
200
  return $dependencies;
201
}
202
203
/**
204
 * @addtogroup updates-6.x-to-7.x
205
 * @{
206
 */
207
208
/**
209
 * Set system.weight to a low value for block module.
210
 *
211
 * Block should go first so that other modules can alter its output
212
 * during hook_page_alter(). Almost everything on the page is a block,
213
 * so before block module runs, there will not be much to alter.
214
 */
215
function block_update_7000() {
216
  db_update('system')
217
    ->fields(array('weight' => '-5'))
218
    ->condition('name', 'block')
219
    ->execute();
220
}
221
222
/**
223
 * Rename {blocks} table to {block}, {blocks_roles} to {block_role} and
224
 * {boxes} to {block_custom}.
225
 */
226
function block_update_7002() {
227
  db_drop_index('blocks', 'list');
228
  db_rename_table('blocks', 'block');
229
  db_rename_table('blocks_roles', 'block_role');
230
  db_rename_table('boxes', 'block_custom');
231
}
232
233
/**
234
 * Change the weight column to normal int.
235
 */
236
function block_update_7003() {
237
  db_change_field('block', 'weight', 'weight', array(
238
    'type' => 'int',
239
    'not null' => TRUE,
240
    'default' => 0,
241
    'description' => 'Block weight within region.',
242
  ), array(
243
    'indexes' => array(
244
      'list' => array('theme', 'status', 'region', 'weight', 'module'),
245
    ),
246
  ));
247
}
248
249
/**
250
 * Add new blocks to new regions, migrate custom variables to blocks.
251
 */
252
function block_update_7004() {
253
  // Collect a list of themes with blocks.
254
  $themes_with_blocks = array();
255
  $result = db_query("SELECT s.name FROM {system} s INNER JOIN {block} b ON s.name = b.theme WHERE s.type = 'theme' GROUP by s.name");
256
257
  $insert = db_insert('block')->fields(array('module', 'delta', 'theme', 'status', 'weight', 'region', 'pages', 'cache'));
258
  foreach ($result as $theme) {
259
    $themes_with_blocks[] = $theme->name;
260
    // Add new system generated help block.
261
    $insert->values(array(
262
      'module' => 'system',
263
      'delta' => 'help',
264
      'theme' => $theme->name,
265
      'status' => 1,
266
      'weight' => 0,
267
      'region' => 'help',
268
      'pages' => '',
269
      'cache' => DRUPAL_CACHE_PER_ROLE,
270
    ));
271
    // Add new system generated main page content block.
272
    $insert->values(array(
273
      'module' => 'system',
274
      'delta' => 'main',
275
      'theme' => $theme->name,
276
      'status' => 1,
277
      'weight' => 0,
278
      'region' => 'content',
279
      'pages' => '',
280
      'cache' => DRUPAL_NO_CACHE,
281
    ));
282
  }
283
  $insert->execute();
284
285
  // Migrate blocks from left/right regions to first/second regions.
286
  db_update('block')
287
    ->fields(array('region' => 'sidebar_first'))
288
    ->condition('region', 'left')
289
    ->execute();
290
  db_update('block')
291
    ->fields(array('region' => 'sidebar_second'))
292
    ->condition('region', 'right')
293
    ->execute();
294
295
  // Migrate contact form information.
296
  $default_format = variable_get('filter_default_format', 1);
297
  if ($contact_help = variable_get('contact_form_information', '')) {
298
    $bid = db_insert('block_custom')
299
      ->fields(array(
300
        'body' => $contact_help,
301
        'info' => 'Contact page help',
302
        'format' => $default_format,
303
      ))
304
      ->execute();
305
306
    $insert = db_insert('block')->fields(array('module', 'delta', 'theme', 'status', 'weight', 'region', 'visibility', 'pages', 'cache'));
307
    foreach ($themes_with_blocks as $theme) {
308
      // Add contact help block for themes, which had blocks.
309
      $insert->values(array(
310
        'module' => 'block',
311
        'delta' => $bid,
312
        'theme' => $theme,
313
        'status' => 1,
314
        'weight' => 5,
315
        'region' => 'help',
316
        'visibility' => BLOCK_VISIBILITY_LISTED,
317
        'pages' => 'contact',
318
        'cache' => DRUPAL_NO_CACHE,
319
      ));
320
    }
321
    drupal_set_message('The contact form information setting was migrated to <a href="' . url('admin/structure/block/manage/block/' . $bid . '/configure') . '">a custom block</a> and set up to only show on the site-wide contact page. The block was set to use the default text format, which might differ from the HTML based format used before. Check the block and ensure that the output is right.');
322
  }
323
  $insert->execute();
324
325
  // Migrate user help setting.
326
  if ($user_help = variable_get('user_registration_help', '')) {
327
    $bid = db_insert('block_custom')->fields(array('body' => $user_help, 'info' => 'User registration guidelines', 'format' => $default_format))->execute();
328
329
    $insert = db_insert('block')->fields(array('module', 'delta', 'theme', 'status', 'weight', 'region', 'visibility', 'pages', 'cache'));
330
    foreach ($themes_with_blocks as $theme) {
331
      // Add user registration help block for themes, which had blocks.
332
      $insert->values(array(
333
        'module' => 'block',
334
        'delta' => $bid,
335
        'theme' => $theme,
336
        'status' => 1,
337
        'weight' => 5,
338
        'region' => 'help',
339
        'visibility' => BLOCK_VISIBILITY_LISTED,
340
        'pages' => 'user/register',
341
        'cache' => DRUPAL_NO_CACHE,
342
      ));
343
    }
344
    drupal_set_message('The user registration guidelines were migrated to <a href="' . url('admin/structure/block/manage/block/' . $bid . '/configure') . '">a custom block</a> and set up to only show on the user registration page. The block was set to use the default text format, which might differ from the HTML based format used before. Check the block and ensure that the output is right.');
345
    $insert->execute();
346
  }
347
348
  // Migrate site mission setting.
349
  if ($mission = variable_get('site_mission')) {
350
    $bid = db_insert('block_custom')->fields(array('body' => $mission, 'info' => 'Site mission', 'format' => $default_format))->execute();
351
352
    $insert = db_insert('block')->fields(array('module', 'delta', 'theme', 'status', 'weight', 'region', 'visibility', 'pages', 'cache'));
353
    foreach ($themes_with_blocks as $theme) {
354
      // Add mission block for themes, which had blocks.
355
      $insert->values(array(
356
        'module' => 'block',
357
        'delta' => $bid,
358
        'theme' => $theme,
359
        'status' => 1,
360
        'weight' => 0,
361
        'region' => 'highlighted',
362
        'visibility' => BLOCK_VISIBILITY_LISTED,
363
        'pages' => '<front>',
364
        'cache' => DRUPAL_NO_CACHE,
365
      ));
366
    }
367
    drupal_set_message('The site mission was migrated to <a href="' . url('admin/structure/block/manage/block/' . $bid . '/configure') . '">a custom block</a> and set up to only show on the front page in the highlighted content region. The block was set to use the default text format, which might differ from the HTML based format used before. Check the block and ensure that the output is right. If your theme does not have a highlighted content region, you might need to <a href="' . url('admin/structure/block') . '">relocate the block</a>.');
368
    $insert->execute();
369
    // Migrate mission to RSS site description.
370
    variable_set('feed_description', $mission);
371
  }
372
373
  // Migrate site footer message to a custom block.
374
  if ($footer_message = variable_get('site_footer', '')) {
375
    $bid = db_insert('block_custom')->fields(array('body' => $footer_message, 'info' => 'Footer message', 'format' => $default_format))->execute();
376
377
    $insert = db_insert('block')->fields(array('module', 'delta', 'theme', 'status', 'weight', 'region', 'pages', 'cache'));
378
    foreach ($themes_with_blocks as $theme) {
379
      // Add site footer block for themes, which had blocks.
380
      // Set low weight, so the block comes early (it used to be
381
      // before the other blocks).
382
      $insert->values(array(
383
        'module' => 'block',
384
        'delta' => $bid,
385
        'theme' => $theme,
386
        'status' => 1,
387
        'weight' => -10,
388
        'region' => 'footer',
389
        'pages' => '',
390
        'cache' => DRUPAL_NO_CACHE,
391
      ));
392
    }
393
    drupal_set_message('The footer message was migrated to <a href="' . url('admin/structure/block/manage/block/' . $bid . '/configure') . '">a custom block</a> and set up to appear in the footer. The block was set to use the default text format, which might differ from the HTML based format used before. Check the block and ensure that the output is right. If your theme does not have a footer region, you might need to <a href="' . url('admin/structure/block') . '">relocate the block</a>.');
394
    $insert->execute();
395
  }
396
397
  // Remove the variables (even if they were saved empty on the admin interface),
398
  // to avoid keeping clutter in the variables table.
399
  variable_del('contact_form_information');
400
  variable_del('user_registration_help');
401
  variable_del('site_mission');
402
  variable_del('site_footer');
403
404
  // Rebuild theme data, so the new 'help' region is identified.
405
  system_rebuild_theme_data();
406
}
407
408
/**
409
 * Update the {block_custom}.format column.
410
 */
411
function block_update_7005() {
412
  // For an explanation of these updates, see the code comments in
413
  // user_update_7010().
414
  db_change_field('block_custom', 'format', 'format', array(
415
    'type' => 'int',
416
    'unsigned' => TRUE,
417
    'not null' => FALSE,
418
    'description' => 'The {filter_format}.format of the block body.',
419
  ));
420
  db_update('block_custom')
421
    ->fields(array('format' => NULL))
422
    ->condition('body', '')
423
    ->condition('format', 0)
424
    ->execute();
425
  $existing_formats = db_query("SELECT format FROM {filter_format}")->fetchCol();
426
  $default_format = variable_get('filter_default_format', 1);
427
  db_update('block_custom')
428
    ->fields(array('format' => $default_format))
429
    ->isNotNull('format')
430
    ->condition('format', $existing_formats, 'NOT IN')
431
    ->execute();
432
}
433
434
/**
435
 * Recreates cache_block table.
436
 *
437
 * Converts fields that hold serialized variables from text to blob.
438
 * Removes 'headers' column.
439
 */
440
function block_update_7006() {
441
  $schema = system_schema_cache_7054();
442
443
  db_drop_table('cache_block');
444
  db_create_table('cache_block', $schema);
445
}
446
447
/**
448
 * Change {block_custom}.format into varchar.
449
 */
450
function block_update_7007() {
451
  db_change_field('block_custom', 'format', 'format', array(
452
    'type' => 'varchar',
453
    'length' => 255,
454
    'not null' => FALSE,
455
    'description' => 'The {filter_format}.format of the block body.',
456
  ));
457
}
458
459
/**
460
 * @} End of "addtogroup updates-6.x-to-7.x".
461
 */
462
463
/**
464
 * @addtogroup updates-7.x-extra
465
 * @{
466
 */
467
468
/**
469
 * Update database to match Drupal 7 schema.
470
 */
471
function block_update_7008() {
472
  db_drop_field('block', 'throttle');
473
}
474
475 b4adf10d Assos Assos
/**
476
 * Increase {block}.title length to 255 characters.
477
 */
478
function block_update_7009() {
479
  db_change_field('block', 'title', 'title',
480
    array(
481
      'type' => 'varchar',
482
      'length' => 255,
483
      'not null' => TRUE,
484
      'default' => '',
485
      'description' => 'Custom title for the block. (Empty string will use block default title, <none> will remove the title, text will cause block to use specified title.)',
486
      'translatable' => TRUE,
487
    )
488
  );
489
}
490
491 85ad3d82 Assos Assos
/**
492
 * @} End of "addtogroup updates-7.x-extra".
493
 */