You are here

function webform_node_update in Webform 6.3

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

Implements hook_node_update().

1 call to webform_node_update()
webform_nodeapi in ./webform.module
Implements hook_nodeapi().

File

./webform.module, line 1041

Code

function webform_node_update($node) {
  if (!in_array($node->type, webform_variable_get('webform_node_types'))) {
    return;
  }

  // Check if this node needs a webform record at all. If it matches the
  // defaults, any existing record will be deleted.
  webform_check_record($node);

  // If a webform row doesn't even exist, we can assume it needs to be inserted.
  // If the the webform matches the defaults, no row will be inserted.
  if (!$node->webform['record_exists']) {
    webform_node_insert($node);
    return;
  }

  // Update the webform entry.
  $node->webform['nid'] = $node->nid;
  drupal_write_record('webform', $node->webform, array(
    'nid',
  ));

  // Compare the webform components and don't do anything if it's not needed.
  $original = node_load($node->nid);
  if ($original->webform['components'] != $node->webform['components']) {
    module_load_include('inc', 'webform', 'includes/webform.components');
    $original_cids = array_keys($original->webform['components']);
    $current_cids = array_keys($node->webform['components']);
    $all_cids = array_unique(array_merge($original_cids, $current_cids));
    $deleted_cids = array_diff($original_cids, $current_cids);
    $inserted_cids = array_diff($current_cids, $original_cids);
    foreach ($all_cids as $cid) {
      if (in_array($cid, $inserted_cids)) {
        webform_component_insert($node->webform['components'][$cid]);
      }
      elseif (in_array($cid, $deleted_cids)) {
        webform_component_delete($node, $original->webform['components'][$cid]);
      }
      elseif ($node->webform['components'][$cid] != $original->webform['components'][$cid]) {
        $node->webform['components'][$cid]['nid'] = $node->nid;
        webform_component_update($node->webform['components'][$cid]);
      }
    }
  }

  // Compare the webform e-mails and don't do anything if it's not needed.
  if ($original->webform['emails'] != $node->webform['emails']) {
    module_load_include('inc', 'webform', 'includes/webform.emails');
    $original_eids = array_keys($original->webform['emails']);
    $current_eids = array_keys($node->webform['emails']);
    $all_eids = array_unique(array_merge($original_eids, $current_eids));
    $deleted_eids = array_diff($original_eids, $current_eids);
    $inserted_eids = array_diff($current_eids, $original_eids);
    foreach ($all_eids as $eid) {
      if (in_array($eid, $inserted_eids)) {
        webform_email_insert($node->webform['emails'][$eid]);
      }
      elseif (in_array($eid, $deleted_eids)) {
        webform_email_delete($node, $original->webform['emails'][$eid]);
      }
      elseif ($node->webform['emails'][$eid] != $original->webform['emails'][$eid]) {
        $node->webform['emails'][$eid]['nid'] = $node->nid;
        webform_email_update($node->webform['emails'][$eid]);
      }
    }
  }

  // Just delete and re-insert roles if they've changed.
  if ($original->webform['roles'] != $node->webform['roles']) {
    db_query('DELETE FROM {webform_roles} WHERE nid = %d', $node->nid);
    foreach (array_filter($node->webform['roles']) as $rid) {
      db_query('INSERT INTO {webform_roles} (nid, rid) VALUES (%d, %d)', $node->nid, $rid);
    }
  }
}