193 lines
5.6 KiB
PHP
193 lines
5.6 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace manager\repo;
|
||
|
use \manager\Database;
|
||
|
use \manager\sessionManager;
|
||
|
|
||
|
class subject{
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
/* RETOURNE LES DONNEES D'UN SUJET D'ID DONNE
|
||
|
*
|
||
|
* @id_subject<int> UID du sujet en question
|
||
|
*
|
||
|
* @return subject<Array> Tableau contenant les donnees du sujet s'il existe sinon retourne FALSE
|
||
|
*
|
||
|
*/
|
||
|
public static function getById($id_subject){
|
||
|
/* [1] On effectue la requete
|
||
|
=========================================================*/
|
||
|
$getSubject = Database::getPDO()->prepare("SELECT idPersonne, pseudo, prenom, nom, id_facebook, telephone
|
||
|
FROM Personnes
|
||
|
WHERE idPersonne = :id_subject");
|
||
|
$getSubject->execute(array( ':id_subject' => $id_subject ));
|
||
|
|
||
|
|
||
|
/* [2] On recupere le resultat de la requete
|
||
|
=========================================================*/
|
||
|
$subjectData = $getSubject->fetch();
|
||
|
|
||
|
// Si l'utilisateur n'existe pas, on retourne FALSE
|
||
|
if( $subjectData === false ) return false;
|
||
|
|
||
|
/* [3] Gestion du retour
|
||
|
=========================================================*/
|
||
|
return Database::delNumeric( $subjectData, true );
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
/* RETOURNE LES DONNEES DE TOUS LES SUJETS
|
||
|
*
|
||
|
* @return subjects<Array> Tableau contenant les donnees des sujets, retourne FALSE si erreur
|
||
|
*
|
||
|
*/
|
||
|
public static function getAll(){
|
||
|
/* [1] On effectue la requete
|
||
|
=========================================================*/
|
||
|
$getSubjects = Database::getPDO()->query("SELECT idPersonne, pseudo, prenom, nom, id_facebook, telephone
|
||
|
FROM Personnes
|
||
|
ORDER BY idPersonne ASC");
|
||
|
|
||
|
|
||
|
/* [2] On recupere le resultat de la requete
|
||
|
=========================================================*/
|
||
|
$subjectsData = $getSubjects->fetchAll();
|
||
|
|
||
|
// Si l'utilisateur n'existe pas, on retourne FALSE
|
||
|
if( $subjectsData === false ) return false;
|
||
|
|
||
|
/* [3] Gestion du retour
|
||
|
=========================================================*/
|
||
|
return Database::delNumeric( $subjectsData );
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
/* CREE UN SUJET
|
||
|
*
|
||
|
* @login<String> Login (identifiant) du sujet
|
||
|
* @password<String> Password du sujet
|
||
|
* @mail<String> Adresse mail du sujet
|
||
|
* @reference<int> Reference vers une Personne (sinon NULL)
|
||
|
* @permissions<Array> Liste des permissions du sujet
|
||
|
*
|
||
|
* @return id_subject<int> Renvoie l'id du sujet cree ou FALSE si erreur
|
||
|
*
|
||
|
*/
|
||
|
public static function create($login, $password, $mail, $reference, $permission){
|
||
|
/* [0] Verification et formattage des INPUT
|
||
|
=========================================================*/
|
||
|
$checkInput = Database::check('sha1', $password);
|
||
|
$checkInput = $checkInput && ( Database::check('id', $reference) || $reference === null );
|
||
|
$checkInput = $checkInput && in_array($permission, array('admin', 'subject'));
|
||
|
|
||
|
// Si erreur en entree, on retourne FAUX
|
||
|
if( !$checkInput ) return false;
|
||
|
|
||
|
|
||
|
/* [1] On verifie que le login/mail et reference sont uniques
|
||
|
=========================================================*/
|
||
|
$checkUnique = Database::getPDO()->prepare("SELECT id_subject
|
||
|
FROM subjects
|
||
|
WHERE login = :login
|
||
|
OR mail = :mail
|
||
|
OR ( reference = :reference AND reference is not NULL )");
|
||
|
$checkUnique->execute(array(
|
||
|
':login' => $login,
|
||
|
':mail' => $mail,
|
||
|
':reference' => $reference
|
||
|
));
|
||
|
$unique = $checkUnique->fetch() === false;
|
||
|
|
||
|
// Si un sujet a le meme LOGIN/MAIL ou REFERENCE, on retourne une erreur
|
||
|
if( !$unique ) return false;
|
||
|
|
||
|
|
||
|
/* [2] Creation du sujet
|
||
|
=========================================================*/
|
||
|
/* (1) Si reference NULL */
|
||
|
if( $reference == null ){
|
||
|
|
||
|
$create = Database::getPDO()->prepare("INSERT INTO subjects(id_subject, login, password, mail, reference, permission)
|
||
|
VALUES(DEFAULT, :login, :password, :mail, NULL, :permission)");
|
||
|
$create->execute(array(
|
||
|
':login' => $login,
|
||
|
':password' => $password,
|
||
|
':mail' => $mail,
|
||
|
':permission' => $permission
|
||
|
));
|
||
|
|
||
|
/* (2) Si reference est defini */
|
||
|
}else{
|
||
|
|
||
|
$create = Database::getPDO()->prepare("INSERT INTO subjects(id_subject, login, password, mail, reference, permission)
|
||
|
VALUES(DEFAULT, :login, :password, :mail, :reference, :permission)");
|
||
|
$create->execute(array(
|
||
|
':login' => $login,
|
||
|
':password' => $password,
|
||
|
':mail' => $mail,
|
||
|
':reference' => (int) $reference,
|
||
|
':permission' => $permission
|
||
|
));
|
||
|
|
||
|
}
|
||
|
|
||
|
/* [3] Verification de la creation + recuperation id
|
||
|
=========================================================*/
|
||
|
$checkCreate = Database::getPDO()->prepare("SELECT id_subject
|
||
|
FROM subjects
|
||
|
WHERE login = :login
|
||
|
AND password = :password
|
||
|
AND mail = :mail
|
||
|
AND ( reference = :reference OR reference is NULL )
|
||
|
AND permission = :permission");
|
||
|
$checkCreate->execute(array(
|
||
|
':login' => $login,
|
||
|
':password' => $password,
|
||
|
':mail' => $mail,
|
||
|
':reference' => (int) $reference,
|
||
|
':permission' => $permission
|
||
|
));
|
||
|
|
||
|
// On recupere l'id du sujet
|
||
|
$id_subject = $checkCreate->fetch();
|
||
|
|
||
|
// Si erreur, on retourne FALSE
|
||
|
if( $id_subject === false ) return false;
|
||
|
|
||
|
/* [4] Gestion du retour
|
||
|
=========================================================*/
|
||
|
return $id_subject['id_subject'];
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
/* SUPRESSION D'UN SUJET D'ID DONNE
|
||
|
*
|
||
|
* @id_subject<int> UID du sujet en question
|
||
|
*
|
||
|
*/
|
||
|
public static function remove($id_subject){
|
||
|
/* [1] On effectue la suppression
|
||
|
=========================================================*/
|
||
|
$getSubject = Database::getPDO()->prepare("DELETE FROM subjects WHERE id_subject = :id_subject");
|
||
|
$getSubject->execute(array( ':id_subject' => $id_subject ));
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
|
||
|
?>
|