You are here

function getlocations_other_load_locations in Get Locations 7.2

Same name and namespace in other branches
  1. 7 getlocations.module \getlocations_other_load_locations()

Function to fetch locations. Supports Geofield and Geolocation modules

Parameters

int $entity_id Entity identifier:

string $entity_type Entity type:

string $module Module name:

Return value

array $locations

1 call to getlocations_other_load_locations()
getlocations_load_locations in ./getlocations.module
Function to fetch locations

File

./getlocations.module, line 2820
getlocations.module @author Bob Hutchinson http://drupal.org/user/52366 @copyright GNU GPL

Code

function getlocations_other_load_locations($entity_id, $entity_type, $module = '') {
  global $language;
  $locations = array();
  if (empty($module)) {
    return $locations;
  }
  $entity_get_info = entity_get_info($entity_type);
  $load_hook = $entity_get_info['load hook'];
  $obj = $load_hook($entity_id);
  $entity_title = $entity_get_info['entity keys']['label'];
  $entity_key = $entity_get_info['entity keys']['id'];

  // nid, cid, uid etc
  $location_name = $obj->{$entity_title};

  // get the field_xxx names
  $objfields = array();
  foreach ($obj as $k => $therest) {
    if (preg_match("/^field_/", $k)) {
      $objfields[] = $k;
    }
  }
  if (empty($objfields)) {
    return $locations;
  }

  // find a geofield
  if ($module == 'geofield') {
    $type = 'geofield';
    $geofields = getlocations_other_get_fieldname($type, $module, $entity_type);
    if (!empty($geofields)) {
      $geofield = '';
      foreach ($objfields as $field) {
        if (in_array($field, $geofields)) {
          $geofield = $field;
        }
      }
      if (empty($geofield)) {
        return $locations;
      }

      // got one
      $geo = $obj->{$geofield};
      $geo_keys = array_keys($geo);
      foreach ($geo_keys as $lang) {
        if ($lang == $language->language || isset($obj->language) && $lang == $obj->language || $lang == 'und') {
          $ct = 0;
          foreach ($geo[$lang] as $g) {
            if (isset($g['lat']) && isset($g['lon'])) {
              if ($latlon = getlocations_latlon_check($g['lat'] . ',' . $g['lon'])) {
                $ll = explode(',', $latlon);
                $locations[$ct]['latitude'] = $ll[0];
                $locations[$ct]['longitude'] = $ll[1];
                $locations[$ct]['entity_key'] = $entity_key;
                $locations[$ct]['entity_id'] = $entity_id;
                $locations[$ct]['entity_type'] = $entity_type;
                $locations[$ct]['name'] = $location_name;
                $ct++;
              }
            }
          }
        }
      }
    }
  }
  elseif ($module == 'geolocation') {
    $type = 'geolocation_latlng';
    $geolocationfields = getlocations_other_get_fieldname($type, $module, $entity_type);
    if (!empty($geolocationfields)) {
      $geolocationfield = '';
      foreach ($objfields as $field) {

        // test for geolocationfields
        if (in_array($field, $geolocationfields)) {
          $geolocationfield = $field;
        }
      }
      if (empty($geolocationfield)) {
        return $locations;
      }

      // got one
      $geo = $obj->{$geolocationfield};
      $geo_keys = array_keys($geo);
      foreach ($geo_keys as $lang) {
        if ($lang == $language->language || isset($obj->language) && $lang == $obj->language || $lang == 'und') {
          $ct = 0;
          foreach ($geo[$lang] as $g) {
            if (isset($g['lat']) && isset($g['lng'])) {
              if ($latlon = getlocations_latlon_check($g['lat'] . ',' . $g['lng'])) {
                $ll = explode(',', $latlon);
                $locations[$ct]['latitude'] = $ll[0];
                $locations[$ct]['longitude'] = $ll[1];
                $locations[$ct]['entity_key'] = $entity_key;
                $locations[$ct]['entity_id'] = $entity_id;
                $locations[$ct]['entity_type'] = $entity_type;
                $locations[$ct]['name'] = $location_name;
                $ct++;
              }
            }
          }
        }
      }
    }
  }

  // addressfield
  if (module_exists('addressfield')) {

    // find an addressfield
    $type = 'addressfield';
    $addressfields = getlocations_other_get_fieldname($type, 'addressfield', $entity_type);
    if (!empty($addressfields)) {
      foreach ($objfields as $field) {
        if (in_array($field, $addressfields)) {
          $addressfield = $field;
        }
      }
      if (empty($addressfield)) {
        return $locations;
      }

      // got one
      $addr = $obj->{$addressfield};
      $addr_keys = array_keys($addr);
      foreach ($addr_keys as $lang) {
        if ($lang == $language->language || isset($obj->language) && $lang == $obj->language || $lang == 'und') {
          $ct = 0;
          foreach ($addr[$lang] as $a) {
            $locations[$ct] += getlocations_addressfield_convert($a);
            $ct++;
          }
        }
      }
    }
  }
  return $locations;
}