<?php

/**
 * @file
 * Parent class to OG migration.
 */

abstract class OgEntityMigration extends Migration {

  public $keyName = 'id';

  public function __construct() {
    parent::__construct(MigrateGroup::getInstance('og_7200'));

    // Create a map object for tracking the relationships between source
    // rows.
    if (empty($this->sourceKey)) {
      $this->sourceKey = array(
        $this->keyName => array(
          'type' => 'int',
          'not null' => TRUE,
        ),
      );
    }

    $this->map = new MigrateSQLMap($this->machineName, $this->sourceKey, MigrateDestinationTable::getKeySchema($this->tableName));
    $this->source = new MigrateSourceSQL($this->query);
    $this->destination = new MigrateDestinationTable($this->tableName);
  }

  /**
   * Get the entity type and entity ID from the deprecated OG table.
   *
   * @param $gid
   *   The deprecated group ID.
   */
  public function getNewGroupId($gid) {
    // Get the entity that is the group.
    $result = db_select('og', 'og')
      ->fields('og', array('entity_type', 'etid'))
      ->condition('gid', $gid, '=')
      ->execute()
      ->fetchAssoc();

    return !empty($result) ? array_values($result) : FALSE;
  }
}
