You are here

protected function RelationFeedsProcessor::existingEntityId in Relation 7

File

relation_feeds/RelationFeedsProcessor.inc, line 341
Class definition of RelationFeedsProcessor.

Class

RelationFeedsProcessor
Creates relations from feed items.

Code

protected function existingEntityId(FeedsSource $source, FeedsParserResult $result) {
  if ($rid = parent::existingEntityId($source, $result)) {
    return $rid;
  }

  // Iterate through all unique targets and test whether they do already
  // exist in the database.
  foreach ($this
    ->uniqueTargets($source, $result) as $target => $value) {
    switch ($target) {
      case 'rid':
        $rid = db_query("SELECT rid FROM {relation} WHERE rid = :rid", array(
          ':rid' => $value,
        ))
          ->fetchField();
        break;
      case 'feeds_source':
        if ($id = feeds_get_importer_id($this->config['bundle'])) {
          $rid = db_query("SELECT fs.feed_rid FROM {relation} r JOIN {feeds_source} fs ON r.rid = fs.feed_rid WHERE fs.id = :id AND fs.source = :source", array(
            ':id' => $id,
            ':source' => $value,
          ))
            ->fetchField();
        }
        break;
    }
    if ($rid) {

      // Return with the first rid found.
      return $rid;
    }
  }

  // If other unique targets didn't match this relation check if endpoints
  // are unique.
  if ($this->config['unique_enpoints']) {
    try {
      $endpoints = $this
        ->mapEndpoints($source, $result);
    } catch (FeedsValidationException $f) {
      return 0;
    }
    $type = $this
      ->getTypeInfo();
    $relations = relation_relation_exists($endpoints, $this->config['bundle'], $type->directional);
    if ($relations) {
      return key($relations);
    }
  }
  return 0;
}