ComunicAPI/RestControllers/searchController.php

43 lines
1.0 KiB
PHP
Raw Permalink Normal View History

2017-05-27 07:30:54 +00:00
<?php
/**
* Search controller
*
* @author Pierre HUBERT
*/
2017-05-27 07:41:52 +00:00
class searchController
2017-05-27 07:30:54 +00:00
{
/**
* Peform a research on the database
*
2018-01-01 14:43:37 +00:00
* @url POST /search/user
2017-06-05 12:22:54 +00:00
* @url POST /user/search
2017-05-27 07:30:54 +00:00
*/
2018-01-01 14:43:37 +00:00
public function search_user(){
2017-06-10 07:42:53 +00:00
user_login_required();
2017-05-27 07:30:54 +00:00
//Check if the query was specified with the request
if(!isset($_POST['query']))
Rest_fatal_error(400, "Please specify search terms");
2018-01-01 17:40:26 +00:00
$query = $_POST['query'];
//Check the query
if(strlen($query) < 1)
Rest_fatal_error(401, "Empty requests not allowed !");
2017-05-27 07:30:54 +00:00
//Check for search limit
$searchLimit = (isset($_POST['searchLimit']) ? toInt($_POST['searchLimit']) : 5);
2017-05-27 07:30:54 +00:00
//Check the limit
2017-05-27 07:41:52 +00:00
if($searchLimit < 1 || $searchLimit > 25)
2017-05-27 07:30:54 +00:00
Rest_fatal_error(401, "Invalid search limit !");
//Perform research on the database and return results
2018-01-01 17:40:26 +00:00
$results = CS::get()->components->search->search_user($query, $searchLimit);
2017-05-27 09:22:55 +00:00
if($results === false)
2017-05-27 07:41:52 +00:00
Rest_fatal_error(500, "An error occured while trying to perform a research in user list !");
2017-05-27 07:30:54 +00:00
2017-05-27 07:41:52 +00:00
//Return results
return $results;
2017-05-27 07:30:54 +00:00
}
}