function ws_pack_search($query, $offset = 0, $limit = 20) { if (!elgg_is_logged_in()) { $ia = elgg_set_ignore_access(true); } $results = ESInterface::get()->search($query, "all", null, null, (int) $limit, (int) $offset); $json = array(); $json["total"] = $results["count"]; if ($results["count"] > 0) { $json["results"] = ws_pack_export_entities($results["hits"]); } else { $json["results"] = array(); } if (!elgg_is_logged_in()) { elgg_set_ignore_access($ia); } return $json; }
/** * Get Entity * * @param int $id Entity GUID * * @return SuccessResult|ErrorResult */ function ws_pack_get_entity($id) { $result = false; $user = elgg_get_logged_in_user_entity(); $api_application = ws_pack_get_current_api_application(); if (!empty($user) && !empty($api_application)) { $options = array("guids" => explode(",", $id)); $entity = elgg_get_entities($options); if ($entity === false) { // error } else { $entity["entity"] = ws_pack_export_entities($entity); $result = new SuccessResult($entity); } } if ($result === false) { $result = new ErrorResult(elgg_echo("ws_pack:error:notfound")); } return $result; }
/** * Serch members * * @param string $search_str String to find * * @return SuccessResult|ErrorResult */ function ws_pack_search_members($search_str) { $result = false; $user = elgg_get_logged_in_user_entity(); $api_application = ws_pack_get_current_api_application(); if (!empty($user) && !empty($api_application)) { $params = array(); $params["query"] = $search_str; $params["type"] = "user"; $search_results = elgg_trigger_plugin_hook("search", "user", $params, array()); if ($search_results === false) { // error } else { $search_results["entities"] = ws_pack_export_entities($search_results["entities"]); $result = new SuccessResult($search_results); } } if ($result === false) { $result = new ErrorResult(elgg_echo("ws_pack:error:notfound")); } return $result; }
/** * Get Notifications * * @return SuccessResult|ErrorResult */ function ws_pack_get_notifications() { $result = false; $user = elgg_get_logged_in_user_entity(); $api_application = ws_pack_get_current_api_application(); if (!empty($user) && !empty($api_application)) { if (!$user_guid) { $user_guid = elgg_get_logged_in_user_guid(); } $options = array('type' => 'object', 'subtype' => 'site_notification', 'limit' => 50, 'order_by' => 'e.last_action desc', 'owner_guid' => $user->guid, 'full_view' => false, 'relationship' => 'hasActor', 'no_results' => elgg_echo('site_notifications:empty')); $notifications = elgg_get_entities_from_metadata($options); if ($notifications === false) { $result = new ErrorResult(elgg_echo("ws_pack:error:notfound")); } else { $notifications["entities"] = ws_pack_export_entities($notifications); $guids = array(); foreach ($notifications["entities"] as $key => $notification) { $notification_guid = $notification["guid"]; if (!in_array($notification_guid, $guids)) { $guids[] = $notification_guid; $notification_entity = get_entity($notification_guid); $owner = get_entity($notification["owner_guid"]); $notification["owner"] = ws_pack_export_entity($owner); $notification["parent_guid"] = $notification_entity->getURL(); $notifications["entities"][$key] = $notification; } else { unset($notifications["entities"][$key]); } } $result = new SuccessResult($notifications); } if ($result === false) { $result = new ErrorResult(elgg_echo("ws_pack:error:notfound")); } } return $result; }
/** * Returns the groups a user is member of * * @param int $user_guid user guid * @param int $offset offset * @param int $limit limit * * @return array|ErrorResult */ function ws_pack_groups_member_of($user_guid = 0, $offset = 0, $limit = 10) { $result = false; // default to the current logged in user if (empty($user_guid)) { $user_guid = elgg_get_logged_in_user_guid(); } if (!empty($user_guid)) { $options = array("type" => "group", "relationship" => "member", "relationship_guid" => $user_guid, "offset" => $offset, "limit" => $limit); if ($entities = elgg_get_entities_from_relationship($options)) { $result = ws_pack_export_entities($entities); } } if ($result === false) { $result = new ErrorResult(elgg_echo("notfound"), WS_PACK_API_NO_RESULTS); } return $result; }
/** * Get Last Conversation * * @param int $user_guid User GUID * @param int $limit Limit of Conversations * @param int $offset Offset * @param int $count Count * * @return SuccessResult|ErrorResult */ function ws_pack_get_last_conversations($user_guid, $limit = 100, $offset = 0, $count = false) { $result = false; $user = elgg_get_logged_in_user_entity(); $api_application = ws_pack_get_current_api_application(); if (!empty($user) && !empty($api_application)) { if (!$user_guid) { $user_guid = elgg_get_logged_in_user_guid(); } $db_prefix = elgg_get_config('dbprefix'); $strings = array('toId', $user_guid, 'msg', 1, 'fromId', $fromto_guid); $map = array(); foreach ($strings as $string) { $id = elgg_get_metastring_id($string); $map[$string] = $id; } $options = array('selects' => array("MAX(e.guid) as guid", "MAX(e.time_created) as time_created"), 'joins' => array("JOIN {$db_prefix}metadata msg_toId on e.guid = msg_toId.entity_guid", "JOIN {$db_prefix}metadata msg_msg on e.guid = msg_msg.entity_guid", "JOIN {$db_prefix}metadata msg_fromId on e.guid = msg_fromId.entity_guid"), 'owner_guid' => $user_guid, 'limit' => $limit, 'offset' => $offset, 'count' => $count); $options["wheres"] = array("msg_msg.name_id='{$map['msg']}' AND msg_msg.value_id='{$map[1]}'"); $options["group_by"] = "msg_fromId.value_id, msg_toId.value_id"; $messages = elgg_get_entities_from_metadata($options); if ($messages === false) { $result = new ErrorResult(elgg_echo("ws_pack:error:notfound")); } else { $messages["entities"] = ws_pack_export_entities($messages); $guids = array(); foreach ($messages["entities"] as $key => $message) { $message_guid = $message["guid"]; if (!in_array($message_guid, $guids)) { $guids[] = $message_guid; $message_entity = get_entity($message_guid); $recipient_id = $message_entity->toId; if ($recipient_id === $user_guid) { $recipient_id = $message_entity->fromId; } $recipient = get_entity($recipient_id); $message["recipient"] = ws_pack_export_entity($recipient); $messages["entities"][$key] = $message; } else { unset($messages["entities"][$key]); } } $result = new SuccessResult($messages); } } if ($result === false) { $result = new ErrorResult(elgg_echo("ws_pack:error:notfound")); } return $result; }
/** * Get Thread * * @param int $thread_id Thread GUID * * @return SuccessResult|ErrorResult */ function ws_pack_get_thread($thread_id = false) { $result = false; $user = elgg_get_logged_in_user_entity(); $api_application = ws_pack_get_current_api_application(); if (!empty($user) && !empty($api_application)) { $threads = elgg_get_entities_from_metadata(array("metadata_name" => "wire_thread", "metadata_value" => $thread_id, "type" => "object", "subtype" => "thewire", "limit" => 20)); // returns guid of wire post if ($threads === false) { // error } else { $wires = array(); $wires["entities"] = ws_pack_export_entities($threads); $guids_thread = array(); foreach ($wires["entities"] as $k => $wt) { $wire_guid_th = $wt["guid"]; if (!in_array($wire_guid_th, $guids_thread)) { $guids_thread[] = $wire_guid_th; $owner_thread = get_entity($wt["owner_guid"]); $wt["owner"] = ws_pack_export_entity($owner_thread); $parent = thewire_get_parent($wire_guid_th); $wt["parent"] = ws_pack_export_entity($parent); if ($parent !== false) { $parent_owner = get_entity($wt["parent"]["owner_guid"]); $wt["parent_owner"] = ws_pack_export_entity($parent_owner); } $wires["entities"][$k] = $wt; } else { unset($wires["entities"][$k]); } } $result = new SuccessResult($wires); } if ($result === false) { $result = new ErrorResult(elgg_echo("ws_pack:error:notfound")); } } return $result; }
/** * Export a single entity and add extra data * * @param ElggEntity $entity entity to export * * @return boolean|ElggEntity */ function ws_pack_export_entity(ElggEntity $entity) { $result = false; // make sure we have an entity if (!empty($entity) && $entity instanceof ElggEntity) { $temp = array($entity); if ($export = ws_pack_export_entities($temp)) { $result = $export[0]; } } return $result; }
/** * Get Discussion * * @param int $guid Discussion GUID * * @return SuccessResult|ErrorResult */ function ws_pack_get_discussion($guid) { $result = false; $user = elgg_get_logged_in_user_entity(); $api_application = ws_pack_get_current_api_application(); if (!empty($user) && !empty($api_application)) { $options = array('type' => 'object', 'order_by' => 'e.last_action desc', 'guid' => $guid, 'full_view' => false, 'no_results' => elgg_echo('discussion:none')); $discussions = elgg_get_entities($options); // returns guid of wire post if ($discussions === false) { $result = new ErrorResult(elgg_echo("ws_pack:error:notfound")); } else { $discussions["entities"] = ws_pack_export_entities($discussions); $guids = array(); foreach ($discussions["entities"] as $key => $discussion) { $discussion_guid = $discussion["guid"]; if (!in_array($discussion_guid, $guids)) { $guids[] = $discussion_guid; $owner = get_entity($discussion["owner_guid"]); $discussion["owner"] = ws_pack_export_entity($owner); $discussions["entities"][$key] = $discussion; } else { unset($discussions["entities"][$key]); } } $result = new SuccessResult($discussions); } if ($result === false) { $result = new ErrorResult(elgg_echo("ws_pack:error:notfound")); } } return $result; }