View source
<?php
class MigrateDestinationWebformSubmission extends MigrateDestination {
public static function getKeySchema() {
return array(
'sid' => array(
'type' => 'int',
'not null' => TRUE,
'unsigned' => TRUE,
),
);
}
protected $node;
public function getWebform() {
return $this->node;
}
protected $component_cids;
public function __construct($node) {
parent::__construct();
if (empty($node)) {
throw new Exception(t("You must provide a webform node"));
}
$types = webform_variable_get('webform_node_types');
if (!in_array($node->type, $types)) {
throw new Exception(t("The node must be configured to accept webform submissions but %type was not", array(
'%type' => $node->type,
)));
}
$this->node = $node;
$this->component_cids = array();
foreach ($this->node->webform['components'] as $component) {
$this->component_cids['data_' . $component['form_key']] = $component['cid'];
}
module_load_include('inc', 'webform', 'includes/webform.submissions');
}
public function __toString() {
return t('Submission for the <a href="!link">%title</a> Webform', array(
'!link' => url('node/' . $this->node->nid),
'%title' => $this->node->title,
));
}
public function fields() {
$fields = array(
'sid' => t('The unique identifier for this submission.'),
'uid' => t('The id of the user that completed this submission.'),
'is_draft' => t('Is this a draft of the submission?'),
'submitted' => t('Timestamp of when the form was submitted.'),
'remote_addr' => t('The IP address of the user that submitted the form.'),
);
foreach ($this->node->webform['components'] as $component) {
$fields['data_' . $component['form_key']] = t('@type: @name', array(
'@type' => $component['type'],
'@name' => $component['name'],
));
}
$fields += migrate_handler_invoke_all('WebformSubmission', 'fields', $this->node);
return $fields;
}
public function prepare($entity, stdClass $source_row) {
$migration = Migration::currentMigration();
$entity->migrate = array(
'machineName' => $migration
->getMachineName(),
);
migrate_handler_invoke_all('WebformSubmission', 'prepare', $entity, $source_row, $this->node);
if (method_exists($migration, 'prepare')) {
$migration
->prepare($entity, $source_row, $this->node);
}
}
public function complete($entity, stdClass $source_row) {
$migration = Migration::currentMigration();
migrate_handler_invoke_all('WebformSubmission', 'complete', $entity, $source_row, $this->node);
if (method_exists($migration, 'complete')) {
$migration
->complete($entity, $source_row, $this->node);
}
}
public function import(stdClass $entity, stdClass $row) {
$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;
$data = array();
foreach ($this->component_cids as $field_name => $cid) {
if (isset($entity->{$field_name})) {
$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;
}
$arguments += array(
'source_type' => 'key',
);
$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);
$this
->prepare($entity, $row);
migrate_instrument_start('webform_submission_update/insert');
if (empty($entity->sid)) {
$updating = FALSE;
$sid = webform_submission_insert($this->node, $entity);
}
else {
$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();
$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;
}
$this
->complete($entity, $row);
return $return;
}
public function bulkRollback(array $sids) {
migrate_instrument_start(__METHOD__);
foreach (webform_get_submissions(array(
'sid' => $sids,
)) as $submission) {
webform_submission_delete($this->node, $submission);
}
migrate_instrument_stop(__METHOD__);
}
}