You are here

public function FeedsDataProcessor::process in Feeds 6

Implementation of FeedsProcessor::process().

Overrides FeedsProcessor::process

File

plugins/FeedsDataProcessor.inc, line 16
Definition of FeedsDataProcessor.

Class

FeedsDataProcessor
Creates simple table records from feed items. Uses Data module.

Code

public function process(FeedsImportBatch $batch, FeedsSource $source) {

  // Count number of created and updated nodes.
  $inserted = $updated = 0;
  $expiry_time = $this
    ->expiryTime();
  while ($item = $batch
    ->shiftItem()) {
    $id = $this
      ->existingItemId($batch, $source);
    if ($id === FALSE || $this->config['update_existing']) {

      // Map item to a data record, feed_nid and timestamp are mandatory.
      $data = array();
      $data['feed_nid'] = $source->feed_nid;
      $data = $this
        ->map($batch, $data);
      if (!isset($data['timestamp'])) {
        $data['timestamp'] = FEEDS_REQUEST_TIME;
      }

      // Only save if this item is not expired.
      if ($expiry_time != FEEDS_EXPIRE_NEVER && $data['timestamp'] < FEEDS_REQUEST_TIME - $expiry_time) {
        continue;
      }

      // Save data.
      if ($id !== FALSE) {
        $data['id'] = $id;
        $this
          ->handler()
          ->update($data, 'id');
        $updated++;
      }
      else {
        $this
          ->handler()
          ->insert($data);
        $inserted++;
      }
    }
  }

  // Set messages.
  if ($inserted) {
    drupal_set_message(format_plural($inserted, 'Created @number item.', 'Created @number items.', array(
      '@number' => $inserted,
    )));
  }
  if ($updated) {
    drupal_set_message(format_plural($updated, 'Updated @number item.', 'Updated @number items.', array(
      '@number' => $updated,
    )));
  }
  if (!$inserted && !$updated) {
    drupal_set_message(t('There are no new items.'));
  }
}