You are here

function getlocations_other_load_locations in Get Locations 7

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

Function to fetch locations. Supports Geofield and Geolocation modules

Parameters

int $id Entity identifier:

string $key Entity key:

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 3122
getlocations.module @author Bob Hutchinson http://drupal.org/user/52366 @copyright GNU GPL

Code

function getlocations_other_load_locations($id, $key = 'vid', $module = '') {
  global $language;
  $locations = array();
  if (empty($module)) {
    return $locations;
  }
  if ($key == 'nid') {
    $entity_type = 'node';
    $obj = node_load($id);
    $location_name = $obj->title;
  }
  elseif ($key == 'vid') {
    $entity_type = 'node';
    $obj = node_load(NULL, $id);
    $location_name = $obj->title;
  }
  elseif ($key == 'uid') {
    $entity_type = 'user';
    $obj = user_load($id);
    $location_name = $obj->name;
  }
  elseif ($key == 'tid') {
    $entity_type = 'taxonomy_term';
    $obj = taxonomy_term_load($id);
    $location_name = $obj->name;
  }
  elseif ($key == 'cid') {
    $entity_type = 'comment';
    $obj = comment_load($id);
    $location_name = $obj->subject;
  }

  // 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][$key] = $id;
                if ($key == 'vid') {
                  $locations[$ct]['nid'] = getlocations_get_nid($id);
                }
                $locations[$ct]['key'] = $key;
                $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][$key] = $id;
                if ($key == 'vid') {
                  $locations[$ct]['nid'] = getlocations_get_nid($id);
                }
                $locations[$ct]['key'] = $key;
                $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;
}