SMMP/build/api/module/historyDefault.php

239 lines
5.3 KiB
PHP
Executable File

<?php
namespace api\module;
use \database\core\DatabaseDriver;
use \manager\sessionManager;
use \error\core\Error;
use \error\core\Err;
use \database\core\Repo;
class historyDefault{
public function __construct(){}
public function __destruct(){}
/* CREATION D'UNE NOUVELLE ENTREE DANS LA BDD
*
* @id_user<int> UID de l'utilisateur
* @id_machine<int> UID la machine
* @id_action<int> UID de l'action
* @timestamp<int> timestamp de l'action
*
* @return status<Boolean> Retourne si oui ou non, tout s'est bien passe
*
*/
public function create($params){
extract($params);
/* [1] Creation de l'utilisateur
=========================================================*/
$create_entry = new Repo('history/create', [
$id_user,
$id_machine,
$id_action,
$timestamp
]);
$id_entry = $create_entry->answer();
// Si une erreur est retournee, on retourne une erreur
if( $id_entry === false )
return ['error' => new Error(Err::ModuleError)];
/* [2] Gestion du retour
=========================================================*/
return [
'id_history' => $id_entry
];
}
/* RENVOIE UN UTILISATEUR EN FONCTION D'UN MOT CLE
*
* @keywords<String> Element de recherche
*
* @return users<Array> Retourne la liste des utilisateurs trouves
*
*/
public function search($params){
extract($params);
// On recupere les donnees
$user = new Repo('history/search', [
$_SESSION['WAREHOUSE']['id'],
$keywords
]);
return [ 'history' => $user->answer() ];
}
/* RENVOIE LA LISTE EXHAUSTIVE DES ACCES
*
* @return history<Array> Liste des entrées de l'historique
*
*/
public function getAll(){
// On recupere les donnees
$entries = new Repo('history/getAll', [ $_SESSION['WAREHOUSE']['id'] ]);
return [ 'history' => $entries->answer() ];
}
/* SUPPRIME UNE ENTREE
*
* @id_history<int> UID de l'entree en question
*
* @return status<Boolean> Retourne si oui ou non tout s'est bien deroule
*
*/
public function delete($params){
extract($params);
/* [1] On supprime l'utilisateur
=========================================================*/
$del_entry = new Repo('history/delete', [
$_SESSION['WAREHOUSE']['id'],
$id_history
]);
$deleted_entry = $del_entry->answer();
return [ 'status' => $deleted_entry ];
}
/* ARCHIVE L'HISTORIQUE D'UN ENTREPOT
*
* @return archive<File> Fichier de l'archive (.csv format)
*
*/
public function archive($params){
extract($params);
/* [1] Récupération des logs
=========================================================*/
/* (1) Requête */
$logs = new Repo('history/getAll', [$_SESSION['WAREHOUSE']['id']]);
/* (2) Gestion erreur */
if( $logs->error->get() != Err::Success )
return [ 'error' => $logs->error ];
/* [2] Création du contenu fichier
=========================================================*/
$BODY = "TIMESTAMP,MACHINE,UTILISATEUR,ACTION\n";
foreach($logs->answer() as $log)
$BODY .= $log['timestamp'].','.$log['machine_name'].','.$log['user_name'].','.$log['action_name']."\n";
/* [3] Renvoi du fichier
=========================================================*/
return [
'headers' => [
'Content-Type' => 'text/csv',
'Content-Disposition' => 'attachment; filename=archive-'.date('dmY').'.csv'
],
'body' => $BODY
];
}
/* (x) Return the machine history for an history entry
*
* @id_entry<id> UID of the history entry
*
* @return timeline<array> Machine timeline data
*
---------------------------------------------------------*/
public function get_timeline($params){
extract($params);
/* (1) Get history entry data
---------------------------------------------------------*/
/* (1) Request */
$entry = new Repo('history/getById', [$id_entry]);
/* (2) Get response */
$entry = $entry->answer();
/* (3) Manage error */
if( !is_array($entry) )
return ['error' => new Error(Err::RepoError)];
/* (2) Get history for machine
---------------------------------------------------------*/
/* (1) Request */
$timeline = new Repo('history/getByIdMachine', [
$_SESSION['WAREHOUSE']['id'],
$entry['id_machine']
]);
/* (2) Get response */
$timeline = $timeline->answer();
/* (3) Manage error */
if( $timeline === false )
return ['error' => new Error(Err::RepoError)];
/* (3) Return data
---------------------------------------------------------*/
return [ 'timeline' => $timeline ];
}
/* (x) Return the entry data for an history id
*
* @id_entry<id> UID of the history entry
*
* @return data<array> Entry data
*
---------------------------------------------------------*/
public function getById($params){
extract($params);
/* (1) Get history entry data
---------------------------------------------------------*/
/* (1) Request */
$entry = new Repo('history/getById', [$id_entry]);
/* (2) Get response */
$entry = $entry->answer();
/* (3) Manage error */
if( !is_array($entry) )
return ['error' => new Error(Err::RepoError)];
/* (2) Return data
---------------------------------------------------------*/
return [ 'entry' => $entry ];
}
}
?>