<?php

/**
 * @file
 * Feeds Tamper - basic API functions and hook implementations.
 */

/**
 * Load version agnostic code.
 */
include_once('feeds_tamper.inc');

/**
 * Implements hook_feeds_after_parse().
 *
 * This is the meat of the whole deal. After every Feeds run, before going into
 * processing, this gets called and modifies the feed items.
 *
 * @todo Add support for source plugins.
 */
function feeds_tamper_feeds_after_parse(FeedsSource $source, FeedsParserResult $result) {
  $importer_instances = feeds_tamper_load_by_importer($source->importer->id, FALSE);

  // Don't go through all of the nonsense if we don't have anything to do.
  if (empty($importer_instances)) {
    return;
  }

  $parser = $source->importer->parser;
  $is_csv = get_class($parser) == 'FeedsCSVParser';

  $sources = array_fill_keys(feeds_tamper_get_unique_source_list($source->importer), '');

  $plugins = feeds_tamper_get_plugins();
  foreach (array_keys($result->items) as $item_key) {
    // Manually advance the current_item key since we can't use shiftItem().
    $result->current_item = $result->items[$item_key];

    // Initialize every source value.
    $result->items[$item_key] += $sources;

    foreach ($importer_instances as $element_key => $instances) {
      if ($is_csv) {
        $element_key = drupal_strtolower($element_key);
      }

      // Plugins assume that everything lives in the item array.
      $result->items[$item_key][$element_key] = $parser->getSourceElement($source, $result, $element_key);
      $is_array = is_array($result->items[$item_key][$element_key]);

      foreach ($instances as $instance) {
        // If the item was unset by previous plugin, jump ahead.
        if (!isset($result->items[$item_key])) {
          break 2;
        }

        $plugin = $plugins[$instance->plugin_id];

        if ($is_array && $plugin['multi'] == 'loop') {
          foreach ($result->items[$item_key][$element_key] as &$i) {
            $plugin['callback']($result, $item_key, $element_key, $i, $instance->settings, $source);
          }
        }
        elseif ($is_array && $plugin['multi'] == 'direct') {
          $plugin['callback']($result, $item_key, $element_key, $result->items[$item_key][$element_key], $instance->settings, $source);
        }
        elseif (!$is_array && $plugin['single'] != 'skip') {
          $plugin['callback']($result, $item_key, $element_key, $result->items[$item_key][$element_key], $instance->settings, $source);
        }
      }
    }
  }

  $result->current_item = NULL;
}

/**
 * Implements hook_feeds_parser_sources_alter().
 */
function feeds_tamper_feeds_parser_sources_alter(&$sources, $content_type) {
  $sources['Blank source 1'] = array(
    'description' => t("A source provided by Feeds Tamper with no value of it's own."),
  );
}

/**
 * Implements hook_feeds_processor_targets_alter().
 */
function feeds_tamper_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
  $targets['Temporary target 1'] = array(
    'description' => t('A field that stores the source data temporarily so that it can be used with the Feeds Tamper rewrite plugin.'),
  );
}

/**
 * Core hooks.
 */

/**
 * Implements hook_permission().
 */
function feeds_tamper_permission() {
  $perms = array(
    'administer feeds_tamper' => array(
      'title' => t('Administer Feeds Tamper'),
      'description' => t('Create, edit and delete plugins for any importer.'),
    ),
  );
  foreach (feeds_importer_load_all() as $importer) {
    $name = array('%name' => $importer->config['name']);
    $perms['tamper ' . $importer->id] = array(
      'title' => t('Tamper %name feeds', $name),
      'description' => t('Create, edit and delete plugins for %name feeds.', $name),
    );
  }
  return $perms;
}

/**
 * Implements hook_ctools_plugin_type().
 */
function feeds_tamper_ctools_plugin_type() {
  return array(
    'plugins' => array(
      'use hooks' => FALSE,
      'defaults' => array(
        'validate' => FALSE,
        'multi' => FALSE,
        'category' => 'Other',
        'single' => FALSE,
        'default description' => '',
        'defaults' => array(),
      ),
    ),
  );
}

/**
 * Exports a list of Feeds Tamper plugins to code.
 *
 * @param string $importer_id
 *   The id of the importer to export plugins for.
 * @param string $indent
 *   The level of indentation. Defaults to no indentation.
 *
 * @return string
 *   An exported list of plugins.
 */
function feeds_tamper_export($importer_id, $indent = '') {
  ctools_include('export');
  $result = ctools_export_load_object('feeds_tamper', 'conditions', array('importer' => $importer_id));

  if (!empty($result)) {

    $output = array('$export = array();' . "\n");

    foreach ($result as $object) {
      $output[] = ctools_export_object('feeds_tamper', $object, $indent);
      $output[] = '$export[$feeds_tamper->id] = $feeds_tamper;' . "\n";
    }

    return trim(implode("\n", $output));
  }
}
