401 lines
10 KiB
PHP
Executable File
401 lines
10 KiB
PHP
Executable File
<?php
|
|
|
|
namespace manager\repo;
|
|
use \manager\Database;
|
|
use \manager\sessionManager;
|
|
use \manager\Checker;
|
|
use \manager\repo\cluster as clusterRepo;
|
|
|
|
class machine extends parentRepo{
|
|
|
|
protected static function table_name(){ static $table_name = 'machine'; return $table_name; }
|
|
|
|
/* CREATION D'UNE MACHINE
|
|
*
|
|
* @id_warehouse<int> UID de l'entrepot
|
|
* @name<String> Nom de la machine
|
|
*
|
|
* @return id_machine<int> Renvoie l'UID de la machine cree
|
|
* Renvoie FALSE si une erreur occure
|
|
*
|
|
*/
|
|
public static function create($id_warehouse, $name){
|
|
/* [1] Verification de l'unicite de name
|
|
=========================================================*/
|
|
$check_unique = self::getByName($id_warehouse, $name) === false;
|
|
|
|
// Si une machine a deja ce nom, on renvoie une erreur
|
|
if( !$check_unique ){
|
|
return false;
|
|
}
|
|
|
|
|
|
/* [2] Creation de la machine
|
|
=========================================================*/
|
|
$insert_machine = Database::getPDO()->prepare("INSERT INTO machine(id_machine, id_warehouse, name, token)
|
|
VALUES(DEFAULT, :id_warehouse, :name, :token)");
|
|
$insert_machine->execute([
|
|
':id_warehouse' => $id_warehouse,
|
|
':name' => $name,
|
|
':token' => sessionManager::secure_hash( uniqid() )
|
|
]);
|
|
|
|
/* [3] On retourne l'id_machine ou FALSE si erreur
|
|
=========================================================*/
|
|
$check_machine = self::getByName($id_warehouse, $name);
|
|
|
|
// Si n'existe pas on retourne FALSE
|
|
if( $check_machine === false )
|
|
return false;
|
|
|
|
// Sinon, on retourne son id
|
|
return $check_machine['id_machine'];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* RENVOIE UNE LISTE DE MACHINE EN FONCTION D'UN MOT CLE
|
|
*
|
|
* @id_warehouse<int> UID de l'entrepot
|
|
* @keyword<String> Element de recherche
|
|
*
|
|
* @return machines<Array> Retourne les machines trouvees
|
|
*
|
|
*/
|
|
public static function search($id_warehouse, $keyword){
|
|
// On recupere les donnees
|
|
$searchmachines = Database::getPDO()->prepare("SELECT id_machine, name FROM machine
|
|
WHERE id_warehouse = :id_warehouse
|
|
AND name LIKE '%".$keyword."%'");
|
|
|
|
$searchmachines->execute([
|
|
':id_warehouse' => $id_warehouse
|
|
]);
|
|
|
|
return Database::delNumeric( $searchmachines->fetchAll() );
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* RENVOIE LES GROUPES AUQUEL APPARTIENT UNE MACHINE DONNEE
|
|
*
|
|
* @id_warehouse<int> UID de l'entrepot
|
|
* @id_machine<int> UID de la machine en question
|
|
*
|
|
* @return clusters<Array> Retourne la liste des groupes auquel appartient la machine
|
|
*
|
|
*/
|
|
public static function getClusters($id_warehouse, $id_machine){
|
|
/* [1] On redige/execute la requete
|
|
=========================================================*/
|
|
$get_clusters = Database::getPDO()->prepare("SELECT c.*
|
|
FROM machine_cluster as c, machine_cluster_merge as cm, machine as m
|
|
WHERE cm.id_machine_cluster = c.id_machine_cluster
|
|
AND cm.id_machine = m.id_machine
|
|
AND cm.id_machine = :id_machine
|
|
AND c.id_warehouse = m.id_warehouse
|
|
AND m.id_warehouse = :id_warehouse
|
|
ORDER BY c.name");
|
|
$get_clusters->execute([
|
|
':id_warehouse' => $id_warehouse,
|
|
':id_machine' => $id_machine
|
|
]);
|
|
|
|
/* [2] On retourne la liste des groupes
|
|
=========================================================*/
|
|
return Database::delNumeric( $get_clusters->fetchAll() );
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* MODIFICATION D'UNE MACHINE DONNEE
|
|
*
|
|
* @id_warehouse<int> UID de l'entrepot
|
|
* @id_machine<int> UID de la machine
|
|
* @name<String> Identifiant l'utilisateur
|
|
*
|
|
* @return status<Boolean> Renvoie si oui ou non tout s'est bien passe
|
|
*
|
|
*/
|
|
public static function edit($id_warehouse, $id_machine=null, $name=null){
|
|
/* [1] Verification de l'unicite de name (si differents)
|
|
=========================================================*/
|
|
// On recupere les machine ayant le meme name (si existent)
|
|
$getbyname = self::getByName($id_warehouse, $name);
|
|
|
|
$check_unique = is_array($getbyname) && $getbyname['id_machine'] == $id_machine || !is_array($getbyname);
|
|
|
|
// Si un utilisateur a deja ce name (sauf lui), on renvoie une erreur
|
|
if( !$check_unique )
|
|
return false;
|
|
|
|
/* [2] Modification de la machine
|
|
=========================================================*/
|
|
$edit_machine = Database::getPDO()->prepare("UPDATE machine
|
|
SET name = :name
|
|
WHERE id_machine = :id_machine
|
|
AND id_warehouse = :id_warehouse");
|
|
$edit_machine->execute([
|
|
':name' => $name,
|
|
':id_machine' => $id_machine,
|
|
':id_warehouse' => $id_warehouse
|
|
]);
|
|
|
|
|
|
|
|
/* [3] On retourne si tout a ete modifie, si erreur
|
|
=========================================================*/
|
|
$check_machine = self::getById($id_warehouse, $id_machine);
|
|
|
|
$checker = $check_machine['id_machine'] == $id_machine;
|
|
$checker = $checker && $check_machine['name'] == $name;
|
|
|
|
// On retourne l'etat de la modification
|
|
return $checker;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* SUPPRIME UNE MACHINE DONNE
|
|
*
|
|
* @id_warehouse<int> UID de l'entrepot
|
|
* @id_machine<int> UID de la machine en question
|
|
*
|
|
* @return status<Boolean> Retourne si oui ou non la machine a bien ete supprime
|
|
*
|
|
*/
|
|
public static function delete($id_warehouse, $id_machine){
|
|
/* [1] On redige/execute la requete
|
|
=========================================================*/
|
|
$delete_machine = Database::getPDO()->prepare("DELETE FROM machine
|
|
WHERE id_machine = :id_machine
|
|
AND id_warehouse = :id_warehouse");
|
|
$delete_machine->execute([
|
|
':id_machine' => $id_machine,
|
|
':id_warehouse' => $id_warehouse
|
|
]);
|
|
|
|
|
|
/* [2] On verifie que la machine n'existe plus
|
|
=========================================================*/
|
|
return ( self::getById($id_warehouse, $id_machine) === false );
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* RETOURNE UNE MACHINE SPECIFIQUE
|
|
*
|
|
* @id_warehouse<id> UID de l'entrepot
|
|
* @id_machine<id> UID de la machine
|
|
*
|
|
* @return machine<Array> Données de la machine
|
|
* FALSE si aucun résultat
|
|
*
|
|
*/
|
|
public static function getById($id_warehouse, $id_machine){
|
|
/* [1] On rédige/execute la requête
|
|
=========================================================*/
|
|
$get = Database::getPDO()->prepare("SELECT m.id_machine, m.name
|
|
FROM machine as m
|
|
WHERE m.id_warehouse = :id_warehouse
|
|
AND m.id_machine = :id_machine");
|
|
$get->execute([
|
|
':id_warehouse' => $id_warehouse,
|
|
':id_machine' => $id_machine
|
|
]);
|
|
|
|
/* [2] Gestion des données
|
|
=========================================================*/
|
|
$found = $get->fetch();
|
|
|
|
// Si aucun résultat
|
|
if( $found === false )
|
|
return false;
|
|
|
|
return Database::delNumeric( $found, true );
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* RETOURNE UNE MACHINE SPECIFIQUE
|
|
*
|
|
* @id_warehouse<id> UID de l'entrepot
|
|
* @name<String> Nom de la machine
|
|
*
|
|
* @return machine<Array> Données de la machine
|
|
* FALSE si aucun résultat
|
|
*
|
|
*/
|
|
public static function getByName($id_warehouse, $name){
|
|
/* [1] On rédige/execute la requête
|
|
=========================================================*/
|
|
$get = Database::getPDO()->prepare("SELECT m.id_machine, m.name
|
|
FROM machine as m
|
|
WHERE m.id_warehouse = :id_warehouse
|
|
AND m.name = :name");
|
|
$get->execute([
|
|
':id_warehouse' => $id_warehouse,
|
|
':name' => $name
|
|
]);
|
|
|
|
/* [2] Gestion des données
|
|
=========================================================*/
|
|
$found = $get->fetch();
|
|
|
|
// Si aucun résultat
|
|
if( $found === false )
|
|
return false;
|
|
|
|
return Database::delNumeric( $found, true );
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* RETOURNE UNE MACHINE SPECIFIQUE
|
|
*
|
|
* @id_warehouse<id> UID de l'entrepot
|
|
* @token<String> Token de la machine
|
|
*
|
|
* @return machine<Array> Données de la machine
|
|
* FALSE si aucun résultat
|
|
*
|
|
*/
|
|
public static function getByToken($id_warehouse, $token){
|
|
/* [1] On rédige/execute la requête
|
|
=========================================================*/
|
|
$get = Database::getPDO()->prepare("SELECT m.id_machine, m.name
|
|
FROM machine as m
|
|
WHERE m.id_warehouse = :id_warehouse
|
|
AND m.token = :token");
|
|
$get->execute([
|
|
':id_warehouse' => $id_warehouse,
|
|
':token' => $token
|
|
]);
|
|
|
|
/* [2] Gestion des données
|
|
=========================================================*/
|
|
$found = $get->fetch();
|
|
|
|
// Si aucun résultat
|
|
if( $found === false )
|
|
return false;
|
|
|
|
return Database::delNumeric( $found, true );
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* RETOURNE TOUTES LES MACHINES DE L'ENTREPOT
|
|
*
|
|
* @id_warehouse<id> UID de l'entrepot
|
|
*
|
|
* @return machines<Array> Données des la machine
|
|
*
|
|
*/
|
|
public static function getAll($id_warehouse){
|
|
/* [1] On rédige/execute la requête
|
|
=========================================================*/
|
|
$get = Database::getPDO()->prepare("SELECT m.id_machine, m.name
|
|
FROM machine as m
|
|
WHERE m.id_warehouse = :id_warehouse");
|
|
$get->execute([
|
|
':id_warehouse' => $id_warehouse
|
|
]);
|
|
|
|
return Database::delNumeric( $get->fetchAll() );
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* VERIFIE MET A JOUR LE TOKEN DE SYNCHRONISATION
|
|
*
|
|
* @id_warehouse<int> UID de l'entrepot
|
|
* @token<String> Token de synchronisation
|
|
* @newToken<String> Nouveau token de synchronisation (optionnel, uniquement quand on arrive à la fin du cycle de la hashChain)
|
|
*
|
|
* @return status<Boolean> VRAI si le token est correct, sinon FALSE
|
|
*
|
|
*/
|
|
public static function checkToken($id_warehouse, $token, $newToken=null){
|
|
/* [1] On vérifie le token
|
|
=========================================================*/
|
|
$hash = sessionManager::secure_hash($token);
|
|
|
|
$byToken = self::getByToken($id_warehouse, $hash);
|
|
|
|
// Si aucun résultat, erreur
|
|
if( count($byToken) < 1 )
|
|
return false;
|
|
|
|
|
|
/* [2] On met à jour le token
|
|
=========================================================*/
|
|
$updateToken = Database::getPDO()->prepare("UPDATE machine
|
|
SET token = :token
|
|
WHERE id_machine = :id_machine");
|
|
$updateToken->execute([
|
|
':token' => Checker::run('hash', $newToken) ? $newToken : $token,
|
|
':id_machine' => $byToken[0]['id_machine']
|
|
]);
|
|
|
|
|
|
/* [3] On retourne que tout s'est bien déroulé
|
|
=========================================================*/
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
?>
|