You are here

function webform_features_rebuild in Webform Features 7.3

Same name and namespace in other branches
  1. 7.4 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 171
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;
      }
      $webform_machine_name = $data['webform']['machine_name'];
      $node = (object) $data;
      $node->webform['emails'] = array();
      $node->webform['components'] = array();
      node_object_prepare($node);

      // Get the author back if possible.
      $node->uid = 0;
      if (!empty($node->webform_features_author) && ($author = user_load_by_name($node->webform_features_author))) {
        $node->uid = $author->uid;
      }
      $node->revision_uid = 0;
      if (!empty($node->webform_features_revision_author) && ($author = user_load_by_name($node->webform_features_revision_author))) {
        $node->revision_uid = $author->uid;
      }

      // 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;

      // 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 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;
        }
      }

      // The hook_alter signature is:
      // hook_webform_features_rebuild_alter(&node, $module);
      drupal_alter('webform_features_rebuild', $node, $module);

      // Final save.
      node_save($node);
    }
  }
}