You are here

function webform_template_node_update in Webform Template 7

Same name and namespace in other branches
  1. 7.4 webform_template.module \webform_template_node_update()

Implements hook_node_update().

File

./webform_template.module, line 155

Code

function webform_template_node_update($node) {
  if (isset($_SESSION['webform_template'])) {

    // Allow components clean up extra data, such as uploaded files.
    module_load_include('inc', 'webform', 'includes/webform.components');
    foreach ($node->webform['components'] as $cid => $component) {
      webform_component_delete($node, $component);
    }

    // Remove any trace of webform data from the database.
    db_delete('webform')
      ->condition('nid', $node->nid)
      ->execute();
    db_delete('webform_component')
      ->condition('nid', $node->nid)
      ->execute();
    db_delete('webform_emails')
      ->condition('nid', $node->nid)
      ->execute();
    db_delete('webform_roles')
      ->condition('nid', $node->nid)
      ->execute();
    db_delete('webform_submissions')
      ->condition('nid', $node->nid)
      ->execute();
    db_delete('webform_submitted_data')
      ->condition('nid', $node->nid)
      ->execute();
    $template = node_load($_SESSION['webform_template']);
    $webform = $template->webform;
    $node->webform = $webform;
    $node->webform['nid'] = $node->nid;
    module_load_include('inc', 'webform', 'includes/webform.components');
    module_load_include('inc', 'webform', 'includes/webform.emails');
    $node->webform['record_exists'] = (bool) drupal_write_record('webform', $node->webform);

    // Insert the components into the database.
    if (isset($node->webform['components']) && !empty($node->webform['components'])) {
      foreach ($node->webform['components'] as $cid => $component) {
        $component['nid'] = $node->nid;
        webform_component_insert($component);
      }
    }

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

    // Integration with other modules.
    if (module_exists('webform_validation')) {
      _webform_template_webform_validation_helper($node, $template, 'update');
    }

    // Allow other modules to modify the webform.
    module_invoke_all('webform_template_update', $node, $template);

    // Clean up session.
    unset($_SESSION['webform_template']);
  }
}