From b745c9ef08aabe6fe01c5b8ad2682121d04aedc0 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Mon, 13 Nov 2017 13:25:31 +0100 Subject: [PATCH] Update repo user/search to match any keywords (space-separated) with a OR condition + if no search input -> display all --- build/database/repo/user.php | 84 +++++++++++++++++++++++++++++------- public_html/view/js/users.js | 18 +++++++- 2 files changed, 84 insertions(+), 18 deletions(-) diff --git a/build/database/repo/user.php b/build/database/repo/user.php index 16a41ba..f669b01 100755 --- a/build/database/repo/user.php +++ b/build/database/repo/user.php @@ -67,25 +67,77 @@ */ public static function search($id_warehouse, $keyword){ - // make keyword lowercase - $keyword = strtolower($keyword); + /* (1) Format keyword + ---------------------------------------------------------*/ { - // On recupere les donnees - $searchusers = DatabaseDriver::getPDO()->prepare("SELECT * FROM user - 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."%' - ) - "); + /* (1) Make all lowercase */ + $keyword = strtolower($keyword); - $searchusers->execute([ - ':id_warehouse' => $id_warehouse - ]); + /* (2) Create a keyword set (separator: space) */ + $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(); } diff --git a/public_html/view/js/users.js b/public_html/view/js/users.js index 5a9f1c9..7601d2d 100755 --- a/public_html/view/js/users.js +++ b/public_html/view/js/users.js @@ -71,6 +71,20 @@ if( section.view.element != null ){ /* (3) Gestion de la recherche instantannee */ 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 = { path: 'userDefault/search', keywords: section.view.search.bar.value @@ -86,7 +100,7 @@ if( section.view.element != null ){ // On enregistre tous les UID dans un tableau var uid_list = []; 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 var user_list = document.querySelectorAll(section.view.text + '> article.inline-box[id]'); @@ -94,7 +108,7 @@ if( section.view.element != null ){ // Pour chaque utilisateur for( var i = 0 ; i < user_list.length ; i++ ){ // 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'); // Si ne doit pas etre visible else