2
0

alert.class.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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)
  70. {
  71. $return = NULL;
  72. foreach ($_array as $value) {
  73. $return .= "<div>" . $value . "</div>";
  74. }
  75. return $return;
  76. }
  77. public static function recErrorInput(string $_string)
  78. {
  79. if (empty($_SESSION["alert"]["input"])) {
  80. $_SESSION["alert"]["input"] = array();
  81. }
  82. array_push($_SESSION["alert"]["input"], $_string);
  83. }
  84. public static function ifErrorInput(string $_string)
  85. {
  86. return (in_array($_string, $_SESSION["alert"]["input"])) ? TRUE : FALSE;
  87. }
  88. private static function printToast(string $_style, string $_icon, array $_texte)
  89. {
  90. echo ' <div class="toast hide" role="alert" aria-live="assertive" aria-atomic="true" data-delay="1500" style="margin-top:10px; ' . $_style . '">
  91. <div class="toast-header">' . $_icon . '</div><div class="toast-body">' . self::printAlert($_texte) . '</div>
  92. </div>';
  93. }
  94. public static function show()
  95. {
  96. if (self::getSuccess() or self::getError()) {
  97. echo '<div aria-live="polite" aria-atomic="true" class="position-fixed bottom-0 right-0 p-3" style="z-index: 5; right: 5px; bottom: 0;">';
  98. if (self::getSuccess()) {
  99. $style = 'background:#d4edda;';
  100. $icon = '<span data-feather="info" style="color:green;"></span> <strong style="color:green; margin-left:5px;" class="mr-auto">Succès</strong>';
  101. $texte = self::getSuccess();
  102. self::printToast($style, $icon, $texte);
  103. }
  104. if (self::getWarning()) {
  105. $style = 'background:#fff3cd;';
  106. $icon = '<span data-feather="alert-circle" style="color:orange;"></span> <strong style="color:orange; margin-left:5px;" class="mr-auto">Attention</strong>';
  107. $texte = self::getWarning();
  108. self::printToast($style, $icon, $texte);
  109. }
  110. if (self::getError()) {
  111. $style = 'background:#f8d7da;';
  112. $icon = '<span data-feather="alert-triangle" style="color:red;"></span> <strong style="color:red; margin-left:5px;" class="mr-auto">Erreur</strong>';
  113. $texte = self::getError();
  114. self::printToast($style, $icon, $texte);
  115. }
  116. echo '</div>';
  117. }
  118. self::destroyAlert();
  119. }
  120. public static function destroyAlert()
  121. {
  122. unset($_SESSION["alert"]);
  123. }
  124. }