You are here

public function MigrateDestinationRoomsUnit::import in Rooms - Drupal Booking for Hotels, B&Bs and Vacation Rentals 7

Imports a single rooms unit.

Parameters

stdClass $unit: Rooms unit object to build. Prefilled with any fields mapped in the Migration.

stdClass $row: Raw source data object - passed through to prepare/complete handlers.

Return value

array|bool Array of key fields (unit_id only in this case) of the node that was saved if successful. FALSE on failure.

Throws

MigrateException

Overrides MigrateDestination::import

File

modules/rooms_unit/rooms_unit.migrate.inc, line 95
Class MigrateDestinationRoomsUnit.

Class

MigrateDestinationRoomsUnit

Code

public function import(stdClass $unit, stdClass $row) {

  // Convert stdClass to RoomsUnit.
  $rooms_unit = new RoomsUnit((array) $unit);

  // Updating previously-migrated unit?
  $migration = Migration::currentMigration();
  if (isset($row->migrate_map_destid1)) {
    if (isset($rooms_unit->unit_id)) {
      if ($rooms_unit->unit_id != $row->migrate_map_destid1) {
        throw new MigrateException(t("Incoming unit_id !unit_id and map destination unit_id !destid1 don't match", array(
          '!unit_id' => $rooms_unit->unit_id,
          '!destid1' => $row->migrate_map_destid1,
        )));
      }
    }
    else {
      $rooms_unit->unit_id = $row->migrate_map_destid1;
    }
  }
  if ($migration
    ->getSystemOfRecord() == Migration::DESTINATION) {
    if (!isset($rooms_unit->unit_id)) {
      throw new MigrateException(t('System-of-record is DESTINATION, but no destination unit_id provided'));
    }
    $old_rooms_unit = rooms_unit_load($rooms_unit->unit_id);
    if (empty($old_rooms_unit)) {
      throw new MigrateException(t('System-of-record is DESTINATION, but rooms_unit !unit_id does not exist', array(
        '!unit_id' => $rooms_unit->unit_id,
      )));
    }
  }
  if (!isset($rooms_unit->type)) {

    // Default the type to our designated destination bundle (by doing this
    // conditionally, we permit some flexibility in terms of implementing
    // migrations which can affect more than one type).
    $rooms_unit->type = $this->bundle;
  }

  // Set some required properties.
  if ($migration
    ->getSystemOfRecord() == Migration::SOURCE) {
    if (empty($rooms_unit->language)) {
      $rooms_unit->language = $this->language;
    }
    if (isset($rooms_unit->created)) {
      $created = MigrationBase::timestamp($rooms_unit->created);
    }
    else {
      $rooms_unit->created = REQUEST_TIME;
    }
    if (isset($created)) {
      $rooms_unit->created = $created;
    }
    if (!isset($rooms_unit->changed)) {
      $rooms_unit->changed = REQUEST_TIME;
    }
  }

  // Invoke migration prepare handlers
  $this
    ->prepare($rooms_unit, $row);

  // Trying to update an existing rooms_unit
  if ($migration
    ->getSystemOfRecord() == Migration::DESTINATION) {

    // Incoming data overrides existing data, so only copy non-existent fields
    foreach ($old_rooms_unit as $field => $value) {

      // An explicit NULL in the source data means to wipe to old value (i.e.,
      // don't copy it over from $old_rooms_unit)
      if (property_exists($rooms_unit, $field) && $rooms_unit->{$field} === NULL) {

        // Ignore this field
      }
      elseif (!isset($rooms_unit->{$field})) {
        $rooms_unit->{$field} = $old_rooms_unit->{$field};
      }
    }
  }
  if (isset($rooms_unit->unit_id)) {
    $updating = TRUE;
  }
  else {
    $updating = FALSE;
  }
  migrate_instrument_start('rooms_unit_save');
  rooms_unit_save($rooms_unit);
  migrate_instrument_stop('rooms_unit_save');
  if (isset($rooms_unit->unit_id)) {
    if ($updating) {
      $this->numUpdated++;
    }
    else {
      $this->numCreated++;
    }
    $return = array(
      $rooms_unit->unit_id,
    );
  }
  else {
    $return = FALSE;
  }
  $this
    ->complete($rooms_unit, $row);
  return $return;
}