00001 <?php
00015 interface Locatable {
00017 public function setLocation($location);
00018
00025 public function setLatLong($lat, $long);
00026
00031 public function getLatitude();
00032
00037 public function getLongitude();
00038
00043 public function getLocation();
00044 }
00045
00054 function elgg_geocode_location($location) {
00055 global $CONFIG;
00056
00057
00058 if (is_array($location)) {
00059 $location = implode(', ', $location);
00060 }
00061
00062 $location = sanitise_string($location);
00063
00064
00065 $cached_location = get_data_row("SELECT * from {$CONFIG->dbprefix}geocode_cache WHERE location='$location'");
00066
00067 if ($cached_location) {
00068 return array('lat' => $cached_location->lat, 'long' => $cached_location->long);
00069 }
00070
00071
00072 $return = false;
00073 $return = trigger_plugin_hook('geocode', 'location', array('location' => $location), $return);
00074
00075
00076 if (($return) && (is_array($return))) {
00077 $lat = (float)$return['lat'];
00078 $long = (float)$return['long'];
00079
00080
00081 execute_delayed_write_query("INSERT DELAYED INTO {$CONFIG->dbprefix}geocode_cache (location, lat, `long`) VALUES ('$location', '{$lat}', '{$long}') ON DUPLICATE KEY UPDATE lat='{$lat}', `long`='{$long}'");
00082 }
00083
00084 return $return;
00085 }
00086
00104 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) {
00105 global $CONFIG;
00106
00107 if ($subtype === false || $subtype === null || $subtype === 0) {
00108 return false;
00109 }
00110
00111 $lat = (real)$lat;
00112 $long = (real)$long;
00113 $radius = (real)$radius;
00114
00115 $order_by = sanitise_string($order_by);
00116 $limit = (int)$limit;
00117 $offset = (int)$offset;
00118 $site_guid = (int) $site_guid;
00119 if ($site_guid == 0) {
00120 $site_guid = $CONFIG->site_guid;
00121 }
00122
00123 $where = array();
00124
00125 if (is_array($type)) {
00126 $tempwhere = "";
00127 if (sizeof($type)) {
00128 foreach($type as $typekey => $subtypearray) {
00129 foreach($subtypearray as $subtypeval) {
00130 $typekey = sanitise_string($typekey);
00131 if (!empty($subtypeval)) {
00132 $subtypeval = (int) get_subtype_id($typekey, $subtypeval);
00133 } else {
00134 $subtypeval = 0;
00135 }
00136 if (!empty($tempwhere)) $tempwhere .= " or ";
00137 $tempwhere .= "(e.type = '{$typekey}' and e.subtype = {$subtypeval})";
00138 }
00139 }
00140 }
00141 if (!empty($tempwhere)) {
00142 $where[] = "({$tempwhere})";
00143 }
00144 } else {
00145 $type = sanitise_string($type);
00146 $subtype = get_subtype_id($type, $subtype);
00147
00148 if ($type != "") {
00149 $where[] = "e.type='$type'";
00150 }
00151
00152 if ($subtype!=="") {
00153 $where[] = "e.subtype=$subtype";
00154 }
00155 }
00156
00157 if ($owner_guid != "") {
00158 if (!is_array($owner_guid)) {
00159 $owner_array = array($owner_guid);
00160 $owner_guid = (int) $owner_guid;
00161 $where[] = "e.owner_guid = '$owner_guid'";
00162 } else if (sizeof($owner_guid) > 0) {
00163 $owner_array = array_map('sanitise_int', $owner_guid);
00164
00165 $owner_guid = implode(",",$owner_guid);
00166 $where[] = "e.owner_guid in ({$owner_guid})" ;
00167 }
00168 if (is_null($container_guid)) {
00169 $container_guid = $owner_array;
00170 }
00171 }
00172
00173 if ($site_guid > 0) {
00174 $where[] = "e.site_guid = {$site_guid}";
00175 }
00176
00177 if (!is_null($container_guid)) {
00178 if (is_array($container_guid)) {
00179 foreach($container_guid as $key => $val) $container_guid[$key] = (int) $val;
00180 $where[] = "e.container_guid in (" . implode(",",$container_guid) . ")";
00181 } else {
00182 $container_guid = (int) $container_guid;
00183 $where[] = "e.container_guid = {$container_guid}";
00184 }
00185 }
00186
00187
00188 $loc_join = "
00189 JOIN {$CONFIG->dbprefix}metadata loc_start on e.guid=loc_start.entity_guid
00190 JOIN {$CONFIG->dbprefix}metastrings loc_start_name on loc_start.name_id=loc_start_name.id
00191 JOIN {$CONFIG->dbprefix}metastrings loc_start_value on loc_start.value_id=loc_start_value.id
00192
00193 JOIN {$CONFIG->dbprefix}metadata loc_end on e.guid=loc_end.entity_guid
00194 JOIN {$CONFIG->dbprefix}metastrings loc_end_name on loc_end.name_id=loc_end_name.id
00195 JOIN {$CONFIG->dbprefix}metastrings loc_end_value on loc_end.value_id=loc_end_value.id
00196 ";
00197
00198 $lat_min = $lat - $radius;
00199 $lat_max = $lat + $radius;
00200 $long_min = $long - $radius;
00201 $long_max = $long + $radius;
00202
00203 $where[] = "loc_start_name.string='geo:lat'";
00204 $where[] = "loc_start_value.string>=$lat_min";
00205 $where[] = "loc_start_value.string<=$lat_max";
00206 $where[] = "loc_end_name.string='geo:long'";
00207 $where[] = "loc_end_value.string >= $long_min";
00208 $where[] = "loc_end_value.string <= $long_max";
00209
00210 if (!$count) {
00211 $query = "SELECT e.* from {$CONFIG->dbprefix}entities e $loc_join where ";
00212 } else {
00213 $query = "SELECT count(e.guid) as total from {$CONFIG->dbprefix}entities e $loc_join where ";
00214 }
00215 foreach ($where as $w) {
00216 $query .= " $w and ";
00217 }
00218
00219 $query .= get_access_sql_suffix('e');
00220
00221 if (!$count) {
00222 $query .= " order by n.calendar_start $order_by";
00223
00224 if ($limit) {
00225 $query .= " limit $offset, $limit";
00226 }
00227 $dt = get_data($query, "entity_row_to_elggstar");
00228 return $dt;
00229 } else {
00230 $total = get_data_row($query);
00231 return $total->total;
00232 }
00233 }
00234
00248 function list_entities_location($location, $type= "", $subtype = "", $owner_guid = 0, $limit = 10, $fullview = true, $viewtypetoggle = false, $navigation = true) {
00249 return list_entities_from_metadata('location', $location, $type, $subtype, $owner_guid, $limit, $fullview, $viewtypetoggle, $navigation);
00250 }
00251
00267 function list_entities_in_area($lat, $long, $radius, $type= "", $subtype = "", $owner_guid = 0, $limit = 10, $fullview = true, $viewtypetoggle = false, $navigation = true) {
00268
00269 $offset = (int) get_input('offset');
00270 $count = get_entities_in_area($lat, $long, $radius, $type, $subtype, $owner_guid, "", $limit, $offset, true);
00271 $entities = get_entities_in_area($lat, $long, $radius, $type, $subtype, $owner_guid, "", $limit, $offset);
00272
00273 return elgg_view_entity_list($entities, $count, $offset, $limit, $fullview, $viewtypetoggle, $navigation);
00274 }
00275
00276
00277 define("MILE", 0.01515);
00278 define("KILOMETER", 0.00932);
00279
00280
00281
00282