ComunicAPI/RestControllers/searchController.php

43 lines
1.0 KiB
PHP
Raw Normal View History

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