<?php

/**
 * Implements hook_features_api().
 */
function node_export_features_features_api() {
  $features = array();
  $features['node_export_features'] = array(
    'name' => t('Node export'),
    'feature_source' => TRUE,
    'default_hook' => 'node_export_features_default',
    'default_file' => FEATURES_DEFAULTS_INCLUDED,
  );
  return $features;
}

/**
 * Implements hook_features_export_options().
 */
function node_export_features_features_export_options() {
  $options = array();

  $query = db_select('node', 'n')
    ->fields('n', array('nid', 'title', 'type'))
    ->orderBy('type')
    ->orderBy('title')
    ->range(0, 250)
    ->addTag('node_export_features');

  $result = $query->execute();

  foreach ($result as $row) {
    $uuid = uuid_get_uuid('node', 'nid', $row->nid);
    if (empty($uuid)) {
      drupal_set_message(
        t('Some nodes are <strong>not</strong> available for export' .
          ' because of missing UUIDs. Ensure UUIDs are being generated for' .
          ' all content types and click the <em>Create missing UUIDs</em>' .
          ' button on the <a href="!url">UUID settings page</a> to help' .
          ' resolve this issue.',
          array('!url' => url('admin/settings/uuid'))
        ),
        'warning',
        FALSE
      );
    }
    else {
      $options[$uuid] = t('@type: @title', array(
        '@type' => node_type_get_name($row->type),
        '@title' => $row->title,
      ));
    }
  }

  if (count($options) == 250) {
    drupal_set_message(
      t('Due to limitations in Features only the first 250 nodes are available.'
      . '  The query is tagged <em>node_export_features</em> if you want to'
      . ' alter it.'),
      'warning'
    );
  }

  return $options;
}

/**
 * Implements hook_features_export().
 */
function node_export_features_features_export($data, &$export, $module_name = '') {
  $pipe = array();

  $export['dependencies']['module'] = 'node_export_features';

  foreach ($data as $uuid) {
    $query = db_select('node', 'n')
      ->fields('n', array('type'))
      ->condition('n.uuid', $uuid);
    $type = $query->execute()->fetchField();

    $export['features']['node_export_features'][$uuid] = $uuid;
    $pipe['node'][$type] = $type;
  }

  return $pipe;
}

/**
 * Implements hook_features_export_render().
 */
function node_export_features_features_export_render($module, $data, $export = NULL) {
  $nids = entity_get_id_by_uuid('node', $data);
  $result = node_export($nids);
  if ($result['success']) {
    $node_export['code_string'] = $result['output'];
    $node_export_code = '  $node_export = ' . features_var_export($node_export) . ';';
  }
  else {
    foreach ($result['output'] as $error) {
      $node_export_code  = '  // ' . $error . PHP_EOL;
    }
    $node_export_code .= '  $node_export = array();';
  }
  $node_export_code .= PHP_EOL . '  return $node_export;';
  return array('node_export_features_default' => $node_export_code);
}

/**
 * Implements hook_features_revert().
 */
function node_export_features_features_revert($module = NULL) {
  node_export_features_features_rebuild($module);
}

/**
 * Implements hook_features_rebuild().
 */
function node_export_features_features_rebuild($module) {
  $node_export = features_get_default('node_export_features', $module);
  if (!empty($node_export)) {
    $result = node_export_import($node_export['code_string']);
    if (!$result['success']) {
      foreach ($result['output'] as $error) {
        drupal_set_message($error, 'error');
      }
    }
    else {
      foreach ($result['output'] as $status) {
        drupal_set_message($status);
      }
    }
  }
}
