<?php

/**
 * @file
 * The Node export relation module.
 *
 * Helps maintain relationships between nodes during node export operations.
 */

/**
 * Callback for node reference settings form.
 */
function node_export_relation_form_node_export_settings_alter(&$form, &$form_state, $form_id) {
  if (module_exists('node_reference')) {
    module_load_include('inc', 'node_export_relation', 'node_export_relation.node_reference');
    node_export_relation_settings_form_node_reference($form, $form_state, $form_id);
  }
  /*
  // None of the settings are supported yet.

  if (module_exists('og')) {
    module_load_include('inc', 'node_export_relation', 'node_export_relation.og');
    node_export_relation_settings_form_og($form, $form_state, $form_id);
  }
  */
}

/**
 * Implements hook_node_export_alter().
 */
function node_export_relation_node_export_alter(&$nodes, $format) {

  if (module_exists('node_reference')) {
    module_load_include('inc', 'node_export_relation', 'node_export_relation.node_reference');
    if (variable_get('node_export_node_reference_auto_inc', TRUE)) {
      $keyed_nodes = array();
      foreach ($nodes as $node) {
        $keyed_nodes[$node->nid] = $node;
      }
      // Keyed nodes are important for preventing same node loaded again via references.
      foreach (array_keys($keyed_nodes) as $nid) {
        node_export_relation_node_reference_load($keyed_nodes, $nid);
      }
      $nodes = array_values($keyed_nodes);
    }
  }

  if (module_exists('og')) {
    module_load_include('inc', 'node_export_relation', 'node_export_relation.og');
    // Go through nodes and set UUIDs for their groups.
    node_export_relation_og_set_group_uuids($nodes);
  }

  if (module_exists('taxonomy')) {
    module_load_include('inc', 'node_export_relation', 'node_export_relation.taxonomy');
    foreach ($nodes as $node) {
      node_export_relation_taxonomy_build_term_reference($node);
    }
  }

}

/**
 * Implements hook_node_export_import_alter().
 */
function node_export_relation_node_export_import_alter(&$nodes, $format) {

  if (module_exists('node_reference')) {
    module_load_include('inc', 'node_export_relation', 'node_export_relation.node_reference');
    foreach ($nodes as $node) {
      node_export_relation_node_reference_map($node->nid, $node->uuid);
    }
  }

  if (module_exists('og')) {
    module_load_include('inc', 'node_export_relation', 'node_export_relation.og');
    // Now go through the groupes and switch back the UUIDS to actual ids.
    node_export_relation_og_set_group_nids($nodes);
  }

  if (module_exists('taxonomy')) {
    module_load_include('inc', 'node_export_relation', 'node_export_relation.taxonomy');
    foreach ($nodes as $node) {
      node_export_relation_taxonomy_restore_term_reference($node);
    }
  }

}

/**
 * Implements hook_node_export_after_import_alter().
 */
function node_export_relation_node_export_after_import_alter(&$nodes, $format) {

  if (module_exists('node_reference')) {
    module_load_include('inc', 'node_export_relation', 'node_export_relation.node_reference');
    // Now that all nodes are imported/updated we can restore the node referenced from UUIDS
    // NOTE: This is done because if the node referenced node is new, they we have
    // to wait for node to be saved.
    node_export_relation_node_reference_update($nodes);
  }

}

