KXStudio Website https://kx.studio/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

198 lines
5.9KB

  1. <?php
  2. /**
  3. * Class: Flash
  4. * Stores messages (notice, warning, message) to display to the user after a redirect.
  5. */
  6. class Flash {
  7. # Array: $notices
  8. # Manages notices.
  9. private $notices = array();
  10. # Array: $warnings
  11. # Manages warnings.
  12. private $warnings = array();
  13. # Array: $messages
  14. # Manages messages.
  15. private $messages = array();
  16. # Array: $all
  17. # Manages all Flashes.
  18. private $all = array();
  19. # Boolean: $exists
  20. # Do any Flashes exist?
  21. static $exists = array("message" => false,
  22. "notice" => false,
  23. "warning" => false,
  24. null => false);
  25. /**
  26. * Function: __construct
  27. * Removes empty notification variables from the session.
  28. */
  29. private function __construct() {
  30. foreach (array("messages", "notices", "warnings") as $type)
  31. if (isset($_SESSION[$type]) and empty($_SESSION[$type]))
  32. unset($_SESSION[$type]);
  33. }
  34. /**
  35. * Function: prepare
  36. * Prepare the structure of the "flash" session value.
  37. */
  38. static function prepare($type) {
  39. if (!isset($_SESSION))
  40. $_SESSION = array();
  41. if (!isset($_SESSION[$type]))
  42. $_SESSION[$type] = array();
  43. }
  44. /**
  45. * Function: message
  46. * Add a message (neutral) to the session.
  47. *
  48. * Parameters:
  49. * $message - Message to display.
  50. * $redirect_to - URL to redirect to after the message is stored.
  51. */
  52. static function message($message, $redirect_to = null) {
  53. self::prepare("messages");
  54. $_SESSION['messages'][] = Trigger::current()->filter($message, "flash_message", $redirect_to);
  55. if (isset($redirect_to))
  56. redirect($redirect_to);
  57. }
  58. /**
  59. * Function: notice
  60. * Add a notice (positive) message to the session.
  61. *
  62. * Parameters:
  63. * $message - Message to display.
  64. * $redirect_to - URL to redirect to after the message is stored.
  65. */
  66. static function notice($message, $redirect_to = null) {
  67. self::prepare("notices");
  68. $_SESSION['notices'][] = Trigger::current()->filter($message, "flash_notice_message", $redirect_to);
  69. if (TESTER)
  70. exit("SUCCESS: ".$message);
  71. if (isset($redirect_to))
  72. redirect($redirect_to);
  73. }
  74. /**
  75. * Function: warning
  76. * Add a warning (negative) message to the session.
  77. *
  78. * Parameters:
  79. * $message - Message to display.
  80. * $redirect_to - URL to redirect to after the message is stored.
  81. */
  82. static function warning($message, $redirect_to = null) {
  83. self::prepare("warnings");
  84. $_SESSION['warnings'][] = Trigger::current()->filter($message, "flash_warning_message", $redirect_to);
  85. if (TESTER)
  86. exit("ERROR: ".$message);
  87. if (isset($redirect_to))
  88. redirect($redirect_to);
  89. }
  90. /**
  91. * Function: messages
  92. * Calls <Flash.serve> "messages".
  93. */
  94. public function messages() {
  95. return $this->serve("messages");
  96. }
  97. /**
  98. * Function: notices
  99. * Calls <Flash.serve> "notices".
  100. */
  101. public function notices() {
  102. return $this->serve("notices");
  103. }
  104. /**
  105. * Function: warnings
  106. * Calls <Flash.serve> "warnings".
  107. */
  108. public function warnings() {
  109. return $this->serve("warnings");
  110. }
  111. /**
  112. * Function: all
  113. * Returns an associative array of all messages and destroys their session values.
  114. *
  115. * Returns:
  116. * An array of every message available, in the form of [type => [messages]].
  117. */
  118. public function all() {
  119. return array("messages" => $this->messages(),
  120. "notices" => $this->notices(),
  121. "warnings" => $this->warnings());
  122. }
  123. /**
  124. * Function: serve
  125. * Serves a message of type $type and destroys it from the session.
  126. *
  127. * Parameters:
  128. * $type - Type of messages to serve.
  129. *
  130. * Returns:
  131. * An array of messages of the requested type.
  132. */
  133. public function serve($type) {
  134. if (!empty($_SESSION[$type]))
  135. self::$exists[depluralize($type)] = self::$exists[null] = true;
  136. if (isset($_SESSION[$type])) {
  137. $this->$type = $_SESSION[$type];
  138. $_SESSION[$type] = array();
  139. }
  140. return $this->$type;
  141. }
  142. /**
  143. * Function: exists
  144. * Checks for flash messages.
  145. *
  146. * Parameters:
  147. * $type - The type of message to check for.
  148. */
  149. static function exists($type = null) {
  150. if (self::$exists[$type])
  151. return self::$exists[$type];
  152. if (isset($type))
  153. return self::$exists[$type] = !empty($_SESSION[pluralize($type)]);
  154. else
  155. foreach (array("messages", "notices", "warnings") as $type)
  156. if (!empty($_SESSION[$type]))
  157. return self::$exists[depluralize($type)] = self::$exists[null] = true;
  158. return false;
  159. }
  160. /**
  161. * Function: current
  162. * Returns a singleton reference to the current class.
  163. */
  164. public static function & current() {
  165. static $instance = null;
  166. return $instance = (empty($instance)) ? new self() : $instance ;
  167. }
  168. }