og_og_membership.migrate.inc 3.56 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
<?php

/**
 * @file
 * Re-populate group ID and group entity, deprecating the OG group entity.
 *
 * Class should be included only if this is an upgrade from branch 7.x-1.x
 * to branch 7.x-2.x
 */

class OgMigrateMembership extends OgEntityMigration {

  /**
   * The name of the table migrate will query.
   */
  public $tableName = 'og_membership';

  /**
   * Indicate we are updating existing data.
   */
  protected $systemOfRecord = Migration::DESTINATION;

  public function __construct() {
    $this->description = t('Re-populate group ID and group entity, deprecating the OG group entity.');

    $query = db_select('og_membership', 'ogm');
    $query->innerJoin('og', 'og', 'ogm.gid = og.gid OR (ogm.group_type = og.entity_type AND ogm.gid = og.etid)');
    $query
      ->fields('ogm', array('id', 'etid', 'entity_type', 'field_name'));

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

    $this->query = $query;
    parent::__construct();

    $fields = array(
      'id',
      'group_type',
      'gid',
      'entity_type',
      'etid',
      'field_name',
    );

    foreach ($fields as $field) {
      $this->addFieldMapping($field, $field);
    }
  }

51
52
53
54
  /**
   * Reject the source row if the group or group content are missing.
   */
  public function prepareRow($row) {
55
56
57
58
59
60
61
62
    $entity_type = $row->entity_type;
    $etid = $row->etid;
    $group_type = $row->group_type;
    $gid = $row->gid;

    if (!$group_content = entity_load_single($entity_type, $etid)) {
      // The OG membership was somehow not deleted when the entity
      // was deleted.
63
      return FALSE;
64
65
66
    }

    if (!$group = entity_load_single($group_type, $gid)) {
67
      return FALSE;
68
    }
69
70
71
72
73
74
75
76
77
78
79
    return parent::prepareRow($row);
  }

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

    $group_content = entity_load_single($entity_type, $etid);
    $group = entity_load_single($group_type, $gid);
80
81
82

    list(,, $group_bundle) = entity_extract_ids($group_type, $group);
    if (!$field_name = og_get_best_group_audience_field($entity_type, $group_content, $group_type, $group_bundle)) {
83
84
85
86
      // Create a new field. Pick an unused name, if the settings don't match.
      // To maintain some backwards compatibility, if the group type is a node,
      // we try to set its name to OG_AUDIENCE FIELD.
      $field_name = $group_type == 'node' ? OG_AUDIENCE_FIELD : substr("og_$group_type", 0, 32);
87
88
89
90
91
      $i = 1;

      $og_field = og_fields_info(OG_AUDIENCE_FIELD);
      $og_field['field']['settings']['target_type'] = $group_type;
      list(,, $bundle) = entity_extract_ids($entity_type, $group_content);
92
93
94
95
96
97
98
99
100
101
102

      while ($field = field_info_field($field_name)) {
        if ($field['settings']['target_type'] == $group_type && empty($field['settings']['handler_settings']['target_bundles']) || in_array($bundle, $field['settings']['handler_settings']['target_bundles'])) {
          // An existing field.
          $field_name = $field['field_name'];
          break;
        }
        $field_name = substr("og_$group_type", 0, 32 - strlen($i)) . $i;
        ++$i;
      }

103
104
105
106
107
108
109
110
      og_create_field($field_name, $entity_type, $bundle, $og_field);
    }
    $entity->field_name = $field_name;
  }

  /**
   * Override Migration::postImport().
   *
111
   * Remove OG-memberships that should have been deleted.
112
113
114
115
116
117
118
119
120
121
   */
  protected function postImport() {
    if (!$this->isComplete()) {
      return;
    }
    db_delete('og_membership')
      ->condition('group_type', '')
      ->execute();
  }
}