You are here

public function MigrateDestinationWebformSubmission::import in Migrate Extras 7.2

Import a record.

Parameters

$entity: Webform submission object to build. This is the complete object after saving.

$source_row: Raw source data object - passed through to complete handlers.

Overrides MigrateDestination::import

File

./webform.inc, line 169

Class

MigrateDestinationWebformSubmission
Destination class for the webform_submissions table.

Code

public function import(stdClass $entity, stdClass $row) {

  // Updating previously-migrated content?
  $migration = Migration::currentMigration();
  if (isset($row->migrate_map_destid1)) {
    if (isset($entity->sid) && $entity->sid != $row->migrate_map_destid1) {
      throw new MigrateException(t("Incoming sid !sid and map destination sid !destid1 don't match", array(
        '!sid' => $entity->sid,
        '!destid1' => $row->migrate_map_destid1,
      )));
    }
    else {
      $entity->sid = $row->migrate_map_destid1;
    }
  }
  $entity->nid = $this->node->nid;

  // Move the data from our custom keys back to webform's component ids.
  $data = array();
  foreach ($this->component_cids as $field_name => $cid) {
    if (isset($entity->{$field_name})) {

      // Move the arguments out and kill any extraneous wrapper arrays.
      $value = $entity->{$field_name};
      $arguments = array();
      if (is_array($value) && isset($value['arguments'])) {
        $arguments = (array) $value['arguments'];
        unset($value['arguments']);
        $value = count($value) ? reset($value) : $value;
      }

      // Avoid a warning if they passed in an empty array.
      $arguments += array(
        'source_type' => 'key',
      );

      // By default passed to select components are assumed to be the
      // key. If the key should be looked up use the add a
      // array('source_type' => 'value') argument to the field mapping.
      $component = $this->node->webform['components'][$cid];
      if ($component['type'] == 'select' && $arguments['source_type'] == 'value') {
        $options = _webform_select_options($component);
        $id = array_search($value, $options);
        $data[$cid] = $id === FALSE ? NULL : $id;
      }
      else {
        $data[$cid] = $value;
      }
      unset($entity->{$field_name});
    }
  }
  $entity->data = webform_submission_data($this->node, $data);

  // Invoke migration prepare handlers
  $this
    ->prepare($entity, $row);
  migrate_instrument_start('webform_submission_update/insert');

  // Determine if it's an insert or update.
  if (empty($entity->sid)) {
    $updating = FALSE;
    $sid = webform_submission_insert($this->node, $entity);
  }
  else {

    // If the sid was specified but doesn't exist we'll need to stick an
    // empty record in so webform's update has something to stick to.
    $status = db_merge('webform_submissions')
      ->key(array(
      'sid' => $entity->sid,
    ))
      ->insertFields(array(
      'sid' => $entity->sid,
      'nid' => $entity->nid,
      'submitted' => $entity->submitted,
      'remote_addr' => $entity->remote_addr,
      'is_draft' => $entity->is_draft,
      'bundle' => $entity->bundle,
    ))
      ->execute();

    // If db_merge() makes no changes $status is NULL so make a less
    // elegant comparison.
    $updating = MergeQuery::STATUS_INSERT !== $status;
    $sid = webform_submission_update($this->node, $entity);
  }
  migrate_instrument_stop('webform_submission_update/insert');
  if (isset($sid)) {
    $entity->sid = $sid;
    if ($updating) {
      $this->numUpdated++;
    }
    else {
      $this->numCreated++;
    }
    $return = array(
      $sid,
    );
  }
  else {
    $return = FALSE;
  }

  // Invoke migration complete handlers
  $this
    ->complete($entity, $row);
  return $return;
}