You are here

protected static function FeedImport::saveEntities in Feed Import 7

Same name and namespace in other branches
  1. 7.2 feed_import.inc.php \FeedImport::saveEntities()

Saves/updates all created entities

Parameters

array &$feed: Feed info array

array &$items: An array with entities

1 call to FeedImport::saveEntities()
FeedImport::processFeed in ./feed_import.inc.php
This function is choosing process function and executes it

File

./feed_import.inc.php, line 584
Feed import class for parsing and processing content

Class

FeedImport
@file Feed import class for parsing and processing content

Code

protected static function saveEntities(&$feed, &$items) {

  // Get existing items for update
  if (!empty(self::$generatedHashes)) {
    $ids = self::getEntityIdsFromHash(self::$generatedHashes);

    // Reset all generated hashes
    self::$generatedHashes = array();
  }
  else {
    $ids = array();
  }

  // This sets expire timestamp
  $feed['time'] = (int) $feed['time'];

  // Report data
  self::$report['total'] = count($items);

  // Now we create real entityes or update existent
  foreach ($items as &$item) {

    // Check if item is skipped
    if ($item == NULL) {
      continue;
    }

    // Save hash and remove from item
    $hash = $item->{self::$tempHash};
    unset($item->{self::$tempHash});

    // Check if item is already imported
    if (isset($ids[$hash])) {
      $changed = FALSE;

      // Load entity
      try {
        $entity = call_user_func(self::$functionLoad, $ids[$hash]->entity_id);
      } catch (Exception $e) {
        continue;
      }

      // If entity is missing then skip
      if (empty($entity)) {
        unset($entity);
        continue;
      }
      $lang = $item->language;

      // Find if entity is different from last feed
      foreach ($item as $key => &$value) {
        if (is_array($value)) {
          if (!isset($entity->{$key}[$lang]) || empty($entity->{$key}[$lang]) || count($entity->{$key}[$lang]) != count($value[$lang])) {
            $changed = TRUE;
            $entity->{$key} = $value;
          }
          elseif (count($value[$lang]) <= 1) {
            $col = isset($value[$lang][0]) ? key($value[$lang][0]) : '';
            if ($entity->{$key}[$lang][0][$col] != $value[$lang][0][$col]) {
              $changed = TRUE;
              $entity->{$key} = $value;
            }
            unset($col);
          }
          else {
            $col = key($value[$lang][0]);
            $temp = array();
            foreach ($entity->{$key}[$lang] as &$ev) {
              $temp[][$col] = $ev[$col];
            }
            if ($temp != $value[$lang]) {
              $changed = TRUE;
              $entity->{$key} = $value;
            }
            unset($temp, $col);
          }
        }
        else {
          if (!isset($entity->{$key}) || $entity->{$key} != $value) {
            $changed = TRUE;
            $entity->{$key} = $value;
          }
        }
      }
      $ok = TRUE;

      // Check if entity is changed and save changes
      if ($changed) {
        try {
          call_user_func(self::$functionSave, $entity);

          // Set report about updated items
          self::$report['updated']++;
        } catch (Exception $e) {
          $ok = FALSE;
        }
      }
      else {

        // Set report about rescheduled items
        self::$report['rescheduled']++;
      }
      if ($ok) {

        // Add to update ids
        self::updateIds($ids[$hash]->id);
      }

      // Free some memory
      unset($ids[$hash], $entity, $lang);
    }
    else {

      // Mark as new
      $item->{$feed['entity_info']['#table_pk']} = NULL;
      $ok = TRUE;
      try {

        // Save imported item
        call_user_func(self::$functionSave, $item);
      } catch (Exception $e) {
        $ok = FALSE;
      }
      if ($ok) {
        $vars = array(
          $feed['id'],
          $feed['entity_info']['#entity'],
          $item->{$feed['entity_info']['#table_pk']},
          $hash,
          $feed['time'] ? time() + $feed['time'] : 0,
        );

        // Insert into feed import hash table
        self::insertItem($vars);

        // Set report about new items
        self::$report['new']++;
      }
    }

    // No need anymore
    $item = NULL;
  }

  // No need anymore
  unset($items, $ids);

  // Insert left items
  self::insertItem(NULL);
  $vars = array(
    'expire' => $feed['time'] ? time() + $feed['time'] : 0,
    'feed_id' => $feed['id'],
  );

  // Update ids for existing items
  self::updateIds($vars);
}