og_user_roles.migrate.inc 2.65 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php

/**
 * @file
 * Update the group ID in user roles.
 *
 * Class should be included only if this is an upgrade from branch 7.x-1.x
 * to branch 7.x-2.x
 */

class OgMigrateUserRoles extends OgEntityMigration {

  public $tableName = 'og_users_roles';

  protected $dependencies = array('OgMigrateRoles');

  protected $defaultRoles;

  protected $sourceKey = array(
    'uid' => array(
      'type' => 'int',
      'not null' => TRUE,
    ),
    'rid' => array(
      'type' => 'int',
      'not null' => TRUE,
    ),
    'gid' => array(
      'type' => 'int',
      'not null' => TRUE,
      // Set the alias, so the query in MigrateSourceSQL::performRewind()
      // will not fail.
      'alias' => 'og',
    ),
  );

  public function __construct() {
    $this->description = t('Update the group ID in user roles.');

    $query = db_select('temp_og_users_roles', 'ogur');
    $query->innerJoin('og', 'og', 'ogur.gid = og.gid');

    $query
      ->fields('ogur', array('uid', 'rid'));

    $query->addField('og', 'etid', 'gid');
    $query->addField('og', 'entity_type', 'group_type');

    $this->query = $query;

    parent::__construct();

    $this->addFieldMapping('uid', 'uid');
    $this->addFieldMapping('rid');
    $this->addFieldMapping('gid');
    $this->addFieldMapping('group_type');
  }

  /**
   * Override Migration::preImport().
   */
  protected function preImport() {
    parent::preImport();
    $this->defaultRoles = og_roles('', '');
  }

  public function prepare($entity, $row) {
    $group_type = $row->group_type;
    $gid = $row->gid;

    if (!$group = entity_load_single($group_type, $gid)) {
      // Some installations might have missing entities, so we don't assume
      // they exist.
      return;
    }
    // Get the bundle from the entity.
    list(,, $bundle) = entity_extract_ids($group_type, $group);

    // Get the per-bundle roles, and replace the role ID, with the
    // per-bundle role ID.
    $og_roles = og_roles($group_type, $bundle, $gid);
    if (!empty($this->defaultRoles[$row->rid]) && $rid = array_search($this->defaultRoles[$row->rid], $og_roles)) {
      // Assign the new role ID.
      $entity->rid = $rid;
    }

    $entity->group_type = $row->group_type;
    $entity->gid = $row->gid;
  }

  /**
   * Override Migration::postImport().
   *
   * Remove the global roles. We didn't do so it in OgMigrateRoles as
   * we need it in this class, when calling og_roles().
   * Also, delete legacy tables.
   */
  protected function postImport() {
    if (!$this->isComplete() || !$this->defaultRoles) {
      return;
    }
    parent::postImport();

    foreach (array_keys($this->defaultRoles) as $rid) {
      og_role_delete($rid);
    }
  }
}