2
0

event.class.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. <?php
  2. class event
  3. {
  4. public static function getFiche(int $_id)
  5. {
  6. db::query("SELECT * FROM " . DB_T_EVENTS . " WHERE id = :id");
  7. db::bind(':id', $_id);
  8. return db::single();
  9. }
  10. public static function getFicheByMd5(string $_md5)
  11. {
  12. db::query("SELECT * FROM " . DB_T_EVENTS . " WHERE md5 = :md5");
  13. db::bind(':md5', $_md5);
  14. return db::single();
  15. }
  16. public static function getQRCode(int $_id, $_width = 50, string $_link = NULL)
  17. {
  18. db::query("SELECT md5 FROM " . DB_T_EVENTS . " WHERE id = :id");
  19. db::bind(':id', $_id);
  20. $md5 = db::single()["md5"];
  21. $link = core::base64_url_encode("https://" . DOMAIN_EVENTS . "/?e=" . $md5);
  22. if ($_link == NULL) {
  23. echo '<img src="/qrcode.php?q= ' . $link . '" width="' . $_width . '" >';
  24. } else {
  25. echo '<a href="https://' . DOMAIN_EVENTS . '/qrcode.php?q=' . $link . '" target="_blank"><img src="/qrcode.php?q= ' . $link . '" width="' . $_width . '" ></a>';
  26. }
  27. }
  28. public static function getEvents()
  29. {
  30. db::query("SELECT "
  31. . "" . DB_T_EVENTS . ".id, "
  32. . "" . DB_T_EVENTS . ".md5, "
  33. . "" . DB_T_EVENTS . ".titre, "
  34. . "" . DB_T_EVENTS . ".description, "
  35. . "" . DB_T_EVENTS . ".startDate, "
  36. . "COUNT(" . DB_T_EVENTS_INSCRITS . ".id_salarie) AS m_global, "
  37. . "COUNT(" . DB_T_EVENTS_INSCRITS . ".present) AS m_inscrit, "
  38. . "" . DB_T_EVENTS . ".endDate, "
  39. . "IF(" . DB_T_EVENTS . ".type_emargement = 1, 'Contôle', IF(" . DB_T_EVENTS . ".type_emargement = 2, 'Autonome', 'Contrôle & Autonome')) AS type_emargement, "
  40. . "IF(" . DB_T_EVENTS . ".type_inscription = 1, 'Incription', 'Libre') AS type_inscription, "
  41. . "" . DB_T_EVENTS . ".actif, "
  42. . "" . DB_T_EVENTS . ".cree, "
  43. . "" . DB_T_EVENTS . ".maj, "
  44. . "CONCAT (" . DB_T_USER . ".prenom, ' ', " . DB_T_USER . ".nom) AS 'user' "
  45. . "FROM " . DB_T_EVENTS . " "
  46. . "LEFT JOIN " . DB_T_EVENTS_INSCRITS . " ON " . DB_T_EVENTS_INSCRITS . ".id_evenement = " . DB_T_EVENTS . ".id "
  47. . "INNER JOIN " . DB_T_USER . " ON " . DB_T_EVENTS . ".id_user = " . DB_T_USER . ".id "
  48. . "GROUP BY 1");
  49. return db::resultset();
  50. }
  51. public static function connectSalarie(array $_post)
  52. {
  53. if (empty($_post["ident"])) {
  54. return FALSE;
  55. } elseif (empty($_post["date"])) {
  56. return FALSE;
  57. }
  58. if (is_int($_post["ident"])) {
  59. $login = intval($_post["ident"]);
  60. } else {
  61. $login = $_post["ident"];
  62. }
  63. $date = new DateTime($_post["date"]);
  64. $date = $date->format('Y-m-d H:i:s');
  65. db::query("SELECT "
  66. . "id, "
  67. . "sel, "
  68. . "nom, "
  69. . "prenom, "
  70. . "actif, "
  71. . "contrat, "
  72. . "jourEntree "
  73. . "FROM " . DB_T_SALARIES . " "
  74. . "WHERE loginId = :login OR idLocal = :login");
  75. db::bind(':login', $login);
  76. $salarie = db::single();
  77. if (isset($salarie["id"])) {
  78. if ($salarie["actif"] == 0) {
  79. return FALSE;
  80. } elseif ($salarie["jourEntree"] != $date) {
  81. return FALSE;
  82. } else {
  83. $_SESSION["salarie"] = array(
  84. "id" => $salarie["id"],
  85. "sel" => $salarie["sel"],
  86. "nom" => $salarie["nom"],
  87. "prenom" => $salarie["prenom"]
  88. );
  89. return TRUE;
  90. }
  91. } else {
  92. return FALSE;
  93. }
  94. return FALSE;
  95. }
  96. public static function lastEvenement()
  97. {
  98. db::query("SELECT MAX(id) AS id FROM " . DB_T_EVENTS);
  99. return db::single()["id"];
  100. }
  101. public static function getIdEvenementByMd5(string $_md5)
  102. {
  103. db::query("SELECT id FROM " . DB_T_EVENTS . " WHERE md5 = :md5");
  104. db::bind(':md5', $_md5);
  105. return db::single()["id"];
  106. }
  107. public static function presentEvenement(int $_idSalarie)
  108. {
  109. db::query("SELECT "
  110. . "" . DB_T_EVENTS . ".id, "
  111. . "" . DB_T_EVENTS . ".md5, "
  112. . "" . DB_T_EVENTS . ".titre, "
  113. . "" . DB_T_EVENTS . ".startDate, "
  114. . "" . DB_T_EVENTS . ".endDate, "
  115. . "" . DB_T_EVENTS . ".type_emargement, "
  116. . "" . DB_T_EVENTS_INSCRITS . ".sel, "
  117. . "" . DB_T_EVENTS_INSCRITS . ".code, "
  118. . "" . DB_T_EVENTS_INSCRITS . ".present "
  119. . "FROM " . DB_T_EVENTS . " "
  120. . "LEFT JOIN " . DB_T_EVENTS_INSCRITS . " ON " . DB_T_EVENTS_INSCRITS . ".id_evenement = " . DB_T_EVENTS . ".id AND " . DB_T_EVENTS_INSCRITS . ".id_salarie = :id_salarie "
  121. . "WHERE " . DB_T_EVENTS . ".startDate <= CURRENT_TIMESTAMP() "
  122. . "AND " . DB_T_EVENTS . ".endDate >= CURRENT_TIMESTAMP() "
  123. . "AND ((" . DB_T_EVENTS . ".type_inscription = 1 AND " . DB_T_EVENTS_INSCRITS . ".id_salarie = :id_salarie) OR (" . DB_T_EVENTS . ".type_inscription = 0))"
  124. . "AND " . DB_T_EVENTS . ".actif = 1");
  125. db::bind(':id_salarie', $_idSalarie);
  126. $row = db::resultset();
  127. return $row;
  128. }
  129. public static function deleteEvenement(int $_id)
  130. {
  131. db::query("DELETE FROM " . DB_T_EVENTS . " WHERE id = :id");
  132. db::bind(':id', $_id);
  133. return db::execute();
  134. }
  135. public static function deleteInscriptionEvenement(int $_idEvenement)
  136. {
  137. db::query("DELETE FROM " . DB_T_EVENTS_INSCRITS . " WHERE id_evenement = :id_evenement");
  138. db::bind(':id_evenement', $_idEvenement);
  139. return db::execute();
  140. }
  141. public static function checkUrl(string $_url)
  142. {
  143. return parse_url($_url)["host"];
  144. }
  145. public static function getEvenementbyQRCode(string $_QRCode)
  146. {
  147. $query = parse_url($_QRCode)["query"];
  148. parse_str($query, $params);
  149. return $params;
  150. }
  151. public static function checkEmargement(int $_id_evenement)
  152. {
  153. db::query("SELECT "
  154. . "COUNT(present) AS nb "
  155. . "FROM " . DB_T_EVENTS_INSCRITS . " "
  156. . "WHERE " . DB_T_EVENTS_INSCRITS . ".id_evenement = :id_evenement");
  157. db::bind(':id_evenement', $_id_evenement);
  158. $nb = db::single()["nb"];
  159. return $nb;
  160. }
  161. public static function getInscriptionEvenementById(int $_id_evenement, int $_id_salarie)
  162. {
  163. db::query("SELECT "
  164. . "" . DB_T_EVENTS_INSCRITS . ".id_salarie, "
  165. . "" . DB_T_EVENTS_INSCRITS . ".present, "
  166. . "" . DB_T_EVENTS . ".titre "
  167. . "FROM " . DB_T_EVENTS_INSCRITS . " "
  168. . "INNER JOIN " . DB_T_EVENTS . " ON " . DB_T_EVENTS_INSCRITS . ".id_evenement = " . DB_T_EVENTS . ".id "
  169. . "WHERE " . DB_T_EVENTS_INSCRITS . ".id_salarie = :id_salarie AND " . DB_T_EVENTS_INSCRITS . ".id_evenement = :id_evenement");
  170. db::bind(':id_salarie', $_id_salarie);
  171. db::bind(':id_evenement', $_id_evenement);
  172. $inscription = db::single();
  173. return $inscription;
  174. }
  175. public static function getInscription(int $_id_event){
  176. db::query("SELECT "
  177. . "" . DB_T_SALARIES . ".id, "
  178. . "" . DB_T_SALARIES . ".loginId, "
  179. . "" . DB_T_SALARIES . ".nom, "
  180. . "" . DB_T_SALARIES . ".prenom, "
  181. . "" . DB_T_SALARIES . ".sexe, "
  182. . "" . DB_T_SALARIES . ".contrat, "
  183. . "" . DB_T_SALARIES . ".lieu, "
  184. . "" . DB_T_SALARIES . ".actif, "
  185. . "" . DB_T_EVENTS_INSCRITS . ".id_evenement, "
  186. . "" . DB_T_EVENTS_INSCRITS . ".present "
  187. . "FROM " . DB_T_SALARIES . " "
  188. . "INNER JOIN " . DB_T_EVENTS_INSCRITS . " ON " . DB_T_EVENTS_INSCRITS . ".id_salarie = " . DB_T_SALARIES . ".id "
  189. . "WHERE " . DB_T_EVENTS_INSCRITS . ".id_evenement = " . $_id_event);
  190. return db::resultset();
  191. }
  192. public static function checkEvenementBySalarie(string $_md5)
  193. {
  194. $return = array();
  195. $now = time();
  196. db::query("SELECT "
  197. . "" . DB_T_SALARIES . ".id, "
  198. . "" . DB_T_SALARIES . ".actif, "
  199. . "" . DB_T_SALARIES . ".contrat "
  200. . "FROM " . DB_T_SALARIES . " "
  201. . "WHERE " . DB_T_SALARIES . ".id = :id");
  202. db::bind(':id', session::getId("salarie"));
  203. $salarie = db::single();
  204. db::query("SELECT "
  205. . "" . DB_T_EVENTS_INSCRITS . ".id_salarie, "
  206. . "" . DB_T_EVENTS_INSCRITS . ".present "
  207. . "FROM " . DB_T_EVENTS . " "
  208. . "LEFT JOIN " . DB_T_EVENTS_INSCRITS . " ON " . DB_T_EVENTS_INSCRITS . ".id_evenement = " . DB_T_EVENTS . ".id "
  209. . "WHERE " . DB_T_EVENTS_INSCRITS . ".id_salarie = :id_salarie AND " . DB_T_EVENTS . ".md5 = :md5");
  210. db::bind(':id_salarie', session::getId("salarie"));
  211. db::bind(':md5', $_md5);
  212. $inscription = db::single();
  213. db::query("SELECT "
  214. . "" . DB_T_EVENTS . ".id, "
  215. . "" . DB_T_EVENTS . ".md5, "
  216. . "" . DB_T_EVENTS . ".titre, "
  217. . "" . DB_T_EVENTS . ".startDate, "
  218. . "" . DB_T_EVENTS . ".endDate, "
  219. . "" . DB_T_EVENTS . ".type_emargement, "
  220. . "" . DB_T_EVENTS . ".type_inscription, "
  221. . "" . DB_T_EVENTS . ".actif "
  222. . "FROM " . DB_T_EVENTS . " "
  223. . "WHERE " . DB_T_EVENTS . ".md5 = :md5");
  224. db::bind(':md5', $_md5);
  225. $evenement = db::single();
  226. // print_r($inscription); echo "<br />";
  227. // print_r($evenement); echo "<br />";
  228. if ($salarie["id"] == NULL) {
  229. $return["result"] = FALSE;
  230. $return["description"] = "Vous n'êtes pas un salarié rattaché à notre CSE";
  231. } elseif ($salarie["actif"] == 0) {
  232. $return["result"] = FALSE;
  233. $return["description"] = "Vous n'êtes plus un salarié rattaché à notre CSE";
  234. } elseif ($evenement["actif"] == 0) {
  235. $return["result"] = FALSE;
  236. $return["description"] = "Cet évènement n'est pas activée";
  237. } elseif ($evenement["type_inscription"] == 1 and $inscription["id_salarie"] == NULL) {
  238. $return["result"] = FALSE;
  239. $return["description"] = "Vous n'êtes pas inscrit à cet évènement";
  240. } elseif ($now < strtotime($evenement["startDate"])) {
  241. $return["result"] = FALSE;
  242. $return["description"] = "Cet évènement n'a pas encore commencé";
  243. } elseif ($now > (strtotime($evenement["endDate"]) + EMEMARGEMENT_END)) {
  244. $return["result"] = FALSE;
  245. $return["description"] = "Cet évènement est terminé";
  246. } elseif (isset($inscription["present"])) {
  247. $return["result"] = FALSE;
  248. $return["description"] = "Votre émargement a déjà été pris en compte<br />le " . core::convertDate($inscription["present"]);
  249. } else {
  250. $return["result"] = TRUE;
  251. $return["description"] = "Votre émargement a été pris en compte";
  252. }
  253. $return["evenement"] = $evenement["titre"];
  254. return $return;
  255. }
  256. public static function emargementEvenement(int $_idEvenement, int $_idSalarie)
  257. {
  258. $inscription = self::getInscriptionEvenementById($_idEvenement, $_idSalarie);
  259. $evenement = self::getFiche($_idEvenement);
  260. if (!isset($inscription["present"]) or empty($inscription["present"])) {
  261. if (isset($inscription["id_salarie"])) {
  262. db::query("UPDATE " . DB_T_EVENTS_INSCRITS . " SET present = CURRENT_TIMESTAMP() WHERE id_evenement = :id_evenement AND id_salarie = :id_salarie");
  263. db::bind(':id_evenement', $_idEvenement);
  264. db::bind(':id_salarie', $_idSalarie);
  265. db::execute();
  266. $return["result"] = TRUE;
  267. $return["evenement"] = $evenement["titre"];
  268. $return["description"] = "L'émargement a été pris en compte";
  269. } else {
  270. db::query("INSERT INTO " . DB_T_EVENTS_INSCRITS . " (id_evenement, id_salarie, present) "
  271. . "VALUES (:id_evenement, :id_salarie, CURRENT_TIMESTAMP())");
  272. db::bind(':id_evenement', $_idEvenement);
  273. db::bind(':id_salarie', $_idSalarie);
  274. db::execute();
  275. $return["result"] = TRUE;
  276. $return["evenement"] = $evenement["titre"];
  277. $return["description"] = "L'émargement a été pris en compte";
  278. }
  279. } else {
  280. $return["result"] = FALSE;
  281. $return["description"] = "Emargement déjà pris en compte le " . core::convertDate($inscription["present"]);
  282. }
  283. return $return;
  284. }
  285. public static function emargementEvenementByQRCode()
  286. {
  287. $idSalarie = core::getGet("s");
  288. $md5Evenement = core::getGet("e");
  289. $key = core::getGet("k");
  290. $salarie = salaries::get_salarieById($idSalarie);
  291. $constructKey = md5($md5Evenement . $salarie["sel"]);
  292. if ($constructKey == $key) {
  293. $checkEmarge = self::emargementEvenement(self::getIdEvenementByMd5($md5Evenement), $salarie["id"]);
  294. return $checkEmarge;
  295. } else {
  296. $return["result"] = FALSE;
  297. $return["description"] = "Le QR-Code n'est pas valide";
  298. return $return;
  299. }
  300. }
  301. public static function printEventBouton(array $_array)
  302. {
  303. switch ($_array["nb"]) {
  304. case 0:
  305. $class_fadeIn = "first";
  306. break;
  307. case 1:
  308. $class_fadeIn = "second";
  309. break;
  310. case 2:
  311. $class_fadeIn = "third";
  312. break;
  313. case 3:
  314. $class_fadeIn = "fourth";
  315. break;
  316. default:
  317. $class_fadeIn = "fifth";
  318. break;
  319. }
  320. if ($_array["type_emargement"] == 1) {
  321. $explication = '<p class="card-text">
  322. <div>[ QRCode à présenter à votre arrivée ]</div>
  323. <br />
  324. <div><span data-feather="maximize"></span></div>
  325. </p>';
  326. $meta = 'data-toggle="modal" data-target="#QRCodeModal" data-qrr-qrcode-regexp="^https?:\/\/" data-sel="' . $_array["url_qrcode"] . '" data-titre="' . $_array["titre"] . '"';
  327. $class_card = 'showQRCode';
  328. $txt["footer"] = "Cliquez ici pour afficher votre QRCode";
  329. } else {
  330. $explication = '<p class="card-text">
  331. <div>[ QRCode à flasher vous même ]</div>
  332. <br />
  333. <div><span data-feather="camera"></span></div>
  334. </p>';
  335. $meta = 'data-event="' . $_array["md5"] . '" data-qrr-target="#QRCodeRead" ';
  336. $class_card = 'qrcodeReader';
  337. $txt["footer"] = "Cliquez ici pour scanner le QRCode";
  338. }
  339. if (isset($_array["present"])) {
  340. $meta = "";
  341. $explication = '<p class="card-text">Votre émargement a bien été pris en compte</p>';
  342. $class["card-text"] = "text-success";
  343. $class["card"] = "";
  344. $txt["footer"] = "Emargement réalisé le " . core::convertDate($_array["present"]);
  345. } else {
  346. $class["card-text"] = "text-dark";
  347. $class["card"] = $class_card;
  348. }
  349. echo ' <div ' . $meta . ' class="card mb-3 text-center ' . $class["card"] . ' ' . $class["card-text"] . ' fadeIn ' . $class_fadeIn . '">
  350. <div class="card-header"><small>Du ' . core::convertDate($_array["startDate"]) . ' au ' . core::convertDate($_array["endDate"]) . '</small></div>
  351. <div class="card-body">
  352. <h5 class="card-title">' . $_array["titre"] . '</h5>
  353. ' . $explication . '
  354. </div>
  355. <div class="card-footer"><small>' . $txt["footer"] . '</small></div>
  356. </div>';
  357. }
  358. public static function printIconeResultFlash(string $_result)
  359. {
  360. if ($_result == 1) {
  361. echo ' <div class="swal2-icon swal2-success swal2-animate-success-icon" style="display: flex;">
  362. <div class="swal2-success-circular-line-left" style="background-color: rgb(255, 255, 255);"></div>
  363. <span class="swal2-success-line-tip"></span>
  364. <span class="swal2-success-line-long"></span>
  365. <div class="swal2-success-ring"></div>
  366. <div class="swal2-success-fix" style="background-color: rgb(255, 255, 255);"></div>
  367. <div class="swal2-success-circular-line-right" style="background-color: rgb(255, 255, 255);"></div>
  368. </div>';
  369. } else {
  370. echo ' <div class="swal2-icon swal2-error swal2-animate-error-icon" style="display: flex;">
  371. <span class="swal2-x-mark"><span class="swal2-x-mark-line-left"></span><span class="swal2-x-mark-line-right"></span></span>
  372. </div>';
  373. }
  374. }
  375. private static function uniqueCode(string $_string){
  376. db::query("SELECT "
  377. . "IF(" . DB_T_EVENTS_INSCRITS . ".id_salarie IS NOT NULL, 0, 1) AS exite"
  378. . "FROM " . DB_T_EVENTS_INSCRITS . " "
  379. . "WHERE " . DB_T_EVENTS_INSCRITS . ".code = " . $_string);
  380. return db::single()["exite"];
  381. }
  382. private static function generateCode(int $_nb){
  383. $string = "";
  384. $chaine = "AZERTYUPQSDFGHJKLMWXCVBN23456789";
  385. for ($i = 0; $i < $_nb; $i++) {
  386. $index = rand(0, strlen($chaine) - 1);
  387. $string .= $chaine[$index];
  388. }
  389. return $string;
  390. }
  391. public static function getGenerateCode(int $_nb = 8) // ALTER TABLE `evenement_salaries` ADD `code` VARCHAR(8) NULL AFTER `sel`; ALTER TABLE `evenement_salaries` ADD INDEX(`code`);
  392. {
  393. $tmp = self::generateCode($_nb);
  394. for($valide = 0; $valide == 1; $valide = self::uniqueCode($tmp)){
  395. if($valide == 1){
  396. break;
  397. } else {
  398. $tmp = self::generateCode($_nb);
  399. }
  400. }
  401. return $tmp;
  402. }
  403. }