View source
<?php
function asset_migrate_api() {
return array(
'api' => 2,
);
}
class MigrateDestinationAsset extends MigrateDestinationEntity {
public static function getKeySchema() {
return array(
'aid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'description' => 'ID of a destination asset',
),
);
}
public function __construct($bundle, array $options = array()) {
parent::__construct('asset', $bundle, $options);
}
public function fields() {
$fields = array();
$fields['aid'] = t('Asset: Existing asset ID');
$fields['uid'] = t('Asset: Authored by (uid)');
$fields['title'] = t('Asset: Title');
$fields['created'] = t('Asset: Created timestamp');
$fields['changed'] = t('Asset: Modified timestamp');
$fields += migrate_handler_invoke_all('Entity', 'fields', $this->entityType, $this->bundle);
$fields += migrate_handler_invoke_all('Asset', 'fields', $this->entityType, $this->bundle);
return $fields;
}
public function bulkRollback(array $aids) {
migrate_instrument_start('asset_delete_multiple');
$this
->prepareRollback($aids);
entity_delete_multiple('asset', $aids);
$this
->completeRollback($aids);
migrate_instrument_stop('asset_delete_multiple');
}
public function import(stdClass $mapped_asset, stdClass $row) {
$asset = new Asset(array(
'type' => $this->bundle,
));
foreach ((array) $mapped_asset as $key => $value) {
$asset->{$key} = $value;
}
$migration = Migration::currentMigration();
if (isset($row->migrate_map_destid1)) {
if (isset($asset->aid)) {
if ($asset->aid != $row->migrate_map_destid1) {
throw new MigrateException(t("Incoming aid !aid and map destination aid !destid1 don't match", array(
'!aid' => $asset->aid,
'!destid1' => $row->migrate_map_destid1,
)));
}
}
else {
$asset->aid = $row->migrate_map_destid1;
}
}
if ($migration
->getSystemOfRecord() == Migration::DESTINATION) {
if (!isset($asset->aid)) {
throw new MigrateException(t('System-of-record is DESTINATION, but no destination aid provided'));
}
$old_asset = asset_load($asset->aid);
if (!isset($asset->created)) {
$asset->created = $old_asset->created;
}
if (!isset($asset->uid)) {
$asset->uid = $old_asset->uid;
}
}
if ($migration
->getSystemOfRecord() == Migration::SOURCE) {
if (isset($asset->created)) {
$asset->created = MigrationBase::timestamp($asset->created);
}
else {
$asset->created = REQUEST_TIME;
}
if (isset($asset->changed)) {
$changed = MigrationBase::timestamp($asset->changed);
}
if (!isset($asset->uid)) {
global $user;
$asset->uid = $user->uid;
}
}
$this
->prepare($asset, $row);
if ($migration
->getSystemOfRecord() == Migration::DESTINATION) {
foreach ($old_asset as $field => $value) {
if (property_exists($asset, $field) && $asset->{$field} === NULL) {
}
elseif (!isset($asset->{$field})) {
$asset->{$field} = $old_asset->{$field};
}
}
}
if (isset($asset->aid)) {
$updating = TRUE;
}
else {
$updating = FALSE;
}
if (!empty($asset->title)) {
migrate_instrument_start('asset_save');
$asset
->save();
migrate_instrument_stop('asset_save');
}
else {
throw new MigrateException(t("Asset for the article !path wasn't created as long as it doesn't have any associated title.", array(
'!path' => $row->lien,
)));
}
if (isset($asset->aid)) {
if ($updating) {
$this->numUpdated++;
}
else {
$this->numCreated++;
}
$return = array(
$asset->aid,
);
}
else {
$return = FALSE;
}
$this
->complete($asset, $row);
return $return;
}
}