460 lines
12 KiB
PHP
Executable File
460 lines
12 KiB
PHP
Executable File
<?php
|
||
|
||
namespace manager\repo;
|
||
use \manager\Database;
|
||
use \manager\ManagerError;
|
||
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
|
||
* @class<int> Type d'entités du groupe
|
||
* @owner<int> Si défini -> UID de l'entité qui a ce groupe comme groupe PERSONNEL
|
||
*
|
||
* @return id_user<int> Renvoie l'UID du groupe cree
|
||
* Renvoie FALSE si une erreur occure
|
||
*
|
||
*/
|
||
public static function create($id_warehouse, $name, $class, $owner=null){
|
||
/* [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, personal)
|
||
VALUES(DEFAULT, :id_warehouse, :name, :class, :personal)");
|
||
$insert_user->execute([
|
||
':id_warehouse' => $id_warehouse,
|
||
':name' => $name,
|
||
':class' => $class,
|
||
':personal' => $owner
|
||
]);
|
||
|
||
/* [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 error<int> Renvoie le code 'ManagerError'
|
||
*
|
||
*/
|
||
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 ManagerError::NoMatchFound;
|
||
|
||
|
||
|
||
/* [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 ManagerError::NoMatchFound;
|
||
|
||
|
||
/* [3] Verification que l'existente n'existe pas deja
|
||
=========================================================*/
|
||
$already_cluster_merge = self::getMerge($id_warehouse, $id_cluster, $id_entity, $class);
|
||
|
||
// Si l'association existe deja, on ne la cree donc pas
|
||
if( $already_cluster_merge !== false )
|
||
return ManagerError::Success;
|
||
|
||
|
||
/* [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_warehouse, $id_cluster, $id_entity, $class);
|
||
|
||
// Si n'existe pas on retourne FALSE
|
||
if( !is_array($check_cluster_merge) )
|
||
return ManagerError::RepoError;
|
||
|
||
// Sinon, on retourne son id
|
||
return ManagerError::Success;
|
||
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
/* 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 error<int> Renvoie le code 'ManagerError'
|
||
*
|
||
*/
|
||
public static function unlink($id_warehouse, $id_cluster, $id_entity, $class){
|
||
/* [1] On récupère l'UID du lien
|
||
=========================================================*/
|
||
/* (1) On récupère le lien */
|
||
$get_merge_id = self::getMerge($id_warehouse, $id_cluster, $id_entity, $class);
|
||
|
||
/* (2) Si on ne trouve rien */
|
||
if( $get_merge_id == false )
|
||
return ManagerError::NoMatchFound;
|
||
|
||
|
||
|
||
/* [2] On supprime le lien
|
||
=========================================================*/
|
||
$delete_cluster_merge = Database::getPDO()->prepare("DELETE FROM cluster_merge
|
||
WHERE id_cluster_merge = :uid");
|
||
$delete_cluster_merge->execute([
|
||
':uid' => $get_merge_id['id_cluster_merge']
|
||
]);
|
||
|
||
|
||
/* [2] On verifie que l'association n'existe plus
|
||
=========================================================*/
|
||
if( self::getMerge($id_warehouse, $id_cluster, $id_entity, $class) !== false )
|
||
return ManagerError::ModuleError;
|
||
|
||
|
||
return ManagerError::Success;
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
/* RENVOIE L'ID D'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 association<int> Renvoie l'UID de l'association cree
|
||
* Renvoie FALSE si une erreur occure
|
||
*
|
||
*/
|
||
public static function getMerge($id_warehouse, $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 c.id_warehouse = :id_warehouse
|
||
AND cm.id_cluster = :id_cluster
|
||
AND cm.id_entity = :id_entity
|
||
AND c.class = :class");
|
||
|
||
// On execute la requete
|
||
$request->execute([
|
||
':id_warehouse' => $id_warehouse,
|
||
':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 LA LISTE DES GROUPES D'UN ENTREPOT
|
||
*
|
||
* @id_warehouse<int> UID de l'entrepot
|
||
*
|
||
* @return clusters<Array> Renvoie la liste des groupes
|
||
* Renvoie FALSE si une erreur occure
|
||
*
|
||
*/
|
||
public static function getAll($id_warehouse){
|
||
// On ecrit la requete
|
||
$request = Database::getPDO()->prepare("SELECT c.id_cluster, c.name, c.class
|
||
FROM cluster as c
|
||
WHERE c.id_warehouse = :id_warehouse
|
||
AND c.personal is null
|
||
ORDER BY c.name ASC");
|
||
|
||
// On execute la requete
|
||
$request->execute([
|
||
':id_warehouse' => $id_warehouse
|
||
]);
|
||
|
||
// On retourne en supprimant les indices numeriques
|
||
return Database::delNumeric( $request->fetchAll() );
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
/* RENVOIE LA LISTE D'UN GROUPE SPECIFIQUE
|
||
*
|
||
* @id_warehouse<int> UID de l'entrepot
|
||
* @id<int> UID du groupe
|
||
*
|
||
* @return cluster<Array> Renvoie le groupe correspondant
|
||
* Renvoie FALSE si une erreur occure
|
||
*
|
||
*/
|
||
public static function getById($id_warehouse, $id){
|
||
// On ecrit la requete
|
||
$request = Database::getPDO()->prepare("SELECT c.*
|
||
FROM cluster as c
|
||
WHERE c.id_warehouse = :id_warehouse
|
||
AND c.id_cluster = :id");
|
||
|
||
// On execute la requete
|
||
$request->execute([
|
||
':id_warehouse' => $id_warehouse,
|
||
':id' => $id
|
||
]);
|
||
|
||
$fetch = $request->fetch();
|
||
|
||
// Si aucun résultat
|
||
if( $fetch == false )
|
||
return false;
|
||
|
||
// On retourne en supprimant les indices numeriques
|
||
return Database::delNumeric( $fetch, true );
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
/* RENVOIE LA LISTE D'UN GROUPE SPECIFIQUE
|
||
*
|
||
* @id_warehouse<int> UID de l'entrepot
|
||
* @name<String> Nom du groupe
|
||
*
|
||
* @return cluster<Array> Renvoie le groupe correspondant
|
||
* Renvoie FALSE si une erreur occure
|
||
*
|
||
*/
|
||
public static function getByName($id_warehouse, $name){
|
||
// On ecrit la requete
|
||
$request = Database::getPDO()->prepare("SELECT c.*
|
||
FROM cluster as c
|
||
WHERE c.id_warehouse = :id_warehouse
|
||
AND c.name = :name");
|
||
|
||
// On execute la requete
|
||
$request->execute([
|
||
':id_warehouse' => $id_warehouse,
|
||
':name' => $name
|
||
]);
|
||
|
||
$fetch = $request->fetch();
|
||
|
||
// Si aucun résultat
|
||
if( $fetch == false )
|
||
return false;
|
||
|
||
// On retourne en supprimant les indices numeriques
|
||
return Database::delNumeric( $fetch, true );
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
/* RENVOIE LES MEMBRES D'UN GROUPE DONNE
|
||
*
|
||
* @id_warehouse<int> UID de l'entrepot
|
||
* @id_cluster<int> UID du groupe en question
|
||
*
|
||
* @return members<Array> Retourne la liste des membres du groupe
|
||
*
|
||
*/
|
||
public static function getMembers($id_warehouse, $id_cluster){
|
||
/* [1] On recupere la liste des associations
|
||
=========================================================*/
|
||
$get_members = Database::getPDO()->prepare("SELECT id_entity as id_member, c.class
|
||
FROM cluster_merge as cm, cluster as c
|
||
WHERE cm.id_cluster = c.id_cluster
|
||
AND c.id_cluster = :id_cluster
|
||
AND c.id_warehouse = :id_warehouse
|
||
AND c.personal is null
|
||
ORDER BY id_cluster_merge");
|
||
$get_members->execute([
|
||
':id_warehouse' => $id_warehouse,
|
||
':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( $id_warehouse, $member['id_member'] );
|
||
|
||
// Si on a bien recu les informations, on les ajoute
|
||
if( $get_user !== false ){
|
||
$members[$i] = [
|
||
'id_member' => $get_user['id_user'],
|
||
'name' => $get_user['username'],
|
||
'class' => self::USER_CLASS
|
||
];
|
||
}
|
||
|
||
|
||
/* (2) On recupere les informations machine */
|
||
}elseif( $member['class'] == self::MACHINE_CLASS ){
|
||
$get_machine = machineRepo::getById( $id_warehouse, $member['id_member'] );
|
||
|
||
// Si on a bien recu les informations, on les ajoute
|
||
if( $get_machine !== false ){
|
||
$members[$i] = [
|
||
'id_member' => $get_machine['id_machine'],
|
||
'name' => $get_machine['name'],
|
||
'class' => self::MACHINE_CLASS
|
||
];
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
/* [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 );
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
}
|
||
|
||
|
||
?>
|