user.class.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. class user {
  3. public function __construct() {
  4. }
  5. public static function getUserById(int $_id) {
  6. // Récupération des données de l'excel au format Json
  7. db::query("SELECT "
  8. . "" . DB_T_USER . ".id AS id, "
  9. . "" . DB_T_USER . ".email ,"
  10. . "" . DB_T_USER . ".prenom, "
  11. . "" . DB_T_USER . ".nom, "
  12. . "" . DB_T_USER . ".cree, "
  13. . "" . DB_T_USER . ".last_connect, "
  14. . "" . DB_T_USER . ".actif, "
  15. . "" . DB_T_USER . ".id_type, "
  16. . "" . DB_T_USER_TYPE . ".type "
  17. . "FROM " . DB_T_USER . " "
  18. . "INNER JOIN " . DB_T_USER_TYPE . " ON " . DB_T_USER . ".id_type = " . DB_T_USER_TYPE . ".id "
  19. . "WHERE " . DB_T_USER . ".id = :id");
  20. db::bind(':id', $_id);
  21. return db::single();
  22. }
  23. public static function getUsers() {
  24. // Récupération des données de l'excel au format Json
  25. db::query("SELECT "
  26. . "" . DB_T_USER . ".id AS id, "
  27. . "" . DB_T_USER . ".email ,"
  28. . "" . DB_T_USER . ".prenom, "
  29. . "" . DB_T_USER . ".nom, "
  30. . "" . DB_T_USER . ".cree, "
  31. . "" . DB_T_USER . ".last_connect, "
  32. . "" . DB_T_USER . ".actif, "
  33. . "" . DB_T_USER . ".id_type, "
  34. . "" . DB_T_USER_TYPE . ".type "
  35. . "FROM " . DB_T_USER . " "
  36. . "INNER JOIN " . DB_T_USER_TYPE . " ON " . DB_T_USER . ".id_type = " . DB_T_USER_TYPE . ".id");
  37. return db::resultset();
  38. }
  39. public static function connect(array $_input) {
  40. $return = NULL;
  41. if (isset($_input["email"]) AND isset($_input["password"])) {
  42. db::query("SELECT id, email, password, prenom, nom, id_type, actif FROM " . DB_T_USER . " WHERE email = :email");
  43. db::bind(':email', $_input["email"]);
  44. $row = db::single();
  45. if (isset($row["id"])) {
  46. if ($row["actif"] == 0) {
  47. alert::recError("Votre compte est désactivé");
  48. return FALSE;
  49. } elseif (isset($_input["password"]) AND md5($_input["password"]) == $row["password"]) {
  50. $_SESSION["user"] = array(
  51. "id" => $row["id"],
  52. "prenom" => $row["prenom"],
  53. "nom" => $row["nom"],
  54. "idType" => $row["id_type"],
  55. "email" => $row["email"],
  56. "actif" => $row["actif"]
  57. );
  58. self::updateLastConnect($row["id"]);
  59. return TRUE;
  60. } else {
  61. alert::recError("Erreur d'authentification");
  62. return FALSE;
  63. }
  64. } else {
  65. alert::recError("Erreur d'authentification");
  66. return FALSE;
  67. }
  68. } else {
  69. alert::recError("Erreur d'authentification");
  70. return FALSE;
  71. }
  72. }
  73. private static function updateLastConnect(int $_id){
  74. db::query("UPDATE " . DB_T_USER . " SET `last_connect` = CURRENT_TIMESTAMP() WHERE id = :id");
  75. db::bind(':id', $_id);
  76. db::execute();
  77. }
  78. public static function add_user(array $_input){
  79. db::query("INSERT INTO " . DB_T_USER . " "
  80. . "(email, password, prenom, nom, id_type, actif) "
  81. . "VALUES (:email, :password, :prenom, :nom, :id_type, :actif)");
  82. db::bind(':email', $_input["email"]);
  83. db::bind(':password', md5($_input["password"]));
  84. db::bind(':prenom', $_input["prenom"]);
  85. db::bind(':nom', $_input["nom"]);
  86. db::bind(':id_type', $_input["id_type"]);
  87. db::bind(':actif', $_input["actif"]);
  88. try {
  89. db::execute();
  90. alert::recSuccess("La création a bien été prise en compte");
  91. } catch (Exception $ex) {
  92. alert::recError("Erreur lors de la création de l'utilisateur");
  93. header("Location: /?p=user&id=add");
  94. exit();
  95. }
  96. }
  97. public static function lastUser(){
  98. db::query("SELECT MAX(id) AS id FROM ". DB_T_USER);
  99. return db::single()["id"];
  100. }
  101. public static function maj_user(array $_input){
  102. if($_input["password"] != ""){
  103. db::query("UPDATE " . DB_T_USER . " SET password = :password WHERE id = :id");
  104. db::bind(':password', md5($_input["password"]));
  105. db::bind(':id', $_input["id"]);
  106. try {
  107. db::execute();
  108. } catch (Exception $ex) {
  109. alert::recError("Erreur lors de la modification de l'utilisateur");
  110. header("Location: /?p=user&id=" . $_input["id"]);
  111. exit();
  112. }
  113. }
  114. db::query("UPDATE " . DB_T_USER . " SET email = :email, prenom = :prenom, nom = :nom, id_type = :id_type, actif = :actif WHERE id = :id");
  115. db::bind(':email', $_input["email"]);
  116. db::bind(':prenom', $_input["prenom"]);
  117. db::bind(':nom', $_input["nom"]);
  118. db::bind(':id_type', $_input["id_type"]);
  119. db::bind(':actif', $_input["actif"]);
  120. db::bind(':id', $_input["id"]);
  121. try {
  122. db::execute();
  123. alert::recSuccess("La modification a bien été prise en compte");
  124. } catch (Exception $ex) {
  125. alert::recError("Erreur lors de la modification de l'utilisateur");
  126. header("Location: /?p=user&id=" . $_input["id"]);
  127. exit();
  128. }
  129. }
  130. public static function deleteUser(int $_id){
  131. db::query("DELETE FROM ". DB_T_USER ." WHERE id = :id");
  132. db::bind(':id', $_id);
  133. return db::execute();
  134. }
  135. }