Update repo user/search to match any keywords (space-separated) with a OR condition + if no search input -> display all

This commit is contained in:
xdrm-brackets 2017-11-13 13:25:31 +01:00
parent d30ec35636
commit b745c9ef08
2 changed files with 84 additions and 18 deletions

View File

@ -67,25 +67,77 @@
*/ */
public static function search($id_warehouse, $keyword){ public static function search($id_warehouse, $keyword){
// make keyword lowercase /* (1) Format keyword
$keyword = strtolower($keyword); ---------------------------------------------------------*/ {
// On recupere les donnees /* (1) Make all lowercase */
$searchusers = DatabaseDriver::getPDO()->prepare("SELECT * FROM user $keyword = strtolower($keyword);
WHERE id_warehouse = :id_warehouse
AND ( LOWER(code) LIKE '%".$keyword."%'
OR LOWER(username) LIKE '%".$keyword."%'
OR LOWER(firstname) LIKE '%".$keyword."%'
OR LOWER(lastname) LIKE '%".$keyword."%'
OR LOWER(mail) LIKE '%".$keyword."%'
)
");
$searchusers->execute([ /* (2) Create a keyword set (separator: space) */
':id_warehouse' => $id_warehouse $keywords = [];
]); $keywords_tmp = explode(' ', $keyword);
return DatabaseDriver::delNumeric( $searchusers->fetchAll() ); /* (4) Trim each keyword + ignore empty ones (2 consecutive spaces) */
foreach($keywords_tmp as $kw){
// ignore empty keywords
if( strlen(trim($kw)) == 0 )
continue;
// store others
$keywords[] = trim($kw);
}
}
/* (2) Search for each keyword
---------------------------------------------------------*/
/* (1) Initialise id list that will contain each matching user ids */
$user_id_list = [];
/* (2) Request for each keyword */
foreach($keywords as $kw){
// {2.1} Request //
$searchusers = DatabaseDriver::getPDO()->prepare("SELECT id_user FROM user
WHERE id_warehouse = :id_warehouse
AND ( LOWER(code) LIKE '%".$kw."%'
OR LOWER(username) LIKE '%".$kw."%'
OR LOWER(firstname) LIKE '%".$kw."%'
OR LOWER(lastname) LIKE '%".$kw."%'
OR LOWER(mail) LIKE '%".$kw."%'
)
");
// {2.2} Inject params //
$searchusers->execute([ ':id_warehouse' => $id_warehouse ]);
// {2.3} Fetch result //
$matches = DatabaseDriver::delNumeric( $searchusers->fetchAll() );
// {2.4} Only add non-already added ids //
foreach($matches as $match){
// {2.4.1} If not already -> add it //
if( !isset($user_id_list[ $match['id_user'] ]) )
$user_id_list[ $match['id_user'] ] = null;
}
}
/* (3) Join results
---------------------------------------------------------*/
/* (1) Join request */
$join_rq = Table::get('user')
->select('*')
->whereId([array_keys($user_id_list), Rows::COND_IN]);
/* (2) Return result */
return $join_rq->fetch();
} }

View File

@ -71,6 +71,20 @@ if( section.view.element != null ){
/* (3) Gestion de la recherche instantannee */ /* (3) Gestion de la recherche instantannee */
section.view.search.func = function(){ section.view.search.func = function(){
// if no keyword -> show all
if( section.view.search.bar.value.length == 0 ){
// On recupere la liste des elements correspondants aux utilisateurs
var user_list = document.querySelectorAll(section.view.text + '> article.inline-box[id]');
// Affiche chaque carte
for( var i = 0 ; i < user_list.length ; i++ )
user_list[i].remClass('hidden');
return;
}
var search = { var search = {
path: 'userDefault/search', path: 'userDefault/search',
keywords: section.view.search.bar.value keywords: section.view.search.bar.value
@ -86,7 +100,7 @@ if( section.view.element != null ){
// On enregistre tous les UID dans un tableau // On enregistre tous les UID dans un tableau
var uid_list = []; var uid_list = [];
for( var i = 0 ; i < result.users.length ; i++ ) for( var i = 0 ; i < result.users.length ; i++ )
uid_list.push( result.users[i].id_user); uid_list.push( result.users[i].id_user );
// On recupere la liste des elements correspondants aux utilisateurs // On recupere la liste des elements correspondants aux utilisateurs
var user_list = document.querySelectorAll(section.view.text + '> article.inline-box[id]'); var user_list = document.querySelectorAll(section.view.text + '> article.inline-box[id]');
@ -94,7 +108,7 @@ if( section.view.element != null ){
// Pour chaque utilisateur // Pour chaque utilisateur
for( var i = 0 ; i < user_list.length ; i++ ){ for( var i = 0 ; i < user_list.length ; i++ ){
// Si doit etre visible // Si doit etre visible
if( uid_list.indexOf(user_list[i].id) > -1 ) if( uid_list.indexOf(parseInt(user_list[i].id)) > -1 )
user_list[i].remClass('hidden'); user_list[i].remClass('hidden');
// Si ne doit pas etre visible // Si ne doit pas etre visible
else else