2016-07-04 10:51:33 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace manager\repo;
|
|
|
|
use \manager\Database;
|
|
|
|
use \manager\sessionManager;
|
|
|
|
use \manager\repo\cluster as clusterRepo;
|
|
|
|
|
|
|
|
class warehouse extends parentRepo{
|
|
|
|
|
|
|
|
protected static function table_name(){ static $table_name = 'warehouse'; return $table_name; }
|
|
|
|
|
2016-07-04 13:45:29 +00:00
|
|
|
/* CREATION D'UN ENTREPOT
|
2016-07-04 10:51:33 +00:00
|
|
|
*
|
|
|
|
* @name<String> Identifiant de l'entrepot
|
|
|
|
* @password<String> Mot de passe de l'entrepot
|
|
|
|
*
|
|
|
|
* @return id_warehouse<int> Renvoie l'UID de l'entrepot cree
|
|
|
|
* Renvoie FALSE si une erreur occure
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function create($name, $password){
|
|
|
|
/* [1] Verification de l'unicite du nom (name)
|
|
|
|
=========================================================*/
|
|
|
|
$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)");
|
2016-07-04 13:45:29 +00:00
|
|
|
$insert_warehouse->execute([
|
2016-07-04 10:51:33 +00:00
|
|
|
':name' => $name,
|
2016-07-07 15:59:31 +00:00
|
|
|
':password' => sessionManager::secure_hash( $password ),
|
|
|
|
':token' => sessionManager::secure_hash( uniqid() )
|
2016-07-04 13:45:29 +00:00
|
|
|
]);
|
2016-07-04 10:51:33 +00:00
|
|
|
|
|
|
|
/* [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]) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Sinon, on retourne son id
|
|
|
|
return intval( $check_warehouse[0]['id_warehouse'] );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-07-04 12:39:27 +00:00
|
|
|
/* MISE A JOUR DU TOKEN D'UN ENTREPOT
|
|
|
|
*
|
|
|
|
* @id_warehouse<int> UID de l'entrepot
|
|
|
|
*
|
|
|
|
* @return new_token<String> Renvoie le nouveau token
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function updateToken($id_warehouse=null){
|
|
|
|
|
|
|
|
/* [1] On vérifie que l'entrepot existe
|
|
|
|
=========================================================*/
|
|
|
|
if( self::getById($id_warehouse) === false )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
/* [2] On met à jour le token
|
|
|
|
=========================================================*/
|
|
|
|
/* (1) On crée un nouveau token */
|
2016-07-07 15:59:31 +00:00
|
|
|
$new_token = sessionManager::secure_hash( uniqid() );
|
2016-07-04 12:39:27 +00:00
|
|
|
|
|
|
|
/* (2) On applique le nouveau token */
|
|
|
|
$update_token = Database::getPDO()->prepare("UPDATE warehouse
|
2016-07-04 13:45:29 +00:00
|
|
|
SET token = :token
|
2016-07-04 12:39:27 +00:00
|
|
|
WHERE id_warehouse = :id_warehouse");
|
|
|
|
|
2016-07-04 13:45:29 +00:00
|
|
|
$update_token->execute([
|
2016-07-04 12:39:27 +00:00
|
|
|
':token' => $new_token,
|
|
|
|
':id_warehouse' => $id_warehouse
|
2016-07-04 13:45:29 +00:00
|
|
|
]);
|
2016-07-04 12:39:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* [3] On renvoie le token
|
|
|
|
=========================================================*/
|
|
|
|
return $new_token;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-07-04 10:51:33 +00:00
|
|
|
|
2016-07-06 09:32:51 +00:00
|
|
|
/* RETOURNE LA LISTE DES MODULE D'UN ENTREPOT
|
|
|
|
*
|
|
|
|
* @return modules<Array> Liste des modules autorisés pour l'entrepot
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function getModules($id_warehouse){
|
|
|
|
/* [1] On récupère les modules
|
|
|
|
=========================================================*/
|
|
|
|
$getModules = Database::getPDO()->prepare("SELECT m.name
|
|
|
|
FROM warehouse_module as wm, module as m
|
|
|
|
WHERE wm.id_module = m.id_module
|
|
|
|
AND wm.id_warehouse = :id_warehouse
|
|
|
|
ORDER BY m.name ASC");
|
|
|
|
|
|
|
|
$getModules->execute([
|
|
|
|
':id_warehouse' => $id_warehouse
|
|
|
|
]);
|
|
|
|
|
|
|
|
$modules = $getModules->fetchAll();
|
|
|
|
|
|
|
|
/* [2] On formatte les données pour avoir 1 niveau de tableau
|
|
|
|
=========================================================*/
|
|
|
|
foreach($modules as $m=>$module)
|
|
|
|
$modules[$m] = $module['name'];
|
|
|
|
|
|
|
|
|
|
|
|
/* [3] On retourne la liste
|
|
|
|
=========================================================*/
|
|
|
|
return Database::delNumeric( $modules );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-07-04 10:51:33 +00:00
|
|
|
/* MODIFICATION D'UN ENTREPOT
|
|
|
|
*
|
|
|
|
* @id_warehouse<int> UID de l'entrepot
|
|
|
|
* @name<String> Identifiant de l'entrepot
|
|
|
|
* @password<String> Mot de passe de l'entrepot
|
|
|
|
*
|
|
|
|
* @return status<Boolean> Renvoie si oui ou non tout s'est bien passe
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function edit($id_warehouse=null, $name=null, $password=null){
|
|
|
|
/* [1] Verification de l'unicite du nom (name) (si different)
|
|
|
|
=========================================================*/
|
|
|
|
// On recupere les entrepots ayant le meme nom (si existent)
|
|
|
|
// pour éviter les collisions (car le nom doit être unique)
|
2016-07-04 13:45:29 +00:00
|
|
|
$getbyname = self::getByName($name);
|
2016-07-04 10:51:33 +00:00
|
|
|
|
|
|
|
$check_unique = is_array($getbyname) && $getbyname['id_user'] == $id_user || !is_array($getbyname);
|
|
|
|
|
2016-07-04 13:45:29 +00:00
|
|
|
// Si un utilisateur a deja ce name ou cet username (sauf lui), on renvoie une erreur
|
2016-07-04 10:51:33 +00:00
|
|
|
if( !$check_unique )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* [2] Modification de l'utilisateur
|
|
|
|
=========================================================*/
|
2016-07-04 12:39:27 +00:00
|
|
|
$edit_warehouse = Database::getPDO()->prepare("UPDATE warehouse
|
2016-07-04 13:45:29 +00:00
|
|
|
SET name = :name,
|
|
|
|
password = :password
|
2016-07-04 12:39:27 +00:00
|
|
|
WHERE id_warehouse = :id_warehouse");
|
2016-07-04 13:45:29 +00:00
|
|
|
$edit_warehouse->execute([
|
2016-07-04 12:39:27 +00:00
|
|
|
':name' => $name,
|
|
|
|
':password' => $password,
|
|
|
|
':id_warehouse' => $id_warehouse
|
2016-07-04 13:45:29 +00:00
|
|
|
]);
|
2016-07-04 10:51:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [3] On retourne si tout a ete modifie, si erreur
|
|
|
|
=========================================================*/
|
2016-07-04 12:39:27 +00:00
|
|
|
$check_warehouse = self::getById($id_warehouse);
|
2016-07-04 10:51:33 +00:00
|
|
|
|
2016-07-04 12:39:27 +00:00
|
|
|
$checker = $check_warehouse['id_warehouse'] == $id_warehouse;
|
|
|
|
$checker = $checker && $check_warehouse['name'] == $name;
|
|
|
|
$checker = $checker && $check_warehouse['password'] == $password;
|
2016-07-04 10:51:33 +00:00
|
|
|
|
|
|
|
// On retourne l'etat de la modification
|
|
|
|
return $checker;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-07-04 12:39:27 +00:00
|
|
|
/* SUPPRIME UN ENTREPOT DONNE
|
2016-07-04 10:51:33 +00:00
|
|
|
*
|
2016-07-04 12:39:27 +00:00
|
|
|
* @id_warehouse<int> UID de l'entrepot en question
|
2016-07-04 10:51:33 +00:00
|
|
|
*
|
2016-07-04 12:39:27 +00:00
|
|
|
* @return status<Boolean> Retourne si oui ou non l'entrepot a bien ete supprime
|
2016-07-04 10:51:33 +00:00
|
|
|
*
|
|
|
|
*/
|
2016-07-04 12:39:27 +00:00
|
|
|
public static function delete($id_warehouse){
|
2016-07-04 10:51:33 +00:00
|
|
|
/* [1] On redige/execute la requete
|
|
|
|
=========================================================*/
|
2016-07-04 12:39:27 +00:00
|
|
|
$delete_warehouse = Database::getPDO()->prepare("DELETE FROM warehouse WHERE id_warehouse = :id_warehouse");
|
2016-07-04 13:45:29 +00:00
|
|
|
$delete_warehouse->execute([
|
2016-07-04 12:39:27 +00:00
|
|
|
':id_warehouse' => $id_warehouse
|
2016-07-04 13:45:29 +00:00
|
|
|
]);
|
2016-07-04 10:51:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* [2] On verifie que l'utilisateur n'existe plus
|
|
|
|
=========================================================*/
|
2016-07-04 12:39:27 +00:00
|
|
|
return ( self::getById($id_warehouse) === false );
|
2016-07-04 10:51:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
?>
|