alert.class.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. <?php
  2. /**
  3. * Classe `alert`
  4. *
  5. * Cette classe gère les alertes dans le système. Elle permet de :
  6. * - Enregistrer des messages de succès, d'avertissement et d'erreur dans la session.
  7. * - Récupérer et afficher les messages enregistrés.
  8. * - Gérer les onglets actifs et les erreurs liées aux champs de formulaire.
  9. * - Afficher les alertes sous forme de toasts avec des styles personnalisés.
  10. */
  11. class alert
  12. {
  13. /**
  14. * Enregistre un message de succès dans la session.
  15. *
  16. * @param string $_string Le message de succès à enregistrer.
  17. * @return void
  18. */
  19. public static function recSuccess(string $_string)
  20. {
  21. if (empty($_SESSION["alert"]["success"])) {
  22. $_SESSION["alert"]["success"] = array();
  23. }
  24. array_push($_SESSION["alert"]["success"], $_string);
  25. }
  26. /**
  27. * Enregistre un message d'avertissement dans la session.
  28. *
  29. * @param string $_string Le message d'avertissement à enregistrer.
  30. * @return void
  31. */
  32. public static function recWarning(string $_string)
  33. {
  34. if (empty($_SESSION["alert"]["warning"])) {
  35. $_SESSION["alert"]["warning"] = array();
  36. }
  37. array_push($_SESSION["alert"]["warning"], $_string);
  38. }
  39. /**
  40. * Enregistre un message d'erreur dans la session.
  41. *
  42. * @param string $_string Le message d'erreur à enregistrer.
  43. * @return void
  44. */
  45. public static function recError(string $_string)
  46. {
  47. if (empty($_SESSION["alert"]["error"])) {
  48. $_SESSION["alert"]["error"] = array();
  49. }
  50. array_push($_SESSION["alert"]["error"], $_string);
  51. }
  52. /**
  53. * Enregistre un onglet actif dans la session.
  54. *
  55. * @param string $_string L'onglet actif à enregistrer.
  56. * @return void
  57. */
  58. public static function recTab(string $_string)
  59. {
  60. $_SESSION["alert"]["tab"] = $_string;
  61. }
  62. /**
  63. * Récupère les messages de succès enregistrés.
  64. *
  65. * @return array|bool Les messages de succès ou FALSE s'il n'y en a pas.
  66. */
  67. public static function getSuccess()
  68. {
  69. if (!empty($_SESSION["alert"]["success"])) {
  70. return $_SESSION["alert"]["success"];
  71. } else {
  72. return FALSE;
  73. }
  74. }
  75. /**
  76. * Récupère les messages d'avertissement enregistrés.
  77. *
  78. * @return array|bool Les messages d'avertissement ou FALSE s'il n'y en a pas.
  79. */
  80. public static function getWarning()
  81. {
  82. if (!empty($_SESSION["alert"]["warning"])) {
  83. return $_SESSION["alert"]["warning"];
  84. } else {
  85. return FALSE;
  86. }
  87. }
  88. /**
  89. * Récupère les messages d'erreur enregistrés.
  90. *
  91. * @return array|bool Les messages d'erreur ou FALSE s'il n'y en a pas.
  92. */
  93. public static function getError()
  94. {
  95. if (!empty($_SESSION["alert"]["error"])) {
  96. return $_SESSION["alert"]["error"];
  97. } else {
  98. return FALSE;
  99. }
  100. }
  101. /**
  102. * Récupère l'onglet actif enregistré.
  103. *
  104. * @return string|bool L'onglet actif ou FALSE s'il n'y en a pas.
  105. */
  106. public static function getTab()
  107. {
  108. return (isset($_SESSION["alert"]["tab"])) ? $_SESSION["alert"]["tab"] : FALSE;
  109. }
  110. /**
  111. * Vérifie s'il y a des messages de succès enregistrés.
  112. *
  113. * @return bool TRUE s'il y a des messages de succès, FALSE sinon.
  114. */
  115. public static function ifSuccess()
  116. {
  117. return (empty($_SESSION["alert"]["success"])) ? FALSE : TRUE;
  118. }
  119. /**
  120. * Vérifie s'il y a des messages d'erreur enregistrés.
  121. *
  122. * @return bool TRUE s'il y a des messages d'erreur, FALSE sinon.
  123. */
  124. public static function ifError()
  125. {
  126. return (empty($_SESSION["alert"]["error"])) ? FALSE : TRUE;
  127. }
  128. /**
  129. * Vérifie s'il y a un onglet actif enregistré.
  130. *
  131. * @return bool TRUE s'il y a un onglet actif, FALSE sinon.
  132. */
  133. public static function ifTab()
  134. {
  135. return (empty($_SESSION["alert"]["tab"])) ? FALSE : TRUE;
  136. }
  137. /**
  138. * Génère le HTML pour afficher une liste de messages.
  139. *
  140. * @param array|null $_array La liste des messages à afficher.
  141. * @return string|null Le HTML généré ou NULL si aucun message.
  142. */
  143. public static function printAlert(?array $_array = NULL)
  144. {
  145. if ($_array == NULL) {
  146. return NULL;
  147. }
  148. $return = NULL;
  149. foreach ($_array as $value) {
  150. $return .= "<div>" . $value . "</div>";
  151. }
  152. return $return;
  153. }
  154. /**
  155. * Enregistre un message d'erreur lié à un champ de formulaire.
  156. *
  157. * @param string $_string Le champ de formulaire en erreur.
  158. * @return void
  159. */
  160. public static function recErrorInput(string $_string)
  161. {
  162. if (empty($_SESSION["alert"]["input"])) {
  163. $_SESSION["alert"]["input"] = array();
  164. }
  165. array_push($_SESSION["alert"]["input"], $_string);
  166. }
  167. /**
  168. * Vérifie si un champ de formulaire est en erreur.
  169. *
  170. * @param string $_string Le champ de formulaire à vérifier.
  171. * @return bool TRUE si le champ est en erreur, FALSE sinon.
  172. */
  173. public static function ifErrorInput(string $_string)
  174. {
  175. return (in_array($_string, $_SESSION["alert"]["input"])) ? TRUE : FALSE;
  176. }
  177. /**
  178. * Affiche une alerte sous forme de toast.
  179. *
  180. * @param string $_idAlert L'ID de l'alerte.
  181. * @param string $_style Le style CSS de l'alerte.
  182. * @param string $_icon L'icône de l'alerte.
  183. * @param array|null $_texte Le texte de l'alerte.
  184. * @return void
  185. */
  186. private static function printToast(string $_idAlert, string $_style, string $_icon, ?array $_texte = NULL)
  187. {
  188. $text = ($_texte != NULL) ? self::printAlert($_texte) : NULL;
  189. echo ' <div id="' . $_idAlert . '" class="toast hide" role="alert" aria-live="assertive" aria-atomic="true" data-delay="' . TIME_TOAST_ALERT . '" style="margin-top:10px; ' . $_style . '">
  190. <div class="toast-header">' . $_icon . '</div><div class="toast-body" id="' . $_idAlert . 'Txt">' . $text . '</div>
  191. </div>';
  192. }
  193. /**
  194. * Affiche toutes les alertes enregistrées.
  195. *
  196. * @return void
  197. */
  198. public static function show()
  199. {
  200. historique::recordLogs();
  201. echo '<div class="position-fixed bottom-0 right-0 p-3" style="z-index: 5; right: 5px; bottom: 0;">';
  202. $idAlert = "printToastSuccess";
  203. $texte1 = (self::getSuccess()) ? self::getSuccess() : NULL;
  204. $style = 'background:#d4edda;';
  205. $icon = icon::getFont(["type" => "info", "color" => "green", "size" => "18px"]) . ' <strong style="color:green; margin-left:5px;" class="mr-auto">Succès</strong>';
  206. self::printToast($idAlert, $style, $icon, $texte1);
  207. $idAlert = "printToastWarning";
  208. $texte2 = (self::getWarning()) ? self::getWarning() : NULL;
  209. $style = 'background:#fff3cd;';
  210. $icon = icon::getFont(["type" => "warning", "color" => "orange", "size" => "18px"]) . ' <strong style="color:orange; margin-left:5px;" class="mr-auto">Attention</strong>';
  211. self::printToast($idAlert, $style, $icon, $texte2);
  212. $idAlert = "printToastError";
  213. $texte3 = (self::getError()) ? self::getError() : NULL;
  214. $style = 'background:#f8d7da;';
  215. $icon = icon::getFont(["type" => "alert", "color" => "red", "size" => "18px"]) . ' <strong style="color:red; margin-left:5px;" class="mr-auto">Erreur</strong>';
  216. self::printToast($idAlert, $style, $icon, $texte3);
  217. echo '</div>';
  218. echo ($texte1 == NULL) ? '' : '<script>$("#printToastSuccess").toast("show");</script>';
  219. echo ($texte2 == NULL) ? '' : '<script>$("#printToastWarning").toast("show");</script>';
  220. echo ($texte3 == NULL) ? '' : '<script>$("#printToastError").toast("show");</script>';
  221. self::destroyAlert();
  222. }
  223. /**
  224. * Supprime toutes les alertes enregistrées.
  225. *
  226. * @return void
  227. */
  228. public static function destroyAlert()
  229. {
  230. unset($_SESSION["alert"]);
  231. }
  232. }