function webform_node_update in Webform 7.4
Same name and namespace in other branches
- 6.3 webform.module \webform_node_update()
- 7.3 webform.module \webform_node_update()
Implements hook_node_update().
File
- ./
webform.module, line 1583 - This module provides a simple way to create forms and questionnaires.
Code
function webform_node_update($node) {
if (!variable_get('webform_node_' . $node->type, FALSE)) {
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;
}
// 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']);
// Update the webform entry.
drupal_write_record('webform', $webform_record, array(
'nid',
));
// Compare the webform components and don't do anything if it's not needed.
$original = $node->original;
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) {
$node->webform['components'][$cid]['nid'] = $node->nid;
if (in_array($cid, $inserted_cids)) {
webform_component_insert($node->webform['components'][$cid]);
}
elseif (in_array($cid, $deleted_cids)) {
// Delete components only after all updates have been processed.
}
elseif ($node->webform['components'][$cid] != $original->webform['components'][$cid]) {
webform_component_update($node->webform['components'][$cid]);
}
}
// Delete components now that any parent changes have been saved. When
// components are moved and deleted in one operation in FormBuilder, this
// ensures that only the current children are deleted.
foreach ($deleted_cids as $cid) {
webform_component_delete($node, $original->webform['components'][$cid]);
}
}
// Compare the webform conditionals and don't do anything if it's not needed.
if ($original->webform['conditionals'] != $node->webform['conditionals']) {
module_load_include('inc', 'webform', 'includes/webform.conditionals');
// Conditionals don't have unique site-wide IDs or configuration, so our
// update here is a bit more aggressive than for components and e-mails.
// Delete any conditionals no longer in the webform or that have changed.
foreach ($original->webform['conditionals'] as $rgid => $conditional) {
if (!isset($node->webform['conditionals'][$rgid]) || $conditional != $node->webform['conditionals'][$rgid]) {
webform_conditional_delete($node, $conditional);
}
}
// Insert any conditionals not in the original or that have changed.
foreach ($node->webform['conditionals'] as $rgid => $conditional) {
$conditional['nid'] = $node->nid;
$conditional['rgid'] = $rgid;
if (!isset($original->webform['conditionals'][$rgid]) || $original->webform['conditionals'][$rgid] != $conditional) {
webform_conditional_insert($conditional);
}
}
}
// 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) {
$node->webform['emails'][$eid]['nid'] = $node->nid;
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]) {
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_delete('webform_roles')
->condition('nid', $node->nid)
->execute();
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 block settings have been changed.
if (function_exists('block_flush_caches') && $node->webform['block'] != $original->webform['block']) {
block_flush_caches();
}
}