You are here

function location_feeds_set_point in Location Feeds 7

Same name and namespace in other branches
  1. 6 location_feeds.module \location_feeds_set_point()

Sets a latitude or longitude point for the node and attempts to recognize either decimal or DMS notation.

Parameters

$source: Field mapper source settings.

object $entity:

string $target: The target that invoked this

$value:

Return value

$entity

1 string reference to 'location_feeds_set_point'
_location_feeds_fill_targets in ./location_feeds.module
Helper function to add target fields

File

./location_feeds.module, line 280
This module provides location mappers to feed importers.

Code

function location_feeds_set_point($source, $entity, $target, $value) {
  list($field_name, $sub_field) = explode(':', $target);
  if (strpos($sub_field, '][') !== FALSE) {
    list($sub_field, $last_field) = explode('][', $sub_field, 2);
  }
  if (!is_array($value)) {
    $value = array(
      $value,
    );
  }

  // Check if this is a cck field since they're added to the node
  // differently.
  $cck = preg_match('/^field_.+/', $target);
  foreach ($value as $i => $val) {
    $val = trim($val);

    // Attempt to set a value for DMS notation.
    if (!is_numeric($val)) {
      $neg = FALSE;
      if (strpos($val, '-') === 0) {
        $neg = TRUE;
      }
      $val = trim(preg_replace('/[^\\d\\s\\.WSws]/', '', $val));
      $dms = explode(' ', $val);

      // Account for '77° 02' 15.691" W' format.
      if (count($dms) == 4) {
        if ($dms[3] == 'W' || $dms[3] == 'w' || $dms[3] == 'S' || $dms[3] == 's') {
          $neg = TRUE;
          unset($dms[3]);
        }
      }
      if (count($dms) == 3) {
        $val = (abs($dms[0]) + $dms[1] / 60 + $dms[2] / 3600) * ($neg ? -1 : 1);
      }
    }
    if (isset($last_field)) {
      if ($cck) {
        if (!isset($entity->{$field_name}['und'][$i]['location_settings'])) {
          $entity->{$field_name}['und'][$i]['location_settings'] = location_feeds_get_field_info($field_name);
        }
        $entity->{$field_name}['und'][$i][$sub_field][$last_field] = $val;
      }
      else {
        $entity->{$field_name}[$i][$sub_field][$last_field] = $val;
      }
    }
    else {
      if ($cck) {
        if (!isset($entity->{$field_name}['und'][$i]['location_settings'])) {
          $entity->{$field_name}['und'][$i]['location_settings'] = location_feeds_get_field_info($field_name);
        }
        $entity->{$field_name}['und'][$i][$sub_field] = $val;
      }
      else {
        $entity->{$field_name}[$i][$sub_field] = $val;
      }
    }
  }
  return $entity;
}