2016-07-18 10:35:30 +00:00
|
|
|
|
<?php
|
|
|
|
|
|
2016-10-18 14:03:03 +00:00
|
|
|
|
namespace database\repo;
|
|
|
|
|
use \database\core\Database;
|
2016-10-18 17:09:47 +00:00
|
|
|
|
use \error\core\Error;
|
2016-10-18 14:03:03 +00:00
|
|
|
|
use \database\core\Repo;
|
|
|
|
|
use \orm\core\Table;
|
|
|
|
|
use \orm\core\Rows;
|
2016-07-18 10:35:30 +00:00
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
=========================================================*/
|
2016-07-23 15:34:24 +00:00
|
|
|
|
$inserted = Table::get('user_cluster')
|
|
|
|
|
->insert([
|
2016-07-24 17:18:55 +00:00
|
|
|
|
'id_user_cluster' => Rows::INSERT_DEFAULT,
|
2016-07-23 15:34:24 +00:00
|
|
|
|
'id_warehouse' => $id_warehouse,
|
|
|
|
|
'name' => $name
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// Si erreur (car name doit être unique)
|
|
|
|
|
if( !$inserted )
|
2016-07-18 10:35:30 +00:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
2016-07-23 15:34:24 +00:00
|
|
|
|
/* [2] On retourne l'id_group ou FALSE si erreur
|
2016-07-18 10:35:30 +00:00
|
|
|
|
=========================================================*/
|
|
|
|
|
$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
|
|
|
|
|
*
|
2016-10-18 17:09:47 +00:00
|
|
|
|
* @return error<int> Renvoie le code 'Error'
|
2016-07-18 10:35:30 +00:00
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
public static function link($id_warehouse, $id_user_cluster, $id_user){
|
2016-07-23 15:34:24 +00:00
|
|
|
|
/* [1] On vérifie que tout existe
|
2016-07-18 10:35:30 +00:00
|
|
|
|
=========================================================*/
|
2016-07-23 15:34:24 +00:00
|
|
|
|
/* (1) On récupère le groupe */
|
|
|
|
|
$clusterR = Table::get('user_cluster')
|
|
|
|
|
->whereIdWarehouse($id_warehouse)
|
|
|
|
|
->whereId($id_user_cluster)
|
|
|
|
|
->unique()
|
|
|
|
|
->select('id_user_cluster');
|
|
|
|
|
$cluster = $clusterR->fetch();
|
|
|
|
|
|
|
|
|
|
// Si le groupe n'existe pas
|
|
|
|
|
if( $cluster === false )
|
2016-10-18 17:09:47 +00:00
|
|
|
|
return Error::NoMatchFound;
|
2016-07-18 10:35:30 +00:00
|
|
|
|
|
2016-07-23 15:34:24 +00:00
|
|
|
|
/* (2) On récupère l'utilisateur */
|
|
|
|
|
$userR = Table::get('user')
|
|
|
|
|
->whereIdWarehouse($id_warehouse)
|
|
|
|
|
->whereId($id_user)
|
|
|
|
|
->unique()
|
|
|
|
|
->select('id_user');
|
|
|
|
|
$user = $userR->fetch();
|
2016-07-18 10:35:30 +00:00
|
|
|
|
|
2016-07-23 15:34:24 +00:00
|
|
|
|
// Si l'utilisateur n'existe pas
|
|
|
|
|
if( $user === false )
|
2016-10-18 17:09:47 +00:00
|
|
|
|
return Error::NoMatchFound;
|
2016-07-18 10:35:30 +00:00
|
|
|
|
|
2016-07-23 15:34:24 +00:00
|
|
|
|
/* (3) On vérifie qu'il n'y a pas déja d'appartenance */
|
|
|
|
|
$cluster_mergeR = Table::get('user_cluster_merge')
|
|
|
|
|
->select('id_user_cluster_merge')
|
|
|
|
|
->join('id_user_cluster', $clusterR)
|
|
|
|
|
->join('id_user', $userR)
|
|
|
|
|
->unique();
|
|
|
|
|
$cluster_merge_exists = $cluster_mergeR->fetch();
|
2016-07-18 10:35:30 +00:00
|
|
|
|
|
2016-07-23 15:34:24 +00:00
|
|
|
|
// Si la relation existe déja, on ne fait rien
|
|
|
|
|
if( $cluster_merge_exists !== false )
|
2016-10-18 17:09:47 +00:00
|
|
|
|
return Error::Success;
|
2016-07-18 10:35:30 +00:00
|
|
|
|
|
|
|
|
|
|
2016-07-23 15:34:24 +00:00
|
|
|
|
|
|
|
|
|
/* [2] On crée la relation
|
2016-07-18 10:35:30 +00:00
|
|
|
|
=========================================================*/
|
2016-07-23 15:34:24 +00:00
|
|
|
|
/* (1) On insère la relation */
|
|
|
|
|
$inserted = Table::get('user_cluster_merge')
|
|
|
|
|
->insert([
|
2016-07-24 17:18:55 +00:00
|
|
|
|
'id_user_cluster_merge' => Rows::INSERT_DEFAULT,
|
2016-07-23 15:34:24 +00:00
|
|
|
|
'id_user_cluster' => $id_user_cluster,
|
|
|
|
|
'id_user' => $id_user
|
|
|
|
|
]);
|
2016-07-18 10:35:30 +00:00
|
|
|
|
|
2016-07-23 15:34:24 +00:00
|
|
|
|
|
|
|
|
|
/* (2) Si erreur, on la retourne */
|
|
|
|
|
if( !$inserted )
|
2016-10-18 17:09:47 +00:00
|
|
|
|
return Error::RepoError;
|
2016-07-18 10:35:30 +00:00
|
|
|
|
|
2016-07-23 15:34:24 +00:00
|
|
|
|
/* (3) On vérifie quand même que l'entrée existe */
|
|
|
|
|
$cluster_merge_inserted = $cluster_mergeR->fetch();
|
2016-07-18 10:35:30 +00:00
|
|
|
|
|
2016-07-23 15:34:24 +00:00
|
|
|
|
/* (4) Sinon, on renvoie que tout s'est bien passé */
|
2016-10-18 17:09:47 +00:00
|
|
|
|
return is_array($cluster_merge_inserted) ? Error::Success : Error::RepoError;
|
2016-07-18 10:35:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
|
*
|
2016-10-18 17:09:47 +00:00
|
|
|
|
* @return error<int> Renvoie le code 'Error'
|
2016-07-18 10:35:30 +00:00
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
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 )
|
2016-10-18 17:09:47 +00:00
|
|
|
|
return Error::NoMatchFound;
|
2016-07-18 10:35:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [2] On supprime le lien
|
|
|
|
|
=========================================================*/
|
2016-07-23 15:34:24 +00:00
|
|
|
|
$deleted = Table::get('user_cluster_merge')
|
|
|
|
|
->whereId($get_merge_id['id_user_cluster_merge'])
|
|
|
|
|
->delete();
|
2016-07-18 10:35:30 +00:00
|
|
|
|
|
2016-07-23 15:34:24 +00:00
|
|
|
|
// Si erreur
|
|
|
|
|
if( !$deleted )
|
2016-10-18 17:09:47 +00:00
|
|
|
|
return Error::RepoError;
|
2016-07-18 10:35:30 +00:00
|
|
|
|
|
|
|
|
|
/* [2] On verifie que l'association n'existe plus
|
|
|
|
|
=========================================================*/
|
|
|
|
|
if( self::getMerge($id_warehouse, $id_user_cluster, $id_user) !== false )
|
2016-10-18 17:09:47 +00:00
|
|
|
|
return Error::ModuleError;
|
2016-07-18 10:35:30 +00:00
|
|
|
|
|
|
|
|
|
|
2016-10-18 17:09:47 +00:00
|
|
|
|
return Error::Success;
|
2016-07-18 10:35:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 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){
|
2016-07-23 15:34:24 +00:00
|
|
|
|
/* [1] On rédige/exécute la requête
|
|
|
|
|
=========================================================*/
|
|
|
|
|
$cluster_merge = Table::get('user_cluster_merge')
|
|
|
|
|
->whereIdWarehouse($id_warehouse)
|
|
|
|
|
->whereIdUserCluster($id_user_cluster)
|
|
|
|
|
->whereIdUser($id_user)
|
|
|
|
|
->select('*')
|
|
|
|
|
->unique()
|
|
|
|
|
->fetch();
|
|
|
|
|
|
|
|
|
|
/* [2] On retourne le résultat (FALSE si rien trouvé)
|
|
|
|
|
=========================================================*/
|
|
|
|
|
return $cluster_merge;
|
2016-07-18 10:35:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 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){
|
2016-07-23 15:34:24 +00:00
|
|
|
|
/* [1] On rédige/exécute la requête
|
|
|
|
|
=========================================================*/
|
|
|
|
|
$user_clusters = Table::get('user_cluster')
|
|
|
|
|
->whereIdWarehouse($id_warehouse)
|
|
|
|
|
->select('*');
|
|
|
|
|
|
|
|
|
|
//TODO: ORDER BY c.name ASC
|
|
|
|
|
|
|
|
|
|
/* [2] On retourne le résultat
|
|
|
|
|
=========================================================*/
|
|
|
|
|
return $user_clusters->fetch();
|
2016-07-18 10:35:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 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){
|
2016-07-23 15:34:24 +00:00
|
|
|
|
/* [1] On rédige/exécute la requête
|
|
|
|
|
=========================================================*/
|
|
|
|
|
$user_cluster = Table::get('user_cluster')
|
|
|
|
|
->whereIdWarehouse($id_warehouse)
|
|
|
|
|
->whereId($id)
|
|
|
|
|
->select('*')
|
|
|
|
|
->unique();
|
2016-07-18 10:35:30 +00:00
|
|
|
|
|
2016-07-23 15:34:24 +00:00
|
|
|
|
/* [2] On retourne le résultat
|
|
|
|
|
=========================================================*/
|
|
|
|
|
return $user_cluster->fetch();
|
2016-07-18 10:35:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 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){
|
2016-07-23 15:34:24 +00:00
|
|
|
|
/* [1] On rédige/exécute la requête
|
|
|
|
|
=========================================================*/
|
|
|
|
|
$user_cluster = Table::get('user_cluster')
|
|
|
|
|
->whereIdWarehouse($id_warehouse)
|
|
|
|
|
->whereName($name)
|
|
|
|
|
->select('*')
|
|
|
|
|
->unique();
|
2016-07-18 10:35:30 +00:00
|
|
|
|
|
2016-07-23 15:34:24 +00:00
|
|
|
|
/* [2] On retourne le résultat
|
|
|
|
|
=========================================================*/
|
|
|
|
|
return $user_cluster->fetch();
|
2016-07-18 10:35:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
|
=========================================================*/
|
2016-07-23 15:34:24 +00:00
|
|
|
|
$user_cluster = Table::get('user_cluster')
|
|
|
|
|
->whereIdWarehouse($id_warehouse)
|
|
|
|
|
->whereId($id_user_cluster);
|
|
|
|
|
|
|
|
|
|
$user = Table::get('user')->select('*');
|
|
|
|
|
|
|
|
|
|
$user_cluster_merge = Table::get('user_cluster_merge')
|
|
|
|
|
->join('id_user_cluster', $user_cluster)
|
|
|
|
|
->join('id_user', $user);
|
2016-07-18 10:35:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [2] On verifie que le groupe n'existe plus
|
|
|
|
|
=========================================================*/
|
2016-07-23 15:34:24 +00:00
|
|
|
|
return $user_cluster_merge->fetch();
|
2016-07-18 10:35:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 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){
|
2016-07-23 15:34:24 +00:00
|
|
|
|
/* [1] On rédige/exécute la requête
|
|
|
|
|
=========================================================*/
|
|
|
|
|
$search = Table::get('user_cluster')
|
|
|
|
|
->whereIdWarehouse($id_warehouse)
|
|
|
|
|
->whereName(["%$keyword%", Rows::COND_LIKE])
|
|
|
|
|
->select('*');
|
2016-07-18 10:35:30 +00:00
|
|
|
|
|
2016-07-23 15:34:24 +00:00
|
|
|
|
/* [2] On retourne le résultat
|
|
|
|
|
=========================================================*/
|
|
|
|
|
return $search->fetch();
|
2016-07-18 10:35:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-07-20 17:41:43 +00:00
|
|
|
|
|
|
|
|
|
/* MODIFIE LE NOM D'UN GROUPE DONNE
|
|
|
|
|
*
|
|
|
|
|
* @id_user_cluster<int> UID du gorupe
|
|
|
|
|
* @name<String> Nouveau nom du groupe
|
|
|
|
|
*
|
|
|
|
|
* @return status<Boolean> TRUE si tout s'est bien passé, sinon FALSE
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
public static function edit($id_warehouse, $id_user_cluster, $name){
|
2016-07-23 15:34:24 +00:00
|
|
|
|
/* [1] On redige/execute la requete
|
2016-07-20 17:41:43 +00:00
|
|
|
|
=========================================================*/
|
2016-07-23 15:34:24 +00:00
|
|
|
|
$edited = Table::get('user_cluster')
|
|
|
|
|
->whereIdWarehouse($id_warehouse)
|
|
|
|
|
->whereId($id_user_cluster)
|
|
|
|
|
->edit([ 'name' => $name ]);
|
2016-07-20 17:41:43 +00:00
|
|
|
|
|
2016-07-23 15:34:24 +00:00
|
|
|
|
/* [2] On retourne le résultat
|
|
|
|
|
=========================================================*/
|
2016-07-20 17:41:43 +00:00
|
|
|
|
return $edited;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-07-18 10:35:30 +00:00
|
|
|
|
/* 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
|
|
|
|
|
=========================================================*/
|
2016-07-23 15:34:24 +00:00
|
|
|
|
$deleted = Table::get('user_cluster')
|
|
|
|
|
->whereIdWarehouse($id_warehouse)
|
|
|
|
|
->whereId($id_user_cluster)
|
|
|
|
|
->delete();
|
2016-07-18 10:35:30 +00:00
|
|
|
|
|
2016-07-23 15:34:24 +00:00
|
|
|
|
/* [2] On retourne le résultat
|
2016-07-18 10:35:30 +00:00
|
|
|
|
=========================================================*/
|
2016-07-23 15:34:24 +00:00
|
|
|
|
return $deleted;
|
2016-07-18 10:35:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
?>
|