Repository 'warehouse' utilise entièrement ORM

This commit is contained in:
xdrm-brackets 2016-07-23 18:06:39 +02:00
parent 7bcf154afb
commit 2889114b28
3 changed files with 52 additions and 103 deletions

View File

@ -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

View File

@ -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;
// Si un entrepot a deja ce nom, on renvoie une erreur
if( !$check_unique )
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() )
$created = Table::get('warehouse')
->insert([
'id_warehouse' => Rows::DEFAULT,
'name' => $name,
'password' => sessionManager::secure_hash( $password ),
'token' => sessionManager::secure_hash( uniqid() )
]);
// Si name déja pris, erreur
if( !$created )
return false;
/* [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);
$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
=========================================================*/
$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
$updated = Table::get('warehouse')
->whereId($id_warehouse)
->edit([
'name' => $name,
'password' => sessionManager::secure_hash( $password )
]);
/* [3] On retourne si tout a ete modifie, si erreur
/* [2] On retourne le résultat
=========================================================*/
$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;
}

View File

@ -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);
?>