2
0

alert.class.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 recError(string $_string)
  12. {
  13. if (empty($_SESSION["alert"]["error"])) {
  14. $_SESSION["alert"]["error"] = array();
  15. }
  16. array_push($_SESSION["alert"]["error"], $_string);
  17. }
  18. public static function recTab(string $_string)
  19. {
  20. $_SESSION["alert"]["tab"] = $_string;
  21. }
  22. public static function getSuccess()
  23. {
  24. if (!empty($_SESSION["alert"]["success"])) {
  25. return $_SESSION["alert"]["success"];
  26. } else {
  27. return FALSE;
  28. }
  29. }
  30. public static function getError()
  31. {
  32. if (!empty($_SESSION["alert"]["error"])) {
  33. return $_SESSION["alert"]["error"];
  34. } else {
  35. return FALSE;
  36. }
  37. }
  38. public static function getTab()
  39. {
  40. return (isset($_SESSION["alert"]["tab"])) ? $_SESSION["alert"]["tab"] : FALSE;
  41. }
  42. public static function ifSuccess()
  43. {
  44. return (empty($_SESSION["alert"]["success"])) ? FALSE : TRUE;
  45. }
  46. public static function ifError()
  47. {
  48. return (empty($_SESSION["alert"]["error"])) ? FALSE : TRUE;
  49. }
  50. public static function ifTab()
  51. {
  52. return (empty($_SESSION["alert"]["tab"])) ? FALSE : TRUE;
  53. }
  54. public static function printAlert(array $_array)
  55. {
  56. $return = NULL;
  57. foreach ($_array as $value) {
  58. $return .= "<div>" . $value . "</div>";
  59. }
  60. return $return;
  61. }
  62. public static function recErrorInput(string $_string)
  63. {
  64. if (empty($_SESSION["alert"]["input"])) {
  65. $_SESSION["alert"]["input"] = array();
  66. }
  67. array_push($_SESSION["alert"]["input"], $_string);
  68. }
  69. public static function ifErrorInput(string $_string)
  70. {
  71. return (in_array($_string, $_SESSION["alert"]["input"])) ? TRUE : FALSE;
  72. }
  73. private static function printToast(string $_style, string $_icon, array $_texte)
  74. {
  75. echo ' <div class="toast hide" role="alert" aria-live="assertive" aria-atomic="true" data-delay="1500" style="margin-top:10px; ' . $_style . '">
  76. <div class="toast-header">' . $_icon . '</div><div class="toast-body">' . self::printAlert($_texte) . '</div>
  77. </div>';
  78. }
  79. public static function show()
  80. {
  81. if (self::getSuccess() or self::getError()) {
  82. 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;">';
  83. if (self::getSuccess()) {
  84. $style = 'background:#d4edda;';
  85. $icon = '<span data-feather="info" style="color:green;"></span> <strong style="color:green; margin-left:5px;" class="mr-auto">Succès</strong>';
  86. $texte = self::getSuccess();
  87. self::printToast($style, $icon, $texte);
  88. }
  89. if (self::getError()) {
  90. $style = 'background:#f8d7da;';
  91. $icon = '<span data-feather="alert-triangle" style="color:red;"></span> <strong style="color:red; margin-left:5px;" class="mr-auto">Erreur</strong>';
  92. $texte = self::getError();
  93. self::printToast($style, $icon, $texte);
  94. }
  95. echo '</div>';
  96. }
  97. self::destroyAlert();
  98. }
  99. public static function destroyAlert()
  100. {
  101. unset($_SESSION["alert"]);
  102. }
  103. }