415 lines
12 KiB
PHP
Executable File
415 lines
12 KiB
PHP
Executable File
<?php
|
|
|
|
namespace manager\repo;
|
|
use \manager\Database;
|
|
use \manager\repo\cluster as clusterRepo;
|
|
|
|
class user extends parentRepo{
|
|
|
|
protected static function table_name(){ static $table_name = 'user'; return $table_name; }
|
|
|
|
/* CREATION D'UN UTILISATEUR ET DE SON GROUPE INDIVIDUEL
|
|
*
|
|
* @id_warehouse<int> UID de l'entrepot
|
|
* @code<String> Code RFID de l'utilisateur
|
|
* @username<String> Identifiant l'utilisateur
|
|
* @firstname<String> Prenom de l'utilisateur
|
|
* @lastcode<String> Nom de l'utilisateur
|
|
* @mail<String> Adresse email de l'utilisateur
|
|
* @password<String> Mot de passe de l'utilisateur
|
|
* @status<String> Status de l'utilisateur
|
|
*
|
|
* @return id_user<int> Renvoie l'UID de l'utilisateur cree
|
|
* Renvoie FALSE si une erreur occure
|
|
*
|
|
*/
|
|
public static function create($id_warehouse, $code, $username, $firstname, $lastname, $mail, $password, $status){
|
|
/* [1] Verification de l'unicite de code+username
|
|
=========================================================*/
|
|
$check_unique = !is_array(self::getByCode($id_warehouse, $code));
|
|
$check_unique = $check_unique && !is_array(self::getByUsername($id_warehouse, $username));
|
|
|
|
// Si un utilisateur a deja ce code ou cet username, on renvoie une erreur
|
|
if( !$check_unique )
|
|
return false;
|
|
|
|
/* [2] Creation de l'utilisateur
|
|
=========================================================*/
|
|
$insert_user = Database::getPDO()->prepare("INSERT INTO user(id_user, id_warehouse, code, username, firstname, lastname, mail, password, status)
|
|
VALUES(DEFAULT, :id_warehouse, :code, :username, :firstname, :lastname, :mail, :password, :status)");
|
|
$insert_user->execute([
|
|
':id_warehouse' => $id_warehouse,
|
|
':code' => $code,
|
|
':username' => $username,
|
|
':firstname' => $firstname,
|
|
':lastname' => $lastname,
|
|
':mail' => $mail,
|
|
':password' => $password,
|
|
':status' => $status
|
|
]);
|
|
|
|
/* [3] On retourne l'id_user ou FALSE si erreur
|
|
=========================================================*/
|
|
$check_user = self::getByCode($code);
|
|
|
|
// Si n'existe pas on retourne FALSE
|
|
if( $check_user === false )
|
|
return false;
|
|
|
|
// Sinon, on retourne son id
|
|
return $check_user['id_user'];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* RENVOIE UNE LISTE D'UTILISATEURS EN FONCTION D'UN MOT CLE
|
|
*
|
|
* @id_warehouse<int> UID de l'entrepot
|
|
* @keyword<String> Element de recherche
|
|
*
|
|
* @return users<Array> Retourne les utilisateurs trouves
|
|
*
|
|
*/
|
|
public static function search($id_warehouse, $keyword){
|
|
// On recupere les donnees
|
|
$searchusers = Database::getPDO()->prepare("SELECT * FROM user
|
|
WHERE id_warehouse = :id_warehouse
|
|
AND ( code LIKE '%".$keyword."%'
|
|
OR username LIKE '%".$keyword."%'
|
|
OR firstname LIKE '%".$keyword."%'
|
|
OR lastname LIKE '%".$keyword."%'
|
|
OR mail LIKE '%".$keyword."%'
|
|
)
|
|
");
|
|
|
|
$searchusers->execute([
|
|
':id_warehouse' => $id_warehouse
|
|
]);
|
|
|
|
return Database::delNumeric( $searchusers->fetchAll() );
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* RENVOIE LES GROUPES AUQUEL APPARTIENT UN UTILISATEUR DONNE
|
|
*
|
|
* @id_warehouse<int> UID de l'entrepot
|
|
* @id_user<int> UID de l'utilisateur en question
|
|
*
|
|
* @return clusters<Array> Retourne la liste des groupes auquel appartient l'utilisateur
|
|
*
|
|
*/
|
|
public static function getClusters($id_warehouse, $id_user){
|
|
/* [1] On redige/execute la requete
|
|
=========================================================*/
|
|
$get_clusters = Database::getPDO()->prepare("SELECT c.*
|
|
FROM cluster as c, cluster_merge as cm, user as u
|
|
WHERE cm.id_cluster = c.id_cluster
|
|
AND cm.id_entity = u.id_user
|
|
AND cm.id_entity = :id_user
|
|
AND cm.class = :class
|
|
AND u.id_warehouse = :id_warehouse
|
|
ORDER BY c.id_cluster");
|
|
$get_clusters->execute([
|
|
':id_warehouse' => $id_warehouse,
|
|
':id_user' => $id_user,
|
|
':class' => clusterRepo::USER_CLASS
|
|
]);
|
|
|
|
/* [2] On retourne la liste des groupes
|
|
=========================================================*/
|
|
return Database::delNumeric( $get_clusters->fetchAll() );
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* MODIFICATION D'UN UTILISATEUR
|
|
*
|
|
* @id_warehouse<int> UID de l'entrepot
|
|
* @id_user<int> UID de l'utilisateur
|
|
* @code<String> Code RFID de l'utilisateur
|
|
* @username<String> Identifiant l'utilisateur
|
|
* @firstname<String> Prenom de l'utilisateur
|
|
* @lastcode<String> Nom de l'utilisateur
|
|
* @mail<String> Adresse email de l'utilisateur
|
|
* @password<String> Mot de passe de l'utilisateur
|
|
* @status<String> Status de l'utilisateur
|
|
*
|
|
* @return status<Boolean> Renvoie si oui ou non tout s'est bien passe
|
|
*
|
|
*/
|
|
public static function edit($id_warehouse, $id_user=null, $code=null, $username=null, $firstname=null, $lastname=null, $mail=null, $password=null, $status=null){
|
|
/* [1] Verification de l'unicite de code+username (si differents)
|
|
=========================================================*/
|
|
// On recupere les utilisateurs ayant le meme code ou username (si existent)
|
|
// pour éviter les collisions (car code/username doivent être uniquent)
|
|
$getbycode = self::getByCode($id_warehouse, $code);
|
|
$getbyusername = self::getByUsername($id_warehouse, $username);
|
|
|
|
$check_unique = is_array($getbycode) && $getbycode['id_user'] == $id_user || !is_array($getbycode);
|
|
$check_unique = $check_unique && is_array($getbyusername) && $getbyusername['id_user'] == $id_user || !is_array($getbyusername);
|
|
|
|
// 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
|
|
AND id_warehouse = :id_warehouse");
|
|
$edit_user->execute([
|
|
':code' => $code,
|
|
':username' => $username,
|
|
':firstname' => $firstname,
|
|
':lastname' => $lastname,
|
|
':mail' => $mail,
|
|
':password' => $password,
|
|
':status' => $status,
|
|
':id_user' => $id_user,
|
|
':id_warehouse' => $id_warehouse
|
|
]);
|
|
|
|
|
|
|
|
/* [3] On retourne si tout a ete modifie, si erreur
|
|
=========================================================*/
|
|
$check_user = self::getById($id_warehouse, $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_warehouse<int> UID de l'entrepot
|
|
* @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_warehouse, $id_user){
|
|
/* [1] On redige/execute la requete
|
|
=========================================================*/
|
|
$delete_user = Database::getPDO()->prepare("DELETE FROM user
|
|
WHERE id_user = :id_user
|
|
AND id_warehouse = :id_warehouse");
|
|
$delete_user->execute([
|
|
':id_user' => $id_user,
|
|
':id_warehouse' => $id_warehouse
|
|
]);
|
|
|
|
|
|
/* [2] On verifie que l'utilisateur n'existe plus
|
|
=========================================================*/
|
|
return ( self::getById($id_user) === false );
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* RETOURNE UN UTILISATEUR SPECIFIQUE
|
|
*
|
|
* @id_warehouse<int> UID de l'entrepot
|
|
* @id_user<int> UID de l'utilisateur
|
|
*
|
|
* @return user<Array> Données de l'utilisateur
|
|
* FALSE si aucun résultat
|
|
*
|
|
*/
|
|
public static function getById($id_warehouse, $id_user){
|
|
/* [1] On rédige/execute la requête
|
|
=========================================================*/
|
|
$get = Database::getPDO()->prepare("SELECT u.id_user, u.code, u.username, u.firstname, u.lastname, u.mail, u.status
|
|
FROM user as u
|
|
WHERE u.id_warehouse = :id_warehouse
|
|
AND u.id_user = :id_user");
|
|
$get->execute([
|
|
':id_warehouse' => $id_warehouse,
|
|
':id_user' => $id_user
|
|
]);
|
|
|
|
/* [2] Gestion des données
|
|
=========================================================*/
|
|
$found = $get->fetch();
|
|
|
|
// Si aucun résultat
|
|
if( $found === false )
|
|
return false;
|
|
|
|
return Database::delNumeric( $found, true );
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* RETOURNE UN UTILISATEUR SPECIFIQUE
|
|
*
|
|
* @id_warehouse<int> UID de l'entrepot
|
|
* @code<String> Code RFID de l'utilisateur
|
|
*
|
|
* @return user<Array> Données de l'utilisateur
|
|
* FALSE si aucun résultat
|
|
*
|
|
*/
|
|
public static function getByCode($id_warehouse, $code){
|
|
/* [1] On rédige/execute la requête
|
|
=========================================================*/
|
|
$get = Database::getPDO()->prepare("SELECT u.id_user, u.code, u.username, u.firstname, u.lastname, u.mail, u.status
|
|
FROM user as u
|
|
WHERE u.id_warehouse = :id_warehouse
|
|
AND u.code = :code");
|
|
$get->execute([
|
|
':id_warehouse' => $id_warehouse,
|
|
':code' => $code
|
|
]);
|
|
|
|
/* [2] Gestion des données
|
|
=========================================================*/
|
|
$found = $get->fetch();
|
|
|
|
// Si aucun résultat
|
|
if( $found === false )
|
|
return false;
|
|
|
|
return Database::delNumeric( $found, true );
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* RETOURNE UN UTILISATEUR SPECIFIQUE
|
|
*
|
|
* @id_warehouse<int> UID de l'entrepot
|
|
* @username<String> Identifiant de l'utilisateur
|
|
*
|
|
* @return user<Array> Données de l'utilisateur
|
|
* FALSE si aucun résultat
|
|
*
|
|
*/
|
|
public static function getByUsername($id_warehouse, $username){
|
|
/* [1] On rédige/execute la requête
|
|
=========================================================*/
|
|
$get = Database::getPDO()->prepare("SELECT u.id_user, u.code, u.username, u.firstname, u.lastname, u.mail, u.status
|
|
FROM user as u
|
|
WHERE u.id_warehouse = :id_warehouse
|
|
AND u.username = :username");
|
|
$get->execute([
|
|
':id_warehouse' => $id_warehouse,
|
|
':username' => $username
|
|
]);
|
|
|
|
/* [2] Gestion des données
|
|
=========================================================*/
|
|
$found = $get->fetch();
|
|
|
|
// Si aucun résultat
|
|
if( $found === false )
|
|
return false;
|
|
|
|
return Database::delNumeric( $found, true );
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* RETOURNE TOUS LES UTILISATEURS DE L'ENTREPOT
|
|
*
|
|
* @id_warehouse<int> UID de l'entrepot
|
|
*
|
|
* @return users<Array> Données des utilisateurs
|
|
*
|
|
*/
|
|
public static function getAll($id_warehouse){
|
|
/* [1] On rédige/execute la requête
|
|
=========================================================*/
|
|
$get = Database::getPDO()->prepare("SELECT u.id_user, u.code, u.username, u.firstname, u.lastname, u.mail, u.status
|
|
FROM user as u
|
|
WHERE u.id_warehouse = :id_warehouse");
|
|
$get->execute([
|
|
':id_warehouse' => $id_warehouse
|
|
]);
|
|
|
|
/* [2] Gestion des données
|
|
=========================================================*/
|
|
return Database::delNumeric( $get->fetchAll() );
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
?>
|