189 lines
6.1 KiB
PHP
189 lines
6.1 KiB
PHP
<?php
|
|
|
|
namespace manager\repo;
|
|
use \manager\Database;
|
|
use \manager\sessionManager;
|
|
|
|
class subject extends parentRepo{
|
|
|
|
protected static function table_name(){ static $table_name = 'Personnes'; return $table_name; }
|
|
|
|
|
|
|
|
|
|
/* 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
|
|
*
|
|
* @username<String> Pseudo du sujet
|
|
* @firstname<String> Prénom du sujet
|
|
* @lastname<String> Nom du sujet
|
|
* @id_facebook<int> Id facebook du sujet (optionnel)
|
|
* @number<String> Numéro de téléphone du sujet (optionnel)
|
|
*
|
|
* @return id_subject<int> Renvoie l'id du sujet cree ou FALSE si erreur
|
|
*
|
|
*/
|
|
public static function create($username, $firstname, $lastname, $id_facebook, $number){
|
|
/* [0] Verification et formattage des INPUT
|
|
=========================================================*/
|
|
$checkInput = Database::check('varchar(0,30)', $username);
|
|
$checkInput = $checkInput && Database::check('varchar(0,30)', $firstname);
|
|
$checkInput = $checkInput && Database::check('varchar(0,30)', $lastname);
|
|
$checkInput = $checkInput && !!strlen($username.$firstname.$lastname); // Pseudo, prénom, ou nom, au moins un n'est pas vide
|
|
$checkInput = $checkInput && ( Database::check('id', $id_facebook) || is_null($id_facebook) );
|
|
$checkInput = $checkInput && ( Database::check('number', $number) || is_null($number) );
|
|
|
|
// Si erreur en entree, on retourne FAUX
|
|
if( !$checkInput ) return false;
|
|
|
|
/* [1] On écrit la requête
|
|
=========================================================*/
|
|
$request_create = 'INSERT INTO Personnes(idPersonne, pseudo, nom, prenom, id_facebook, telephone) ';
|
|
$request_create .= 'VALUES (';
|
|
$request_create .= 'DEFAULT, '; // idPersone
|
|
$request_create .= strlen($username) ? ':pseudo, ' : 'NULL, '; // pseudo
|
|
$request_create .= strlen($lastname) ? ':nom, ' : 'NULL, '; // nom
|
|
$request_create .= strlen($firstname) ? ':prenom, ' : 'NULL, '; // prenom
|
|
$request_create .= !is_null($id_facebook) ? ':id_facebook, ' : 'NULL, '; // Id facebook
|
|
$request_create .= !is_null($number) ? ':number, ' : 'NULL, '; // Numéro de tél
|
|
$request_create .= ')';
|
|
|
|
/* (1) Si id_facebook 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 ));
|
|
}
|
|
|
|
}
|
|
|
|
|
|
?>
|