2016-02-14 11:37:19 +00:00
|
|
|
<?php
|
|
|
|
|
2016-10-18 14:03:03 +00:00
|
|
|
namespace database\repo;
|
2016-11-05 13:57:35 +00:00
|
|
|
use \database\core\DatabaseDriver;
|
2016-10-18 14:03:03 +00:00
|
|
|
use \orm\core\Table;
|
|
|
|
use \orm\core\Rows;
|
|
|
|
use \api\core\Checker;
|
2016-02-14 11:37:19 +00:00
|
|
|
use \manager\repo\cluster as clusterRepo;
|
|
|
|
|
2016-07-04 10:13:35 +00:00
|
|
|
class machine extends parentRepo{
|
|
|
|
|
|
|
|
protected static function table_name(){ static $table_name = 'machine'; return $table_name; }
|
2016-02-14 11:37:19 +00:00
|
|
|
|
|
|
|
/* CREATION D'UNE MACHINE
|
|
|
|
*
|
2016-07-06 09:56:17 +00:00
|
|
|
* @id_warehouse<int> UID de l'entrepot
|
2016-02-14 13:16:47 +00:00
|
|
|
* @name<String> Nom de la machine
|
2016-02-14 11:37:19 +00:00
|
|
|
*
|
2016-02-14 13:16:47 +00:00
|
|
|
* @return id_machine<int> Renvoie l'UID de la machine cree
|
2016-02-14 11:37:19 +00:00
|
|
|
* Renvoie FALSE si une erreur occure
|
|
|
|
*
|
|
|
|
*/
|
2016-07-09 12:13:08 +00:00
|
|
|
public static function create($id_warehouse, $name){
|
2016-07-23 14:36:58 +00:00
|
|
|
/* [1] Creation de la machine
|
2016-02-14 11:37:19 +00:00
|
|
|
=========================================================*/
|
2016-07-23 14:36:58 +00:00
|
|
|
$inserted = Table::get('machine')->insert([
|
2016-07-24 17:18:55 +00:00
|
|
|
'id_machine' => Rows::INSERT_DEFAULT,
|
2016-07-23 14:36:58 +00:00
|
|
|
'id_warehouse' => $id_warehouse,
|
|
|
|
'name' => $name,
|
2017-02-20 08:57:26 +00:00
|
|
|
'token' => null,
|
|
|
|
'unlock_code' => null
|
2016-07-23 14:36:58 +00:00
|
|
|
]);
|
2016-02-14 11:37:19 +00:00
|
|
|
|
2016-07-23 14:36:58 +00:00
|
|
|
// Si erreur (car name doit être unique)
|
|
|
|
if( !$inserted )
|
2016-02-14 11:37:19 +00:00
|
|
|
return false;
|
|
|
|
|
2016-07-23 14:36:58 +00:00
|
|
|
/* [2] On retourne l'id_machine ou FALSE si erreur
|
2016-02-14 11:37:19 +00:00
|
|
|
=========================================================*/
|
2016-07-09 12:13:08 +00:00
|
|
|
$check_machine = self::getByName($id_warehouse, $name);
|
2016-02-14 11:37:19 +00:00
|
|
|
|
|
|
|
// Si n'existe pas on retourne FALSE
|
2016-02-14 13:16:47 +00:00
|
|
|
if( $check_machine === false )
|
2016-02-14 11:37:19 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Sinon, on retourne son id
|
2016-02-14 13:16:47 +00:00
|
|
|
return $check_machine['id_machine'];
|
2016-02-14 11:37:19 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-16 13:32:33 +00:00
|
|
|
/* RENVOIE UNE LISTE DE MACHINE EN FONCTION D'UN MOT CLE
|
|
|
|
*
|
2016-07-06 09:56:17 +00:00
|
|
|
* @id_warehouse<int> UID de l'entrepot
|
2016-02-16 13:32:33 +00:00
|
|
|
* @keyword<String> Element de recherche
|
|
|
|
*
|
|
|
|
* @return machines<Array> Retourne les machines trouvees
|
|
|
|
*
|
|
|
|
*/
|
2016-07-06 09:56:17 +00:00
|
|
|
public static function search($id_warehouse, $keyword){
|
2016-02-16 13:32:33 +00:00
|
|
|
// On recupere les donnees
|
2016-07-23 14:36:58 +00:00
|
|
|
$search = Table::get('machine')
|
|
|
|
->whereIdWarehouse($id_warehouse)
|
|
|
|
->whereName(["%$keyword%", Rows::COND_LIKE])
|
|
|
|
->select('id_machine')
|
2017-02-19 11:14:03 +00:00
|
|
|
->select('name')
|
|
|
|
->orderby('name', Rows::ORDER_ASC);
|
2016-02-16 13:32:33 +00:00
|
|
|
|
2016-07-23 14:36:58 +00:00
|
|
|
return $search->fetch();
|
2016-02-16 13:32:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-14 11:37:19 +00:00
|
|
|
|
2016-02-14 13:16:47 +00:00
|
|
|
/* RENVOIE LES GROUPES AUQUEL APPARTIENT UNE MACHINE DONNEE
|
|
|
|
*
|
2016-07-06 09:56:17 +00:00
|
|
|
* @id_warehouse<int> UID de l'entrepot
|
2016-02-14 13:16:47 +00:00
|
|
|
* @id_machine<int> UID de la machine en question
|
|
|
|
*
|
|
|
|
* @return clusters<Array> Retourne la liste des groupes auquel appartient la machine
|
|
|
|
*
|
|
|
|
*/
|
2016-07-06 09:56:17 +00:00
|
|
|
public static function getClusters($id_warehouse, $id_machine){
|
2016-02-14 13:16:47 +00:00
|
|
|
/* [1] On redige/execute la requete
|
|
|
|
=========================================================*/
|
2016-07-23 14:36:58 +00:00
|
|
|
$cluster = Table::get('machine_cluster')
|
|
|
|
->whereIdWarehouse($id_warehouse)
|
2017-02-19 11:14:03 +00:00
|
|
|
->orderby('name', Rows::ORDER_ASC)
|
2016-07-23 14:36:58 +00:00
|
|
|
->select('*');
|
|
|
|
$cluster_merge = Table::get('machine_cluster_merge')
|
|
|
|
->whereIdMachine($id_machine)
|
|
|
|
->join('id_machine_cluster', $cluster);
|
2016-02-14 13:16:47 +00:00
|
|
|
|
|
|
|
/* [2] On retourne la liste des groupes
|
|
|
|
=========================================================*/
|
2016-07-23 14:36:58 +00:00
|
|
|
return $cluster_merge->fetch();
|
2016-02-14 11:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-16 14:04:48 +00:00
|
|
|
/* MODIFICATION D'UNE MACHINE DONNEE
|
|
|
|
*
|
2016-07-06 09:56:17 +00:00
|
|
|
* @id_warehouse<int> UID de l'entrepot
|
2016-02-16 14:04:48 +00:00
|
|
|
* @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
|
|
|
|
*
|
|
|
|
*/
|
2016-07-09 12:13:08 +00:00
|
|
|
public static function edit($id_warehouse, $id_machine=null, $name=null){
|
2016-07-23 14:36:58 +00:00
|
|
|
/* [1] Modification de la machine
|
2016-02-16 14:04:48 +00:00
|
|
|
=========================================================*/
|
|
|
|
|
2016-07-23 14:36:58 +00:00
|
|
|
$edited = Table::get('machine')
|
|
|
|
->whereId($id_machine)
|
|
|
|
->whereIdWarehouse($id_warehouse)
|
|
|
|
->edit([ 'name' => $name ]);
|
2016-02-16 14:04:48 +00:00
|
|
|
|
|
|
|
// On retourne l'etat de la modification
|
2016-07-23 14:36:58 +00:00
|
|
|
return $edited;
|
2016-02-16 14:04:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-14 11:37:19 +00:00
|
|
|
/* SUPPRIME UNE MACHINE DONNE
|
|
|
|
*
|
2016-07-06 09:56:17 +00:00
|
|
|
* @id_warehouse<int> UID de l'entrepot
|
2016-02-14 11:37:19 +00:00
|
|
|
* @id_machine<int> UID de la machine en question
|
|
|
|
*
|
|
|
|
* @return status<Boolean> Retourne si oui ou non la machine a bien ete supprime
|
|
|
|
*
|
|
|
|
*/
|
2016-07-06 09:56:17 +00:00
|
|
|
public static function delete($id_warehouse, $id_machine){
|
2016-02-14 11:37:19 +00:00
|
|
|
/* [1] On redige/execute la requete
|
|
|
|
=========================================================*/
|
2016-07-23 14:36:58 +00:00
|
|
|
$deleted = Table::get('machine')
|
|
|
|
->whereId($id_machine)
|
2016-07-23 22:03:02 +00:00
|
|
|
->whereIdWarehouse($id_warehouse)
|
|
|
|
->delete();
|
2016-02-14 11:37:19 +00:00
|
|
|
|
|
|
|
|
2016-07-23 14:36:58 +00:00
|
|
|
/* [2] On verifie que la machine est bien supprimée
|
2016-02-14 11:37:19 +00:00
|
|
|
=========================================================*/
|
2016-07-23 22:03:02 +00:00
|
|
|
return $deleted;
|
2016-02-14 11:37:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-07-03 12:57:25 +00:00
|
|
|
|
2016-07-06 09:56:17 +00:00
|
|
|
/* 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
|
|
|
|
=========================================================*/
|
2016-07-23 14:36:58 +00:00
|
|
|
$machine = Table::get('machine')
|
|
|
|
->whereId($id_machine)
|
|
|
|
->whereIdWarehouse($id_warehouse)
|
2016-07-24 23:14:35 +00:00
|
|
|
->select('id_machine')
|
|
|
|
->select('name')
|
2017-02-19 11:14:03 +00:00
|
|
|
->orderby('id_machine', Rows::ORDER_ASC)
|
2016-07-23 14:36:58 +00:00
|
|
|
->unique();
|
2016-07-06 09:56:17 +00:00
|
|
|
|
2016-07-23 14:36:58 +00:00
|
|
|
return $machine->fetch();
|
2016-07-06 09:56:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
=========================================================*/
|
2016-07-23 14:36:58 +00:00
|
|
|
$machine = Table::get('machine')
|
|
|
|
->whereName($name)
|
|
|
|
->whereIdWarehouse($id_warehouse)
|
2016-07-24 23:14:35 +00:00
|
|
|
->select('id_machine')
|
2017-02-19 11:14:03 +00:00
|
|
|
->select('name')
|
|
|
|
->orderby('name', Rows::ORDER_ASC)
|
2016-07-23 14:36:58 +00:00
|
|
|
->unique();
|
2016-07-06 09:56:17 +00:00
|
|
|
|
2016-07-23 14:36:58 +00:00
|
|
|
return $machine->fetch();
|
2016-07-06 09:56:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-07-09 10:44:06 +00:00
|
|
|
/* 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
|
|
|
|
=========================================================*/
|
2016-07-23 14:36:58 +00:00
|
|
|
$machine = Table::get('machine')
|
|
|
|
->whereToken($token)
|
|
|
|
->whereIdWarehouse($id_warehouse)
|
2016-07-24 23:14:35 +00:00
|
|
|
->select('id_machine')
|
|
|
|
->select('name')
|
2017-02-19 11:14:03 +00:00
|
|
|
->orderby('name', Rows::ORDER_ASC)
|
2016-07-23 14:36:58 +00:00
|
|
|
->unique();
|
2016-07-09 10:44:06 +00:00
|
|
|
|
2016-07-23 14:36:58 +00:00
|
|
|
return $machine->fetch();
|
2016-07-09 10:44:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-07-06 09:56:17 +00:00
|
|
|
/* 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
|
|
|
|
=========================================================*/
|
2016-07-23 14:36:58 +00:00
|
|
|
$machine = Table::get('machine')
|
|
|
|
->whereIdWarehouse($id_warehouse)
|
2016-07-24 23:14:35 +00:00
|
|
|
->select('id_machine')
|
2017-02-19 11:14:03 +00:00
|
|
|
->orderby('name', Rows::ORDER_ASC)
|
2016-07-24 23:14:35 +00:00
|
|
|
->select('name');
|
2016-07-06 09:56:17 +00:00
|
|
|
|
2016-07-23 14:36:58 +00:00
|
|
|
return $machine->fetch();
|
2016-07-06 09:56:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-02-19 11:14:03 +00:00
|
|
|
/* DEBLOQUE UNE MACHINE (PREMIER TOKEN) AVEC UN CODE DE DEBLOCAGE
|
|
|
|
*
|
|
|
|
* @id_warehouse<int> UID de l'entrepot
|
|
|
|
* @id_machine<int> UID de la machine
|
|
|
|
* @unlock_code<string> Code de déblocage
|
|
|
|
* @first_token<string> Premier token de hashage cyclique
|
|
|
|
*
|
|
|
|
* @return unlocked<bool> TRUE si débloqué, sinon FALSE
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function unlock($id_warehouse, $id_machine, $unlock_code, $first_token){
|
|
|
|
/* [1] On vérifie le code déblocage
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) On effectue la requête */
|
|
|
|
$machine = Table::get('machine')
|
2017-02-19 14:46:52 +00:00
|
|
|
->select('id_machine')
|
|
|
|
->select('name')
|
2017-02-19 11:14:03 +00:00
|
|
|
->whereId($id_machine)
|
|
|
|
->whereIdWarehouse($id_warehouse)
|
|
|
|
->whereUnlockCode($unlock_code)
|
|
|
|
->fetch();
|
|
|
|
|
|
|
|
/* (2) On vérifie si on a bien le bon code */
|
2017-02-19 14:46:52 +00:00
|
|
|
if( count($machine) < 1 )
|
2017-02-19 11:14:03 +00:00
|
|
|
return false;
|
2017-02-19 11:33:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* [2] Si le code est bon, on le supprime et on met le token
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) Update (edit) machine */
|
|
|
|
$updated = Table::get('machine')
|
|
|
|
->whereId($id_machine)
|
|
|
|
->whereIdWarehouse($id_warehouse)
|
|
|
|
->edit([
|
|
|
|
'unlock_code' => null,
|
|
|
|
'token' => $first_token
|
|
|
|
]);
|
|
|
|
|
|
|
|
/* (2) Manage edition error */
|
|
|
|
return $updated;
|
|
|
|
|
2017-02-19 11:14:03 +00:00
|
|
|
}
|
2016-02-14 11:37:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2016-07-07 15:59:31 +00:00
|
|
|
/* VERIFIE MET A JOUR LE TOKEN DE SYNCHRONISATION
|
|
|
|
*
|
2016-07-17 08:27:59 +00:00
|
|
|
* @id_warehouse<int> UID de l'entrepot
|
2017-02-19 11:14:03 +00:00
|
|
|
* @token<String> Token de synchronisation
|
2016-07-17 08:27:59 +00:00
|
|
|
* @newToken<String> Nouveau token de synchronisation (optionnel, uniquement quand on arrive à la fin du cycle de la hashChain)
|
2016-07-07 15:59:31 +00:00
|
|
|
*
|
|
|
|
* @return status<Boolean> VRAI si le token est correct, sinon FALSE
|
|
|
|
*
|
|
|
|
*/
|
2016-07-17 08:27:59 +00:00
|
|
|
public static function checkToken($id_warehouse, $token, $newToken=null){
|
2016-07-07 15:59:31 +00:00
|
|
|
/* [1] On vérifie le token
|
|
|
|
=========================================================*/
|
2017-02-19 14:46:52 +00:00
|
|
|
$hash = hash('sha512', $token);
|
2016-07-07 15:59:31 +00:00
|
|
|
|
2016-07-09 10:44:06 +00:00
|
|
|
$byToken = self::getByToken($id_warehouse, $hash);
|
2016-07-07 15:59:31 +00:00
|
|
|
|
|
|
|
// Si aucun résultat, erreur
|
2017-02-19 11:14:03 +00:00
|
|
|
if( $byToken == false )
|
2016-07-07 15:59:31 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
/* [2] On met à jour le token
|
|
|
|
=========================================================*/
|
2016-07-23 14:36:58 +00:00
|
|
|
$updated = Table::get('machine')
|
2017-02-19 14:46:52 +00:00
|
|
|
->whereId($byToken['id_machine'])
|
2016-07-23 14:36:58 +00:00
|
|
|
->edit([
|
|
|
|
'token' => Checker::run('hash', $newToken) ? $newToken : $token,
|
2017-02-19 14:46:52 +00:00
|
|
|
'id_machine' => $byToken['id_machine']
|
2016-07-23 14:36:58 +00:00
|
|
|
]);
|
2016-07-07 15:59:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* [3] On retourne que tout s'est bien déroulé
|
|
|
|
=========================================================*/
|
2016-07-23 14:36:58 +00:00
|
|
|
return $updated;
|
2016-07-07 15:59:31 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-14 11:37:19 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-03 12:57:25 +00:00
|
|
|
?>
|