You are here

function webform_submission_insert in Webform 7.4

Same name and namespace in other branches
  1. 5.2 webform_submissions.inc \webform_submission_insert()
  2. 6.3 includes/webform.submissions.inc \webform_submission_insert()
  3. 6.2 webform_submissions.inc \webform_submission_insert()
  4. 7.3 includes/webform.submissions.inc \webform_submission_insert()

Insert a webform submission entry in the database.

Parameters

$node: The node object containing the current webform.

$submission: The webform submission object to be saved into the database.

Return value

int The new submission SID.

2 calls to webform_submission_insert()
webform_client_form_submit in ./webform.module
Submit handler for saving the form values and sending e-mails.
webform_submission_update in includes/webform.submissions.inc
Update a webform submission entry in the database.

File

includes/webform.submissions.inc, line 150
Submission handling functions.

Code

function webform_submission_insert($node, $submission) {
  $transaction = db_transaction();

  // The submission ID may already be set if being called as an update.
  if (!isset($submission->sid) && (!isset($submission->is_new) || $submission->is_new == FALSE)) {

    // Allow other modules to modify the submission before saving.
    foreach (module_implements('webform_submission_presave') as $module) {
      $function = $module . '_webform_submission_presave';
      $function($node, $submission);
    }
    $submission->nid = $node->webform['nid'];
    if (empty($submission->serial)) {
      $submission->serial = _webform_submission_serial_next_value($node->nid);
    }
    $submission->completed = empty($submission->completed) && !$submission->is_draft ? REQUEST_TIME : $submission->completed;
    drupal_write_record('webform_submissions', $submission);
    $is_new = TRUE;
  }
  foreach ($submission->data as $cid => $values) {
    foreach ($values as $delta => $value) {
      $data = array(
        'nid' => $node->webform['nid'],
        'sid' => $submission->sid,
        'cid' => $cid,
        'no' => $delta,
        'data' => is_null($value) ? '' : $value,
      );
      drupal_write_record('webform_submitted_data', $data);
    }
  }

  // Invoke the insert hook after saving all the data.
  if (isset($is_new)) {
    module_invoke_all('webform_submission_insert', $node, $submission);
  }
  return $submission->sid;
}