Elgg  Version 4.3
SystemMessagesService.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg;
4 
7 
16 
17  const SUCCESS = 'success';
18  const ERROR = 'error';
19  const SESSION_KEY = '_elgg_msgs';
20 
24  protected $session;
25 
31  public function __construct(\ElggSession $session) {
32  $this->session = $session;
33  }
34 
44  public function dumpRegister($register_name = '') {
45  $set = $this->loadRegisters();
46  $return = [];
47 
48  foreach ($set as $prop => $values) {
49  if ($register_name === $prop || $register_name === '') {
50  if ($values || $register_name === $prop) {
51  $return[$prop] = $values;
52  }
53 
54  $set->{$prop} = [];
55  }
56  }
57 
58  // support arbitrary registers for 2.0 BC
59  if ($register_name && !isset($return[$register_name])) {
60  $return[$register_name] = [];
61  }
62 
63  $this->saveRegisters($set);
64  return $return;
65  }
66 
74  public function count($register_name = "") {
75  $set = $this->loadRegisters();
76  $count = 0;
77 
78  foreach ($set as $prop => $values) {
79  if ($register_name === $prop || $register_name === '') {
80  $count += count($values);
81  }
82  }
83 
84  return $count;
85  }
86 
94  public function addSuccessMessage($message) {
95  if (!is_string($message)) {
96  elgg_deprecated_notice('You should only provide a string as the message to ' . __METHOD__, '4.2');
97  }
98 
99  foreach ((array) $message as $str) {
100  $this->addMessage(new \ElggSystemMessage($str, 'success'));
101  }
102  }
103 
111  public function addErrorMessage($message) {
112  if (!is_string($message)) {
113  elgg_deprecated_notice('You should only provide a string as the message to ' . __METHOD__, '4.2');
114  }
115 
116  foreach ((array) $message as $str) {
117  $this->addMessage(new \ElggSystemMessage($str, 'error'));
118  }
119  }
120 
134  public function addMessage($message): void {
135  if (is_array($message)) {
137  }
138 
139  if (!$message instanceof \ElggSystemMessage) {
140  throw new InvalidArgumentException(__METHOD__ . ' $message needs to be an \ElggSystemMessage or an array of options');
141  }
142 
143  $set = $this->loadRegisters();
144  $set->{$message->getType()}[] = $message;
145  $this->saveRegisters($set);
146  }
147 
153  public function loadRegisters() {
154  $registers = $this->session->get(self::SESSION_KEY, []);
155  $set = new RegisterSet();
156  foreach ($registers as $key => $register) {
157  $set->{$key} = $register;
158  }
159  return $set;
160  }
161 
175  public function saveRegisters(RegisterSet $set) {
176  $filter = function ($el) {
177  return ($el instanceof \ElggSystemMessage) && $el->getMessage() !== '';
178  };
179 
180  $data = [];
181  foreach ($set as $prop => $values) {
182  if (!is_array($values)) {
183  continue;
184  }
185  $arr = array_filter($values, $filter);
186  if (!empty($arr)) {
187  $data[$prop] = array_values($arr);
188  }
189  }
190 
191  $this->session->set(self::SESSION_KEY, $data);
192  }
193 }
loadRegisters()
Load the registers from the session.
Exception thrown if an argument is not of the expected type.
elgg_deprecated_notice(string $msg, string $dep_version)
Log a notice about deprecated use of a function, view, etc.
Definition: deprecation.php:52
c Accompany it with the information you received as to the offer to distribute corresponding source complete source code means all the source code for all modules it plus any associated interface definition plus the scripts used to control compilation and installation of the executable as a special the source code distributed need not include anything that is normally and so on of the operating system on which the executable unless that component itself accompanies the executable If distribution of executable or object code is made by offering access to copy from a designated then offering equivalent access to copy the source code from the same place counts as distribution of the source even though third parties are not compelled to copy the source along with the object code You may not or distribute the Program except as expressly provided under this License Any attempt otherwise to sublicense or distribute the Program is void
Definition: LICENSE.txt:215
addMessage($message)
Adds a message to the registry.
Elgg System Message.
if(elgg_trigger_plugin_hook('usersettings:save', 'user', $hooks_params, true)) foreach($request->validation() ->all() as $item) $data
Definition: save.php:53
saveRegisters(RegisterSet $set)
Save the registers to the session.
$count
Definition: ban.php:24
addSuccessMessage($message)
Display a system message on next page load.
dumpRegister($register_name= '')
Empty and return the given register or all registers.
if($container instanceof ElggGroup &&$container->guid!=elgg_get_page_owner_guid()) $key
Definition: summary.php:44
if(isset($_COOKIE['elggperm'])) $session
Definition: login_as.php:28
static factory(array $options)
Create an ElggSystemMessage from an associative array.
System messages service.
$filter
Layout body.
Definition: body.php:18
Represents the state of system messages and errors.
Definition: RegisterSet.php:13
addErrorMessage($message)
Display an error on next page load.
__construct(\ElggSession $session)
Constructor.
count($register_name="")
Counts the number of messages, either globally or in a particular register.