View source
<?php
class MerciReservationMigration extends Migration {
public function __construct($arguments) {
parent::__construct($arguments);
$this->source = new MigrateSourceReservation();
$this->destination = new MigrateDestinationEntityAPI('merci_reservation', 'checkout');
$this->map = new MigrateSQLMap($this->machineName, array(
'nid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
),
), MigrateDestinationEntityAPI::getKeySchema('merci_reservation'));
$this
->addFieldMapping('merci_line_item_reference', 'reservation_items')
->sourceMigration('MerciLineItem');
$this
->addFieldMapping('field_owner', 'uid');
$this
->addFieldMapping('field_operator', 'uid');
$this
->addFieldMapping('created', 'created');
}
}
class MigrateSourceReservation extends MigrateSourceSQL {
public function __construct() {
$query = db_select('node', 'n')
->condition('n.type', 'merci_reservation')
->condition('n.status', 1, '=')
->fields('n');
parent::__construct($query);
}
public function fields($migration = NULL) {
$properties = entity_get_property_info('node');
$fields = array();
foreach ($properties['properties'] as $name => $property_info) {
if (isset($property_info['setter callback'])) {
$fields[$name] = $property_info['description'];
}
}
$fields += migrate_handler_invoke_all('Entity', 'fields', 'node', 'merci_reservation');
return $fields;
}
public function getNextRow() {
$row = parent::getNextRow();
if ($row) {
$node = node_load($row->nid);
if (is_object($node)) {
$node->merci_reservations = merci_reservation_items($node);
$node->reservation_items = array();
foreach ($node->merci_reservations as $reservation) {
$node->reservation_items[] = array(
$reservation['merci_item_nid'],
$row->nid,
);
}
return $node;
}
else {
return NULL;
}
}
else {
return NULL;
}
}
}
class MigrateDestinationReservation extends MigrateDestinationEntityAPI {
protected $bypassDestIdCheck = FALSE;
public static function getKeySchema() {
return array(
'id' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'description' => 'Primary Key: Unique ID.',
),
);
}
public function __construct($bundle, array $options = array()) {
parent::__construct('merci_line_item', $bundle, $options);
}
public function import(stdClass $object, stdClass $row) {
$migration = Migration::currentMigration();
if (!isset($object->type)) {
$object->type = $this->bundle;
}
$this
->prepare($object, $row);
MigrateDestinationEntity::fieldAttachValidate('merci_line_item', $object);
migrate_instrument_start('merci_line_item_save');
entity_save('merci_line_item', $object);
migrate_instrument_stop('merci_line_item_save');
if (isset($object->line_item_id)) {
$this->numCreated++;
$return = array(
$object->line_item_id,
);
}
else {
$return = FALSE;
}
$this
->complete($object, $row);
return $return;
}
public function rollback(array $id) {
static $count = 0;
migrate_instrument_start('merci_line_item_delete');
$this
->prepareRollback($id);
entity_delete('merci_line_item', reset($id));
$this
->completeRollback($id);
migrate_instrument_stop('merci_line_item_delete');
}
}