You are here

function webform_node_insert in Webform 7.4

Same name and namespace in other branches
  1. 6.3 webform.module \webform_node_insert()
  2. 7.3 webform.module \webform_node_insert()

Implements hook_node_insert().

2 calls to webform_node_insert()
webform_ensure_record in ./webform.module
Utility function to ensure that a webform record exists in the database.
webform_node_update in ./webform.module
Implements hook_node_update().

File

./webform.module, line 1512
This module provides a simple way to create forms and questionnaires.

Code

function webform_node_insert($node) {
  if (!variable_get('webform_node_' . $node->type, FALSE)) {
    return;
  }

  // If added directly through node_save(), set defaults for the node.
  if (!isset($node->webform)) {
    $node->webform = array();
  }

  // Ensure values for all defaults are provided. Useful for importing from
  // older versions into newer ones.
  $node->webform += webform_node_defaults();

  // Do not make an entry if this node does not have any Webform settings.
  if ($node->webform == webform_node_defaults() && !in_array($node->type, webform_variable_get('webform_node_types_primary'))) {
    return;
  }
  module_load_include('inc', 'webform', 'includes/webform.components');
  module_load_include('inc', 'webform', 'includes/webform.conditionals');
  module_load_include('inc', 'webform', 'includes/webform.emails');

  // Prepare the record for writing.
  $node->webform['nid'] = $node->nid;
  $webform_record = $node->webform;
  $webform_record['preview_excluded_components'] = implode(',', $webform_record['preview_excluded_components']);

  // Insert the webform.
  $node->webform['record_exists'] = (bool) drupal_write_record('webform', $webform_record);

  // Insert the components into the database. Used with clone.module.
  if (isset($node->webform['components']) && !empty($node->webform['components'])) {
    foreach ($node->webform['components'] as $cid => $component) {

      // Required for clone.module.
      $component['nid'] = $node->nid;
      webform_component_insert($component);
    }
  }

  // Insert conditionals. Also used with clone.module.
  if (isset($node->webform['conditionals']) && !empty($node->webform['conditionals'])) {
    foreach ($node->webform['conditionals'] as $rgid => $conditional) {
      $conditional['nid'] = $node->nid;
      $conditional['rgid'] = $rgid;
      webform_conditional_insert($conditional);
    }
  }

  // Insert emails. Also used with clone.module.
  if (isset($node->webform['emails']) && !empty($node->webform['emails'])) {
    foreach ($node->webform['emails'] as $eid => $email) {
      $email['nid'] = $node->nid;
      webform_email_insert($email);
    }
  }

  // Set the per-role submission access control.
  foreach (array_filter($node->webform['roles']) as $rid) {
    db_insert('webform_roles')
      ->fields(array(
      'nid' => $node->nid,
      'rid' => $rid,
    ))
      ->execute();
  }

  // Flush the block cache if creating a block.
  if (module_exists('block') && $node->webform['block']) {
    block_flush_caches();
  }
}