293 lines
7.0 KiB
PHP
Executable File
293 lines
7.0 KiB
PHP
Executable File
<?php
|
|
|
|
namespace database\repo;
|
|
use \database\core\DatabaseDriver;
|
|
use \database\core\Repo;
|
|
use \error\core\Err;
|
|
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){
|
|
|
|
/* [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
|
|
if( count($check_history) < 1 )
|
|
return false;
|
|
|
|
// Sinon, on retourne son id
|
|
return $check_history[0]['id_history'];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 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();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 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_warehouse, $id_machine){
|
|
|
|
/* [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)
|
|
->whereId($id_machine)
|
|
->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')
|
|
->orderby('timestamp', Rows::ORDER_DESC);
|
|
|
|
return $history->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();
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* RETOURNE UNE ENTREE SPECIFIQUE
|
|
*
|
|
* @id_entry<int> UID de l'entrée historique
|
|
*
|
|
* @return entry<Array> Données de l'entree
|
|
* FALSE si erreur | aucun résultat
|
|
*
|
|
*/
|
|
public static function getById($id_entry){
|
|
/* [1] On rédige/execute la requête
|
|
=========================================================*/
|
|
$entry = Table::get('history')
|
|
->whereId($id_entry)
|
|
->orderby('timestamp', Rows::ORDER_DESC)
|
|
->select('*')
|
|
->unique();
|
|
|
|
return $entry->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')
|
|
->orderby('timestamp', Rows::ORDER_DESC);
|
|
|
|
return $history->fetch();
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 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 timestamp DESC;");
|
|
|
|
$history_r->execute();
|
|
|
|
return DatabaseDriver::delNumeric( $history_r->fetchAll() );
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
?>
|