From f99d1a84350ec392e8515f567cb70a40dfddf9fc Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Mon, 13 Nov 2017 13:34:38 +0100 Subject: [PATCH] Update repo machine/search to match any keywords (space-separated) with a OR condition + minfix user --- build/database/repo/machine.php | 72 +++++++++++++++++++++++++++++---- build/database/repo/user.php | 54 +++++++++++++------------ public_html/view/js/machines.js | 15 +++++++ 3 files changed, 108 insertions(+), 33 deletions(-) diff --git a/build/database/repo/machine.php b/build/database/repo/machine.php index 8cbcf21..9581d6d 100755 --- a/build/database/repo/machine.php +++ b/build/database/repo/machine.php @@ -66,18 +66,74 @@ */ public static function search($id_warehouse, $keyword){ - // make keyword lowercase - $keyword = strtolower($keyword); + /* (1) Format keyword + ---------------------------------------------------------*/ { - // On recupere les donnees - $search = Table::get('machine') - ->whereIdWarehouse($id_warehouse) - ->whereName(["%$keyword%", Rows::COND_LIKE]) + /* (1) Make all lowercase */ + $keyword = strtolower($keyword); + + /* (2) Create a keyword set (separator: space) */ + $keywords = []; + $keywords_tmp = explode(' ', $keyword); + + /* (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 */ + $mac_id_list = []; + + /* (2) Request for each keyword */ + foreach($keywords as $kw){ + + // {2.1} Request // + $searchmac = Table::get('machine') + ->select('id_machine') + ->whereIdWarehouse($id_warehouse) + ->whereName(["%$kw%", Rows::COND_LIKE] ); + + // {2.2} Fetch result // + $matches = $searchmac->fetch(); + + // {2.3} Only add non-already added ids // + foreach($matches as $match){ + + // {2.4.1} If not already -> add it // + if( !isset($mac_id_list[ $match['id_machine'] ]) ) + $mac_id_list[ $match['id_machine'] ] = null; + + } + + } + + } + + + /* (3) Join results + ---------------------------------------------------------*/ + /* (1) Join request */ + $join_rq = Table::get('machine') ->select('id_machine') ->select('name') - ->orderby('name', Rows::ORDER_ASC); + ->whereId([array_keys($mac_id_list), Rows::COND_IN]); + + /* (2) Return result */ + return $join_rq->fetch(); - return $search->fetch(); } diff --git a/build/database/repo/user.php b/build/database/repo/user.php index f669b01..15fc99a 100755 --- a/build/database/repo/user.php +++ b/build/database/repo/user.php @@ -91,41 +91,45 @@ } + /* (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){ + /* (1) Initialise id list that will contain each matching user ids */ + $user_id_list = []; - // {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) Request for each keyword */ + foreach($keywords as $kw){ - // {2.2} Inject params // - $searchusers->execute([ ':id_warehouse' => $id_warehouse ]); + // {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.3} Fetch result // - $matches = DatabaseDriver::delNumeric( $searchusers->fetchAll() ); + // {2.2} Inject params // + $searchusers->execute([ ':id_warehouse' => $id_warehouse ]); - // {2.4} Only add non-already added ids // - foreach($matches as $match){ + // {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; + + } - // {2.4.1} If not already -> add it // - if( !isset($user_id_list[ $match['id_user'] ]) ) - $user_id_list[ $match['id_user'] ] = null; } - } diff --git a/public_html/view/js/machines.js b/public_html/view/js/machines.js index 746813c..3970b71 100755 --- a/public_html/view/js/machines.js +++ b/public_html/view/js/machines.js @@ -66,6 +66,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 mac_list = document.querySelectorAll(section.view.text + '> article.inline-box[id]'); + + // Affiche chaque carte + for( var i = 0 ; i < mac_list.length ; i++ ) + mac_list[i].remClass('hidden'); + + return; + + } + var search = { path: 'machineDefault/search', keywords: section.view.search.bar.value @@ -84,6 +98,7 @@ if( section.view.element != null ){ var machine_list = document.querySelectorAll(section.view.text + '> article.inline-box[id]'); // Pour chaque machine + console.log(uid_list); for( var i = 0 ; i < machine_list.length ; i++ ){ // Si doit etre visible if( uid_list.indexOf(parseInt(machine_list[i].id)) > -1 )