You are here

public function FeedsTermProcessor::process in Feeds 6

Same name and namespace in other branches
  1. 7 plugins/FeedsTermProcessor.inc \FeedsTermProcessor::process()

Implementation of FeedsProcessor::process().

Overrides FeedsProcessor::process

File

plugins/FeedsTermProcessor.inc, line 16
FeedsTermProcessor class.

Class

FeedsTermProcessor
Feeds processor plugin. Create taxonomy terms from feed items.

Code

public function process(FeedsImportBatch $batch, FeedsSource $source) {
  if (empty($this->config['vocabulary'])) {
    throw new Exception(t('You must define a vocabulary for Taxonomy term processor before importing.'));
  }

  // Count created, updated, and invalid terms.
  $created = $updated = $no_name = 0;
  while ($item = $batch
    ->shiftItem()) {

    // Create/update if item does not exist or update existing is enabled.
    if (!($tid = $this
      ->existingItemId($batch, $source)) || $this->config['update_existing'] != FEEDS_SKIP_EXISTING) {

      // Map feed item to a term.
      $term = array();

      // Add term id if available.
      if ($tid) {
        $term['tid'] = $tid;
      }

      // Load the term if configured to update existing terms.
      if (!empty($term['tid']) && $this->config['update_existing'] == FEEDS_UPDATE_EXISTING) {

        // Load term.
        $term = (array) taxonomy_get_term($term['tid'], TRUE);

        // Load feeds_term_item data.
        if ($feeds_term_item = db_fetch_array(db_query("SELECT imported, guid, url, feed_nid FROM {feeds_term_item} WHERE tid = %d", $term['tid']))) {
          $term['feeds_term_item'] = $feeds_term_item;
        }
        else {
          $term['feeds_term_item'] = array();
        }

        // Allow other modules to act.
        // @todo this breaks if hooks don't exist, or hooks don't return the term
        if (module_implements('feeds_taxonomy_load')) {
          $term = module_invoke_all('feeds_taxonomy_load', $term);
        }
      }

      // Add feeds_term_item data.
      $term['feeds_term_item']['id'] = $this->id;

      // Set some defaults.
      $term['feeds_term_item'] += array(
        'feed_nid' => $source->feed_nid,
        'imported' => FEEDS_REQUEST_TIME,
        'url' => '',
        'guid' => '',
      );

      // Map targets.
      $term = $this
        ->map($batch, $term);

      // Check if term name is set, otherwise continue.
      if (empty($term['name'])) {
        $no_name++;
        continue;
      }

      // Save the term.
      $status = taxonomy_save_term($term);

      // Track new and updated terms.
      if ($status === SAVED_UPDATED) {
        $updated++;
      }
      elseif ($status === SAVED_NEW) {
        $created++;
      }
    }
  }

  // Set messages.
  $vocabulary = $this
    ->vocabulary();
  if ($no_name) {
    drupal_set_message(format_plural($no_name, 'There was @number term that could not be imported because its name was empty. Check the mapping settings for the associated taxonomy term processor.', 'There were @number terms that could not be imported because their names were empty. Check the mapping settings for the associated taxonomy term processor.', array(
      '@number' => $no_name,
    )), 'error');
  }
  if ($created) {
    drupal_set_message(format_plural($created, 'Created @number term in !vocabulary.', 'Created @number terms in !vocabulary.', array(
      '@number' => $created,
      '!vocabulary' => $vocabulary->name,
    )));
  }
  elseif ($updated) {
    drupal_set_message(format_plural($updated, 'Updated @number term in !vocabulary.', 'Updated @number terms in !vocabulary.', array(
      '@number' => $updated,
      '!vocabulary' => $vocabulary->name,
    )));
  }
  else {
    drupal_set_message(t('There are no new terms.'));
  }
}