function getdirections_other_load_locations in Get Directions 7.3
Same name and namespace in other branches
- 7.2 getdirections.module \getdirections_other_load_locations()
Load one or more locations For Geofield and Geolocation modules
Parameters
int $entity_id:
string $entity_type:
Return value
array
1 call to getdirections_other_load_locations()
- getdirections_load_locations in ./
getdirections.module - Load one or more locations
File
- ./
getdirections.module, line 1746 - Fetches google map directions.
Code
function getdirections_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 = '';
if (isset($entity_get_info['entity keys']['label'])) {
$entity_title = $entity_get_info['entity keys']['label'];
}
elseif ($entity_type == 'user') {
$entity_title = 'name';
}
$entity_key = $entity_get_info['entity keys']['id'];
// nid, cid, uid etc
$location_name = '';
if (isset($obj->{$entity_title})) {
$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 = getdirections_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]['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 = getdirections_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]['entity_key'] = $entity_key;
$locations[$ct]['entity_id'] = $entity_id;
$locations[$ct]['entity_type'] = $entity_type;
$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_get_fieldname($type, 'addressfield', $entity_type);
if (!empty($addressfields)) {
$addressfield = '';
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;
}