You are here

protected static function FeedImport::createEntity in Feed Import 7

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

Create Entity object

Parameters

array &$feed: Feed info array

object &$item: Current SimpleXMLElement object

Return value

object Created Entity

2 calls to FeedImport::createEntity()
FeedImport::processFeedChunked in ./feed_import.inc.php
Imports and process a huge xml in chunks
FeedImport::processFeedNormal in ./feed_import.inc.php
Imports and process a feed normally

File

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

Class

FeedImport
@file Feed import class for parsing and processing content

Code

protected static function createEntity(&$feed, &$item) {

  // Create new object to hold fields values
  $entity = new stdClass();

  // Check if item already exists
  $uniq = self::getXpathValue($item, $feed['xpath']['#uniq']);

  // Create a hash to identify this item in bd
  $entity->{self::$tempHash} = self::createHash($uniq, $feed['id'], $feed['entity_info']['#entity']);

  // add to hashes array
  self::$generatedHashes[] = $entity->{self::$tempHash};

  // Set default language, this can be changed by language item
  $entity->language = LANGUAGE_NONE;

  // Get all fields
  foreach ($feed['xpath']['#items'] as &$field) {
    $i = 0;
    $aux = '';
    $count = count($field['#xpath']);

    // Check ONCE if we have to filter or prefilter field
    $prefilter = !empty($field['#pre_filter']);
    $filter = !empty($field['#filter']);

    // Loop through xpaths until we have data, otherwise use default value
    while ($i < $count) {
      if (!$field['#xpath'][$i]) {
        $i++;
        continue;
      }
      $aux = self::getXpathValue($item, $field['#xpath'][$i]);
      if ($prefilter) {
        $pfval = self::applyFilter($aux, $field['#pre_filter']);

        // If item doesn't pass prefilter than go to next option
        if (!self::hasContent($pfval)) {
          $i++;
          continue;
        }
        unset($pfval);
      }

      // If filter passed prefilter then apply filter and exit while loop
      if (self::hasContent($aux)) {
        if ($filter) {
          $aux = self::applyFilter($aux, $field['#filter']);
        }
        break;
      }
      $i++;
    }

    // If we don't have any data we take default action
    if (!self::hasContent($aux)) {
      switch ($field['#default_action']) {

        // Provide default value
        // This is also default action
        case 'default_value':
        default:
          $aux = $field['#default_value'];
          break;

        // Provide default value before it was filtered
        case 'default_value_filtered':
          $aux = self::applyFilter($field['#default_value'], $field['#filter']);
          break;

        // Skip this item by returning NULL
        case 'skip_item':
          return NULL;
          break;

        // Don't add this field to entity
        case 'ignore_field':
          continue 2;
          break;
      }
    }

    // Set field value
    if ($field['#column']) {
      if (is_array($aux)) {
        $i = 0;
        foreach ($aux as &$auxv) {
          $entity->{$field['#field']}[$entity->language][$i][$field['#column']] = $auxv;
          $i++;
        }
      }
      else {
        $entity->{$field['#field']}[$entity->language][0][$field['#column']] = $aux;
      }
    }
    else {
      $entity->{$field['#field']} = $aux;
    }

    // No need anymore, free memory
    unset($aux);
  }
  return $entity;
}