Update repo machine/search to match any keywords (space-separated) with a OR condition + minfix user

This commit is contained in:
xdrm-brackets 2017-11-13 13:34:38 +01:00
parent b745c9ef08
commit f99d1a8435
3 changed files with 108 additions and 33 deletions

View File

@ -66,18 +66,74 @@
*/ */
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 */
$search = Table::get('machine') $keyword = strtolower($keyword);
->whereIdWarehouse($id_warehouse)
->whereName(["%$keyword%", Rows::COND_LIKE]) /* (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('id_machine')
->select('name') ->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();
} }

View File

@ -91,41 +91,45 @@
} }
/* (2) Search for each keyword /* (2) Search for each keyword
---------------------------------------------------------*/ ---------------------------------------------------------*/ {
/* (1) Initialise id list that will contain each matching user ids */
$user_id_list = [];
/* (2) Request for each keyword */ /* (1) Initialise id list that will contain each matching user ids */
foreach($keywords as $kw){ $user_id_list = [];
// {2.1} Request // /* (2) Request for each keyword */
$searchusers = DatabaseDriver::getPDO()->prepare("SELECT id_user FROM user foreach($keywords as $kw){
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 // // {2.1} Request //
$searchusers->execute([ ':id_warehouse' => $id_warehouse ]); $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 // // {2.2} Inject params //
$matches = DatabaseDriver::delNumeric( $searchusers->fetchAll() ); $searchusers->execute([ ':id_warehouse' => $id_warehouse ]);
// {2.4} Only add non-already added ids // // {2.3} Fetch result //
foreach($matches as $match){ $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;
} }
} }

View File

@ -66,6 +66,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 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 = { var search = {
path: 'machineDefault/search', path: 'machineDefault/search',
keywords: section.view.search.bar.value 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]'); var machine_list = document.querySelectorAll(section.view.text + '> article.inline-box[id]');
// Pour chaque machine // Pour chaque machine
console.log(uid_list);
for( var i = 0 ; i < machine_list.length ; i++ ){ for( var i = 0 ; i < machine_list.length ; i++ ){
// Si doit etre visible // Si doit etre visible
if( uid_list.indexOf(parseInt(machine_list[i].id)) > -1 ) if( uid_list.indexOf(parseInt(machine_list[i].id)) > -1 )