ComunicAPI/classes/components/search.php

71 lines
1.6 KiB
PHP
Raw Normal View History

2017-05-27 07:41:52 +00:00
<?php
/**
2018-01-01 14:43:37 +00:00
* Search controller
2017-05-27 07:41:52 +00:00
*
* @author Pierre HUBERT
*/
2018-01-01 14:43:37 +00:00
class search {
2017-05-27 07:41:52 +00:00
/**
* Search for user in the database
*
2018-01-01 14:43:37 +00:00
* @param string $query The query to research on the database
* @param int $limit The number of results to return on the screen (default: 10)
* @return array the result of the result
2017-05-27 07:41:52 +00:00
*/
2018-01-01 14:43:37 +00:00
public function search_user(string $query, int $limit = 10) : array{
2017-05-27 08:52:12 +00:00
//Prepare query string
$query = "%".str_replace(" ", "%", $query)."%";
//Prepare a request on the database
$tableName = "utilisateurs";
$conditions = "WHERE (nom LIKE ?) || (prenom LIKE ?) || (CONCAT(prenom, '%', nom) LIKE ?) || (CONCAT(nom, '%', prenom) LIKE ?) LIMIT ".$limit*1;
$datasCond = array($query, $query, $query, $query);
$fields = array("ID");
//Perform the request on the database
$results = CS::get()->db->select($tableName, $conditions, $datasCond, $fields);
//Prepare return
$return = array();
foreach($results as $value){
$return[] = $value["ID"];
}
//Return result
return $return;
2017-05-27 07:41:52 +00:00
}
/**
* 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 ? AND visibility != ".GroupInfo::SECRET_GROUP,
array($query),
array("id")
);
//Parse and return results
$list = array();
foreach($results as $el)
$list[] = $el["id"];
return $list;
}
2017-05-27 07:41:52 +00:00
}
//Register class
2018-01-01 14:43:37 +00:00
Components::register("search", new search());