From 2889114b28b217b534e2f901a203b4acd73b696e Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sat, 23 Jul 2016 18:06:39 +0200 Subject: [PATCH] =?UTF-8?q?Repository=20'warehouse'=20utilise=20enti=C3=A8?= =?UTF-8?q?rement=20ORM?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- manager/repo/admin.php | 8 ++- manager/repo/warehouse.php | 117 +++++++++++++------------------------ test/automate.php | 30 ++-------- 3 files changed, 52 insertions(+), 103 deletions(-) diff --git a/manager/repo/admin.php b/manager/repo/admin.php index 178f249..d3a0069 100644 --- a/manager/repo/admin.php +++ b/manager/repo/admin.php @@ -42,11 +42,11 @@ $check_admin = self::getByUsername($username); // Si n'existe pas on retourne FALSE - if( count($check_admin) == 0 ) + if( $check_admin === false ) return false; // Sinon, on retourne son id - return $check_admin[0]['id_admin']; + return $check_admin['id_admin']; @@ -171,6 +171,7 @@ ->whereId($id_admin) ->whereIdWarehouse($id_warehouse) ->unique() + ->select('*') ->fetch(); /* [2] On retourne le résultat @@ -201,6 +202,7 @@ ->whereUsername($username) ->whereIdWarehouse($id_warehouse) ->unique() + ->select('*') ->fetch(); /* [2] On retourne le résultat @@ -231,6 +233,7 @@ ->whereMail($mail) ->whereIdWarehouse($id_warehouse) ->unique() + ->select('*') ->fetch(); /* [2] On retourne le résultat @@ -261,6 +264,7 @@ ->whereToken($token) ->whereIdWarehouse($id_warehouse) ->unique() + ->select('*') ->fetch(); /* [2] On retourne le résultat diff --git a/manager/repo/warehouse.php b/manager/repo/warehouse.php index 41c8c3a..da76573 100644 --- a/manager/repo/warehouse.php +++ b/manager/repo/warehouse.php @@ -3,7 +3,8 @@ namespace manager\repo; use \manager\Database; use \manager\sessionManager; - use \manager\repo\cluster as clusterRepo; + use \manager\ORM\Table; + use \manager\ORM\Rows; class warehouse extends parentRepo{ @@ -19,34 +20,30 @@ * */ public static function create($name, $password){ - /* [1] Verification de l'unicite du nom (name) + /* [1] On rédige/exécute la requête =========================================================*/ - $check_unique = count(self::getByName($name)) == 0; + $created = Table::get('warehouse') + ->insert([ + 'id_warehouse' => Rows::DEFAULT, + 'name' => $name, + 'password' => sessionManager::secure_hash( $password ), + 'token' => sessionManager::secure_hash( uniqid() ) + ]); - // Si un entrepot a deja ce nom, on renvoie une erreur - if( !$check_unique ) + // Si name déja pris, erreur + if( !$created ) return false; - /* [2] Creation de l'utilisateur - =========================================================*/ - $insert_warehouse = Database::getPDO()->prepare("INSERT INTO warehouse(id_warehouse, name, password, token) - VALUES(DEFAULT, :name, :password, :token)"); - $insert_warehouse->execute([ - ':name' => $name, - ':password' => sessionManager::secure_hash( $password ), - ':token' => sessionManager::secure_hash( uniqid() ) - ]); - /* [3] On retourne l'id_warehouse ou FALSE si erreur =========================================================*/ $check_warehouse = self::getByName($name); // Si n'existe pas on retourne FALSE - if( !isset($check_warehouse[0]) || !is_array($check_warehouse[0]) ) + if( $check_warehouse === false ) return false; // Sinon, on retourne son id - return intval( $check_warehouse[0]['id_warehouse'] ); + return $check_warehouse['id_warehouse']; } @@ -64,7 +61,6 @@ * */ public static function updateToken($id_warehouse=null){ - /* [1] On vérifie que l'entrepot existe =========================================================*/ if( self::getById($id_warehouse) === false ) @@ -76,20 +72,13 @@ /* (1) On crée un nouveau token */ $new_token = sessionManager::secure_hash( uniqid() ); - /* (2) On applique le nouveau token */ - $update_token = Database::getPDO()->prepare("UPDATE warehouse - SET token = :token - WHERE id_warehouse = :id_warehouse"); - - $update_token->execute([ - ':token' => $new_token, - ':id_warehouse' => $id_warehouse - ]); - + $updated = Table::get('warehouse') + ->whereId($id_warehouse) + ->edit([ 'token' => $new_token ]); /* [3] On renvoie le token =========================================================*/ - return $new_token; + return ( $updated ) ? $new_token : false; } @@ -108,17 +97,14 @@ public static function getModules($id_warehouse){ /* [1] On récupère les modules =========================================================*/ - $getModules = Database::getPDO()->prepare("SELECT m.name - FROM module_merge as wm, module as m - WHERE wm.id_module = m.id_module - AND wm.id_warehouse = :id_warehouse - ORDER BY m.name ASC"); + $module = Table::get('module') + ->select('name'); - $getModules->execute([ - ':id_warehouse' => $id_warehouse - ]); + $merge = Table::get('module_merge') + ->whereIdWarehouse($id_warehouse) + ->join('id_module', $module); - $modules = $getModules->fetchAll(); + $modules = $merge->fetch(); /* [2] On formatte les données pour avoir 1 niveau de tableau =========================================================*/ @@ -128,7 +114,7 @@ /* [3] On retourne la liste =========================================================*/ - return Database::delNumeric( $modules ); + return $modules; } @@ -148,42 +134,20 @@ * */ public static function edit($id_warehouse=null, $name=null, $password=null){ - /* [1] Verification de l'unicite du nom (name) (si different) + /* [1] On rédige/exécute la requête =========================================================*/ - // On recupere les entrepots ayant le meme nom (si existent) - // pour éviter les collisions (car le nom doit être unique) - $getbyname = self::getByName($name); + $updated = Table::get('warehouse') + ->whereId($id_warehouse) + ->edit([ + 'name' => $name, + 'password' => sessionManager::secure_hash( $password ) + ]); - $check_unique = is_array($getbyname) && $getbyname['id_user'] == $id_user || !is_array($getbyname); - // Si un utilisateur a deja ce name ou cet username (sauf lui), on renvoie une erreur - if( !$check_unique ) - return false; - - /* [2] Modification de l'utilisateur + /* [2] On retourne le résultat =========================================================*/ - $edit_warehouse = Database::getPDO()->prepare("UPDATE warehouse - SET name = :name, - password = :password - WHERE id_warehouse = :id_warehouse"); - $edit_warehouse->execute([ - ':name' => $name, - ':password' => $password, - ':id_warehouse' => $id_warehouse - ]); - - - - /* [3] On retourne si tout a ete modifie, si erreur - =========================================================*/ - $check_warehouse = self::getById($id_warehouse); - - $checker = $check_warehouse['id_warehouse'] == $id_warehouse; - $checker = $checker && $check_warehouse['name'] == $name; - $checker = $checker && $check_warehouse['password'] == $password; - - // On retourne l'etat de la modification - return $checker; + // FAUX si @name est déja pris et si erreur + return $updated; } @@ -203,15 +167,14 @@ public static function delete($id_warehouse){ /* [1] On redige/execute la requete =========================================================*/ - $delete_warehouse = Database::getPDO()->prepare("DELETE FROM warehouse WHERE id_warehouse = :id_warehouse"); - $delete_warehouse->execute([ - ':id_warehouse' => $id_warehouse - ]); + $deleted = Table::get('warehouse') + ->whereId($id_warehouse) + ->delete(); - /* [2] On verifie que l'utilisateur n'existe plus + /* [2] On verifie que l'entrepot n'existe plus =========================================================*/ - return ( self::getById($id_warehouse) === false ); + return $deleted; } diff --git a/test/automate.php b/test/automate.php index fc569b3..33d7715 100755 --- a/test/automate.php +++ b/test/automate.php @@ -431,33 +431,15 @@ $id_user_cluster = 2; //////////////////////////// - $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 - ]); + $module = Table::get('module') + ->select('name'); - - /* [2] On verifie que le groupe n'existe plus - =========================================================*/ - var_dump( count($get_users->fetchAll()) ); - - - $user_cluster = Table::get('user_cluster') + $merge = Table::get('module_merge') ->whereIdWarehouse($id_warehouse) - ->whereId($id_user_cluster); + ->join('id_module', $module); - $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()) ); + $modules = $merge->fetch(); + var_dump($modules); ?>