416 lines
11 KiB
PHP
416 lines
11 KiB
PHP
<?php
|
||
|
||
namespace manager\repo;
|
||
use \manager\Database;
|
||
use \manager\ManagerError;
|
||
use \manager\Repo;
|
||
|
||
class user_cluster extends parentRepo{
|
||
|
||
protected static function table_name(){ static $table_name = 'user_cluster'; return $table_name; }
|
||
|
||
/* 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){
|
||
/* [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 user_cluster(id_user_cluster, id_warehouse, name)
|
||
VALUES(DEFAULT, :id_warehouse, :name)");
|
||
$insert_user->execute([
|
||
':id_warehouse' => $id_warehouse,
|
||
':name' => $name
|
||
]);
|
||
|
||
/* [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_user_cluster'];
|
||
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
/* ASSOCIATION D'UN UTILISATEUR A UN GROUPE
|
||
*
|
||
* @id_warehouse<int> UID de l'entrepot
|
||
* @id_user_cluster<int> UID du groupe
|
||
* @id_user<int> UID de l'utilisateur
|
||
*
|
||
* @return error<int> Renvoie le code 'ManagerError'
|
||
*
|
||
*/
|
||
public static function link($id_warehouse, $id_user_cluster, $id_user){
|
||
/* [1] Verification de l'existence du groupe
|
||
=========================================================*/
|
||
$group_check = self::getById($id_warehouse, $id_user_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'utilisateur
|
||
=========================================================*/
|
||
$user_checkReq = new Repo('user/getById', [$id_warehouse, $id_user]);
|
||
$user_check = $user_checkReq->answer();
|
||
|
||
|
||
|
||
// Si aucune user existante, on retourne une erreur
|
||
if( $user_check === false )
|
||
return ManagerError::NoMatchFound;
|
||
|
||
|
||
/* [3] Verification que l'existente n'existe pas deja
|
||
=========================================================*/
|
||
$already_user_cluster_merge = self::getMerge($id_warehouse, $id_user_cluster, $id_user);
|
||
|
||
// Si l'association existe deja, on ne la cree donc pas
|
||
if( $already_user_cluster_merge !== false )
|
||
return ManagerError::Success;
|
||
|
||
|
||
/* [4] On cree l'association
|
||
=========================================================*/
|
||
$link_entity = Database::getPDO()->prepare("INSERT INTO user_cluster_merge(id_user_cluster_merge, id_user_cluster, id_user)
|
||
VALUES(DEFAULT, :id_user_cluster, :id_user)");
|
||
$link_entity->execute([
|
||
':id_user_cluster' => $id_user_cluster,
|
||
':id_user' => $id_user
|
||
]);
|
||
|
||
/* [5] On retourne l'id_user_cluster_merge ou FALSE si erreur
|
||
=========================================================*/
|
||
$check_user_cluster_merge = self::getMerge($id_warehouse, $id_user_cluster, $id_user);
|
||
|
||
// Si n'existe pas on retourne FALSE
|
||
if( !is_array($check_user_cluster_merge) )
|
||
return ManagerError::RepoError;
|
||
|
||
// Sinon, on retourne son id
|
||
return ManagerError::Success;
|
||
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
/* SUPPRIME UNE ASSOCIATION D'UN UTILISATEUR A UN GROUPE
|
||
*
|
||
* @id_warehouse<int> UID de l'entrepot
|
||
* @id_user_cluster<int> UID du groupe
|
||
* @id_user<int> UID de l'utilisateur
|
||
*
|
||
* @return error<int> Renvoie le code 'ManagerError'
|
||
*
|
||
*/
|
||
public static function unlink($id_warehouse, $id_user_cluster, $id_user){
|
||
/* [1] On récupère l'UID du lien
|
||
=========================================================*/
|
||
/* (1) On récupère le lien */
|
||
$get_merge_id = self::getMerge($id_warehouse, $id_user_cluster, $id_user);
|
||
|
||
/* (2) Si on ne trouve rien */
|
||
if( $get_merge_id == false )
|
||
return ManagerError::NoMatchFound;
|
||
|
||
|
||
|
||
/* [2] On supprime le lien
|
||
=========================================================*/
|
||
$delete_user_cluster_merge = Database::getPDO()->prepare("DELETE FROM user_cluster_merge
|
||
WHERE id_user_cluster_merge = :uid");
|
||
$delete_user_cluster_merge->execute([
|
||
':uid' => $get_merge_id['id_user_cluster_merge']
|
||
]);
|
||
|
||
|
||
/* [2] On verifie que l'association n'existe plus
|
||
=========================================================*/
|
||
if( self::getMerge($id_warehouse, $id_user_cluster, $id_user) !== false )
|
||
return ManagerError::ModuleError;
|
||
|
||
|
||
return ManagerError::Success;
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
/* RENVOIE L'ID D'UNE ASSOCIATION D'UN UTILISATEUR A UN GROUPE
|
||
*
|
||
* @id_warehouse<int> UID de l'entrepot
|
||
* @id_user_cluster<int> UID du groupe
|
||
* @id_user<int> UID de l'utilisateur
|
||
*
|
||
* @return association<int> Renvoie l'UID de l'association cree
|
||
* Renvoie FALSE si une erreur occure
|
||
*
|
||
*/
|
||
public static function getMerge($id_warehouse, $id_user_cluster, $id_user){
|
||
// On ecrit la requete
|
||
$request = Database::getPDO()->prepare("SELECT cm.*
|
||
FROM user_cluster_merge as cm, user_cluster as c
|
||
WHERE cm.id_user_cluster = c.id_user_cluster
|
||
AND c.id_warehouse = :id_warehouse
|
||
AND cm.id_user_cluster = :id_user_cluster
|
||
AND cm.id_user = :id_user");
|
||
|
||
// On execute la requete
|
||
$request->execute([
|
||
':id_warehouse' => $id_warehouse,
|
||
':id_user_cluster' => $id_user_cluster,
|
||
':id_user' => $id_user
|
||
]);
|
||
|
||
// 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 user_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_user_cluster, c.name
|
||
FROM user_cluster as c
|
||
WHERE c.id_warehouse = :id_warehouse
|
||
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 UN GROUPE SPECIFIQUE
|
||
*
|
||
* @id_warehouse<int> UID de l'entrepot
|
||
* @id<int> UID du groupe
|
||
*
|
||
* @return user_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 user_cluster as c
|
||
WHERE c.id_warehouse = :id_warehouse
|
||
AND c.id_user_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 UN GROUPE SPECIFIQUE
|
||
*
|
||
* @id_warehouse<int> UID de l'entrepot
|
||
* @name<String> Nom du groupe
|
||
*
|
||
* @return user_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 user_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 UTILISATEURS D'UN GROUPE DONNE
|
||
*
|
||
* @id_warehouse<int> UID de l'entrepot
|
||
* @id_user_cluster<int> UID du groupe en question
|
||
*
|
||
* @return members<Array> Retourne la liste des users du groupe
|
||
*
|
||
*/
|
||
public static function getMembers($id_warehouse, $id_user_cluster){
|
||
/* [1] On recupere la liste des associations
|
||
=========================================================*/
|
||
$get_users = Database::getPDO()->prepare("SELECT m.id_user, m.name
|
||
FROM user_cluster_merge as cm, user_cluster as c, user as m
|
||
WHERE cm.id_user_cluster = c.id_user_cluster
|
||
AND c.id_user_cluster = :id_user_cluster
|
||
AND c.id_warehouse = :id_warehouse
|
||
AND cm.id_user = m.id_user
|
||
ORDER BY m.name");
|
||
$get_users->execute([
|
||
':id_warehouse' => $id_warehouse,
|
||
':id_user_cluster' => $id_user_cluster
|
||
]);
|
||
|
||
|
||
/* [2] On verifie que le groupe n'existe plus
|
||
=========================================================*/
|
||
return Database::delNumeric( $get_users->fetchAll() );;
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
/* RENVOIE UNE LISTE DE GROUPES EN FONCTION D'UN MOT CLE
|
||
*
|
||
* @id_warehouse<int> UID de l'entrepot
|
||
* @keyword<String> Element de recherche
|
||
*
|
||
* @return user_clusters<Array> Retourne les groupes trouves
|
||
*
|
||
*/
|
||
public static function search($id_warehouse, $keyword){
|
||
// On recupere les donnees
|
||
$searchuser_clusters = Database::getPDO()->prepare("SELECT * FROM user_cluster
|
||
WHERE id_warehouse = :id_warehouse
|
||
AND name LIKE '%".$keyword."%'");
|
||
|
||
$searchuser_clusters->execute([
|
||
':id_warehouse' => $id_warehouse
|
||
]);
|
||
|
||
return Database::delNumeric( $searchuser_clusters->fetchAll() );
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
/* SUPPRIME UN GROUPE DONNE
|
||
*
|
||
* @id_warehouse<int> UID de l'entrepot
|
||
* @id_user_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_user_cluster){
|
||
/* [1] On redige/execute la requete
|
||
=========================================================*/
|
||
$delete_group = Database::getPDO()->prepare("DELETE FROM user_cluster
|
||
WHERE id_warehouse = :id_warehouse
|
||
AND id_user_cluster = :id_user_cluster");
|
||
$delete_group->execute([
|
||
':id_warehouse' => $id_warehouse,
|
||
':id_user_cluster' => $id_user_cluster
|
||
]);
|
||
|
||
|
||
/* [2] On verifie que le groupe n'existe plus
|
||
=========================================================*/
|
||
return ( self::getById($id_warehouse, $id_user_cluster) === false );
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
}
|
||
|
||
|
||
?>
|