aboutsummaryrefslogtreecommitdiff
path: root/engine/lib/location.php
diff options
context:
space:
mode:
Diffstat (limited to 'engine/lib/location.php')
-rw-r--r--engine/lib/location.php277
1 files changed, 82 insertions, 195 deletions
diff --git a/engine/lib/location.php b/engine/lib/location.php
index f3aae709b..1534c7d7b 100644
--- a/engine/lib/location.php
+++ b/engine/lib/location.php
@@ -2,28 +2,24 @@
/**
* Elgg geo-location tagging library.
*
- * @package Elgg
- * @subpackage Core
+ * @package Elgg.Core
+ * @subpackage Location
*/
/**
* Encode a location into a latitude and longitude, caching the result.
*
* Works by triggering the 'geocode' 'location' plugin
- * hook, and requires a geocoding module to be installed
- * activated in order to work.
+ * hook, and requires a geocoding plugin to be installed.
*
- * @param String $location The location, e.g. "London", or "24 Foobar Street, Gotham City"
- *
- * @return string
+ * @param string $location The location, e.g. "London", or "24 Foobar Street, Gotham City"
+ * @return string|false
*/
function elgg_geocode_location($location) {
global $CONFIG;
- // Handle cases where we are passed an array (shouldn't be
- // but can happen if location is a tag field)
if (is_array($location)) {
- $location = implode(', ', $location);
+ return false;
}
$location = sanitise_string($location);
@@ -38,7 +34,7 @@ function elgg_geocode_location($location) {
// Trigger geocode event if not cached
$return = false;
- $return = trigger_plugin_hook('geocode', 'location', array('location' => $location), $return);
+ $return = elgg_trigger_plugin_hook('geocode', 'location', array('location' => $location), $return);
// If returned, cache and return value
if (($return) && (is_array($return))) {
@@ -58,213 +54,104 @@ function elgg_geocode_location($location) {
/**
* Return entities within a given geographic area.
*
- * @param float $lat Latitude
- * @param float $long Longitude
- * @param float $radius The radius
- * @param string $type The type of entity (eg "user", "object" etc)
- * @param string $subtype The arbitrary subtype of the entity
- * @param int $owner_guid The GUID of the owning user
- * @param string $order_by The field to order by; by default, time_created desc
- * @param int $limit The number of entities to return; 10 by default
- * @param int $offset The indexing offset, 0 by default
- * @param boolean $count Count entities
- * @param int $site_guid Site GUID. 0 for current, -1 for any
- * @param int|array $container_guid Container GUID
+ * Also accepts all options available to elgg_get_entities().
+ *
+ * @see elgg_get_entities
+ *
+ * @param array $options Array in format:
+ *
+ * latitude => FLOAT Latitude of the location
*
- * @return array A list of entities.
+ * longitude => FLOAT Longitude of the location
+ *
+ * distance => FLOAT/ARR (
+ * latitude => float,
+ * longitude => float,
+ * )
+ * The distance in degrees that determines the search box. A
+ * single float will result in a square in degrees.
+ * @warning The Earth is round.
+ *
+ * @see ElggEntity::setLatLong()
+ *
+ * @return mixed If count, int. If not count, array. false on errors.
+ * @since 1.8.0
*/
-function get_entities_in_area($lat, $long, $radius, $type = "", $subtype = "", $owner_guid = 0,
-$order_by = "", $limit = 10, $offset = 0, $count = false, $site_guid = 0, $container_guid) {
+function elgg_get_entities_from_location(array $options = array()) {
global $CONFIG;
-
- if ($subtype === false || $subtype === null || $subtype === 0) {
+
+ if (!isset($options['latitude']) || !isset($options['longitude']) ||
+ !isset($options['distance'])) {
return false;
}
- $lat = (float)$lat;
- $long = (float)$long;
- $radius = (float)$radius;
-
- $order_by = sanitise_string($order_by);
- $limit = (int)$limit;
- $offset = (int)$offset;
- $site_guid = (int) $site_guid;
- if ($site_guid == 0) {
- $site_guid = $CONFIG->site_guid;
- }
-
- $where = array();
-
- if (is_array($type)) {
- $tempwhere = "";
- if (sizeof($type)) {
- foreach ($type as $typekey => $subtypearray) {
- foreach ($subtypearray as $subtypeval) {
- $typekey = sanitise_string($typekey);
- if (!empty($subtypeval)) {
- $subtypeval = (int) get_subtype_id($typekey, $subtypeval);
- } else {
- $subtypeval = 0;
- }
- if (!empty($tempwhere)) {
- $tempwhere .= " or ";
- }
-
- $tempwhere .= "(e.type = '{$typekey}' and e.subtype = {$subtypeval})";
- }
- }
- }
- if (!empty($tempwhere)) {
- $where[] = "({$tempwhere})";
- }
+ if (!is_array($options['distance'])) {
+ $lat_distance = (float)$options['distance'];
+ $long_distance = (float)$options['distance'];
} else {
- $type = sanitise_string($type);
- $subtype = get_subtype_id($type, $subtype);
-
- if ($type != "") {
- $where[] = "e.type='$type'";
- }
-
- if ($subtype !== "") {
- $where[] = "e.subtype=$subtype";
- }
- }
-
- if ($owner_guid != "") {
- if (!is_array($owner_guid)) {
- $owner_array = array($owner_guid);
- $owner_guid = (int) $owner_guid;
- $where[] = "e.owner_guid = '$owner_guid'";
- } else if (sizeof($owner_guid) > 0) {
- $owner_array = array_map('sanitise_int', $owner_guid);
- // Cast every element to the owner_guid array to int
- $owner_guid = implode(",", $owner_guid); //
- $where[] = "e.owner_guid in ({$owner_guid})" ; //
- }
- if (is_null($container_guid)) {
- $container_guid = $owner_array;
- }
+ $lat_distance = (float)$options['distance']['latitude'];
+ $long_distance = (float)$options['distance']['longitude'];
}
- if ($site_guid > 0) {
- $where[] = "e.site_guid = {$site_guid}";
+ $lat = (float)$options['latitude'];
+ $long = (float)$options['longitude'];
+ $lat_min = $lat - $lat_distance;
+ $lat_max = $lat + $lat_distance;
+ $long_min = $long - $long_distance;
+ $long_max = $long + $long_distance;
+
+ $wheres = array();
+ $wheres[] = "lat_name.string='geo:lat'";
+ $wheres[] = "lat_value.string >= $lat_min";
+ $wheres[] = "lat_value.string <= $lat_max";
+ $wheres[] = "lon_name.string='geo:long'";
+ $wheres[] = "lon_value.string >= $long_min";
+ $wheres[] = "lon_value.string <= $long_max";
+
+ $joins = array();
+ $joins[] = "JOIN {$CONFIG->dbprefix}metadata lat on e.guid=lat.entity_guid";
+ $joins[] = "JOIN {$CONFIG->dbprefix}metastrings lat_name on lat.name_id=lat_name.id";
+ $joins[] = "JOIN {$CONFIG->dbprefix}metastrings lat_value on lat.value_id=lat_value.id";
+ $joins[] = "JOIN {$CONFIG->dbprefix}metadata lon on e.guid=lon.entity_guid";
+ $joins[] = "JOIN {$CONFIG->dbprefix}metastrings lon_name on lon.name_id=lon_name.id";
+ $joins[] = "JOIN {$CONFIG->dbprefix}metastrings lon_value on lon.value_id=lon_value.id";
+
+ // merge wheres to pass to get_entities()
+ if (isset($options['wheres']) && !is_array($options['wheres'])) {
+ $options['wheres'] = array($options['wheres']);
+ } elseif (!isset($options['wheres'])) {
+ $options['wheres'] = array();
}
+ $options['wheres'] = array_merge($options['wheres'], $wheres);
- if (!is_null($container_guid)) {
- if (is_array($container_guid)) {
- foreach ($container_guid as $key => $val) {
- $container_guid[$key] = (int) $val;
- }
- $where[] = "e.container_guid in (" . implode(",", $container_guid) . ")";
- } else {
- $container_guid = (int) $container_guid;
- $where[] = "e.container_guid = {$container_guid}";
- }
+ // merge joins to pass to get_entities()
+ if (isset($options['joins']) && !is_array($options['joins'])) {
+ $options['joins'] = array($options['joins']);
+ } elseif (!isset($options['joins'])) {
+ $options['joins'] = array();
}
+ $options['joins'] = array_merge($options['joins'], $joins);
- // Add the calendar stuff
- $loc_join = "
- JOIN {$CONFIG->dbprefix}metadata loc_start on e.guid=loc_start.entity_guid
- JOIN {$CONFIG->dbprefix}metastrings loc_start_name on loc_start.name_id=loc_start_name.id
- JOIN {$CONFIG->dbprefix}metastrings loc_start_value on loc_start.value_id=loc_start_value.id
-
- JOIN {$CONFIG->dbprefix}metadata loc_end on e.guid=loc_end.entity_guid
- JOIN {$CONFIG->dbprefix}metastrings loc_end_name on loc_end.name_id=loc_end_name.id
- JOIN {$CONFIG->dbprefix}metastrings loc_end_value on loc_end.value_id=loc_end_value.id
- ";
-
- $lat_min = $lat - $radius;
- $lat_max = $lat + $radius;
- $long_min = $long - $radius;
- $long_max = $long + $radius;
-
- $where[] = "loc_start_name.string='geo:lat'";
- $where[] = "loc_start_value.string>=$lat_min";
- $where[] = "loc_start_value.string<=$lat_max";
- $where[] = "loc_end_name.string='geo:long'";
- $where[] = "loc_end_value.string >= $long_min";
- $where[] = "loc_end_value.string <= $long_max";
-
- if (!$count) {
- $query = "SELECT e.* from {$CONFIG->dbprefix}entities e $loc_join where ";
- } else {
- $query = "SELECT count(e.guid) as total from {$CONFIG->dbprefix}entities e $loc_join where ";
- }
- foreach ($where as $w) {
- $query .= " $w and ";
- }
-
- $query .= get_access_sql_suffix('e'); // Add access controls
-
- if (!$count) {
- $query .= " order by n.calendar_start $order_by";
- // Add order and limit
- if ($limit) {
- $query .= " limit $offset, $limit";
- }
- $dt = get_data($query, "entity_row_to_elggstar");
- return $dt;
- } else {
- $total = get_data_row($query);
- return $total->total;
- }
+ return elgg_get_entities_from_relationship($options);
}
/**
- * List entities in a given location
+ * Returns a viewable list of entities from location
*
- * @param string $location Location
- * @param string $type The type of entity (eg "user", "object" etc)
- * @param string $subtype The arbitrary subtype of the entity
- * @param int $owner_guid The GUID of the owning user
- * @param int $limit The number of entities to display per page (default: 10)
- * @param bool $fullview Whether or not to display the full view (default: true)
- * @param bool $viewtypetoggle Whether or not to allow gallery view
- * @param bool $navigation Display pagination? Default: true
+ * @param array $options Options array
*
- * @return string A viewable list of entities
- */
-function list_entities_location($location, $type= "", $subtype = "", $owner_guid = 0, $limit = 10,
-$fullview = true, $viewtypetoggle = false, $navigation = true) {
-
- return list_entities_from_metadata('location', $location, $type, $subtype, $owner_guid, $limit,
- $fullview, $viewtypetoggle, $navigation);
-}
-
-/**
- * List items within a given geographic area.
- *
- * @param real $lat Latitude
- * @param real $long Longitude
- * @param real $radius The radius
- * @param string $type The type of entity (eg "user", "object" etc)
- * @param string $subtype The arbitrary subtype of the entity
- * @param int $owner_guid The GUID of the owning user
- * @param int $limit The number of entities to display per page (default: 10)
- * @param bool $fullview Whether or not to display the full view (default: true)
- * @param bool $viewtypetoggle Whether or not to allow gallery view
- * @param bool $navigation Display pagination? Default: true
+ * @see elgg_list_entities()
+ * @see elgg_get_entities_from_location()
*
- * @return string A viewable list of entities
+ * @return string The viewable list of entities
+ * @since 1.8.0
*/
-function list_entities_in_area($lat, $long, $radius, $type= "", $subtype = "", $owner_guid = 0,
-$limit = 10, $fullview = true, $viewtypetoggle = false, $navigation = true) {
-
- $offset = (int) get_input('offset');
- $count = get_entities_in_area($lat, $long, $radius, $type, $subtype, $owner_guid,
- "", $limit, $offset, true);
- $entities = get_entities_in_area($lat, $long, $radius, $type, $subtype, $owner_guid,
- "", $limit, $offset);
-
- return elgg_view_entity_list($entities, $count, $offset, $limit, $fullview,
- $viewtypetoggle, $navigation);
+function elgg_list_entities_from_location(array $options = array()) {
+ return elgg_list_entities($options, 'elgg_get_entities_from_location');
}
// Some distances in degrees (approximate)
+// @todo huh? see warning on elgg_get_entities_from_location()
define("MILE", 0.01515);
define("KILOMETER", 0.00932);
-
-// @todo get objects within x miles by entities, metadata and relationship
-
-// @todo List \ No newline at end of file