og.install 33.5 KB
Newer Older
Amitaibu's avatar
Amitaibu committed
1
<?php
2

Amitaibu's avatar
Amitaibu committed
3 4
/**
 * @file
5
 * Install, update, and uninstall functions for the Organic groups module.
Amitaibu's avatar
Amitaibu committed
6 7
 */

amitaibu's avatar
amitaibu committed
8
/**
9
 * Implements hook_install().
amitaibu's avatar
amitaibu committed
10
 */
11
function og_install() {
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
  // Add "User request" to the default group membership type.
  $field_name = OG_MEMBERSHIP_REQUEST_FIELD;
  if (!field_info_field($field_name)) {
    $field = array(
      'field_name' => $field_name,
      'type' => 'text_long',
      'entity_types' => array('og_membership'),
      'cardinality' => 1,
      // Although we would prefer to not allow deleting this field, we can not
      // lock it, as it will not allow adding it to other bundles.
      'locked' => FALSE,
    );
    $field = field_create_field($field);
  }

  if (!field_info_instance('og_membership', $field_name, OG_MEMBERSHIP_TYPE_DEFAULT)) {
    $instance = array(
      'field_name' => $field_name,
      'bundle' => OG_MEMBERSHIP_TYPE_DEFAULT,
      'entity_type' => 'og_membership',
      'label' => t('Request message'),
      'description' => t('This is the text a user may send to the group administrators.'),
      'required' => FALSE,
    );
    field_create_instance($instance);
  }
amitaibu's avatar
amitaibu committed
38 39
}

amitaibu's avatar
amitaibu committed
40
/**
amitaibu's avatar
amitaibu committed
41
 * Implements hook_uninstall().
amitaibu's avatar
amitaibu committed
42
 */
amitaibu's avatar
amitaibu committed
43
function og_uninstall() {
44 45 46 47
  db_delete('variable')
    ->condition('name', 'og_group_manager_default_rids_%', 'LIKE')
    ->execute();

48 49 50 51
  db_delete('variable')
    ->condition('name', 'og_is_group_default_access__%__%', 'LIKE')
    ->execute();

amitaibu's avatar
amitaibu committed
52
  $vars = array(
amitaibu's avatar
amitaibu committed
53 54 55
    'og_context',
    'og_group_manager_full_access',
    'og_skip_access',
56
    'og_update_batch_size',
57
    'og_upgrade_7001',
58
    'og_node_access_strict',
59
    'og_features_ignore_og_fields',
60
    'og_maintain_overridden_roles',
amitaibu's avatar
amitaibu committed
61 62 63 64
  );
  foreach ($vars as $var) {
    variable_del($var);
  }
65 66 67 68 69 70 71

  // Delete OG-core fields.
  $og_fields = array(
    'group_group',
    'og_description',
    'og_membership_request',
  );
72 73 74 75 76 77 78 79 80

  foreach (field_info_instances() as $bundles) {
    foreach ($bundles as $instances) {
      foreach ($instances as $instance) {
        if (in_array($instance['field_name'], $og_fields)) {
          field_delete_instance($instance);
        }
        else {
          $field = field_info_field($instance['field_name']);
81
          if ($field['type'] == 'entityreference' && ($field['settings']['handler'] == 'og' || strpos($field['settings']['handler'], 'og_') === 0)) {
82 83 84 85
            // Last instance will take care also of deleting the field itself.
            field_delete_instance($instance);
          }
        }
86
      }
87 88
    }
  }
amitaibu's avatar
amitaibu committed
89 90
}

91

Amitaibu's avatar
Amitaibu committed
92
/**
amitaibu's avatar
amitaibu committed
93
 * Implements hook_schema().
Amitaibu's avatar
Amitaibu committed
94
 */
amitaibu's avatar
amitaibu committed
95
function og_schema() {
Amitaibu's avatar
Amitaibu committed
96 97
  $schema = array();

amitaibu's avatar
amitaibu committed
98
  $schema['og_role_permission'] = array(
Amitaibu's avatar
Amitaibu committed
99 100 101 102 103 104 105 106 107 108
    'description' => 'Stores the permissions assigned to user roles per group.',
    'fields' => array(
      'rid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'Foreign Key: {role}.rid.',
      ),
      'permission' => array(
        'type' => 'varchar',
109
        'length' => 128,
Amitaibu's avatar
Amitaibu committed
110 111 112 113
        'not null' => TRUE,
        'default' => '',
        'description' => 'A single permission granted to the role identified by rid.',
      ),
114 115 116 117 118 119 120
      'module' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => "The module declaring the permission.",
      ),
Amitaibu's avatar
Amitaibu committed
121 122 123 124 125 126
    ),
    'primary key' => array('rid', 'permission'),
    'indexes' => array(
      'permission' => array('permission'),
    ),
    'foreign keys' => array(
amitaibu's avatar
amitaibu committed
127 128 129 130
      'og_role' => array(
        'table' => 'og_role',
        'columns' => array('rid' => 'rid'),
      ),
Amitaibu's avatar
Amitaibu committed
131 132 133
    ),
  );

amitaibu's avatar
amitaibu committed
134
  $schema['og_role'] = array(
Amitaibu's avatar
Amitaibu committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148
    'description' => 'Stores user roles per group.',
    'fields' => array(
      'rid' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'Primary Key: Unique role ID.',
      ),
      'gid'               => array(
        'description'     => "The group's unique ID.",
        'type'            => 'int',
        'size'            => 'normal',
        'not null'        => TRUE,
      ),
149 150 151 152 153 154 155 156 157 158 159 160 161 162
      'group_type' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => "The group's entity type.",
      ),
      'group_bundle' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => "The group's bundle name.",
      ),
Amitaibu's avatar
Amitaibu committed
163 164 165 166 167 168 169 170 171
      'name' => array(
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
        'default' => '',
        'description' => 'Unique role name per group.',
      ),
    ),
    'primary key' => array('rid'),
172 173 174
    'indexes' => array(
      'lookup' => array('group_type', 'group_bundle', 'gid'),
    ),
Amitaibu's avatar
Amitaibu committed
175 176
  );

amitaibu's avatar
amitaibu committed
177
  $schema['og_users_roles'] = array(
Amitaibu's avatar
Amitaibu committed
178 179 180 181 182 183 184 185 186 187 188 189 190 191
    'description' => 'Maps users to roles.',
    'fields' => array(
      'uid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Primary Key: {users}.uid for user.',
      ),
      'rid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
amitaibu's avatar
amitaibu committed
192
        'description' => 'Primary Key: {og_role}.rid for role.',
Amitaibu's avatar
Amitaibu committed
193 194 195 196 197 198 199
      ),
      'gid'               => array(
        'description'     => "The group's unique ID.",
        'type'            => 'int',
        'size'            => 'normal',
        'not null'        => TRUE,
      ),
200 201 202 203 204 205 206
      'group_type' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => "The group's entity type.",
      ),
Amitaibu's avatar
Amitaibu committed
207
    ),
208
    'primary key' => array('uid', 'rid', 'gid'),
Amitaibu's avatar
Amitaibu committed
209 210 211 212
    'indexes' => array(
      'rid' => array('rid'),
    ),
    'foreign keys' => array(
amitaibu's avatar
amitaibu committed
213 214 215 216 217 218 219 220
      'user' => array(
        'table' => 'users',
        'columns' => array('uid' => 'uid'),
      ),
      'og_role' => array(
        'table' => 'og_role',
        'columns' => array('rid' => 'rid'),
      ),
Amitaibu's avatar
Amitaibu committed
221 222 223
    ),
  );

224
  $schema['og_membership_type'] = array(
Amitaibu's avatar
Amitaibu committed
225
    'description' => 'The group membership type.',
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
    'fields' => array(
      // Although the "name" should be enough as the primary key, the numeric ID
      // is required for the internal use of entity API.
      'id' => array(
        'type' => 'serial',
        'not null' => TRUE,
        'description' => 'Primary Key: Numeric group membership type ID.',
      ),
      'name' => array(
        'description' => 'The unified identifier for a group membership type.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'description' => array(
        'description' => 'Description for this group membership type.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
248 249 250 251 252 253 254 255 256 257 258 259 260 261
      'status' => array(
        'type' => 'int',
        'not null' => TRUE,
        // This is equivilent to ENTITY_CUSTOM.
        'default' =>  0x01,
        'size' => 'tiny',
        'description' => 'The exportable status of the entity.',
      ),
      'module' => array(
        'description' => 'The name of the providing module if the entity has been defined in code.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
      ),
262 263 264 265 266 267 268 269

      'language' => array(
        'description' => 'The {languages}.language of this membership type.',
        'type' => 'varchar',
        'length' => 12,
        'not null' => TRUE,
        'default' => '',
      ),
270
    ),
271 272 273 274 275 276
    'primary key' => array('id'),
    'unique keys' => array(
      'name' => array('name'),
    ),
  );

277 278 279 280
  $schema['og_membership'] = array(
    'description' => 'The group membership table.',
    'fields' => array(
      'id' => array(
Amitaibu's avatar
Amitaibu committed
281
        'description' => "The group membership's unique ID.",
282 283 284 285
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
286
      'type' => array(
287 288 289 290 291 292
        'description' => 'Reference to a group membership type.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
293 294
      'etid' => array(
        'description' => "The entity ID.",
295 296 297 298
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
299 300 301 302 303 304 305
      ),
      'entity_type' => array(
        'description' => "The entity type (e.g. node, comment, etc').",
        'type' => 'varchar',
        'length' => '32',
        'not null' => TRUE,
        'default' => '',
306 307 308 309 310 311 312
      ),
      'gid'               => array(
        'description'     => "The group's unique ID.",
        'type'            => 'int',
        'size'            => 'normal',
        'not null'        => TRUE,
      ),
313 314 315 316 317 318 319
      'group_type' => array(
        'description' => "The group's entity type (e.g. node, comment, etc').",
        'type' => 'varchar',
        'length' => '32',
        'not null' => TRUE,
        'default' => '',
      ),
320 321 322 323 324 325 326 327 328 329 330 331 332
      'state' => array(
        'description' => 'The state of the group content.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'default' => '',
      ),
      'created' => array(
        'description' => 'The Unix timestamp when the group content was created.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
333 334 335 336 337 338 339
      'field_name' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => "The name of the field holding the group ID, the OG memebership is associated with.",
      ),
340 341 342 343 344 345 346
      'language' => array(
        'description' => 'The {languages}.language of this membership.',
        'type' => 'varchar',
        'length' => 12,
        'not null' => TRUE,
        'default' => '',
      ),
347
    ),
amitaibu's avatar
amitaibu committed
348
    'primary key' => array('id'),
349 350 351
    'indexes' => array(
      // Entity index; When searching for an entity, we use both the id and type.
      'entity' => array('etid', 'entity_type'),
352
      'group' => array('gid', 'group_type'),
353
      'group_type' => array('group_type'),
354
    ),
355
    'foreign keys' => array(
356 357 358 359
      'og_membership_type' => array(
        'table' => 'og_membership_type',
        'columns' => array('name' => 'name'),
      ),
360
      'group' => array(
361 362 363 364 365 366
        'table' => 'og',
        'columns' => array('gid' => 'gid'),
      ),
    ),
  );

367 368 369 370 371 372 373 374 375 376 377 378
  // Cache bins for Entity-cache module.
  $cache_schema = drupal_get_schema_unprocessed('system', 'cache');
  $types = array(
    'og_membership_type',
    'og_membership',
  );

  foreach ($types as $type) {
    $schema["cache_entity_$type"] = $cache_schema;
    $schema["cache_entity_$type"]['description'] = "Cache table used to store $type entity records.";
  }

Amitaibu's avatar
Amitaibu committed
379
  return $schema;
380 381
}

382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
/**
 * Return the schema for upgrade 7000.
 */
function og_schema_7000_info() {
  $schema = array();

  $schema['og'] = array(
    'description'     => 'Store information related to the groups.',
    'fields'          => array(
      'gid' => array(
        'description' => "The group's unique ID.",
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'etid' => array(
        'description' => "The entity ID.",
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'entity_type' => array(
        'description' => "The entity type (e.g. node, comment, etc').",
        'type' => 'varchar',
        'length' => '32',
        'not null' => TRUE,
        'default' => '',
      ),
      'label' => array(
        'description' => "The entity label (e.g. node's title).",
        'type' => 'varchar',
        'length' => '255',
        'not null' => TRUE,
        'default' => '',
      ),
      'state' => array(
        'description' => 'The state of the group (i.e. active or disabled).',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 1,
      ),
      'created' => array(
        'description' => 'The Unix timestamp when the group was created.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array('gid'),
  );

  $schema['og_role_permission'] = array(
    'description' => 'Stores the permissions assigned to user roles per group.',
    'fields' => array(
      'rid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'Foreign Key: {role}.rid.',
      ),
      'permission' => array(
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
        'default' => '',
        'description' => 'A single permission granted to the role identified by rid.',
      ),
      'module' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => "The module declaring the permission.",
      ),
    ),
    'primary key' => array('rid', 'permission'),
    'indexes' => array(
      'permission' => array('permission'),
    ),
    'foreign keys' => array(
      'og_role' => array(
        'table' => 'og_role',
        'columns' => array('rid' => 'rid'),
      ),
    ),
  );

  $schema['og_role'] = array(
    'description' => 'Stores user roles per group.',
    'fields' => array(
      'rid' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'Primary Key: Unique role ID.',
      ),
      'gid'               => array(
        'description'     => "The group's unique ID.",
        'type'            => 'int',
        'size'            => 'normal',
        'not null'        => TRUE,
      ),
      'name' => array(
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
        'default' => '',
        'description' => 'Unique role name per group.',
      ),
    ),
    'primary key' => array('rid'),
  );

  $schema['og_users_roles'] = array(
    'description' => 'Maps users to roles.',
    'fields' => array(
      'uid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Primary Key: {users}.uid for user.',
      ),
      'rid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Primary Key: {og_role}.rid for role.',
      ),
      'gid'               => array(
        'description'     => "The group's unique ID.",
        'type'            => 'int',
        'size'            => 'normal',
        'not null'        => TRUE,
      ),
    ),
    'primary key' => array('uid', 'rid', 'gid'),
    'indexes' => array(
      'rid' => array('rid'),
    ),
    'foreign keys' => array(
      'user' => array(
        'table' => 'users',
        'columns' => array('uid' => 'uid'),
      ),
      'og_role' => array(
        'table' => 'og_role',
        'columns' => array('rid' => 'rid'),
      ),
    ),
  );

  $schema['og_membership_type'] = array(
    'description' => 'The group membership type.',
    'fields' => array(
      // Although the "name" should be enough as the primary key, the numeric ID
      // is required for the internal use of entity API.
      'id' => array(
        'type' => 'serial',
        'not null' => TRUE,
        'description' => 'Primary Key: Numeric group membership type ID.',
      ),
      'name' => array(
        'description' => 'The unified identifier for a group membership type.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'description' => array(
        'description' => 'Description for this group membership type.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'status' => array(
        'type' => 'int',
        'not null' => TRUE,
        // This is equivilent to ENTITY_CUSTOM.
        'default' =>  0x01,
        'size' => 'tiny',
        'description' => 'The exportable status of the entity.',
      ),
      'module' => array(
        'description' => 'The name of the providing module if the entity has been defined in code.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
      ),
    ),
    'primary key' => array('id'),
    'unique keys' => array(
      'name' => array('name'),
    ),
  );

  $schema['og_membership'] = array(
    'description' => 'The group membership table.',
    'fields' => array(
      'id' => array(
        'description' => "The group membership's unique ID.",
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'name' => array(
        'description' => 'Reference to a group membership type.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'etid' => array(
        'description' => "The entity ID.",
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'entity_type' => array(
        'description' => "The entity type (e.g. node, comment, etc').",
        'type' => 'varchar',
        'length' => '32',
        'not null' => TRUE,
        'default' => '',
      ),
      'gid'               => array(
        'description'     => "The group's unique ID.",
        'type'            => 'int',
        'size'            => 'normal',
        'not null'        => TRUE,
      ),
      'state' => array(
        'description' => 'The state of the group content.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'default' => '',
      ),
      'created' => array(
        'description' => 'The Unix timestamp when the group content was created.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array('id'),
    'foreign keys' => array(
      'og_membership_type' => array(
        'table' => 'og_membership_type',
        'columns' => array('name' => 'name'),
      ),
      'group' => array(
        'table' => 'og',
        'columns' => array('gid' => 'gid'),
      ),
    ),
  );

  return $schema;
}

647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
/**
 * Return the schema for upgrade 7001.
 */
function og_schema_7001_info() {
  $schema = array();
  $schema['og_membership_type'] = array(
    'description' => 'The group membership type.',
    'fields' => array(
      // Although the "name" should be enough as the primary key, the numeric ID
      // is required for the internal use of entity API.
      'id' => array(
        'type' => 'serial',
        'not null' => TRUE,
        'description' => 'Primary Key: Numeric group membership type ID.',
      ),
      'name' => array(
        'description' => 'The unified identifier for a group membership type.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'description' => array(
        'description' => 'Description for this group membership type.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'status' => array(
        'type' => 'int',
        'not null' => TRUE,
        // This is equivilent to ENTITY_CUSTOM.
        'default' =>  0x01,
        'size' => 'tiny',
        'description' => 'The exportable status of the entity.',
      ),
      'module' => array(
        'description' => 'The name of the providing module if the entity has been defined in code.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
      ),
    ),
    'primary key' => array('id'),
    'unique keys' => array(
      'name' => array('name'),
    ),
  );

  $schema['og_membership'] = array(
    'description' => 'The group membership table.',
    'fields' => array(
      'id' => array(
        'description' => "The group membership's unique ID.",
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ),
      'type' => array(
        'description' => 'Reference to a group membership type.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'etid' => array(
        'description' => "The entity ID.",
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'entity_type' => array(
        'description' => "The entity type (e.g. node, comment, etc').",
        'type' => 'varchar',
        'length' => '32',
        'not null' => TRUE,
        'default' => '',
      ),
      'gid'               => array(
        'description'     => "The group's unique ID.",
        'type'            => 'int',
        'size'            => 'normal',
        'not null'        => TRUE,
      ),
      'state' => array(
        'description' => 'The state of the group content.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => FALSE,
        'default' => '',
      ),
      'created' => array(
        'description' => 'The Unix timestamp when the group content was created.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array('id'),
    'indexes' => array(
      // Entity index; When searching for an entity, we use both the id and type.
      'entity' => array('etid', 'entity_type'),
      'gid' => array('gid'),
    ),
    'foreign keys' => array(
      'og_membership_type' => array(
        'table' => 'og_membership_type',
        'columns' => array('name' => 'name'),
      ),
      'group' => array(
        'table' => 'og',
        'columns' => array('gid' => 'gid'),
      ),
    ),
  );

  return $schema;
}

768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822
/**
 * Return the schema for upgrade 7200.
 *
 * Currently we hold only the {og_users_role} table.
 */
function og_schema_7200_info() {
  $schema['og_users_roles'] = array(
    'description' => 'Maps users to roles.',
    'fields' => array(
      'uid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Primary Key: {users}.uid for user.',
      ),
      'rid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'Primary Key: {og_role}.rid for role.',
      ),
      'gid'               => array(
        'description'     => "The group's unique ID.",
        'type'            => 'int',
        'size'            => 'normal',
        'not null'        => TRUE,
      ),
      'group_type' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => "The group's entity type.",
      ),
    ),
    'primary key' => array('uid', 'rid', 'gid'),
    'indexes' => array(
      'rid' => array('rid'),
    ),
    'foreign keys' => array(
      'user' => array(
        'table' => 'users',
        'columns' => array('uid' => 'uid'),
      ),
      'og_role' => array(
        'table' => 'og_role',
        'columns' => array('rid' => 'rid'),
      ),
    ),
  );
  return $schema;
}

823 824 825 826 827 828 829 830 831 832 833
/**
 * Implements hook_field_schema().
 */
function og_field_schema($field) {
  $columns = array(
    'gid' => array(
      'description' => 'The group unique ID.',
      'type' => 'float',
      'unsigned' => TRUE,
      'not null' => FALSE,
    ),
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
    // This columns should be deprecated and removed, as the group membership
    // entity takes care of it. However, there is currently no way to remove
    // them.
    'state' => array(
      'description' => 'The state of the group content.',
      'type' => 'varchar',
      'length' => 255,
      'not null' => FALSE,
      'default' => '',
    ),
    'created' => array(
      'description' => 'The Unix timestamp when the group content was created.',
      'type' => 'int',
      'not null' => TRUE,
      'default' => 0,
    ),
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
  );
  return array(
    'columns' => $columns,
    'indexes' => array(
      'gid' => array('gid'),
    ),
    'foreign keys' => array(
      'og' => array(
        'table' => 'og',
        'columns' => array('gid' => 'gid'),
      ),
    ),
  );
}

amitaibu's avatar
amitaibu committed
865 866 867 868
/**
 * Upgrade from Organic groups 6 to 7.
 */
function og_update_7000(&$sandbox) {
869 870 871 872 873 874 875
  if (db_field_exists('og', 'nid') && !db_table_exists('d6_og')) {
    // Rename the old table, so we can populate the new {og} table using API
    // functions, that assume the new DB structure.
    db_rename_table('og', 'd6_og');
    db_rename_table('og_ancestry', 'd6_og_ancestry');
    db_rename_table('og_uid', 'd6_og_uid');

876 877 878 879 880 881 882 883 884 885
    // The tables that will need to be processed.
    $tables = array('d6_og', 'd6_og_ancestry', 'd6_og_uid');

    // If OG user roles module was installed in the past, make a copy of
    // its table.
    if (db_table_exists('og_users_roles')) {
      db_rename_table('og_users_roles', 'd6_og_users_roles');
      $tables[] = 'd6_og_users_roles';
    }

886
    $schema = og_schema_7000_info();
887 888
    foreach (array('og', 'og_role_permission', 'og_role', 'og_users_roles') as $table) {
      db_create_table($table, $schema[$table]);
889
    }
890

891 892 893
    // Temporary variable to indicate we need to add OG related fields.
    variable_set('og_7000_add_field', TRUE);
    variable_set('og_ui_7000_add_field', TRUE);
894
    return t('Renamed Organic groups 6 tables. Enable "Migrate-UI" module to continue the migration of data. Once migration is done you may disable "Migrate" module');
895
  }
896
  return t('No change needed in Organic groups schema.');
897 898 899
}

/**
900
 * Add group membership entities instead of field data.
901 902
 */
function og_update_7001(&$sandbox) {
903
  // Due to changes in the upgrade path (e.g. moving data migration to
904 905 906
  // og-migrate) some users already have the tables, so be careful about
  // the creation of these tables.
  $schema = og_schema_7001_info();
Amitaibu's avatar
Amitaibu committed
907
  if (!db_table_exists('og_membership')) {
908 909 910
    db_create_table('og_membership', $schema['og_membership']);
  }

Amitaibu's avatar
Amitaibu committed
911
  if (!db_table_exists('og_membership_type')) {
912 913
    db_create_table('og_membership_type', $schema['og_membership_type']);
  }
914 915 916 917 918 919
}

/**
 * Add indexes to og and og_membership tables.
 */
function og_update_7100(&$sandbox) {
920 921 922 923 924 925 926 927
  // Make sure we don't try to re-add an index that was added in previous
  // updates.
  if (!db_index_exists('og_membership', 'entity')) {
    db_add_index('og_membership', 'entity', array('etid', 'entity_type'));
  }
  if (!db_index_exists('og_membership', 'gid')) {
    db_add_index('og_membership', 'gid', array('gid'));
  }
928
  return t('Added table indexes to {og_membership} table.');
929
}
930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948

/**
 * Rename the 'name' column to 'type' in {OG membership}.
 */
function og_update_7101() {
  if (db_field_exists('og_membership', 'type')) {
    // Return early if field already created in og_update_7000().
    return;
  }
  $column = array(
    'description' => 'Reference to a group membership type.',
    'type' => 'varchar',
    'length' => 255,
    'not null' => TRUE,
    'default' => '',
  );
  db_change_field('og_membership', 'name', 'type', $column);
}

949
/**
950
 * Deprecate OG group entity, and add per-bundle roles.
951
 */
952
function og_update_7200() {
953 954 955 956 957 958 959 960 961 962
  // Delete the old group-audience field. The OG membership is not
  // affected by this.
  foreach (field_info_instances() as $entity_type => $bundles) {
    foreach ($bundles as $bundle => $field_names) {
      if (!empty($field_names['group_audience'])) {
        field_delete_instance($field_names['group_audience']);
      }
    }
  }

963 964
  if (!module_enable(array('entityreference'))) {
    throw new DrupalUpdateException('This version of OG requires Entity reference, but it could not be enabled.');
965
  }
966

967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989
  $column = array(
    'description' => "The group's entity type (e.g. node, comment, etc').",
    'type' => 'varchar',
    'length' => '32',
    'not null' => TRUE,
    'default' => '',
  );

  db_add_field('og_membership', 'group_type', $column);

  // Re-add index.
  db_drop_index('og_membership', 'gid');
  db_add_index('og_membership', 'group', array('gid', 'group_type'));

  $column = array(
    'type' => 'varchar',
    'length' => 255,
    'not null' => TRUE,
    'default' => '',
    'description' => "The group's entity type.",
  );

  db_add_field('og_role', 'group_type', $column);
990 991 992 993 994 995 996 997 998


  // Rename {og_users_roles} to allow easier migration.
  // Re-create a clean {og_users_roles}.
  db_rename_table('og_users_roles', 'temp_og_users_roles');

  $schema = og_schema_7200_info();
  db_create_table('og_users_roles', $schema['og_users_roles']);

999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017

  $column = array(
    'type' => 'varchar',
    'length' => 255,
    'not null' => TRUE,
    'default' => '',
    'description' => "The group's bundle name.",
  );
  db_add_field('og_role', 'group_bundle', $column);

  $column = array(
    'type' => 'varchar',
    'length' => 255,
    'not null' => TRUE,
    'default' => '',
    'description' => "The name of the field holding the group ID, the OG memebership is associated with.",
  );
  db_add_field('og_membership', 'field_name', $column);

1018 1019 1020
  // Delete OG-migrate's table.
  db_drop_table('og_migrate');

1021 1022 1023
  // Variable cleanup.
  variable_del('og_skip_access');

1024
  // Clear entity cache.
1025 1026
  // See http://drupal.org/node/996236
  entity_info_cache_clear();
1027

1028 1029 1030
  // Temporary variable to indicate we need to migrate OGUR data.
  variable_set('og_7200_ogur_roles', TRUE);

1031
  return t('Enable "Migrate-UI" module to continue the migration of data. Once migration is done you may disable "Migrate" module');
1032
}
1033

1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
/**
 * Add "language" column to all OG membership's related entities.
 */
function og_update_7201(&$sandbox) {
  //Update the existing entities, with the default language.
  $langcode = language_default()->language;

  $tables = array(
    'og_membership_type',
    'og_membership',
  );

  if (!isset($sandbox['total'])) {
    foreach ($tables as $key => $table) {
      $name = str_replace('_', ' ', $table);
      $column = array(
        'description' => "The {languages}.language of this $name.",
        'type' => 'varchar',
        'length' => 12,
        'not null' => TRUE,
        'default' => $langcode,
      );
      db_add_field($table, 'language', $column);

      if ($table == 'og_membership') {
        // We don't want to time out when updating og_membership, so we will
        // process it using batches.
        $query = db_select($table);
        $sandbox['last'] = 0;
        $sandbox['total'] = $query->countQuery()->execute()->fetchField();
        $sandbox['#finished'] = 0;
      }
      else {
        db_update($table)
          ->fields(array(
            'language' => $langcode,
          ))
          ->execute();
      }
    }
  }
  elseif ($sandbox['last'] <= $sandbox['total']  && $sandbox['total'] > 0) {
    // Update og_memberships.
    $batch_size = 200;
    db_update('og_membership')
      ->fields(array(
        'language' => $langcode,
      ))
      ->condition('id', $sandbox['last'], '>')
      ->execute();

    $sandbox['last'] += $batch_size;
    $sandbox['#finished'] = min(0.99, $sandbox['last'] / $sandbox['total']);
  }
  else {
    // Finished processing.
    $sandbox['#finished'] = 1;
  }
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
}

/**
 * Extend the length of permission field in og_role_permission.
 */
function og_update_7202() {
  $column = array(
    'type' => 'varchar',
    'length' => 128,
    'not null' => TRUE,
    'default' => '',
    'description' => 'A single permission granted to the role identified by rid.',
  );

  db_change_field('og_role_permission', 'permission', 'permission', $column);
  return t('Updated column definition for {og_role_permission} table.');
}
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165

/**
 * Update group-audience field to use "OG group" widget type.
 */
function og_update_7203() {
  if (!module_exists('field_sql_storage')) {
    return;
  }

  // List of secondary group-audience fields.
  $secondary_fields = array();

  foreach (field_info_fields() as $field_name => $field) {
    if ($field['type'] != 'entityreference' || $field['settings']['handler'] != 'og') {
      continue;
    }

    if (!empty($field['settings']['handler_settings']['primary_field'])) {
      field_delete_field($field_name);
      $secondary_fields[] = $field_name;
      continue;
    }

    foreach ($field['bundles'] as $entity_type => $bundles) {
      foreach ($bundles as $bundle) {
        $instance = field_info_instance($entity_type, $field_name, $bundle);
        $instance['settings']['behaviors']['og_widget'] = array(
          'status' => TRUE,
          'default' => array(
            'widget_type' => 'options_select',
          ),
          'admin' => array(
            'widget_type' => 'entityreference_autocomplete',
          ),
        );

        $instance['widget'] = array(
          'type' => 'og_complex',
          'module' => 'og',
          'settings' => array(),
        );

        field_update_instance($instance);
      }
    }
  }

  if ($secondary_fields) {
    $output = t('The following "secondary" fields were deleted, as they are no longer in use:');
    $output .= theme('item_list', array('items' => $secondary_fields));
  }
  else {
    $output = t('All group-audience fields were updated');
  }

  return $output;
}
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185

/**
 * Create cache bins for Entity-cache module.
 */
function og_update_7204() {
  $cache_schema = drupal_get_schema_unprocessed('system', 'cache');
  $types = array(
    'og_membership_type',
    'og_membership',
  );

  foreach ($types as $type) {
    $schema["cache_entity_$type"] = $cache_schema;
    $schema["cache_entity_$type"]['description'] = "Cache table used to store $type entity records.";
  }

  foreach ($schema as $name => $table) {
    db_create_table($name, $table);
  }
}
1186

1187
/**
1188 1189 1190 1191
 * Add an index to {og_membership} on group_type.
 */
function og_update_7205() {
  db_add_index('og_membership', 'group_type', array('group_type'));
1192
}
1193 1194 1195 1196 1197 1198 1199

/**
 * Add an index to {og_role} on group_type, group_bundle and gid to improve speed of og_roles().
 */
function og_update_7206() {
  db_add_index('og_role', 'lookup', array('group_type', 'group_bundle', 'gid'));
}