You are here

function getdirections_other_load_locations in Get Directions 7.2

Same name and namespace in other branches
  1. 7.3 getdirections.module \getdirections_other_load_locations()
1 call to getdirections_other_load_locations()
getdirections_load_locations in ./getdirections.module

File

./getdirections.module, line 1667
Fetches google map directions.

Code

function getdirections_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 = getdirections_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) {
            $locations[$ct]['latitude'] = $g['lat'];
            $locations[$ct]['longitude'] = $g['lon'];
            $locations[$ct][$key] = $id;
            if ($key == 'vid') {
              $locations[$ct]['nid'] = getdirections_get_nid($id);
            }
            $locations[$ct]['key'] = $key;
            $ct++;
          }
        }
      }
    }
  }
  elseif ($module == 'geolocation') {
    $type = 'geolocation_latlng';
    $geolocationfields = getdirections_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) {
            $locations[$ct]['latitude'] = $g['lat'];
            $locations[$ct]['longitude'] = $g['lng'];
            $locations[$ct][$key] = $id;
            if ($key == 'vid') {
              $locations[$ct]['nid'] = getdirections_get_nid($id);
            }
            $locations[$ct]['key'] = $key;
            $locations[$ct]['name'] = $location_name;
            $ct++;
          }
        }
      }
    }
  }

  // sanity check
  if (empty($locations)) {
    return $locations;
  }

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

    // find an addressfield
    $type = 'addressfield';
    $addressfields = getdirections_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) {
            if (isset($locations[$ct])) {
              $locations[$ct] += getdirections_addressfield_convert($a);
            }
            $ct++;
          }
        }
      }
    }
  }
  return $locations;
}