| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- <?php
- /**
- * Classe `alert`
- *
- * Cette classe gère les alertes dans le système. Elle permet de :
- * - Enregistrer des messages de succès, d'avertissement et d'erreur dans la session.
- * - Récupérer et afficher les messages enregistrés.
- * - Gérer les onglets actifs et les erreurs liées aux champs de formulaire.
- * - Afficher les alertes sous forme de toasts avec des styles personnalisés.
- */
- class alert
- {
- /**
- * Enregistre un message de succès dans la session.
- *
- * @param string $_string Le message de succès à enregistrer.
- * @return void
- */
- public static function recSuccess(string $_string)
- {
- if (empty($_SESSION["alert"]["success"])) {
- $_SESSION["alert"]["success"] = array();
- }
- array_push($_SESSION["alert"]["success"], $_string);
- }
- /**
- * Enregistre un message d'avertissement dans la session.
- *
- * @param string $_string Le message d'avertissement à enregistrer.
- * @return void
- */
- public static function recWarning(string $_string)
- {
- if (empty($_SESSION["alert"]["warning"])) {
- $_SESSION["alert"]["warning"] = array();
- }
- array_push($_SESSION["alert"]["warning"], $_string);
- }
- /**
- * Enregistre un message d'erreur dans la session.
- *
- * @param string $_string Le message d'erreur à enregistrer.
- * @return void
- */
- public static function recError(string $_string)
- {
- if (empty($_SESSION["alert"]["error"])) {
- $_SESSION["alert"]["error"] = array();
- }
- array_push($_SESSION["alert"]["error"], $_string);
- }
- /**
- * Enregistre un onglet actif dans la session.
- *
- * @param string $_string L'onglet actif à enregistrer.
- * @return void
- */
- public static function recTab(string $_string)
- {
- $_SESSION["alert"]["tab"] = $_string;
- }
- /**
- * Récupère les messages de succès enregistrés.
- *
- * @return array|bool Les messages de succès ou FALSE s'il n'y en a pas.
- */
- public static function getSuccess()
- {
- if (!empty($_SESSION["alert"]["success"])) {
- return $_SESSION["alert"]["success"];
- } else {
- return FALSE;
- }
- }
- /**
- * Récupère les messages d'avertissement enregistrés.
- *
- * @return array|bool Les messages d'avertissement ou FALSE s'il n'y en a pas.
- */
- public static function getWarning()
- {
- if (!empty($_SESSION["alert"]["warning"])) {
- return $_SESSION["alert"]["warning"];
- } else {
- return FALSE;
- }
- }
- /**
- * Récupère les messages d'erreur enregistrés.
- *
- * @return array|bool Les messages d'erreur ou FALSE s'il n'y en a pas.
- */
- public static function getError()
- {
- if (!empty($_SESSION["alert"]["error"])) {
- return $_SESSION["alert"]["error"];
- } else {
- return FALSE;
- }
- }
- /**
- * Récupère l'onglet actif enregistré.
- *
- * @return string|bool L'onglet actif ou FALSE s'il n'y en a pas.
- */
- public static function getTab()
- {
- return (isset($_SESSION["alert"]["tab"])) ? $_SESSION["alert"]["tab"] : FALSE;
- }
- /**
- * Vérifie s'il y a des messages de succès enregistrés.
- *
- * @return bool TRUE s'il y a des messages de succès, FALSE sinon.
- */
- public static function ifSuccess()
- {
- return (empty($_SESSION["alert"]["success"])) ? FALSE : TRUE;
- }
- /**
- * Vérifie s'il y a des messages d'erreur enregistrés.
- *
- * @return bool TRUE s'il y a des messages d'erreur, FALSE sinon.
- */
- public static function ifError()
- {
- return (empty($_SESSION["alert"]["error"])) ? FALSE : TRUE;
- }
- /**
- * Vérifie s'il y a un onglet actif enregistré.
- *
- * @return bool TRUE s'il y a un onglet actif, FALSE sinon.
- */
- public static function ifTab()
- {
- return (empty($_SESSION["alert"]["tab"])) ? FALSE : TRUE;
- }
- /**
- * Génère le HTML pour afficher une liste de messages.
- *
- * @param array|null $_array La liste des messages à afficher.
- * @return string|null Le HTML généré ou NULL si aucun message.
- */
- public static function printAlert(?array $_array = NULL)
- {
- if ($_array == NULL) {
- return NULL;
- }
- $return = NULL;
- foreach ($_array as $value) {
- $return .= "<div>" . $value . "</div>";
- }
- return $return;
- }
- /**
- * Enregistre un message d'erreur lié à un champ de formulaire.
- *
- * @param string $_string Le champ de formulaire en erreur.
- * @return void
- */
- public static function recErrorInput(string $_string)
- {
- if (empty($_SESSION["alert"]["input"])) {
- $_SESSION["alert"]["input"] = array();
- }
- array_push($_SESSION["alert"]["input"], $_string);
- }
- /**
- * Vérifie si un champ de formulaire est en erreur.
- *
- * @param string $_string Le champ de formulaire à vérifier.
- * @return bool TRUE si le champ est en erreur, FALSE sinon.
- */
- public static function ifErrorInput(string $_string)
- {
- return (in_array($_string, $_SESSION["alert"]["input"])) ? TRUE : FALSE;
- }
- /**
- * Affiche une alerte sous forme de toast.
- *
- * @param string $_idAlert L'ID de l'alerte.
- * @param string $_style Le style CSS de l'alerte.
- * @param string $_icon L'icône de l'alerte.
- * @param array|null $_texte Le texte de l'alerte.
- * @return void
- */
- private static function printToast(string $_idAlert, string $_style, string $_icon, ?array $_texte = NULL)
- {
- $text = ($_texte != NULL) ? self::printAlert($_texte) : NULL;
- 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 . '">
- <div class="toast-header">' . $_icon . '</div><div class="toast-body" id="' . $_idAlert . 'Txt">' . $text . '</div>
- </div>';
- }
- /**
- * Affiche toutes les alertes enregistrées.
- *
- * @return void
- */
- public static function show()
- {
- historique::recordLogs();
- echo '<div class="position-fixed bottom-0 right-0 p-3" style="z-index: 5; right: 5px; bottom: 0;">';
- $idAlert = "printToastSuccess";
- $texte1 = (self::getSuccess()) ? self::getSuccess() : NULL;
- $style = 'background:#d4edda;';
- $icon = icon::getFont(["type" => "info", "color" => "green", "size" => "18px"]) . ' <strong style="color:green; margin-left:5px;" class="mr-auto">Succès</strong>';
- self::printToast($idAlert, $style, $icon, $texte1);
- $idAlert = "printToastWarning";
- $texte2 = (self::getWarning()) ? self::getWarning() : NULL;
- $style = 'background:#fff3cd;';
- $icon = icon::getFont(["type" => "warning", "color" => "orange", "size" => "18px"]) . ' <strong style="color:orange; margin-left:5px;" class="mr-auto">Attention</strong>';
- self::printToast($idAlert, $style, $icon, $texte2);
- $idAlert = "printToastError";
- $texte3 = (self::getError()) ? self::getError() : NULL;
- $style = 'background:#f8d7da;';
- $icon = icon::getFont(["type" => "alert", "color" => "red", "size" => "18px"]) . ' <strong style="color:red; margin-left:5px;" class="mr-auto">Erreur</strong>';
- self::printToast($idAlert, $style, $icon, $texte3);
- echo '</div>';
- echo ($texte1 == NULL) ? '' : '<script>$("#printToastSuccess").toast("show");</script>';
- echo ($texte2 == NULL) ? '' : '<script>$("#printToastWarning").toast("show");</script>';
- echo ($texte3 == NULL) ? '' : '<script>$("#printToastError").toast("show");</script>';
- self::destroyAlert();
- }
- /**
- * Supprime toutes les alertes enregistrées.
- *
- * @return void
- */
- public static function destroyAlert()
- {
- unset($_SESSION["alert"]);
- }
- }
|