2016-02-14 11:37:19 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace manager\repo;
|
|
|
|
use \manager\Database;
|
|
|
|
use \manager\repo\machine as machineRepo;
|
|
|
|
use \manager\repo\user as userRepo;
|
|
|
|
|
2016-07-04 10:13:35 +00:00
|
|
|
class cluster extends parentRepo{
|
|
|
|
|
|
|
|
protected static function table_name(){ static $table_name = 'cluster'; return $table_name; }
|
2016-02-14 11:37:19 +00:00
|
|
|
|
2016-02-14 14:17:26 +00:00
|
|
|
const USER_CLASS = 0;
|
|
|
|
const MACHINE_CLASS = 1;
|
|
|
|
|
2016-02-14 11:37:19 +00:00
|
|
|
/* CREATION D'UN GROUPE
|
|
|
|
*
|
|
|
|
* @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($name){
|
|
|
|
/* [1] Verification de l'unicite de name
|
|
|
|
=========================================================*/
|
|
|
|
$check_unique = ( self::getByName($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, name)
|
|
|
|
VALUES(DEFAULT, :name)");
|
|
|
|
$insert_user->execute(array(
|
|
|
|
':name' => $name
|
|
|
|
));
|
|
|
|
|
|
|
|
/* [3] On retourne l'id_group ou FALSE si erreur
|
|
|
|
=========================================================*/
|
|
|
|
$check_group = self::getByName($name);
|
|
|
|
|
|
|
|
// Si n'existe pas on retourne FALSE
|
|
|
|
if( $check_group === false )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Sinon, on retourne son id
|
|
|
|
return $check_group['id_cluster'];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-02-14 12:28:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-14 11:37:19 +00:00
|
|
|
/* 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
|
2016-02-14 14:17:26 +00:00
|
|
|
* @class<int> Spefication du type d'entite (0=user;1=machine)
|
2016-02-14 11:37:19 +00:00
|
|
|
*
|
|
|
|
* @return association<int> Renvoie l'UID de l'association cree
|
|
|
|
* Renvoie FALSE si une erreur occure
|
|
|
|
*
|
|
|
|
*/
|
2016-02-14 14:17:26 +00:00
|
|
|
public static function link($id_cluster, $id_entity, $class){
|
2016-02-14 11:37:19 +00:00
|
|
|
/* [1] Verification de l'existence du groupe
|
|
|
|
=========================================================*/
|
|
|
|
$group_check = self::getById($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
|
|
|
|
=========================================================*/
|
2016-02-14 14:17:26 +00:00
|
|
|
$entity_exists = false;
|
|
|
|
/* (1) Utilisateur */
|
2016-07-02 16:35:34 +00:00
|
|
|
if( $class == self::USER_CLASS ){
|
2016-02-14 14:17:26 +00:00
|
|
|
// On verifie si c'est un utilisateur
|
|
|
|
$user_check = userRepo::getById($id_entity);
|
2016-07-03 12:20:42 +00:00
|
|
|
$entity_exists = ( $user_check !== FALSE );
|
2016-02-14 14:17:26 +00:00
|
|
|
|
2016-07-02 16:35:34 +00:00
|
|
|
/* (2) Machine */
|
|
|
|
}elseif( $class == self::MACHINE_CLASS ){
|
2016-02-14 14:17:26 +00:00
|
|
|
// On verifie si c'est une machine
|
|
|
|
$machine_check = machineRepo::getById($id_entity);
|
|
|
|
$entity_exists = ( $machine_check !== FALSE );
|
|
|
|
}
|
2016-02-14 11:37:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Si aucune entite existante, on retourne une erreur
|
2016-02-14 14:17:26 +00:00
|
|
|
if( !$entity_exists )
|
2016-02-14 11:37:19 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
|
2016-02-14 12:28:35 +00:00
|
|
|
/* [3] Verification que l'existente n'existe pas deja
|
|
|
|
=========================================================*/
|
2016-02-14 14:17:26 +00:00
|
|
|
$already_cluster_merge = self::getMerge($id_cluster, $id_entity, $class);
|
2016-02-14 12:28:35 +00:00
|
|
|
|
|
|
|
// Si l'association existe deja, on ne la cree donc pas
|
|
|
|
if( $already_cluster_merge !== false )
|
|
|
|
return $already_cluster_merge['id_cluster_merge'];
|
2016-02-14 11:37:19 +00:00
|
|
|
|
2016-02-14 12:28:35 +00:00
|
|
|
|
|
|
|
/* [4] On cree l'association
|
2016-02-14 11:37:19 +00:00
|
|
|
=========================================================*/
|
2016-02-14 14:17:26 +00:00
|
|
|
$link_entity = Database::getPDO()->prepare("INSERT INTO cluster_merge(id_cluster_merge, id_cluster, id_entity, class)
|
|
|
|
VALUES(DEFAULT, :id_cluster, :id_entity, :class)");
|
2016-02-14 11:37:19 +00:00
|
|
|
$link_entity->execute(array(
|
|
|
|
':id_cluster' => $id_cluster,
|
2016-02-14 14:17:26 +00:00
|
|
|
':id_entity' => $id_entity,
|
|
|
|
':class' => $class
|
2016-02-14 11:37:19 +00:00
|
|
|
));
|
|
|
|
|
2016-02-14 12:28:35 +00:00
|
|
|
/* [5] On retourne l'id_cluster_merge ou FALSE si erreur
|
2016-02-14 11:37:19 +00:00
|
|
|
=========================================================*/
|
2016-02-14 14:17:26 +00:00
|
|
|
$check_cluster_merge = self::getMerge($id_cluster, $id_entity, $class);
|
2016-02-14 11:37:19 +00:00
|
|
|
|
|
|
|
// Si n'existe pas on retourne FALSE
|
2016-07-03 12:20:42 +00:00
|
|
|
if( !is_array($check_cluster_merge) )
|
2016-02-14 11:37:19 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Sinon, on retourne son id
|
|
|
|
return $check_cluster_merge['id_cluster_merge'];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-14 12:28:35 +00:00
|
|
|
/* SUPPRIME 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
|
2016-02-14 14:17:26 +00:00
|
|
|
* @class<int> Spefication du type d'entite (0=user;1=machine)
|
2016-02-14 12:28:35 +00:00
|
|
|
*
|
|
|
|
* @return status<Boolean> Retourne si oui ou non l'association a bien ete supprimee
|
|
|
|
*
|
|
|
|
*/
|
2016-02-14 14:17:26 +00:00
|
|
|
public static function unlink($id_cluster, $id_entity, $class){
|
2016-02-14 12:28:35 +00:00
|
|
|
/* [1] On redige/execute la requete
|
|
|
|
=========================================================*/
|
|
|
|
$delete_cluster_merge = Database::getPDO()->prepare("DELETE FROM cluster_merge
|
|
|
|
WHERE id_cluster = :id_cluster
|
2016-02-14 14:17:26 +00:00
|
|
|
AND id_entity = :id_entity
|
|
|
|
AND class = :class");
|
2016-02-14 12:28:35 +00:00
|
|
|
$delete_cluster_merge->execute(array(
|
|
|
|
':id_cluster' => $id_cluster,
|
2016-02-14 14:17:26 +00:00
|
|
|
':id_entity' => $id_entity,
|
|
|
|
':class' => $class
|
2016-02-14 12:28:35 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
|
|
|
|
/* [2] On verifie que l'association n'existe plus
|
|
|
|
=========================================================*/
|
2016-02-14 14:17:26 +00:00
|
|
|
return ( self::getMerge($id_cluster, $id_entity, $class) === false );
|
2016-02-14 12:28:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-14 11:37:19 +00:00
|
|
|
/* 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
|
2016-02-14 14:17:26 +00:00
|
|
|
* @class<int> Spefication du type d'entite (0=user;1=machine)
|
2016-02-14 11:37:19 +00:00
|
|
|
*
|
|
|
|
* @return association<int> Renvoie l'UID de l'association cree
|
|
|
|
* Renvoie FALSE si une erreur occure
|
|
|
|
*
|
|
|
|
*/
|
2016-02-14 14:17:26 +00:00
|
|
|
public static function getMerge($id_cluster, $id_entity, $class){
|
2016-02-14 11:37:19 +00:00
|
|
|
// On ecrit la requete
|
2016-07-02 16:35:34 +00:00
|
|
|
$request = Database::getPDO()->prepare("SELECT * FROM cluster_merge
|
2016-02-14 11:37:19 +00:00
|
|
|
WHERE id_cluster = :id_cluster
|
2016-02-14 14:17:26 +00:00
|
|
|
AND id_entity = :id_entity
|
|
|
|
AND class = :class");
|
2016-02-14 11:37:19 +00:00
|
|
|
|
|
|
|
// On execute la requete
|
|
|
|
$request->execute(array(
|
|
|
|
':id_cluster' => $id_cluster,
|
2016-02-14 14:17:26 +00:00
|
|
|
':id_entity' => $id_entity,
|
|
|
|
':class' => $class
|
2016-02-14 11:37:19 +00:00
|
|
|
));
|
|
|
|
|
|
|
|
// On recupere 1 seule relation
|
2016-02-14 14:17:26 +00:00
|
|
|
$answer = $request->fetch();
|
2016-02-14 11:37:19 +00:00
|
|
|
|
|
|
|
// Gestion d'erreur -> aucun resultat
|
|
|
|
if( $answer === false )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// On retourne en supprimant les indices numeriques
|
|
|
|
return Database::delNumeric( $answer, true );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-14 14:53:59 +00:00
|
|
|
/* 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(array(
|
|
|
|
':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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-14 11:37:19 +00:00
|
|
|
/* SUPPRIME UN GROUPE DONNE
|
|
|
|
*
|
|
|
|
* @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_cluster){
|
|
|
|
/* [1] On redige/execute la requete
|
|
|
|
=========================================================*/
|
|
|
|
$delete_group = Database::getPDO()->prepare("DELETE FROM cluster WHERE id_cluster = :id_cluster");
|
|
|
|
$delete_group->execute(array(
|
|
|
|
':id_cluster' => $id_cluster
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
|
|
/* [2] On verifie que le groupe n'existe plus
|
|
|
|
=========================================================*/
|
|
|
|
return ( self::getById($id_group) === false );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-07-02 16:35:34 +00:00
|
|
|
|
2016-02-14 11:37:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-02 16:35:34 +00:00
|
|
|
?>
|