Now history.view.search searches through users+machines (as on its views) + TODO: manage space separated keywords

This commit is contained in:
xdrm-brackets 2017-11-13 12:17:26 +01:00
parent ad4143771c
commit d30ec35636
3 changed files with 51 additions and 17 deletions

View File

@ -235,34 +235,60 @@
/* [1] Recherche dans les relations 'machines' /* [1] Recherche dans les relations 'machines'
=========================================================*/ =========================================================*/
/* (1) On exécute la requête */ /* (1) On exécute la requête */
$machines_r = new Repo('machine/search', [$id_warehouse, $keyword]); $machines_rq = new Repo('machine/search', [$id_warehouse, $keyword]);
$machines_rs = $machines_rq->answer();
/* (2) Gestion succès */ /* (2) Gestion succès */
if( $machines_r->error->get() == Err::Success ) if( is_array($machines_rs) )
foreach($machines_r->answer() as $row) foreach($machines_rs as $row)
$machine_ids[] = $row['id_machine']; $machine_ids[] = $row['id_machine'];
/* [2] Recherche dans les relations 'users' /* [2] Recherche dans les relations 'users'
=========================================================*/ =========================================================*/
/* (1) On cherche dans les utilisateurs */ /* (1) On cherche dans les utilisateurs */
$users_r = new Repo('user/search', [$id_warehouse, $keyword]); $users_rq = new Repo('user/search', [$id_warehouse, $keyword]);
$users_rs = $users_rq->answer();
/* (2) Gestion succès */ /* (2) Gestion succès */
if( $users_r->error->get() == Err::Success ) if( is_array($users_rs) )
foreach($users_r->answer() as $row) foreach($users_rs as $row)
$user_ids[] = $row['id_user']; $user_ids[] = $row['id_user'];
/* [3] On rédige/execute la requête /* [3] On rédige/execute la requête
=========================================================*/ =========================================================*/
$history_r = DatabaseDriver::getPDO()->prepare("SELECT id_history FROM history /* (1) Recherche par utilisateur */
WHERE id_user IN (SELECT id_user FROM user WHERE username LIKE '%$keyword%') $history_r_user = Table::get('history')
or id_machine IN (SELECT id_machine FROM machine WHERE name LIKE '%$keyword%') ->select('id_history')
ORDER BY timestamp DESC;"); ->whereIdUser([ $user_ids, Rows::COND_IN ]);
$history_r->execute(); /* (2) Recherche par utilisateur */
$history_r_machine = Table::get('history')
->select('id_history')
->whereIdMachine([ $machine_ids, Rows::COND_IN ]);
return DatabaseDriver::delNumeric( $history_r->fetchAll() ); /* (3) On enregistre les entrées par utilisateur */
$already_id = [];
$ids = $history_r_user->fetch();
foreach($ids as $user_result)
$already_id[ $user_result['id_history'] ] = null;
/* (4) On ajoute les entrées par machines (si pas déja) */
foreach($history_r_machine->fetch() as $machine_result)
if( !isset($already_id[$machine_result['id_history']]) ){
$ids[] = $machine_result;
$already_id[ $machine_result['id_history'] ] = null;
}
/* (5) On retourne le total */
return $ids;
} }

View File

@ -65,6 +65,10 @@
* *
*/ */
public static function search($id_warehouse, $keyword){ public static function search($id_warehouse, $keyword){
// make keyword lowercase
$keyword = strtolower($keyword);
// On recupere les donnees // On recupere les donnees
$search = Table::get('machine') $search = Table::get('machine')
->whereIdWarehouse($id_warehouse) ->whereIdWarehouse($id_warehouse)

View File

@ -66,14 +66,18 @@
* *
*/ */
public static function search($id_warehouse, $keyword){ public static function search($id_warehouse, $keyword){
// make keyword lowercase
$keyword = strtolower($keyword);
// On recupere les donnees // On recupere les donnees
$searchusers = DatabaseDriver::getPDO()->prepare("SELECT * FROM user $searchusers = DatabaseDriver::getPDO()->prepare("SELECT * FROM user
WHERE id_warehouse = :id_warehouse WHERE id_warehouse = :id_warehouse
AND ( code LIKE '%".$keyword."%' AND ( LOWER(code) LIKE '%".$keyword."%'
OR username LIKE '%".$keyword."%' OR LOWER(username) LIKE '%".$keyword."%'
OR firstname LIKE '%".$keyword."%' OR LOWER(firstname) LIKE '%".$keyword."%'
OR lastname LIKE '%".$keyword."%' OR LOWER(lastname) LIKE '%".$keyword."%'
OR mail LIKE '%".$keyword."%' OR LOWER(mail) LIKE '%".$keyword."%'
) )
"); ");