You are here

class MigrateDestinationRoomsUnit in Rooms - Drupal Booking for Hotels, B&Bs and Vacation Rentals 7

Hierarchy

Expanded class hierarchy of MigrateDestinationRoomsUnit

File

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

View source
class MigrateDestinationRoomsUnit extends MigrateDestinationEntity {
  function __construct($bundle, array $options = array()) {
    parent::__construct('rooms_unit', $bundle, $options);
  }
  public static function getKeySchema() {
    return array(
      'unit_id' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'description' => 'ID of destination bookable unit',
      ),
    );
  }

  /**
   * Returns a list of fields available to be mapped for the rooms unit (bundle)
   *
   * @param Migration $migration
   *  Optionally, the migration containing this destination.
   * @return array
   *  Keys: machine names of the fields (to be passed to addFieldMapping)
   *  Values: Human-friendly descriptions of the fields.
   */
  public function fields($migration = NULL) {
    $fields = array();

    // First the core (room_unit table) properties
    $fields['unit_id'] = t('Existing Unit ID');
    $fields['type'] = t('Unit Type');
    $fields['name'] = t('Unit Name');
    $fields['base_price'] = t('Base price');
    $fields['default_state'] = t('Default state');
    $fields['bookable'] = t('Default state');
    $fields['min_sleeps'] = t('Min sleeps');
    $fields['max_sleeps'] = t('Max sleeps');
    $fields['min_children'] = t('Min children');
    $fields['max_children'] = t('Max children');
    $fields['singles'] = t('Single beds');
    $fields['doubles'] = t('Double beds');
    $fields['data'] = t('Room unit data');

    // Then add in anything provided by handlers
    $fields += migrate_handler_invoke_all('Entity', 'fields', $this->entityType, $this->bundle, $migration);
    $fields += migrate_handler_invoke_all('RoomsUnit', 'fields', $this->entityType, $this->bundle, $migration);
    return $fields;
  }

  /**
   * Put into the data field all stuff that must go there.
   *
   * @param $rooms_unit
   * @param \stdClass $source_row
   *  Raw source data object - passed through to prepare handlers.
   * @internal param $entity
   *  RoomsUnit object to build. Prefilled with any fields mapped in the Migration.*  RoomsUnit object to build. Prefilled with any fields mapped in the Migration.
   */
  public function prepare($rooms_unit, stdClass $source_row) {
    parent::prepare($rooms_unit, $source_row);

    // Store bed arrangement into data.
    $data = array(
      'bed_arrangement' => array(
        'singles' => $rooms_unit->singles,
        'doubles' => $rooms_unit->doubles,
      ),
    );
    $rooms_unit->data = $data;
  }

  /**
   * Imports a single rooms unit.
   *
   * @param stdClass $unit
   *  Rooms unit object to build. Prefilled with any fields mapped in the Migration.
   * @param stdClass $row
   *  Raw source data object - passed through to prepare/complete handlers.
   * @return 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
   */
  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;
  }

  /**
   * Delete a batch of room units at once.
   *
   * @param $units_ids
   *  Array of rooms_unit IDs to be deleted.
   */
  public function bulkRollback(array $units_ids) {
    migrate_instrument_start('rooms_unit_delete_multiple');
    $this
      ->prepareRollback($units_ids);
    rooms_unit_delete_multiple($units_ids);
    $this
      ->completeRollback($units_ids);
    migrate_instrument_stop('rooms_unit_delete_multiple');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MigrateDestination::$numCreated protected property Maintain stats on the number of destination objects created or updated.
MigrateDestination::$numUpdated protected property
MigrateDestination::getCreated public function
MigrateDestination::getUpdated public function
MigrateDestination::resetStats public function Reset numCreated and numUpdated back to 0.
MigrateDestinationEntity::$bundle protected property The bundle (node type, vocabulary, etc.) of the destination.
MigrateDestinationEntity::$entityType protected property The entity type (node, user, taxonomy_term, etc.) of the destination.
MigrateDestinationEntity::$language protected property Default language for text fields in this destination.
MigrateDestinationEntity::$textFormat protected property Default input format for text fields in this destination.
MigrateDestinationEntity::array_flatten public static function Flattens an array of allowed values.
MigrateDestinationEntity::complete public function Give handlers a shot at modifying the object (or taking additional action) after saving it.
MigrateDestinationEntity::completeRollback public function Give handlers a shot at cleaning up after an entity has been rolled back.
MigrateDestinationEntity::fieldAttachValidate public static function Perform field validation against the field data in an entity. Wraps field_attach_validate to handle exceptions cleanly and provide maximum information for identifying the cause of validation errors.
MigrateDestinationEntity::getBundle public function
MigrateDestinationEntity::getEntityType public function
MigrateDestinationEntity::getLanguage public function
MigrateDestinationEntity::getTextFormat public function
MigrateDestinationEntity::prepareRollback public function Give handlers a shot at cleaning up before an entity has been rolled back.
MigrateDestinationEntity::__toString public function Derived classes must implement __toString(). Overrides MigrateDestination::__toString
MigrateDestinationRoomsUnit::bulkRollback public function Delete a batch of room units at once.
MigrateDestinationRoomsUnit::fields public function Returns a list of fields available to be mapped for the rooms unit (bundle) Overrides MigrateDestination::fields
MigrateDestinationRoomsUnit::getKeySchema public static function
MigrateDestinationRoomsUnit::import public function Imports a single rooms unit. Overrides MigrateDestination::import
MigrateDestinationRoomsUnit::prepare public function Put into the data field all stuff that must go there. Overrides MigrateDestinationEntity::prepare
MigrateDestinationRoomsUnit::__construct function Simply save the key schema. Overrides MigrateDestinationEntity::__construct