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; }
|
|
|
|
|
|
|
|
/* CREATION D'UN UTILISATEUR ET DE SON GROUPE INDIVIDUEL
|
|
|
|
*
|
|
|
|
* @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)");
|
|
|
|
$insert_warehouse->execute(array(
|
|
|
|
':name' => $name,
|
|
|
|
':password' => sessionManager::secure_sha1( $password ),
|
|
|
|
':token' => sessionManager::secure_sha1( 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]) )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Sinon, on retourne son id
|
|
|
|
return intval( $check_warehouse[0]['id_warehouse'] );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 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)
|
|
|
|
$getbyname = self::getByName($code);
|
|
|
|
|
|
|
|
$check_unique = is_array($getbyname) && $getbyname['id_user'] == $id_user || !is_array($getbyname);
|
|
|
|
|
|
|
|
// Si un utilisateur a deja ce code ou cet username (sauf lui), on renvoie une erreur
|
|
|
|
if( !$check_unique )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* [2] Modification de l'utilisateur
|
|
|
|
=========================================================*/
|
|
|
|
$edit_user = Database::getPDO()->prepare("UPDATE user
|
|
|
|
SET code = :code,
|
|
|
|
username = :username,
|
|
|
|
firstname = :firstname,
|
|
|
|
lastname = :lastname,
|
|
|
|
mail = :mail,
|
|
|
|
password = :password,
|
|
|
|
status = :status
|
|
|
|
WHERE id_user = :id_user");
|
|
|
|
$edit_user->execute(array(
|
|
|
|
':code' => $code,
|
|
|
|
':username' => $username,
|
|
|
|
':firstname' => $firstname,
|
|
|
|
':lastname' => $lastname,
|
|
|
|
':mail' => $mail,
|
|
|
|
':password' => $password,
|
|
|
|
':status' => $status,
|
|
|
|
':id_user' => $id_user
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* [3] On retourne si tout a ete modifie, si erreur
|
|
|
|
=========================================================*/
|
|
|
|
$check_user = self::getById($id_user);
|
|
|
|
|
|
|
|
$checker = $check_user['id_user'] == $id_user;
|
|
|
|
$checker = $checker && $check_user['code'] == $code;
|
|
|
|
$checker = $checker && $check_user['username'] == $username;
|
|
|
|
$checker = $checker && $check_user['firstname'] == $firstname;
|
|
|
|
$checker = $checker && $check_user['lastname'] == $lastname;
|
|
|
|
$checker = $checker && $check_user['mail'] == $mail;
|
|
|
|
$checker = $checker && $check_user['password'] == $password;
|
|
|
|
$checker = $checker && $check_user['status'] == $status;
|
|
|
|
|
|
|
|
// On retourne l'etat de la modification
|
|
|
|
return $checker;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* SUPPRIME UN UTILISATEUR DONNE
|
|
|
|
*
|
|
|
|
* @id_user<int> UID de l'utilisateur en question
|
|
|
|
*
|
|
|
|
* @return status<Boolean> Retourne si oui ou non l'utilisateur a bien ete supprime
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public static function delete($id_user){
|
|
|
|
/* [1] On redige/execute la requete
|
|
|
|
=========================================================*/
|
|
|
|
$delete_user = Database::getPDO()->prepare("DELETE FROM user WHERE id_user = :id_user");
|
|
|
|
$delete_user->execute(array(
|
|
|
|
':id_user' => $id_user
|
|
|
|
));
|
|
|
|
|
|
|
|
|
|
|
|
/* [2] On verifie que l'utilisateur n'existe plus
|
|
|
|
=========================================================*/
|
|
|
|
return ( self::getById($id_user) === false );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
?>
|