You are here

public static function FeedImport::createEntity in Feed Import 7.2

Same name and namespace in other branches
  1. 7 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

6 calls to FeedImport::createEntity()
FeedImport::processCSV in ./feed_import.inc.php
Imports and process a CSV file First line must contain column names!
FeedImport::processHTMLPage in ./feed_import.inc.php
Imports and process a HTML page
FeedImport::processJSON in ./feed_import.inc.php
Process JSON.
FeedImport::processXML in ./feed_import.inc.php
Imports and process a feed normally
FeedImport::processXMLChunked in ./feed_import.inc.php
Imports and process a huge xml in chunks

... See full list

File

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

Class

FeedImport
@file Feed import class for parsing and processing content.

Code

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

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

  // Reference current entity.
  self::$current = $entity;

  // Check if items must be monitorized and saved in hashes table.
  if ($feed['xpath']['#uniq']) {

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

    // Hash item can be a property so we must extract it.
    if (is_array($uniq)) {
      $uniq = isset($uniq[0]) ? $uniq[0] : reset($uniq);
    }

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

    // Add to hashes array.
    self::$generatedHashes[] = $entity->{self::$tempHash};
  }
  else {
    $entity->{self::$tempHash} = NULL;
  }

  // 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']);

    // 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 ($field['#pre_filter']) {
        $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 ($field['#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 = $field['#filter'] ? self::applyFilter($field['#default_value'], $field['#filter']) : NULL;
          break;

        // Skip this item by returning NULL.
        case 'skip_item':
          self::$current = NULL;
          return NULL;
          break;

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

    // Set field value.
    // If is object then don't set just column value, set object-array value.
    if ($field['#column']) {
      if (is_array($aux)) {
        $i = 0;
        foreach ($aux as &$auxv) {
          if (is_object($auxv)) {
            $auxv = (array) $auxv;
            $entity->{$field['#field']}[$entity->language][$i] = $auxv;
          }
          else {
            $entity->{$field['#field']}[$entity->language][$i][$field['#column']] = $auxv;
          }
          $i++;
        }
      }
      else {
        if (is_object($aux)) {
          $aux = (array) $aux;
          $entity->{$field['#field']}[$entity->language][0] = $aux;
        }
        else {
          $entity->{$field['#field']}[$entity->language][0][$field['#column']] = $aux;
        }
      }
    }
    else {

      // If this isn't a field then get only first value.
      if (is_array($aux) || is_object($aux)) {

        // This still can be array but if so then problem is elsewhere.
        $aux = reset($aux);
      }
      $entity->{$field['#field']} = $aux;
    }

    // No need anymore, free memory.
    unset($aux);
  }
  self::$current = NULL;
  return $entity;
}