diff --git a/RestControllers/SearchController.php b/RestControllers/SearchController.php new file mode 100644 index 0000000..94265bf --- /dev/null +++ b/RestControllers/SearchController.php @@ -0,0 +1,109 @@ + "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; + } +} \ No newline at end of file diff --git a/RestControllers/searchController.php b/RestControllers/searchController.php deleted file mode 100644 index 7c549a1..0000000 --- a/RestControllers/searchController.php +++ /dev/null @@ -1,43 +0,0 @@ - 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; - } -} \ No newline at end of file diff --git a/classes/components/search.php b/classes/components/search.php index de0921e..dcdcfdd 100644 --- a/classes/components/search.php +++ b/classes/components/search.php @@ -37,6 +37,34 @@ class search { //Return result 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 diff --git a/classes/models/SearchResult.php b/classes/models/SearchResult.php index 84ed4e5..711f092 100644 --- a/classes/models/SearchResult.php +++ b/classes/models/SearchResult.php @@ -15,6 +15,17 @@ class SearchResult { private $kind; 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 public function set_kind(int $kind){ $this->kind = $kind;