2017-01-11 15:28:21 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace database\repo;
|
|
|
|
use \database\core\DatabaseDriver;
|
2017-07-29 16:45:24 +00:00
|
|
|
use \database\core\Repo;
|
|
|
|
use \error\core\Err;
|
2017-01-11 15:28:21 +00:00
|
|
|
use \orm\core\Table;
|
|
|
|
use \orm\core\Rows;
|
|
|
|
|
|
|
|
class history extends parentRepo{
|
|
|
|
|
|
|
|
protected static function table_name(){ static $table_name = 'history'; return $table_name; }
|
|
|
|
|
|
|
|
/* CREATION D'UNE ENTREE DANS L'HISTORIQUE
|
|
|
|
*
|
|
|
|
* @id_user<int> UID de l'utilisateur
|
|
|
|
* @id_machine<int> UID de la machine
|
|
|
|
* @id_action<int> UID de l'action
|
|
|
|
* @timestamp<int> Timestamp de l'action
|
|
|
|
*
|
|
|
|
* @return id_history<int> Renvoie l'UID de l'entree cree
|
|
|
|
* Renvoie FALSE si une erreur occure
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function create($id_user, $id_machine, $id_action, $timestamp){
|
2017-02-20 09:46:39 +00:00
|
|
|
|
2017-01-11 15:28:21 +00:00
|
|
|
/* [1] On retourne l'id_history ou FALSE si erreur
|
|
|
|
=========================================================*/
|
|
|
|
$inserted = Table::get('history')->insert([
|
|
|
|
'id_history' => Rows::INSERT_DEFAULT,
|
|
|
|
'id_user' => $id_user,
|
|
|
|
'id_machine' => $id_machine,
|
|
|
|
'id_action' => $id_action,
|
|
|
|
'timestamp' => $timestamp
|
|
|
|
]);
|
|
|
|
|
|
|
|
// Si erreur d'insertion, erreur
|
|
|
|
if( !$inserted )
|
|
|
|
return false; // Si pb d'unicité du code ou username (car sont uniques) ou autre
|
|
|
|
|
|
|
|
|
|
|
|
/* [2] On récupère l'id_history
|
|
|
|
=========================================================*/
|
|
|
|
$check_history = self::getByTimestamp($timestamp);
|
|
|
|
|
|
|
|
// Si on trouve pas, erreur
|
2017-02-20 07:21:34 +00:00
|
|
|
if( count($check_history) < 1 )
|
2017-01-11 15:28:21 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Sinon, on retourne son id
|
2017-02-20 07:21:34 +00:00
|
|
|
return $check_history[0]['id_history'];
|
2017-01-11 15:28:21 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* SUPPRIME UNE ENTREE DONNEE
|
|
|
|
*
|
|
|
|
* @id_history<int> UID de l'entreee
|
|
|
|
*
|
|
|
|
* @return status<Boolean> Retourne si oui ou non l'entree a bien ete supprimee
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function delete($id_history){
|
|
|
|
/* [1] On redige/execute la requete
|
|
|
|
=========================================================*/
|
|
|
|
$delete = Table::get('history')
|
|
|
|
->whereId($id_history);
|
|
|
|
|
|
|
|
// On retourne l'état
|
|
|
|
return $delete->delete();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-02-20 09:46:39 +00:00
|
|
|
/* RETOURNE UNE ENTREE SPECIFIQUE
|
|
|
|
*
|
|
|
|
* @id_machine<int> UID de la machine
|
|
|
|
*
|
|
|
|
* @return entry<Array> Données de l'entree
|
|
|
|
* FALSE si aucun résultat
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function getByIdMachine($id_machine){
|
|
|
|
/* [1] On rédige/execute la requête
|
|
|
|
=========================================================*/
|
|
|
|
$machine = Table::get('history')
|
|
|
|
->whereIdMachine($id_machine)
|
|
|
|
->orderby('timestamp', Rows::ORDER_DESC)
|
|
|
|
->select('*');
|
|
|
|
|
|
|
|
return $machine->fetch();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* RETOURNE UNE ENTREE SPECIFIQUE
|
|
|
|
*
|
|
|
|
* @id_user<int> UID de l'utilisateur
|
|
|
|
*
|
|
|
|
* @return entry<Array> Données de l'entree
|
|
|
|
* FALSE si aucun résultat
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function getByIdUser($id_user){
|
|
|
|
/* [1] On rédige/execute la requête
|
|
|
|
=========================================================*/
|
|
|
|
$user = Table::get('history')
|
|
|
|
->whereIdUser($id_user)
|
|
|
|
->orderby('timestamp', Rows::ORDER_DESC)
|
|
|
|
->select('*');
|
|
|
|
|
|
|
|
return $user->fetch();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-01-11 15:28:21 +00:00
|
|
|
/* RETOURNE UNE ENTREE SPECIFIQUE
|
|
|
|
*
|
|
|
|
* @id_history<int> UID de l'entree
|
|
|
|
*
|
|
|
|
* @return entry<Array> Données de l'entree
|
|
|
|
* FALSE si aucun résultat
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function getById($id_history){
|
|
|
|
/* [1] On rédige/execute la requête
|
|
|
|
=========================================================*/
|
|
|
|
$user = Table::get('user')
|
2017-02-20 09:46:39 +00:00
|
|
|
->whereId($id_history)
|
|
|
|
->orderby('timestamp', Rows::ORDER_DESC)
|
|
|
|
->select('*');
|
2017-01-11 15:28:21 +00:00
|
|
|
|
|
|
|
return $user->fetch();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* RETOURNE TOUT L'HISTORIQUE DE L'ENTREPOT
|
|
|
|
*
|
|
|
|
* @id_warehouse<int> UID de l'entrepot
|
|
|
|
*
|
|
|
|
* @return entries<Array> Entrees de l'historique
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function getAll($id_warehouse){
|
|
|
|
/* [1] On rédige/execute la requête
|
|
|
|
=========================================================*/
|
|
|
|
$users = Table::get('user')
|
|
|
|
->whereIdWarehouse($id_warehouse)
|
|
|
|
->select('id_user')
|
|
|
|
->select('username', null, null, 'user_name')
|
|
|
|
->select('firstname', null, null, 'user_firstname')
|
|
|
|
->select('lastname', null, null, 'user_lastname');
|
|
|
|
|
|
|
|
$machines = Table::get('machine')
|
|
|
|
->whereIdWarehouse($id_warehouse)
|
|
|
|
->select('id_machine')
|
|
|
|
->select('name', null, null, 'machine_name');
|
|
|
|
|
|
|
|
$actions = Table::get('action')
|
|
|
|
->select('id_action')
|
|
|
|
->select('name', null, null, 'action_name');
|
|
|
|
|
|
|
|
|
|
|
|
$history = Table::get('history')
|
|
|
|
->join('id_user', $users)
|
|
|
|
->join('id_machine', $machines)
|
|
|
|
->join('id_action', $actions)
|
|
|
|
->select('id_history')
|
|
|
|
->select('timestamp')
|
2017-02-19 17:07:04 +00:00
|
|
|
->orderby('timestamp', Rows::ORDER_DESC);
|
2017-01-11 15:28:21 +00:00
|
|
|
|
|
|
|
return $history->fetch();
|
|
|
|
}
|
|
|
|
|
2017-07-29 16:45:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* RENVOIE UNE LISTE DE LOGS EN FONCTION D'UN MOT CLE
|
|
|
|
*
|
|
|
|
* @id_warehouse<int> UID de l'entrepot
|
|
|
|
* @keyword<String> Element de recherche
|
|
|
|
*
|
|
|
|
* @return logs<Array> Retourne les logs trouvees
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function search($id_warehouse, $keyword){
|
|
|
|
|
|
|
|
$machine_ids = [];
|
|
|
|
$user_ids = [];
|
|
|
|
|
|
|
|
/* [1] Recherche dans les relations 'machines'
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) On exécute la requête */
|
|
|
|
$machines_r = new Repo('machine/search', [$id_warehouse, $keyword]);
|
|
|
|
|
|
|
|
/* (2) Gestion succès */
|
|
|
|
if( $machines_r->error->get() == Err::Success )
|
|
|
|
foreach($machines_r->answer() as $row)
|
|
|
|
$machine_ids[] = $row['id_machine'];
|
|
|
|
|
|
|
|
/* [2] Recherche dans les relations 'users'
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) On cherche dans les utilisateurs */
|
|
|
|
$users_r = new Repo('user/search', [$id_warehouse, $keyword]);
|
|
|
|
|
|
|
|
/* (2) Gestion succès */
|
|
|
|
if( $users_r->error->get() == Err::Success )
|
|
|
|
foreach($users_r->answer() as $row)
|
|
|
|
$user_ids[] = $row['id_user'];
|
|
|
|
|
|
|
|
|
|
|
|
/* [3] On rédige/execute la requête
|
|
|
|
=========================================================*/
|
|
|
|
$history_r = DatabaseDriver::getPDO()->prepare("SELECT id_history FROM history
|
|
|
|
WHERE id_user IN (SELECT id_user FROM user WHERE username LIKE '%$keyword%')
|
|
|
|
or id_machine IN (SELECT id_machine FROM machine WHERE name LIKE '%$keyword%')
|
|
|
|
ORDER BY id_history ASC;");
|
|
|
|
|
|
|
|
$history_r->execute();
|
|
|
|
|
|
|
|
return DatabaseDriver::delNumeric( $history_r->fetchAll() );
|
|
|
|
}
|
|
|
|
|
2017-01-11 15:28:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
?>
|