Can search for users and groups from the API

This commit is contained in:
Pierre HUBERT 2018-07-28 17:15:50 +02:00
parent eea4378a9c
commit 53b72bd767
4 changed files with 148 additions and 43 deletions

View File

@ -0,0 +1,109 @@
<?php
/**
* Search controller
*
* @author Pierre HUBERT
*/
class SearchController
{
/**
* Search results kinds to API
*/
const SEARCH_RESULTS_KINDS = array(
SearchResult::KIND_USER => "user",
SearchResult::KIND_GROUP => "group"
);
/**
* Peform a research on the database
*
* @url POST /search/user
* @url POST /user/search
*/
public function search_user(){
user_login_required();
//Check if the query was specified with the request
if(!isset($_POST['query']))
Rest_fatal_error(400, "Please specify search terms");
$query = $_POST['query'];
//Check the query
if(strlen($query) < 1)
Rest_fatal_error(401, "Empty requests not allowed !");
//Check for search limit
$searchLimit = (isset($_POST['searchLimit']) ? toInt($_POST['searchLimit']) : 5);
//Check the limit
if($searchLimit < 1 || $searchLimit > 25)
Rest_fatal_error(401, "Invalid search limit !");
//Perform research on the database and return results
$results = CS::get()->components->search->search_user($query, $searchLimit);
if($results === false)
Rest_fatal_error(500, "An error occured while trying to perform a research in user list !");
//Return results
return $results;
}
/**
* Peform a global search (search for groups + users)
*
* @url POST /search/global
*/
public function searchGlobal(){
user_login_required();
//Get search query
$query = postString("query", 1);
//Set abitrary limit
$limit = 10;
$results = array();
//First, search for groups
foreach(components()->search->search_group($query, $limit) as $groupID)
$results[] = new SearchResult(SearchResult::KIND_GROUP, $groupID);
$limit -= count($results);
//Then search for users
foreach(components()->search->search_user($query, $limit) as $userID)
$results[] = new SearchResult(SearchResult::KIND_USER, $userID);
//Parse and return result
return self::MultipleSearchResultToAPI($results);
}
/**
* Parse multiple SearchResult entry to API
*
* @param array $list The list of SearchResults to parse
* @return array Generated array
*/
public static function MultipleSearchResultToAPI(array $list) : array {
$data = array();
foreach($list as $entry)
$data[] = self::SearchResultToAPI($entry);
return $data;
}
/**
* Turn a SearchResult object into API object
*
* @param SearchResult $result The result to process
* @return array Generated entry
*/
public static function SearchResultToAPI(SearchResult $result) : array {
$data = array();
$data["kind"] = self::SEARCH_RESULTS_KINDS[$result->get_kind()];
$data["id"] = $result->get_kind_id();
return $data;
}
}

View File

@ -1,43 +0,0 @@
<?php
/**
* Search controller
*
* @author Pierre HUBERT
*/
class searchController
{
/**
* Peform a research on the database
*
* @url POST /search/user
* @url POST /user/search
*/
public function search_user(){
user_login_required();
//Check if the query was specified with the request
if(!isset($_POST['query']))
Rest_fatal_error(400, "Please specify search terms");
$query = $_POST['query'];
//Check the query
if(strlen($query) < 1)
Rest_fatal_error(401, "Empty requests not allowed !");
//Check for search limit
$searchLimit = (isset($_POST['searchLimit']) ? toInt($_POST['searchLimit']) : 5);
//Check the limit
if($searchLimit < 1 || $searchLimit > 25)
Rest_fatal_error(401, "Invalid search limit !");
//Perform research on the database and return results
$results = CS::get()->components->search->search_user($query, $searchLimit);
if($results === false)
Rest_fatal_error(500, "An error occured while trying to perform a research in user list !");
//Return results
return $results;
}
}

View File

@ -37,6 +37,34 @@ class search {
//Return result //Return result
return $return; return $return;
} }
/**
* Search for groups in the database
*
* @param string $query
* @param int $limit (default = 10)
* @return array List of results
*/
public function search_group(string $query, int $limit = 10){
//Query string
$query = "%".$query."%";
//Request
$results = db()->select(
GroupsComponent::GROUPS_LIST_TABLE,
"WHERE name LIKE ?",
array($query),
array("id")
);
//Parse and return results
$list = array();
foreach($results as $el)
$list[] = $el["id"];
return $list;
}
} }
//Register class //Register class

View File

@ -15,6 +15,17 @@ class SearchResult {
private $kind; private $kind;
private $kind_id; private $kind_id;
/**
* Constructor of the object
*
* @param int $kind The kind of result (group, user...)
* @param int $kind_id The ID of the result
*/
public function SearchResult(int $kind, int $kind_id){
$this->set_kind($kind);
$this->set_kind_id($kind_id);
}
//Set and get the kind of object //Set and get the kind of object
public function set_kind(int $kind){ public function set_kind(int $kind){
$this->kind = $kind; $this->kind = $kind;