diff --git a/manager/ORM/Rows.php b/manager/ORM/Rows.php index 4489374..37cb09e 100644 --- a/manager/ORM/Rows.php +++ b/manager/ORM/Rows.php @@ -698,6 +698,7 @@ * * @return data Tableau contenant les champs sélectionnés * @return data Valeur du champ sélectionné (si 1 seul champ) + * @return ERROR Retourne FALSE si rien n'est trouvé * */ public function fetch($unique=null){ diff --git a/manager/repo/machine.php b/manager/repo/machine.php index 879991c..2c970e8 100755 --- a/manager/repo/machine.php +++ b/manager/repo/machine.php @@ -263,8 +263,7 @@ =========================================================*/ $machine = Table::get('machine') ->whereIdWarehouse($id_warehouse) - ->select(['id_machine', 'name']) - ->unique(); + ->select(['id_machine', 'name']); return $machine->fetch(); } diff --git a/manager/repo/machine_cluster.php b/manager/repo/machine_cluster.php index dfdb067..f77faea 100644 --- a/manager/repo/machine_cluster.php +++ b/manager/repo/machine_cluster.php @@ -4,6 +4,8 @@ use \manager\Database; use \manager\ManagerError; use \manager\Repo; + use \manager\ORM\Table; + use \manager\ORM\Rows; class machine_cluster extends parentRepo{ @@ -363,16 +365,16 @@ * */ public static function search($id_warehouse, $keyword){ - // On recupere les donnees - $searchmachine_clusters = Database::getPDO()->prepare("SELECT * FROM machine_cluster - WHERE id_warehouse = :id_warehouse - AND name LIKE '%".$keyword."%'"); + /* [1] On rédige/exécute la requête + =========================================================*/ + $search = Table::get('machine_cluster') + ->whereIdWarehouse($id_warehouse) + ->whereName(["%$keyword%", Rows::COND_LIKE]) + ->select('*'); - $searchmachine_clusters->execute([ - ':id_warehouse' => $id_warehouse - ]); - - return Database::delNumeric( $searchmachine_clusters->fetchAll() ); + /* [2] On retourne le résultat + =========================================================*/ + return $search->fetch(); } diff --git a/manager/repo/user_cluster.php b/manager/repo/user_cluster.php index 4f0f9c4..06ebc87 100644 --- a/manager/repo/user_cluster.php +++ b/manager/repo/user_cluster.php @@ -4,6 +4,8 @@ use \manager\Database; use \manager\ManagerError; use \manager\Repo; + use \manager\ORM\Table; + use \manager\ORM\Rows; class user_cluster extends parentRepo{ @@ -21,23 +23,19 @@ public static function create($id_warehouse, $name){ /* [1] Verification de l'unicite de name =========================================================*/ - $check_unique = ( self::getByName($id_warehouse, $name) === false ); + $inserted = Table::get('user_cluster') + ->insert([ + 'id_user_cluster' => Rows::DEFAULT, + 'id_warehouse' => $id_warehouse, + 'name' => $name + ]); - // Si un groupe a deja ce nom, on renvoie une erreur - if( !$check_unique ) + // Si erreur (car name doit être unique) + if( !$inserted ) 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 + /* [2] On retourne l'id_group ou FALSE si erreur =========================================================*/ $check_group = self::getByName($id_warehouse, $name); @@ -66,57 +64,66 @@ * */ public static function link($id_warehouse, $id_user_cluster, $id_user){ - /* [1] Verification de l'existence du groupe + /* [1] On vérifie que tout existe =========================================================*/ - $group_check = self::getById($id_warehouse, $id_user_cluster); + /* (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, on retourne ue erreur - if( $group_check === false ) + // Si le groupe n'existe pas + if( $cluster === false ) return ManagerError::NoMatchFound; + /* (2) On récupère l'utilisateur */ + $userR = Table::get('user') + ->whereIdWarehouse($id_warehouse) + ->whereId($id_user) + ->unique() + ->select('id_user'); + $user = $userR->fetch(); - - /* [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 ) + // Si l'utilisateur n'existe pas + if( $user === false ) return ManagerError::NoMatchFound; + /* (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(); - /* [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 ) + // Si la relation existe déja, on ne fait rien + if( $cluster_merge_exists !== 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 + /* [2] On crée la relation =========================================================*/ - $check_user_cluster_merge = self::getMerge($id_warehouse, $id_user_cluster, $id_user); + /* (1) On insère la relation */ + $inserted = Table::get('user_cluster_merge') + ->insert([ + 'id_user_cluster_merge' => Rows::DEFAULT, + 'id_user_cluster' => $id_user_cluster, + 'id_user' => $id_user + ]); - // Si n'existe pas on retourne FALSE - if( !is_array($check_user_cluster_merge) ) + + /* (2) Si erreur, on la retourne */ + if( !$inserted ) return ManagerError::RepoError; - // Sinon, on retourne son id - return ManagerError::Success; + /* (3) On vérifie quand même que l'entrée existe */ + $cluster_merge_inserted = $cluster_mergeR->fetch(); + /* (4) Sinon, on renvoie que tout s'est bien passé */ + return is_array($cluster_merge_inserted) ? ManagerError::Success : ManagerError::RepoError; } @@ -148,12 +155,13 @@ /* [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'] - ]); + $deleted = Table::get('user_cluster_merge') + ->whereId($get_merge_id['id_user_cluster_merge']) + ->delete(); + // Si erreur + if( !$deleted ) + return ManagerError::RepoError; /* [2] On verifie que l'association n'existe plus =========================================================*/ @@ -181,30 +189,19 @@ * */ 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"); + /* [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(); - // 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 ); + /* [2] On retourne le résultat (FALSE si rien trouvé) + =========================================================*/ + return $cluster_merge; } @@ -222,19 +219,17 @@ * */ 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"); + /* [1] On rédige/exécute la requête + =========================================================*/ + $user_clusters = Table::get('user_cluster') + ->whereIdWarehouse($id_warehouse) + ->select('*'); - // On execute la requete - $request->execute([ - ':id_warehouse' => $id_warehouse - ]); + //TODO: ORDER BY c.name ASC - // On retourne en supprimant les indices numeriques - return Database::delNumeric( $request->fetchAll() ); + /* [2] On retourne le résultat + =========================================================*/ + return $user_clusters->fetch(); } @@ -253,26 +248,17 @@ * */ 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"); + /* [1] On rédige/exécute la requête + =========================================================*/ + $user_cluster = Table::get('user_cluster') + ->whereIdWarehouse($id_warehouse) + ->whereId($id) + ->select('*') + ->unique(); - // 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 ); + /* [2] On retourne le résultat + =========================================================*/ + return $user_cluster->fetch(); } @@ -291,26 +277,17 @@ * */ 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"); + /* [1] On rédige/exécute la requête + =========================================================*/ + $user_cluster = Table::get('user_cluster') + ->whereIdWarehouse($id_warehouse) + ->whereName($name) + ->select('*') + ->unique(); - // 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 ); + /* [2] On retourne le résultat + =========================================================*/ + return $user_cluster->fetch(); } @@ -332,22 +309,20 @@ public static function getMembers($id_warehouse, $id_user_cluster){ /* [1] On recupere la liste des associations =========================================================*/ - $get_users = Database::getPDO()->prepare("SELECT u.* - FROM user_cluster_merge as cm, user_cluster as c, user as u - 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 = u.id_user - ORDER BY u.username"); - $get_users->execute([ - ':id_warehouse' => $id_warehouse, - ':id_user_cluster' => $id_user_cluster - ]); + $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); /* [2] On verifie que le groupe n'existe plus =========================================================*/ - return Database::delNumeric( $get_users->fetchAll() );; + return $user_cluster_merge->fetch(); } @@ -363,16 +338,16 @@ * */ 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."%'"); + /* [1] On rédige/exécute la requête + =========================================================*/ + $search = Table::get('user_cluster') + ->whereIdWarehouse($id_warehouse) + ->whereName(["%$keyword%", Rows::COND_LIKE]) + ->select('*'); - $searchuser_clusters->execute([ - ':id_warehouse' => $id_warehouse - ]); - - return Database::delNumeric( $searchuser_clusters->fetchAll() ); + /* [2] On retourne le résultat + =========================================================*/ + return $search->fetch(); } @@ -390,17 +365,15 @@ * */ public static function edit($id_warehouse, $id_user_cluster, $name){ - /* [1] On rédige la requête + /* [1] On redige/execute la requete =========================================================*/ - $edit_group = Database::getPDO()->prepare("UPDATE user_cluster - SET name = :name - WHERE id_user_cluster = :id_user_cluster"); - $edited = $edit_group->execute([ - ':name' => $name, - ':id_user_cluster' => $id_user_cluster - ]); - + $edited = Table::get('user_cluster') + ->whereIdWarehouse($id_warehouse) + ->whereId($id_user_cluster) + ->edit([ 'name' => $name ]); + /* [2] On retourne le résultat + =========================================================*/ return $edited; } @@ -419,18 +392,14 @@ 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 - ]); + $deleted = Table::get('user_cluster') + ->whereIdWarehouse($id_warehouse) + ->whereId($id_user_cluster) + ->delete(); - - /* [2] On verifie que le groupe n'existe plus + /* [2] On retourne le résultat =========================================================*/ - return ( self::getById($id_warehouse, $id_user_cluster) === false ); + return $deleted; } diff --git a/test/automate.php b/test/automate.php index 067a3b2..fc569b3 100755 --- a/test/automate.php +++ b/test/automate.php @@ -427,23 +427,37 @@ // ->delete(); - $keyword = 'h%s'; + $id_warehouse = 7; + $id_user_cluster = 2; + //////////////////////////// - $searchmachines = Database::getPDO()->prepare("SELECT id_machine, name FROM machine - WHERE id_warehouse = :id_warehouse - AND name LIKE '%".$keyword."%'"); - - $searchmachines->execute([ - ':id_warehouse' => 7 + $get_users = Database::getPDO()->prepare("SELECT u.* + FROM user_cluster_merge as cm, user_cluster as c, user as u + 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 = u.id_user"); + $get_users->execute([ + ':id_warehouse' => $id_warehouse, + ':id_user_cluster' => $id_user_cluster ]); - var_dump($searchmachines->fetchAll()); + + /* [2] On verifie que le groupe n'existe plus + =========================================================*/ + var_dump( count($get_users->fetchAll()) ); - var_dump( Table::get('machine') - ->whereIdWarehouse(7) - ->whereName(["%$keyword%", Rows::COND_LIKE]) - ->select('id_machine') - ->select('name')->fetch() ); + $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); + + var_dump( count($user_cluster_merge->fetch()) ); ?> diff --git a/view/js/groups-min.js b/view/js/groups-min.js index 60c6879..da1b9d6 100644 --- a/view/js/groups-min.js +++ b/view/js/groups-min.js @@ -2,25 +2,26 @@ var section={view:{text:'#CONTAINER > section[data-sublink="view"] ',element:doc edit:{text:'#CONTAINER > section[data-sublink="edit"] ',element:document.querySelector('#CONTAINER > section[data-sublink="edit"]')},members:{text:'#CONTAINER > section[data-sublink="members"] ',element:document.querySelector('#CONTAINER > section[data-sublink="members"]'),chooser:{text:'#CONTAINER > section[data-sublink="members"] form#choose-cluster ',element:document.querySelector('#CONTAINER > section[data-sublink="members"] form#choose-cluster')}},permissions:{text:'#CONTAINER > section[data-sublink="permissions"] ', element:document.querySelector('#CONTAINER > section[data-sublink="permissions"]')}}; if(null!=section.view.element){1 article.inline-box[id]");for(c=0;c article.inline-box[id]");for(b=0;b article.inline-box[id]'); diff --git a/view/js/machines-min.js b/view/js/machines-min.js index 7ca9783..b148102 100644 --- a/view/js/machines-min.js +++ b/view/js/machines-min.js @@ -17,6 +17,6 @@ searchindex_edit=0;2<=pageManager.vars.length&&!isNaN(pageManager.vars[1])&&api. else{var b={path:"machineDefault/search",keywords:section.edit.input.search.keyword.value};api.send(b,function(a){0==a.ModuleError&&(section.edit.input.search.submit.anim("active",1E3),searchindex_edit=0,found_edit=a.machines,section.edit.input.search.sum.innerHTML=found_edit.length,autofill_edit());lastkeywords_edit=b.keywords})}},!1);section.edit.input.submit.addEventListener("click",function(a){a.preventDefault();if(""!=section.edit.input.search.id.value){var b={path:"machineDefault/edit",id_machine:section.edit.input.search.id.value, name:section.edit.input.name.value};api.send(b,function(a){0==a.ModuleError&&1==a.status?(console.log("Machine modifiee!"),section.edit.input.submit.anim("active",1500),pageManager.vars[1]=b.id_machine,navSubMenu("view"),document.location=""):console.error("ModuleError::"+a.ModuleError)})}},!1)} if(null!=section.groups.element){section.groups.link={remove:document.querySelectorAll(section.groups.text+".link_remove[data-cluster]"),edit:document.querySelectorAll(section.groups.text+".link_edit[data-cluster]")};section.groups.group={rem:document.querySelectorAll(section.groups.text+".rem-member[data-cluster][data-member]"),add:document.querySelectorAll(section.groups.text+".add-member[data-cluster]")};section.groups.searchbar.addEventListener("keyup",function(a){api.send({path:"clusterDefault/search", -keywords:section.groups.searchbar.value,"class":1},function(a){if(0==a.ModuleError){for(var d=[],c=0;c article.inline-box[id]");for(c=0;c article.inline-box[id]");for(c=0;c article.inline-box[id]'); @@ -648,7 +648,7 @@ if( section.groups.element != null ){ // Pour chaque cluster for( var i = 0 ; i < cluster_list.length ; i++ ){ // Si doit etre visible - if( uid_list.indexOf(cluster_list[i].id) > -1 ) + if( uid_list.indexOf( parseInt(cluster_list[i].id) ) > -1 ) cluster_list[i].remClass('hidden'); // Si ne doit pas etre visible else diff --git a/view/js/users-min.js b/view/js/users-min.js index 2ffa815..2a5d963 100644 --- a/view/js/users-min.js +++ b/view/js/users-min.js @@ -20,6 +20,6 @@ id:document.querySelector(section.edit.text+"#edit_search_id"),submit:document.q {path:"userDefault/edit",id_user:parseInt(section.edit.input.search.id.value),code:section.edit.input.code.value,username:section.edit.input.username.value,firstname:section.edit.input.firstname.value,lastname:section.edit.input.lastname.value,mail:section.edit.input.mail.value};api.send(c,function(a){console.log(a);0==a.ModuleError&&1==a.status?(console.log("Utilisateur modifie!"),section.edit.input.submit.anim("active",1500),pageManager.vars[1]=c.id_user,navSubMenu("view"),document.location=""): console.error("ModuleError::"+a.ModuleError)})}},!1)} if(null!=section.groups.element){section.groups.link={remove:document.querySelectorAll(section.groups.text+".link_remove[data-cluster]"),edit:document.querySelectorAll(section.groups.text+".link_edit[data-cluster]")};section.groups.group={rem:document.querySelectorAll(section.groups.text+".rem-member[data-cluster][data-member]"),add:document.querySelectorAll(section.groups.text+".add-member[data-cluster]")};section.groups.searchbar.addEventListener("keyup",function(a){api.send({path:"clusterDefault/search", -keywords:section.groups.searchbar.value,"class":0},function(a){if(0==a.ModuleError){for(var d=[],b=0;b article.inline-box[id]");for(b=0;b article.inline-box[id]");for(b=0;b -1 ) + if( uid_list.indexOf( parseInt(cluster_list[i].id) ) > -1 ) cluster_list[i].remClass('hidden'); // Si ne doit pas etre visible else