alert.class.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. class alert
  3. {
  4. public static function recSuccess(string $_string)
  5. {
  6. if (empty($_SESSION["alert"]["success"])) {
  7. $_SESSION["alert"]["success"] = array();
  8. }
  9. array_push($_SESSION["alert"]["success"], $_string);
  10. }
  11. public static function recWarning(string $_string)
  12. {
  13. if (empty($_SESSION["alert"]["warning"])) {
  14. $_SESSION["alert"]["warning"] = array();
  15. }
  16. array_push($_SESSION["alert"]["warning"], $_string);
  17. }
  18. public static function recError(string $_string)
  19. {
  20. if (empty($_SESSION["alert"]["error"])) {
  21. $_SESSION["alert"]["error"] = array();
  22. }
  23. array_push($_SESSION["alert"]["error"], $_string);
  24. }
  25. public static function recTab(string $_string)
  26. {
  27. $_SESSION["alert"]["tab"] = $_string;
  28. }
  29. public static function getSuccess()
  30. {
  31. if (!empty($_SESSION["alert"]["success"])) {
  32. return $_SESSION["alert"]["success"];
  33. } else {
  34. return FALSE;
  35. }
  36. }
  37. public static function getWarning()
  38. {
  39. if (!empty($_SESSION["alert"]["warning"])) {
  40. return $_SESSION["alert"]["warning"];
  41. } else {
  42. return FALSE;
  43. }
  44. }
  45. public static function getError()
  46. {
  47. if (!empty($_SESSION["alert"]["error"])) {
  48. return $_SESSION["alert"]["error"];
  49. } else {
  50. return FALSE;
  51. }
  52. }
  53. public static function getTab()
  54. {
  55. return (isset($_SESSION["alert"]["tab"])) ? $_SESSION["alert"]["tab"] : FALSE;
  56. }
  57. public static function ifSuccess()
  58. {
  59. return (empty($_SESSION["alert"]["success"])) ? FALSE : TRUE;
  60. }
  61. public static function ifError()
  62. {
  63. return (empty($_SESSION["alert"]["error"])) ? FALSE : TRUE;
  64. }
  65. public static function ifTab()
  66. {
  67. return (empty($_SESSION["alert"]["tab"])) ? FALSE : TRUE;
  68. }
  69. public static function printAlert(?array $_array = NULL)
  70. {
  71. if($_array == NULL){
  72. return NULL;
  73. }
  74. $return = NULL;
  75. foreach ($_array as $value) {
  76. $return .= "<div>" . $value . "</div>";
  77. }
  78. return $return;
  79. }
  80. public static function recErrorInput(string $_string)
  81. {
  82. if (empty($_SESSION["alert"]["input"])) {
  83. $_SESSION["alert"]["input"] = array();
  84. }
  85. array_push($_SESSION["alert"]["input"], $_string);
  86. }
  87. public static function ifErrorInput(string $_string)
  88. {
  89. return (in_array($_string, $_SESSION["alert"]["input"])) ? TRUE : FALSE;
  90. }
  91. private static function printToast(string $_idAlert, string $_style, string $_icon, ?array $_texte = NULL)
  92. {
  93. $text = ($_texte != NULL) ? self::printAlert($_texte) : NULL;
  94. echo ' <div id="' . $_idAlert . '" class="toast hide" role="alert" aria-live="assertive" aria-atomic="true" data-delay="1500" style="margin-top:10px; ' . $_style . '">
  95. <div class="toast-header">' . $_icon . '</div><div class="toast-body" id="' . $_idAlert . 'Txt">' . $text . '</div>
  96. </div>';
  97. }
  98. public static function show()
  99. {
  100. historique::recordLogs();
  101. echo '<div class="position-fixed bottom-0 right-0 p-3" style="z-index: 5; right: 5px; bottom: 0;">';
  102. $idAlert = "printToastSuccess";
  103. $texte1 = (self::getSuccess()) ? self::getSuccess() : NULL;
  104. $style = 'background:#d4edda;';
  105. $icon = '<span data-feather="info" style="color:green;"></span> <strong style="color:green; margin-left:5px;" class="mr-auto">Succès</strong>';
  106. self::printToast($idAlert, $style, $icon, $texte1);
  107. $idAlert = "printToastWarning";
  108. $texte2 = (self::getWarning()) ? self::getWarning() : NULL;
  109. $style = 'background:#fff3cd;';
  110. $icon = '<span data-feather="alert-circle" style="color:orange;"></span> <strong style="color:orange; margin-left:5px;" class="mr-auto">Attention</strong>';
  111. self::printToast($idAlert, $style, $icon, $texte2);
  112. $idAlert = "printToastError";
  113. $texte3 = (self::getError()) ? self::getError() : NULL;
  114. $style = 'background:#f8d7da;';
  115. $icon = '<span data-feather="alert-triangle" style="color:red;"></span> <strong style="color:red; margin-left:5px;" class="mr-auto">Erreur</strong>';
  116. self::printToast($idAlert, $style, $icon, $texte3);
  117. echo '</div>';
  118. echo ($texte1 == NULL) ? '' : '<script>$("#printToastSuccess").toast("show");</script>';
  119. echo ($texte2 == NULL) ? '' : '<script>$("#printToastWarning").toast("show");</script>';
  120. echo ($texte3 == NULL) ? '' : '<script>$("#printToastError").toast("show");</script>';
  121. self::destroyAlert();
  122. }
  123. public static function destroyAlert()
  124. {
  125. unset($_SESSION["alert"]);
  126. }
  127. }