320 lines
8.9 KiB
PHP
Executable File
320 lines
8.9 KiB
PHP
Executable File
<?php
|
|
|
|
namespace manager\repo;
|
|
use \manager\Database;
|
|
use \manager\repo\machine as machineRepo;
|
|
use \manager\repo\user as userRepo;
|
|
|
|
class cluster extends parentRepo{
|
|
|
|
protected static function table_name(){ static $table_name = 'cluster'; return $table_name; }
|
|
|
|
const USER_CLASS = 0;
|
|
const MACHINE_CLASS = 1;
|
|
|
|
/* CREATION D'UN GROUPE
|
|
*
|
|
* @id_warehouse<int> UID de l'entrepot
|
|
* @name<String> Nom du groupe
|
|
*
|
|
* @return id_user<int> Renvoie l'UID du groupe cree
|
|
* Renvoie FALSE si une erreur occure
|
|
*
|
|
*/
|
|
public static function create($id_warehouse, $name, $class){
|
|
/* [1] Verification de l'unicite de name
|
|
=========================================================*/
|
|
$check_unique = ( self::getByName($id_warehouse, $name) === false );
|
|
|
|
// Si un groupe a deja ce nom, on renvoie une erreur
|
|
if( !$check_unique )
|
|
return false;
|
|
|
|
|
|
/* [2] Creation du groupe
|
|
=========================================================*/
|
|
$insert_user = Database::getPDO()->prepare("INSERT INTO cluster(id_cluster, id_warehouse, name, class)
|
|
VALUES(DEFAULT, :id_warehouse, :name, :class)");
|
|
$insert_user->execute([
|
|
':id_warehouse' => $id_warehouse,
|
|
':name' => $name,
|
|
':class' => $class
|
|
]);
|
|
|
|
/* [3] On retourne l'id_group ou FALSE si erreur
|
|
=========================================================*/
|
|
$check_group = self::getByName($id_warehouse, $name);
|
|
|
|
// Si n'existe pas on retourne FALSE
|
|
if( $check_group === false )
|
|
return false;
|
|
|
|
// Sinon, on retourne son id
|
|
return $check_group['id_cluster'];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* ASSOCIATION D'UN(E) UTILISATEUR/MACHINE A UN GROUPE
|
|
*
|
|
* @id_warehouse<int> UID de l'entrepot
|
|
* @id_cluster<int> UID du groupe
|
|
* @id_entity<int> UID de la machine ou de l'utilisateur
|
|
* @class<int> Spefication du type d'entite (0=user;1=machine)
|
|
*
|
|
* @return association<int> Renvoie l'UID de l'association cree
|
|
* Renvoie FALSE si une erreur occure
|
|
*
|
|
*/
|
|
public static function link($id_warehouse, $id_cluster, $id_entity, $class){
|
|
/* [1] Verification de l'existence du groupe
|
|
=========================================================*/
|
|
$group_check = self::getById($id_warehouse, $id_cluster);
|
|
|
|
// Si le groupe n'existe pas, on retourne ue erreur
|
|
if( $group_check === false )
|
|
return false;
|
|
|
|
|
|
|
|
/* [2] Verification de l'existente de l'entite
|
|
=========================================================*/
|
|
$entity_exists = false;
|
|
/* (1) Utilisateur */
|
|
if( $class == self::USER_CLASS ){
|
|
// On verifie si c'est un utilisateur
|
|
$user_check = userRepo::getById($id_warehouse, $id_entity);
|
|
$entity_exists = ( $user_check !== FALSE );
|
|
|
|
/* (2) Machine */
|
|
}elseif( $class == self::MACHINE_CLASS ){
|
|
// On verifie si c'est une machine
|
|
$machine_check = machineRepo::getById($id_warehouse, $id_entity);
|
|
$entity_exists = ( $machine_check !== FALSE );
|
|
}
|
|
|
|
|
|
// Si aucune entite existante, on retourne une erreur
|
|
if( !$entity_exists )
|
|
return false;
|
|
|
|
|
|
/* [3] Verification que l'existente n'existe pas deja
|
|
=========================================================*/
|
|
$already_cluster_merge = self::getMerge($id_cluster, $id_entity, $class);
|
|
|
|
// Si l'association existe deja, on ne la cree donc pas
|
|
if( $already_cluster_merge !== false )
|
|
return $already_cluster_merge['id_cluster_merge'];
|
|
|
|
|
|
/* [4] On cree l'association
|
|
=========================================================*/
|
|
$link_entity = Database::getPDO()->prepare("INSERT INTO cluster_merge(id_cluster_merge, id_cluster, id_entity)
|
|
VALUES(DEFAULT, :id_cluster, :id_entity)");
|
|
$link_entity->execute([
|
|
':id_cluster' => $id_cluster,
|
|
':id_entity' => $id_entity
|
|
]);
|
|
|
|
/* [5] On retourne l'id_cluster_merge ou FALSE si erreur
|
|
=========================================================*/
|
|
$check_cluster_merge = self::getMerge($id_cluster, $id_entity, $class);
|
|
|
|
// Si n'existe pas on retourne FALSE
|
|
if( !is_array($check_cluster_merge) )
|
|
return false;
|
|
|
|
// Sinon, on retourne son id
|
|
return $check_cluster_merge['id_cluster_merge'];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* SUPPRIME UNE ASSOCIATION D'UN(E) UTILISATEUR/MACHINE A UN GROUPE
|
|
*
|
|
* @id_warehouse<int> UID de l'entrepot
|
|
* @id_cluster<int> UID du groupe
|
|
* @id_entity<int> UID de la machine ou de l'utilisateur
|
|
* @class<int> Spefication du type d'entite (0=user;1=machine)
|
|
*
|
|
* @return status<Boolean> Retourne si oui ou non l'association a bien ete supprimee
|
|
*
|
|
*/
|
|
public static function unlink($id_warehouse, $id_cluster, $id_entity, $class){
|
|
/* [1] On redige/execute la requete
|
|
=========================================================*/
|
|
$delete_cluster_merge = Database::getPDO()->prepare("DELETE FROM cluster_merge as cm, cluster as c
|
|
WHERE cm.id_cluster = c.id_cluster
|
|
AND c.id_warehouse = :id_warehouse
|
|
AND cm.id_cluster = :id_cluster
|
|
AND cm.id_entity = :id_entity
|
|
AND c.class = :class");
|
|
$delete_cluster_merge->execute([
|
|
':id_warehouse' => $id_warehouse,
|
|
':id_cluster' => $id_cluster,
|
|
':id_entity' => $id_entity,
|
|
':class' => $class
|
|
]);
|
|
|
|
|
|
/* [2] On verifie que l'association n'existe plus
|
|
=========================================================*/
|
|
return ( self::getMerge($id_cluster, $id_entity, $class) === false );
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* RENVOIE L'ID D'UNE ASSOCIATION D'UN(E) UTILISATEUR/MACHINE A UN GROUPE
|
|
*
|
|
* @id_cluster<int> UID du groupe
|
|
* @id_entity<int> UID de la machine ou de l'utilisateur
|
|
* @class<int> Spefication du type d'entite (0=user;1=machine)
|
|
*
|
|
* @return association<int> Renvoie l'UID de l'association cree
|
|
* Renvoie FALSE si une erreur occure
|
|
*
|
|
*/
|
|
public static function getMerge($id_cluster, $id_entity, $class){
|
|
// On ecrit la requete
|
|
$request = Database::getPDO()->prepare("SELECT cm.*
|
|
FROM cluster_merge as cm, cluster as c
|
|
WHERE cm.id_cluster = c.id_cluster
|
|
AND cm.id_cluster = :id_cluster
|
|
AND cm.id_entity = :id_entity
|
|
AND c.class = :class");
|
|
|
|
// On execute la requete
|
|
$request->execute([
|
|
':id_cluster' => $id_cluster,
|
|
':id_entity' => $id_entity,
|
|
':class' => $class
|
|
]);
|
|
|
|
// On recupere 1 seule relation
|
|
$answer = $request->fetch();
|
|
|
|
// Gestion d'erreur -> aucun resultat
|
|
if( $answer === false )
|
|
return false;
|
|
|
|
// On retourne en supprimant les indices numeriques
|
|
return Database::delNumeric( $answer, true );
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* RENVOIE LES MEMBRES D'UN GROUPE DONNE
|
|
*
|
|
* @id_cluster<int> UID du groupe en question
|
|
*
|
|
* @return members<Array> Retourne la liste des membres du groupe
|
|
*
|
|
*/
|
|
public static function getMembers($id_cluster){
|
|
/* [1] On recupere la liste des associations
|
|
=========================================================*/
|
|
$get_members = Database::getPDO()->prepare("SELECT * FROM cluster_merge
|
|
WHERE id_cluster = :id_cluster
|
|
ORDER BY id_cluster_merge");
|
|
$get_members->execute([ ':id_cluster' => $id_cluster ]);
|
|
|
|
$members = Database::delNumeric( $get_members->fetchAll() );
|
|
|
|
/* [2] On formatte recupere les entites
|
|
=========================================================*/
|
|
foreach($members as $i=>$member){
|
|
|
|
/* (1) On recupere les informations utilisateur */
|
|
if( $member['class'] == self::USER_CLASS ){
|
|
$get_user = userRepo::getById( $member['id_entity'] );
|
|
|
|
// Si on a bien recu les informations, on les ajoute
|
|
if( $get_user !== false )
|
|
$members[$i] = array_merge($member, $get_user );
|
|
|
|
|
|
/* (2) On recupere les informations machine */
|
|
}elseif( $member['class'] == self::MACHINE_CLASS ){
|
|
$get_machine = machineRepo::getById( $member['id_entity'] );
|
|
|
|
// Si on a bien recu les informations, on les ajoute
|
|
if( $get_machine !== false )
|
|
$members[$i] = array_merge($member, $get_machine );
|
|
}
|
|
|
|
}
|
|
|
|
/* [2] On verifie que le groupe n'existe plus
|
|
=========================================================*/
|
|
return $members;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* SUPPRIME UN GROUPE DONNE
|
|
*
|
|
* @id_warehouse<int> UID de l'entrepot
|
|
* @id_cluster<int> UID du groupe en question
|
|
*
|
|
* @return status<Boolean> Retourne si oui ou non le groupe a bien ete supprime
|
|
*
|
|
*/
|
|
public static function delete($id_warehouse, $id_cluster){
|
|
/* [1] On redige/execute la requete
|
|
=========================================================*/
|
|
$delete_group = Database::getPDO()->prepare("DELETE FROM cluster
|
|
WHERE id_warehouse = :id_warehouse
|
|
AND id_cluster = :id_cluster");
|
|
$delete_group->execute([
|
|
':id_warehouse' => $id_warehouse,
|
|
':id_cluster' => $id_cluster
|
|
]);
|
|
|
|
|
|
/* [2] On verifie que le groupe n'existe plus
|
|
=========================================================*/
|
|
return ( self::getById($id_group) === false );
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
?>
|