/** * 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 * * @return array A list of entities. * @deprecated 1.8 Use elgg_get_entities_from_location() */ 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 = NULL) { elgg_deprecated_notice('get_entities_in_area() was deprecated by elgg_get_entities_from_location()!', 1.8); $options = array(); $options['latitude'] = $lat; $options['longitude'] = $long; $options['distance'] = $radius; // set container_guid to owner_guid to emulate old functionality if ($owner_guid != "") { if (is_null($container_guid)) { $container_guid = $owner_guid; } } if ($type) { $options['types'] = $type; } if ($subtype) { $options['subtypes'] = $subtype; } if ($owner_guid) { if (is_array($owner_guid)) { $options['owner_guids'] = $owner_guid; } else { $options['owner_guid'] = $owner_guid; } } if ($container_guid) { if (is_array($container_guid)) { $options['container_guids'] = $container_guid; } else { $options['container_guid'] = $container_guid; } } $options['limit'] = $limit; if ($offset) { $options['offset'] = $offset; } if ($order_by) { $options['order_by']; } if ($site_guid) { $options['site_guid']; } if ($count) { $options['count'] = $count; } return elgg_get_entities_from_location($options); }
/** * Search for events * * @param array $options search options * * @return array */ function event_manager_search_events($options = []) { $dbprefix = elgg_get_config('dbprefix'); $defaults = ['past_events' => false, 'count' => false, 'limit' => (int) get_input('limit', 10), 'offset' => (int) get_input('offset', 0), 'container_guid' => null, 'query' => false, 'meattending' => false, 'owning' => false, 'friendsattending' => false, 'region' => null, 'latitude' => null, 'longitude' => null, 'distance' => null, 'event_type' => false, 'event_start' => null, 'event_end' => null, 'search_type' => "list", 'user_guid' => elgg_get_logged_in_user_guid()]; $options = array_merge($defaults, $options); $entities_options = ['type' => 'object', 'subtype' => 'event', 'offset' => $options['offset'], 'limit' => $options['limit'], 'joins' => [], 'wheres' => [], 'order_by_metadata' => ['name' => 'event_start', 'direction' => 'ASC', 'as' => 'integer']]; if ($options['container_guid']) { // limit for a group $entities_options['container_guid'] = $options['container_guid']; } if ($options['query']) { $entities_options['joins'][] = "JOIN {$dbprefix}objects_entity oe ON e.guid = oe.guid"; $entities_options['wheres'][] = event_manager_search_get_where_sql('oe', ['title', 'description'], $options); } if (!empty($options['event_start'])) { $entities_options['metadata_name_value_pairs'][] = ['name' => 'event_start', 'value' => $options['event_start'], 'operand' => '>=']; } if (!empty($options['event_end'])) { $options['event_end'] += 86400; // add one day $entities_options['metadata_name_value_pairs'][] = ['name' => 'event_end', 'value' => $options['event_end'], 'operand' => '<=']; } if (!$options['past_events']) { // only show from current day or newer (or where event is still running) $current_time = gmmktime(0, 0, 1); if ($options['event_end']) { $entities_options['metadata_name_value_pairs'][] = ['name' => 'event_start', 'value' => $current_time, 'operand' => '>=']; } else { // start date $event_start_id = elgg_get_metastring_id('event_start'); $entities_options['joins'][] = "JOIN {$dbprefix}metadata md_start ON e.guid = md_start.entity_guid"; $entities_options['joins'][] = "JOIN {$dbprefix}metastrings msv_start ON md_start.value_id = msv_start.id"; $entities_options['wheres'][] = "md_start.name_id = {$event_start_id}"; // end date $event_end_id = elgg_get_metastring_id('event_end'); $entities_options['joins'][] = "JOIN {$dbprefix}metadata md_end ON e.guid = md_end.entity_guid"; $entities_options['joins'][] = "JOIN {$dbprefix}metastrings msv_end ON md_end.value_id = msv_end.id"; $entities_options['wheres'][] = "md_end.name_id = {$event_end_id}"; // event start > now $time_start = "(msv_start.string >= {$current_time})"; // or event start before end and end after now $time_end = "((msv_start.string < {$current_time}) AND (msv_end.string > {$current_time}))"; $entities_options['wheres'][] = "({$time_start} OR {$time_end})"; } } if ($options['meattending'] && !empty($options['user_guid'])) { $entities_options['joins'][] = "JOIN {$dbprefix}entity_relationships e_r ON e.guid = e_r.guid_one"; $entities_options['wheres'][] = 'e_r.guid_two = ' . $options['user_guid']; $entities_options['wheres'][] = 'e_r.relationship = "' . EVENT_MANAGER_RELATION_ATTENDING . '"'; } if ($options['owning'] && !empty($options['user_guid'])) { $entities_options['owner_guids'] = [$options['user_guid']]; } if ($options['region']) { $entities_options['metadata_name_value_pairs'][] = ['name' => 'region', 'value' => $options['region']]; } if ($options['event_type']) { $entities_options['metadata_name_value_pairs'][] = ['name' => 'event_type', 'value' => $options['event_type']]; } if ($options['friendsattending'] && !empty($options['user_guid'])) { $friends_guids = []; $user = get_entity($options['user_guid']); if ($friends = $user->getFriends('', false)) { foreach ($friends as $friend) { $friends_guids[] = $friend->getGUID(); } $entities_options['joins'][] = "JOIN {$dbprefix}entity_relationships e_ra ON e.guid = e_ra.guid_one"; $entities_options['wheres'][] = '(e_ra.guid_two IN (' . implode(', ', $friends_guids) . '))'; } else { // return no result $entities_options['joins'] = []; $entities_options['wheres'] = ['(1=0)']; } } if ($options['search_type'] == 'onthemap' && !empty($options['latitude']) && !empty($options['longitude']) && !empty($options['distance'])) { $entities_options['latitude'] = $options['latitude']; $entities_options['longitude'] = $options['longitude']; $entities_options['distance'] = $options['distance']; $entities = elgg_get_entities_from_location($entities_options); $entities_options['count'] = true; $count_entities = elgg_get_entities_from_location($entities_options); } else { $entities = elgg_get_entities_from_metadata($entities_options); $entities_options['count'] = true; $count_entities = elgg_get_entities_from_metadata($entities_options); } $result = ['entities' => $entities, 'count' => $count_entities]; return $result; }
/** * Search for events * * @param array $options search options * * @return array */ function event_manager_search_events($options = array()) { $defaults = array('past_events' => false, 'count' => false, 'container_guid' => null, 'query' => false, 'meattending' => false, 'owning' => false, 'friendsattending' => false, 'region' => null, 'latitude' => null, 'longitude' => null, 'distance' => null, 'event_type' => false, 'past_events' => false, 'search_type' => "list", 'user_guid' => elgg_get_logged_in_user_guid()); $options = array_merge($defaults, $options); $entities_options = array('type' => 'object', 'subtype' => 'event', 'offset' => $options['offset'], 'limit' => $options['limit'], 'joins' => array(), 'wheres' => array(), 'order_by_metadata' => array("name" => 'start_day', "direction" => 'ASC', "as" => "integer")); if ($options["container_guid"]) { // limit for a group $entities_options['container_guid'] = $options['container_guid']; } if ($options['query']) { $entities_options["joins"][] = "JOIN " . elgg_get_config("dbprefix") . "objects_entity oe ON e.guid = oe.guid"; $entities_options['wheres'][] = event_manager_search_get_where_sql('oe', array('title', 'description'), $options); } if (!empty($options['start_day'])) { $entities_options['metadata_name_value_pairs'][] = array('name' => 'start_day', 'value' => $options['start_day'], 'operand' => '>='); } if (!empty($options['end_day'])) { $entities_options['metadata_name_value_pairs'][] = array('name' => 'end_ts', 'value' => $options['end_day'], 'operand' => '<='); } if (!$options['past_events']) { // only show from current day or newer $entities_options['metadata_name_value_pairs'][] = array('name' => 'start_day', 'value' => mktime(0, 0, 1), 'operand' => '>='); } if ($options['meattending'] && !empty($options["user_guid"])) { $entities_options['joins'][] = "JOIN " . elgg_get_config("dbprefix") . "entity_relationships e_r ON e.guid = e_r.guid_one"; $entities_options['wheres'][] = "e_r.guid_two = " . $options["user_guid"]; $entities_options['wheres'][] = "e_r.relationship = '" . EVENT_MANAGER_RELATION_ATTENDING . "'"; } if ($options['owning'] && !empty($options["user_guid"])) { $entities_options['owner_guids'] = array($options["user_guid"]); } if ($options["region"]) { $entities_options['metadata_name_value_pairs'][] = array('name' => 'region', 'value' => $options["region"]); } if ($options["event_type"]) { $entities_options['metadata_name_value_pairs'][] = array('name' => 'event_type', 'value' => $options["event_type"]); } if ($options['friendsattending'] && !empty($options["user_guid"])) { $friends_guids = array(); $user = get_entity($options["user_guid"]); if ($friends = $user->getFriends("", false)) { foreach ($friends as $friend) { $friends_guids[] = $friend->getGUID(); } $entities_options['joins'][] = "JOIN " . elgg_get_config("dbprefix") . "entity_relationships e_ra ON e.guid = e_ra.guid_one"; $entities_options['wheres'][] = "(e_ra.guid_two IN (" . implode(", ", $friends_guids) . "))"; } else { // return no result $entities_options['joins'] = array(); $entities_options['wheres'] = array("(1=0)"); } } if ($options["search_type"] == "onthemap" && !empty($options['latitude']) && !empty($options['longitude']) && !empty($options['distance'])) { $entities_options["latitude"] = $options['latitude']; $entities_options["longitude"] = $options['longitude']; $entities_options["distance"] = $options['distance']; $entities = elgg_get_entities_from_location($entities_options); $entities_options['count'] = true; $count_entities = elgg_get_entities_from_location($entities_options); } else { $entities = elgg_get_entities_from_metadata($entities_options); $entities_options['count'] = true; $count_entities = elgg_get_entities_from_metadata($entities_options); } $result = array("entities" => $entities, "count" => $count_entities); return $result; }
/** * Location */ public function testElggApiGettersEntitiesFromLocation() { // a test location that is out of this world $lat = 500; $long = 500; $delta = 5; $subtypes = $this->getRandomValidSubtypes(array('object'), 1); $subtype = $subtypes[0]; $guids = array(); // our objects $valid = new ElggObject(); $valid->subtype = $subtype; $valid->save(); $guids[] = $valid->getGUID(); $valid->setLatLong($lat, $long); $valid2 = new ElggObject(); $valid2->subtype = $subtype; $valid2->save(); $guids[] = $valid2->getGUID(); $valid2->setLatLong($lat + 2 * $delta, $long + 2 * $delta); // limit to first object $options = array('latitude' => $lat, 'longitude' => $long, 'distance' => $delta); //global $CONFIG; //$CONFIG->debug = 'NOTICE'; $entities = elgg_get_entities_from_location($options); //unset($CONFIG->debug); $this->assertEqual(1, count($entities)); $this->assertEqual($entities[0]->getGUID(), $valid->getGUID()); // get both objects $options = array('latitude' => $lat, 'longitude' => $long, 'distance' => array('latitude' => 2 * $delta, 'longitude' => 2 * $delta)); $entities = elgg_get_entities_from_location($options); $this->assertEqual(2, count($entities)); foreach ($entities as $entity) { $this->assertTrue(in_array($entity->getGUID(), $guids)); } foreach ($guids as $guid) { if ($e = get_entity($guid)) { $e->delete(); } } }