function webform_features_rebuild in Webform Features 7.4
Same name and namespace in other branches
- 7.3 webform_features.features.inc \webform_features_rebuild()
Implements hook_features_rebuild().
1 call to webform_features_rebuild()
- webform_features_revert in ./
webform_features.features.inc - Implements hook_features_revert().
File
- ./
webform_features.features.inc, line 184 - Webform Features features integration.
Code
function webform_features_rebuild($module) {
$nodes = module_invoke($module, 'webform_defaults');
if (!empty($nodes)) {
module_load_include('inc', 'node', 'node.pages');
module_load_include('inc', 'webform', 'includes/webform.components');
module_load_include('inc', 'webform', 'includes/webform.emails');
foreach ($nodes as $data) {
if (empty($data['webform']) || empty($data['webform']['machine_name'])) {
continue;
}
drupal_alter('webform_features_rebuild_data', $data);
$webform_machine_name = $data['webform']['machine_name'];
$node = (object) $data;
$node->webform['emails'] = array();
$node->webform['components'] = array();
node_object_prepare($node);
// Find the matching machine name, with a fresh cache.
$existing = webform_features_machine_name_load($webform_machine_name);
if (empty($existing)) {
$node = node_submit($node);
node_save($node);
$existing = $node;
}
// Reinject base properties.
$node->nid = $existing->nid;
$node->vid = $existing->vid;
$node->webform['nid'] = $existing->nid;
if (!empty($node->path)) {
$node->path['source'] = 'node/' . $existing->nid;
}
// Manage components.
$existing_components_machine_names = array();
foreach ($existing->webform['components'] as $component) {
$existing_components_machine_names[$component['machine_name']] = $component['cid'];
}
_webform_features_rebuild_components($node, $data['webform']['components'], $existing_components_machine_names);
// Manage conditionals, conditionals_rules and conditionals_actions.
db_delete('webform_conditional')
->condition('nid', $node->nid)
->execute();
db_delete('webform_conditional_rules')
->condition('nid', $node->nid)
->execute();
db_delete('webform_conditional_actions')
->condition('nid', $node->nid)
->execute();
if (!empty($data['webform']['conditionals'])) {
foreach ($data['webform']['conditionals'] as $index => $conditional) {
// Replace IDs.
$conditional['nid'] = $node->nid;
// Convert rule source component machine names to IDs.
foreach ($conditional['rules'] as &$rule) {
if (!empty($rule['source_component_machine_name'])) {
$rule['source'] = $existing_components_machine_names[$rule['source']];
unset($rule['source_component_machine_name']);
}
$rule['nid'] = $node->nid;
}
// Convert action target component machine names to IDs.
foreach ($conditional['actions'] as &$action) {
if (!empty($action['target_component_machine_name'])) {
$action['target'] = $existing_components_machine_names[$action['target']];
unset($action['target_component_machine_name']);
}
$action['nid'] = $node->nid;
}
$node->webform['conditionals'][$index] = $conditional;
}
}
// Manage emails.
db_delete('webform_emails')
->condition('nid', $node->nid)
->execute();
if (!empty($data['webform']['emails'])) {
foreach ($data['webform']['emails'] as $email) {
$email['nid'] = $node->nid;
foreach (array_filter($email['components_machine_names']) as $field_name => $active) {
$email[$field_name] = $existing_components_machine_names[$email[$field_name]];
}
unset($email['components_machine_names']);
$email['eid'] = webform_email_insert($email);
$node->webform['emails'][$email['eid']] = $email;
}
}
// Final save.
node_save($node);
}
}
}