SystemMessagesService.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace Elgg;
  3. /**
  4. * WARNING: API IN FLUX. DO NOT USE DIRECTLY.
  5. *
  6. * Use the elgg_* versions instead.
  7. *
  8. * @access private
  9. *
  10. * @package Elgg.Core
  11. * @subpackage UX
  12. * @since 1.11.0
  13. */
  14. class SystemMessagesService {
  15. /**
  16. * @var \ElggSession
  17. */
  18. protected $session;
  19. /**
  20. * Constructor
  21. *
  22. * @param \ElggSession $session The Elgg session
  23. */
  24. public function __construct(\ElggSession $session) {
  25. $this->session = $session;
  26. }
  27. /**
  28. * Empty and return the given register or all registers. In each case, the return value is
  29. * a filtered version of the full registers array.
  30. *
  31. * @param string $name The register. Empty string for all.
  32. *
  33. * @return array The array of registers dumped
  34. */
  35. function dumpRegister($name = '') {
  36. $registers = $this->loadRegisters($name);
  37. if ($name !== "") {
  38. $return = array();
  39. $return[$name] = empty($registers[$name]) ? [] : $registers[$name];
  40. unset($registers[$name]);
  41. } else {
  42. $return = $registers;
  43. $registers = array();
  44. }
  45. $this->saveRegisters($registers);
  46. return $return;
  47. }
  48. /**
  49. * Counts the number of messages, either globally or in a particular register
  50. *
  51. * @param string $register_name Optionally, the register
  52. *
  53. * @return integer The number of messages
  54. */
  55. function count($register_name = "") {
  56. $registers = $this->loadRegisters($register_name);
  57. if ($register_name !== '') {
  58. return empty($registers[$register_name]) ? 0 : count($registers[$register_name]);
  59. }
  60. $count = 0;
  61. foreach ($registers as $register) {
  62. $count += count($register);
  63. }
  64. return $count;
  65. }
  66. /**
  67. * Display a system message on next page load.
  68. *
  69. * @see system_messages()
  70. *
  71. * @param string|array $message Message or messages to add
  72. *
  73. * @return bool
  74. */
  75. function addSuccessMessage($message) {
  76. return $this->addMessageToRegister($message, "success");
  77. }
  78. /**
  79. * Display an error on next page load.
  80. *
  81. * @see system_messages()
  82. *
  83. * @param string|array $error Error or errors to add
  84. *
  85. * @return bool
  86. */
  87. function addErrorMessage($error) {
  88. return $this->addMessageToRegister($error, "error");
  89. }
  90. /**
  91. * Add a message(s) to a named register to be displayed
  92. *
  93. * Messages will not be displayed immediately, but are stored in the queue
  94. * for later display, usually upon next page load.
  95. *
  96. * The method of displaying these messages differs depending upon plugins and
  97. * viewtypes. The core default viewtype retrieves messages in
  98. * {@link views/default/page/shells/default.php} and displays messages as
  99. * javascript popups.
  100. *
  101. * @internal Messages are stored as strings in the Elgg session as ['msg'][$register] array.
  102. *
  103. * @param string|array $message
  104. * @param string $register_name
  105. *
  106. * @return bool
  107. * @access private
  108. */
  109. function addMessageToRegister($message, $register_name = '') {
  110. $registers = $this->loadRegisters($register_name);
  111. if (is_string($message)) {
  112. $message = array($message);
  113. }
  114. if (!isset($registers[$register_name])) {
  115. $registers[$register_name] = [];
  116. }
  117. $registers[$register_name] = array_merge($registers[$register_name], $message);
  118. $this->saveRegisters($registers);
  119. return true;
  120. }
  121. /**
  122. * Load the registers from the session
  123. *
  124. * @param string $accessed_register The register being accessed
  125. *
  126. * @return array
  127. */
  128. protected function loadRegisters($accessed_register = '') {
  129. $registers = $this->session->get('msg', array());
  130. if (!isset($registers[$accessed_register]) && $accessed_register !== '') {
  131. $registers[$accessed_register] = array();
  132. }
  133. return $registers;
  134. }
  135. /**
  136. * Save the registers to the session
  137. *
  138. * @param array $registers The message registers
  139. * @return void
  140. */
  141. protected function saveRegisters(array $registers) {
  142. $this->session->set('msg', $registers);
  143. }
  144. }