<?php

/**
 * Implements hook_node_export_alter().
 */
function node_export_webforms_node_export_alter(&$nodes, $format) {
  // Check if we need to support the Webform Validation module.
  $webform_validation_enabled = module_exists('webform_validation');

  foreach ($nodes as $i => $node) {
    // Remove the "menu" key if it is empty.
    // For some reason it exports in a different order and causes an override status.
    if($node->menu === null) {
      unset($node->menu);
    }

    // Check if the node is a webform.
    if (isset($node->webform)) {

      // We need to remove the nid of each component.
      foreach ($node->webform['components'] as &$component) {
        unset($component['nid']);
      }

      $node->webform_export = $node->webform;

      if($webform_validation_enabled) {
        module_load_include('inc', 'webform_validation', 'webform_validation.admin');
        // Get the rules for this node.
        $rules = webform_validation_get_webform_rules($node);
        $node->webform_validation_rules = $rules;
      }
    }
  }
}

/**
 * Implements hook_node_export_import_alter().
 */
function node_export_webforms_node_export_import_alter(&$nodes, $format, $save) {
  // We need to mode the "webform" property to something custom like "webform_export".
  // If we don't for some reason, the components get screwed up at the end of the import.
  // We'll manually fix that in hook_node_export_after_import_alter().
  foreach ($nodes as $nid => $node) {
    if (isset($nodes[$nid]->webform)) {
      $nodes[$nid]->webform_export = $nodes[$nid]->webform;
      unset($nodes[$nid]->webform);
    }
  }
}

/**
 * Implements hook_node_export_after_import_alter().
 */
function node_export_webforms_node_export_after_import_alter(&$nodes, $format, $save) {
  // Check if we need to support the Webform Validation module.
  $webform_validation_enabled = module_exists('webform_validation');

  foreach ($nodes as $node) {
    // Check if the node is a webform.
    if (isset($node->webform_export)) {
      $existing_node = entity_uuid_load('node', array($node->uuid));
      $node->webform = $node->webform_export;
      $node->webform['record_exists'] = false;
      unset($node->webform_export);

      if (!empty($existing_node)) {
        $existing_node = reset($existing_node);
        // Delete existing webform records, we're about to recreate it.
        db_delete('webform')
          ->condition('nid', $existing_node->nid)
          ->execute();
        db_delete('webform_component')
          ->condition('nid', $existing_node->nid)
          ->execute();
        db_delete('webform_roles')
          ->condition('nid', $existing_node->nid)
          ->execute();
        // Handle
        if($webform_validation_enabled) {
          // Delete existing rules for this node.
          // Get the existing rule IDs so we can check for components to delete as well.
          $rules = webform_validation_get_node_rules($existing_node->nid);
          foreach ($rules as $rid => $rule) {
            // Delete existing rules before we import new ones.
            webform_dynamic_delete_rule($rid);
          }
        }
      }

      // Save the node so the associated webform data is also handled by the webform module's hooks.
      node_save($node);

      if(isset($node->webform_validation_rules)) {
        // Save new rules.
        foreach ($node->webform_validation_rules as $rule) {
          // Need to manually add a few values to the $rule to emulate the admin form.
          // Emulate the "add" form action.
          $rule['action'] = 'add';
          $rule['nid'] = $node->nid;
          $rule['rule_components'] = $rule['components'];
          unset($rule['ruleid']);
          webform_validation_rule_save($rule);
        }
      }
    }
  }
}

