You are here

public function MigrateDestinationVotingApiVote::import in Voting API 7.3

Same name and namespace in other branches
  1. 7.2 votingapi.migrate.inc \MigrateDestinationVotingApiVote::import()

Derived classes must implement import(), to construct one new object (pre-pppulated using field mappings in the Migration). It is expected to call prepare and complete handlers, passing them $row (the raw data from the source).

Overrides MigrateDestination::import

File

./votingapi.migrate.inc, line 57
Migration support for voting api.

Class

MigrateDestinationVotingApiVote
@file Migration support for voting api.

Code

public function import(stdClass $votingapi_vote, stdClass $row) {
  $updating = FALSE;
  $vote = array();
  if (isset($row->migrate_map_destid1)) {

    // We're updating an existing vote - start from the previous data.
    $votes = votingapi_select_votes(array(
      'vote_id' => $row->migrate_map_destid1,
    ));
    if (count($votes)) {
      $vote = reset($votes);
      $updating = TRUE;
    }
  }
  if (!$updating) {
    unset($votingapi_vote->vote_id);
  }
  $this
    ->prepare($votingapi_vote, $row);

  // Votes have to be assigned to an entity.
  if (empty($votingapi_vote->entity_id)) {
    watchdog('VotingAPI Import', 'Skipped import due to empty entity_id for vote import: @serial', array(
      '@serial' => json_encode($row),
    ), WATCHDOG_ERROR);
    return FALSE;
  }
  migrate_instrument_start('votingapi_vote_save');

  // Just converting $entity to an array doesn't work...
  $vote = array();
  foreach ((array) $votingapi_vote as $field => $value) {
    $vote[$field] = $value;
  }
  votingapi_add_votes($vote);
  migrate_instrument_stop('votingapi_vote_save');
  if (isset($vote[0]['vote_id'])) {
    $votingapi_vote->vote_id = $vote[0]['vote_id'];
    $this
      ->complete($votingapi_vote, $row);

    // Store the ID in the hash as it is faster than getting uniques later.
    $this->importedIds[$votingapi_vote->entity_type][$votingapi_vote->entity_id] = TRUE;

    // If we hit the threshold of entity id's we imported, we should
    // recalculate to avoid excessive memory usage (and clear out the array).
    if (count($this->importedIds[$votingapi_vote->entity_type]) >= $this->idRecalculationThreshold) {
      $this
        ->recalculateResults($votingapi_vote->entity_type, array_keys($this->importedIds[$votingapi_vote->entity_type]));
      $this->importedIds[$votingapi_vote->entity_type] = array();
    }
    if ($updating) {
      $this->numUpdated++;
    }
    else {
      $this->numCreated++;
    }
    return array(
      $votingapi_vote->vote_id,
    );
  }
  else {
    return FALSE;
  }
}