You are here

public function MigrateDestinationPrivateMsg::import in Migrate Extras 7.2

Same name and namespace in other branches
  1. 6.2 privatemsg.inc \MigrateDestinationPrivatemsg::import()

Import a single message.

Parameters

$entity: Object object to build. Prefilled with any fields mapped in the Migration.

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

Return value

array Array of key fields of the object that was saved if successful. FALSE on failure.

Overrides MigrateDestination::import

File

./privatemsg.inc, line 98
Privatemag module integration

Class

MigrateDestinationPrivateMsg
@file Privatemag module integration

Code

public function import(stdClass $entity, stdClass $row) {
  $this
    ->prepare($entity, $row);

  // The privatemsg API does not support updating. See http://drupal.org/node/1184984
  // $message['mid'] = $entity->mid;
  // The two user_load() calls here could by slow. If so, one could experiment
  // with entity cache module - http://drupal.org/project/entitycache.
  $options = array();
  if (isset($entity->timestamp)) {
    $options['timestamp'] = Migration::timestamp($entity->timestamp);
  }
  if (isset($entity->author)) {
    $options['author'] = user_load($entity->author);
  }
  if (isset($entity->format)) {
    $options['format'] = $entity->format;
  }
  if (!is_array($entity->recipients)) {
    $entity->recipients = array(
      $entity->recipients,
    );
  }
  foreach ($entity->recipients as $uid) {
    $entity->to[] = user_load($uid);
  }

  // FYI, API is at http://api.worldempire.ch/api/privatemsg/privatemsg.module/function/privatemsg_new_thread/7-2
  $return = privatemsg_new_thread($entity->to, $entity->subject, $entity->body, $options);
  if (!empty($return['success'])) {
    $this
      ->complete((object) $return, $row);

    // Set the read status for the recipient (defaults to unread, so only need
    // to set if read)
    $mid = $return['message']->mid;
    if (isset($entity->is_new) && $entity->is_new == PRIVATEMSG_READ) {
      foreach ($entity->to as $account) {
        privatemsg_message_change_status($mid, $entity->is_new, $account);
      }
    }
    return array(
      $mid,
    );
  }
  else {
    $migration = Migration::currentMigration();
    $migration
      ->saveMessage(reset($return['messages']['error']));
    return FALSE;
  }
}